-
pseudo code for division
I was just wondering how to do basic division in c.I layout for multiplication was to get the complete input in string in an array and separate the string first digit in one array(first digit is till you reach multiplication sign)second digit in other array.since we have arrays and n length digits to multiply it was alright I had predefined array size to 50.But for division how should the basic division proceed....only int is used.
-
Uhh, the same way? What exactly is your question?
-
basic question
how to do basic division...the layout is the way i input the information .but how to divide once you get the nos.......say 20/2 .if 2*10=20 then 10 is answre....as far as array 2 and 0 of 20 are stored as different elements so is is a good way to proceed?
-
You just did basic division...
Code:
#include <stdio.h>
int main(void)
{
printf("%d\n", 20 / 2);
return 0;
}
Are you asking how to do division or how are you asking how to input numbers?
-
i am asking how to calculate ....its not very simple as just 20/2.
I could have 33345/55 also .
here is apart of my code:
scanf(%s,array)//this has 33345/55
//separated in two arrays
//ndigit[0]=3;ndigit[1]=3;ndigit[2]=3;ndigit[3]=4;ndigit[4]=5
//ndigit2[0]=5;ndigit[1]=5
how to divide my int l is count for ndigit and m is count for ndigit2.....
when divide on paper we have 55 as one number and not two separate digits...
anyhelp is appreciated
-
Is there a reason you're making it so complicated?
Code:
printf("%d\n", atoi(ndigit) / atoi(ndigit2));
Just make sure ndigit and ndigit2 still have the character representation of the numbers instead of the actual number values. (e.g. ndigit[0] should be '3', not 3).
-
how would character representation help from usual number...the reason s for the complication:
1.check for input if it is valid or not ex:3g/6 not allowed
2.user i going to input any digit length ...addition subtraction and multiplication were easy since we had carry and I added the carry diigt for my answer.
ex:28976*4657
3.negative input to be consider(this case only printing ouput would need changes)
its suppose to be a calculator with add,subtrct,multiply divide and divide must give divide by zero error.
-
I think the array elements are as'3'...just forgot to mention...
-
Couldn't you just use sscanf()? Or are you trying to implement this yourself for a reason?
Code:
char line[BUFSIZ], op;
int a, b;
if(fgets(line, sizeof(line), stdin)
&& sscanf(line, "%d%c%d", &a, &op, &b) == 3) {
switch(op) {
case '/': /* ... */
}
}
-
I dont quite understand the code you wrote...could you please explain I am just an undergraduate student ,so have never seen a code written this way...
Thanks
-
how does scanf work....wont it interpret 345+23 as since all are strings to separate 345 and + and 23?
-
scanf() and sscanf() are different. The latter does the same thing as the former, but it uses as its source, a string supplied by the programmer to it.
It will take a string like 3432/324, and store, the 3432 in a, '\' in op, and 324 in b. From there on, the switch statement is used to determine which operation was given so as to perform the correct math operation.
Undergraduate student or not, this is basic C code that you should know before you graduate. Crack open the books and references, or better yet, start searching on google and C coding websites for information as to how these functions work.
-
thanks for the explanation.....this is my first programming course and i havent done programming ever before...my course actually starts in june and i am doing this just to get a head start............thanks for ur suggestion of going through c websites etc thats exactly why i join this forum to learn...i have a past project with the specifications and am trying to do it on my own....learn c from scratch without any instructor....
the specifucations also said that DO NOT USE SCANF ;but i couldnt think of any other way of doing this without scanf............
any other ways you could think of?
-
also,i decided to use arrays as specfication also said that the test cases for grading are not in the ranges for int,float or any other data type..say multiply 15 digit number to a 4 digit number...i plan to use memeroy alocation for this purpose but have little idea about it..........
-
Well, you could use strtol(). But perhaps by "don't use scanf()" they mean don't use any standard functions to assist you. And of course strtol() is limited to the size of a long.
The number won't even fit into a double? Or perhaps it needs exact calculations done on it. Either way, you'll have to store the number yourself as you were doing.
Have you ever used dynamic memory allocation? If not, have a look at a reference or a tutorial of some kind -- google found this one: http://vergil.chemistry.gatech.edu/r...l/dynamic.html
Perhaps the easiest way to store numbers like this would be to read a string from the user. Then you could -'0' for each digit character and store the results in another array (or the same one). Then you could actually multiply the digits out.
The trickiest part of this scenario is reading the string from the user. You'd have to dynamically allocate enough memory to hold a string of any length. Look into realloc() or search this forum -- there are lots of examples of this. Throw in the search keyword "fgets" as well.
[edit] You could look at the get_string() function in codeform. It might be hard to read, though.
[/edit]