Thread: I'm Confused! Please HELP!

  1. #1
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17

    I'm Confused! Please HELP!

    I should write a code to add up two 80 digits numbers. At first, program take numbers in form of strings.
    My code is here. Please Help me! what's the problem?

    Code:
    #include <stdio.h>
    #include <conio.h>
    void input (void);
    void change (void);
    void add (void);
    void output (void);
    char a [80], b [80];
    int a_1 [80], b_1 [80], s [1000];
    int main ()
    {
        input ();
        change ();
        add ();
        output ();
        getch ();
        return 0;
    }
    void input (void)
    {
         printf ("Enter First String Of Numbers:\n");
         gets (a);
         printf ("Enter Second String Of Numbers:\n");
         gets (b);
    }
    //*********************
    void change (void)
    {
         int j = 0;
         for (int i = 0; a [i]; i++)
             if (a [i] >= '0' && a [i] <= '9')
                a_1 [j++] = a [i] - 48;
    
        int k = 0;
        for (int i = 0; b [i]; i++)
             if (b [i] >= '0' && b [i] <= '9')
                b_1 [k++] = b [i] - 48;      
    }
    //***********************
    void add (void)
    {
         for (int i = 79; i >= 0; i--)
         {
             s [i] = a_1 [i] + b_1 [i];
             if (a_1 [i] + b_1 [i] >= 10)
             {
             c = a_1 [i] + b_1 [i] - 10;
             s [i] = c;
             s [i - 1] += 1;
         }  
    //************************
    void output (void)
    {
         for (int i = 0; s [i]; i++)
             printf ("%d", s [i]
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    what's the problem?
    That's for you to tell us! I mean, other than the use of non-standard headers like conio.h (let me guess...Turbo C?), using gets(), global variables, arrays meant to hold numbers 80-digits long without enough space, accessing arrays out of bounds, and mismatched brackets?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Fix the two missing curly braces in your add function and then either post the exact compiler errors or tell us what it does wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17
    I know what you are saying, but there is a problem. I'm an iranian physics student and because of sanctions I (We) do not have any compiler by myself. We should write our programs in Notepad and then we can compile it at university. So I wrote this code in notepad and then I read it and I guess there are some problems in algorithm.
    If you can help me, You are the man!, if not, again thanks for your attentions.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you still can use something like codepad to online compile your code written in notepad
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17
    Thanks. I compiled it, NO error is taken, but Program still doesn't work.

    Code:
    #include <stdio.h>
    #include <conio.h>
    void input (void);
    void change (void);
    void output (void);
    char a [80], b [80];
    int a_1 [100], b_1 [100], s [1000];
    int main ()
    {
        input ();
        change ();
        //add ();
        output ();
        getch ();
        return 0;
    }
    void input (void)
    {
         printf ("Enter First String Of Numbers:\n");
         gets (a);
         printf ("Enter Second String Of Numbers:\n");
         gets (b);
    }
    //*********************
    void change (void)
    {
         int j = 0;
         for (int i = 0; a [i]; i++)
             if (a [i] >= '0' && a [i] <= '9')
                a_1 [j++] = a [i] - 48;
    
        int k = 0;
        for (int i = 0; b [i]; i++)
             if (b [i] >= '0' && b [i] <= '9')
                b_1 [k++] = b [i] - 48;
          
        int m, i;        
        for (m = j; m >=0; m--)
        {
            s [i] = a_1 [i] + b_1 [i];
            if (s [i] > 9){
            s[i] -= 10;
            s [i -1] += 1;}
            }              
    }
    //************
    void output (void)
    {
         for (int i = 0; s [i]; i++)
             printf ("%d", s [i]);
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what I see with your code (when compiling as C++ - in C mode there is more)
    Code:
    Line 18: error: conio.h: No such file or directory
    In function 'int main()':
    Line 14: error: 'getch' was not declared in this scope
    compilation terminated due to -Wfatal-errors.
    Last edited by vart; 01-14-2012 at 08:29 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The way to debug your program is to give it some simple problems, and insert a print line of code showing the intermediate results to you, followed by a getchar() to pause the program long enough that you can study it and determine where it's goofing up. Put the print and getchar statements, right inside the for loop that is doing the adding.

    Be sure to include all the "edge" cases - adding 9+0, 9+1, 9+9, 0+0, etc.

  9. #9
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17
    Quote Originally Posted by vart View Post
    what I see with your code (when compiling as C++ - in C mode there is more)
    Code:
    Line 18: error: conio.h: No such file or directory
    In function 'int main()':
    Line 14: error: 'getch' was not declared in this scope
    compilation terminated due to -Wfatal-errors.
    I think this errors are not related to my problem! I mean, I have algorithm problem.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by alireza beygi View Post
    I think this errors are not related to my problem! I mean, I have algorithm problem.
    You are correct, that is not the problem.

  11. #11
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17
    Any one can help me?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, look at what your program is doing, right from the start. Is the change function putting the digits into the a_1 and b_1 arrays, correctly? It looks good to me, but you should do a print out after a_1 and b_1 have received the digits, to confirm it. I'm not going to do that for you.

    What I would do is first, fill the a_1 and b_1 arrays, with zero's, because right now, they will have undetermined values in them. You could use a for loop on them or memset, or other techniques, whichever you want. Something like this.

    Code:
    int i=0
    while(i < 100) {
       a_1[i]=b_1[i]=0;
       ++i;
    }
    Then before that last add part in the change function, you need to compare j and k, and use whichever one is larger to control (stop), the adding for loop. You don't want to use just j or just k to control the loop, you want to use the larger of the two variables.

    Give that a try and see if you can make that go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm confused..
    By .synq in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2011, 05:05 PM
  2. confused
    By mopar123 in forum C Programming
    Replies: 18
    Last Post: 09-01-2005, 04:06 PM
  3. confused...
    By Darkcoder in forum Game Programming
    Replies: 4
    Last Post: 03-09-2005, 01:07 AM
  4. Very confused
    By Aerie in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 01:08 PM
  5. Little Confused..?
    By ikidd in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 06:44 PM