display the process of division in c

N

nineteen

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!
 
A

Anand

nineteen said:
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.
 
U

usenet

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 ?
 
U

usenet

nineteen said:
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
 
N

nineteen

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,dor);// call the recursive function to complete the
formula

}

thanks.
 
N

nineteen

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.
 
P

Pieter Droogendijk

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>
 
B

Barry Schwarz

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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top