pseudo code for division

This is a discussion on pseudo code for division within the C Programming forums, part of the General Programming Boards category; I was just wondering how to do basic division in c.I layout for multiplication was to get the complete input ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    35

    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.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Uhh, the same way? What exactly is your question?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    35

    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?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You just did basic division...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("&#37;d\n", 20 / 2);
      return 0;
    }
    Are you asking how to do division or how are you asking how to input numbers?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    35
    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

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Is there a reason you're making it so complicated?
    Code:
    printf("&#37;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).
    Last edited by itsme86; 05-30-2007 at 11:49 AM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    35
    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.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    35
    I think the array elements are as'3'...just forgot to mention...

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, "&#37;d%c%d", &a, &op, &b) == 3) {
        switch(op) {
        case '/':  /* ... */
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    35
    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

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    35
    how does scanf work....wont it interpret 345+23 as since all are strings to separate 345 and + and 23?

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    35
    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?

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    35
    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..........

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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]
    Last edited by dwks; 05-31-2007 at 01:55 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21