473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

display the process of division in c

for example: 16/5:
3
--------
5)16
15
--------
1
the user input the dividend and divisor,the program should display the
upright formula like the example.
and , the program must use recursive arithmetic.
help, thanks!

Dec 23 '05 #1
8 2494
nineteen wrote:
for example: 16/5:
3
--------
5)16
15
--------
1
the user input the dividend and divisor,the program should display the
upright formula like the example.
and , the program must use recursive arithmetic.
help, thanks!

Ok.

--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://www.eskimo.com/~scs/C-faq/top.html
Dec 23 '05 #2
> for example: 16/5:
3
--------
5)16
15
--------
1
the user input the dividend and divisor,the program should display the
upright formula like the example. and , the program must use recursive
arithmetic.


Sounds like a nice assignment. What's your problem ?

--
:wq
^X^Cy^K^X^C^C^C^C
Dec 23 '05 #3

us****@zevv.nl wrote:

Sounds like a nice assignment. What's your problem ?

I can't make all the numbers in its proper position.
so I looks for help here.

Dec 23 '05 #4
nineteen <nt*****@gmail.com> wrote:

us****@zevv.nl wrote:

Sounds like a nice assignment. What's your problem ?

I can't make all the numbers in its proper position.
so I looks for help here.


No problem, there are probably a lot of people here who are able and willing
to help you.

It would be a good start if you post the code you have got at this moment,
and some explanations about which parts you think are correct and which
parts you have troubles with. We can look into your code, see if there are
any obvious flaws, and give you hints on how to improve things.

Good luck,

Ico

--
:wq
^X^Cy^K^X^C^C^C^C
Dec 23 '05 #5
thanks very much.
the code I have written:
my original intension is use a matrix ( the result[10][20] array in
the code ) to display all the things.
the first line is the quotient, the second line is "------------", and
so on.
but ,I soon found it's difficult to get the values of all the
individual bits of all the numbers.
for example,32344/16=2021, at first line,I must store how long is the
quotient, ( in this example,it's 4,and I must use a
variant to record it.)
I must store every bit of the number "2021", ( because I don't know its
length,so I must use a array.)
The worst, I won't know how long the dividend and the divisor will be.
and all the temporary values are request dividing to separate bit,too.
If the dividend is very big,and the divisor is small, the will be many
temporary numbers ,
so I found it's almost impossible .

(-_-## my English is poor ,I'm not sure whether I have expressed my
meaning clearly.
so there may be many redundant words.)

this is my half code:

#include<stdio.h>
int result[10][20]={0},m,n; //the result matrix,m and n show the row
and the column index
int stack[10]={0};//a simple "stack" .
int i=0;//a temporary variant
void divide(int dend,int dor)
{

}
int main()
{

int dend,dor,mend,subend; //dividend,divisor,minuend,subtrahend for
short
int j,k,m,n; //temp variant
int t[10];//temporary stack,used for store the individual bit of a
number.
printf("please input the dividend and the divisor.\n");
scanf("%d%d",&dend,&dor);
mend=dend;
stack[i++]=mend;//the first number is the dividend.
while(mend/10>dor)
{
mend/=10;
stack[i++]=mend;
}
--i;//put i to the actual position of the values.
// for example ,if the dividend is 12345,the divisor is
48,the stack array will store: 12345,1234,123

for(j=1,k=0;j<mend;j*=10)
k++;
//k tell where should the quotient appear.
j=0;
m=dend/dor;
while(m>0)
{
t[j++]=m%10;
m/=10;
}
--j;
m=k;
while(j>=0)
{
result[0][4+m++]=t[j--];
}
//the first line has been done.

divide(stack[i],dor);// call the recursive function to complete the
formula

}

thanks.

Dec 23 '05 #6
complement:
in the last "while" loop,the column index "4+m++" means:
I reserve 4 blanks for the divisor,i.e., the divisor must less than
10000,
and ,as this example:
3
--------
5)16
15
--------
1
there is a ')' after the divisor, so all the lines should move right 5
bits.
5 add m, is the first place of the quotient's first bit.

Dec 23 '05 #7
On Fri, 23 Dec 2005 04:31:25 -0800, nineteen wrote:
thanks very much.
the code I have written:
my original intension is use a matrix ( the result[10][20] array in
the code ) to display all the things.
the first line is the quotient, the second line is "------------", and
so on.
but ,I soon found it's difficult to get the values of all the
individual bits of all the numbers.
for example,32344/16=2021, at first line,I must store how long is the
quotient, ( in this example,it's 4,and I must use a
variant to record it.)
I must store every bit of the number "2021", ( because I don't know its
length,so I must use a array.)
The worst, I won't know how long the dividend and the divisor will be.
and all the temporary values are request dividing to separate bit,too.
If the dividend is very big,and the divisor is small, the will be many
temporary numbers ,
so I found it's almost impossible . <snip>

I didn't much bother to understand the bulk of the code, but If I may make
a small suggestion: It's better to approach this problem in steps.

Do the division /first/, and store the occasional number in an array of
integers.

When it's done, print the whole thing using /another/ function, and pass
it the array (and it's length) you filled in the first function.

By separating the division and the formatting, you're breaking the problem
up in smaller pieces. You'll be able to concentrate on getting the
division right, without turning it into a jumble. Functions Are A Good
Thing: A get_number_length() and a print_dashey_line(), would be quite
handy.

When formatting, you'll also have the advantage of having every number you
want to print ready. You'll be able to tell what the longest and shortest
numbers are, and format appropriately (printf ("%*.d", width, number)
might be useful here). You also don't have to store every digit of every
number, just store the numbers. Remember that you should check if you
reach the end of the array, and if you want extra cookies, stretch the
array (malloc(), realloc()).

I can't give you anything really concrete, because I don't know exactly
what you want and how you want it formatted. But at least it's something
to think about.
#include<stdio.h>
Blank lines good.
int result[10][20]={0},m,n; //the result matrix,m and n show the row
and the column index
There are two (or more) reasons C++-style comments are unwanted:
1) They've only been standard since C99
2) They can wrap on usenet, and cause problems, which is the case right
now.

Also, the 'm,n' at the end there is a bit hidden.
It's a good idea to group together related definitions, and seperate
unrelated ones. In this case:

int result[10][20] = {0};
int m, n;

In your case, it might already have caused some confusion, because you
define both m and n /AGAIN/ in main().
int stack[10]={0};//a simple "stack" .
int i=0;//a temporary variant
void divide(int dend,int dor)
{

} int main()
{
/* dividend,divisor,minuend,subtrahend for short */ int dend,dor,mend,subend; /* temp variant */ int j,k,m,n; /*
* temporary stack,used for store the individual bit of a number.
*/ int t[10];
Most of these identifiers are sort of bad. Why not name 't' 'temp_stack'
or some such? Are there better names for j, k, m and n?
printf("please input the dividend and the divisor.\n");
scanf("%d%d",&dend,&dor);


It's always a good idea to check scanf()'s return value. Someone might try
to input "Fourty and Two".

<snip jumble>
--
Pieter Droogendijk <pieter at binky dot org dot uk>
PGP/1E92DBBC [ Make way for the Emperor's Finest. ] binky.org.uk

Dec 23 '05 #8
On 23 Dec 2005 02:31:48 -0800, "nineteen" <nt*****@gmail.com> wrote:
for example: 16/5:
3
--------
5)16
15
--------
1
the user input the dividend and divisor,the program should display the
upright formula like the example.
and , the program must use recursive arithmetic.
help, thanks!


Rather than perform your output from integers (as described in a
subsequent message), consider using strings.

You have four numbers of interest: dividend, divisor, quotient, and
remainder. Use integers to process the first two to get the last two.
Then use sprintf to convert each to a string. Use strlen to get the
length of each string. This will provide all the formatting data your
need.

The key line is the line with
<divisor>)<dividend>
You can now determine how long this line is (length of divisor string
+ length of dividend string + 1). Your line of ---- above this line
obviously must start at (length of divisor + 1) and be (length of
dividend) long. I'm sure you can work out the rest of the formatting.

Once you have all the alignment numbers, just print each string in
its proper place. You can use for loops with fputc to print a
variable number of leading blanks or a variable number of '-'.
<<Remove the del for email>>
Dec 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: LRW | last post by:
I'm doing the following to try to display queried data into verticle columns, and I'm getting odd behaviors. For example, if there are only 4 items to display, it skips item number 3: Item1 ...
2
by: Pat Scott | last post by:
I am looking for a general purpose javascript snippet that enables me to <div> or <span> HTML to make portions of the form be hidden and then appear. The form contains about 12 sections and some...
1
by: jujubean | last post by:
I have data a query produces which looks like: (The two fields are division and item) Division___Item A_________X A_________Y A_________Z B_________P B_________Q B_________R
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
1
by: mrquestions | last post by:
Hey, new to the forum, have looked everywhere for some information of the following query but can't find it anywhere... Within a site I am building a division calls on an include function to...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.