473,794 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2526
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,s ubend; //dividend,diviso r,minuend,subtr ahend 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",&d end,&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<m end;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_leng th() and a print_dashey_li ne(), 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,diviso r,minuend,subtr ahend for short */ int dend,dor,mend,s ubend; /* 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",&d end,&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>)<divi dend>
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
1815
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 Item4 Item 2 If there are 10 items, it duplicates some: Item 1 Item 4 Item 7
2
2724
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 sections can contain subsections. When the user clicks on a checkbox, I want previously hidden text boxes, radio buttons, text, drop down menus, and additional checkboxes to appear. In some cases, clicking on a checkbox in a section can cause...
1
1348
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
2431
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 replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import division" but if it is never coming (in this decade, that is) then it would be a
10
3197
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/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...
2
7053
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, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
1
9776
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 display a webpage ($page), so only the one template page is used throughout the entire site. It has been all smooth sailing up until I wanted to have a MouseOver image (image.src), become "selected" upon requesting its specific page. <? if ($page =...
2
3874
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 result of the division be truncated to integer. However, today I reviewed the method to be used in Python to get true division, and this gave
10
1721
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 constructs with the HTML document itself. An opposite approach to this form of dynamic HTML production is called push-style templating, as coined by Terence Parr:
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.