Thread: simple program giving me issues:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    simple program giving me issues:

    All I want to do is divide the smaller number into the larger one. I has no issues test and getting the right answer. Now I wanted to pass the remainder and this is where the issues came.

    After I can effectively get the remainder passed to (int sum2) I want to divide the smaller remainder into the larger one until the remainder is 0. I'll use a "while" statement for this.


    Right now if I divide 9 / 3 = 0 r 3
    3 / 9 = 0 r 3

    5 / 5 = 32767 R 1606416048

    This is a very simple program and I going in circles here. Can anyone help.

    Code:
    
    
    int remainder (int, int);           // function protoype //
    
    #include <stdio.h>
    
    
    int remainder(int x, int y)         // function header //
    {
        int sum1, sum2;
        
        if (x > y) {                    // the lesser divided into the highier // 
            sum1 = y / x;
            sum2 = y%x;                 // the remainder is passed to sum2 //
        }
        if (y > x) {
            sum1 = x / y;
            sum2 = x%y;
        }
      
        return printf("The answer is %d and the remainder is %d", sum1, sum2);
        
        
    }
    
    int main(int argc, const char * argv[])
    {
    
        int x,y;
        
        printf("Enter two numbers\n");
        scanf("%d%d",&x, &y);
        
        remainder(x, y);        // function called //
        
        return 0;
    }
    
    
    Last edited by jocdrew21; 07-13-2013 at 01:38 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Well - what do you think your program should print when y==x?
    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

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Three issues.

    If x == y, sum1 and sum2 will be referenced without having a value stored in them, which seems to be the problem you're most concerned with. I'm sure you can fix that.

    const char ** isn't compatible with char **, so your definition for main may not be allowed by your C implementation.

    If scanf returns a value that isn't 2, x or y will be referenced without having a value stored in them.

    EDIT: Also, you don't print a terminating new line to stdout.
    Last edited by Barney McGrew; 07-13-2013 at 02:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MD5 program not giving correct hashes.
    By Syscal in forum C Programming
    Replies: 4
    Last Post: 04-30-2012, 04:11 AM
  2. Help my program is giving a problem
    By zangetsu in forum C Programming
    Replies: 72
    Last Post: 04-04-2011, 10:56 AM
  3. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  4. sprintf giving me issues
    By ramparts in forum C Programming
    Replies: 35
    Last Post: 11-14-2006, 12:40 PM
  5. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM