Thread: Incorrect Difference

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Incorrect Difference

    I'm trying to make a simple calculator program....but when I start to subtract numbers..it gives my incorrect answer....

    Code:
    #include <stdio.h>
    int main()
    {
    	int a[2],i, diff=0;
    	
    	printf("\n\tSubtract Numbers\n\n");
    	for(i=0; i<2; i++)
    	{
    		printf("Enter a number: ");
    		scanf("%d", &a[i]);
    		diff-=a[i];
    	}
    	printf("\n\tThe difference is %d\n", diff);
    }

    Output:
    Enter a number: 13
    Enter a number: 15
    The difference is -28
    It should be -2 right??...because 13-15=-2

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, the code you have now is equivalent to doing this:
    diff = diff - a[0] - a[1];

    Since you want to subtract one number from the other, I recommend deleting line 11, and writing this after line 12 but before printf():
    diff = a[0] - a[1];

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    Quote Originally Posted by whiteflags View Post
    No, the code you have now is equivalent to doing this:
    diff = diff - a[0] - a[1];

    Since you want to subtract one number from the other, I recommend deleting line 11, and writing this after line 12 but before printf():
    diff = a[0] - a[1];
    what if the number of elements is also determined by the user?..

    Like this:

    Code:
        int a[2],i, diff=0, n;
        
        printf("Enter number of elements: ");
        scanf("%d", &n);
        for(i=0; i<n; i++)
        {
            printf("Enter a number: ");
            scanf("%d", &a[i]);
        }
    Last edited by Chronic; 02-25-2017 at 05:56 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I get what you're going for. The actual solution might be odd, because you need to start with at least two operands.

    Maybe something like this. (It's untested, turn in/copy at your own risk)
    Code:
    char op = '\0';
    double a[2] = {0.0, 0.0};
    scanf("%lf%c%lf", &a[0], &op, &a[1]);
    a[0] = 
       (op == '+')? a[0] + a[1] : 
       (op == '-')? a[0] - a[1] :
       (op == '*')? a[0] * a[1] :
       (op == '/' && a[1] != 0.0) ? a[0] / a[1] :
       0.0;
    while (op != '=')
    {
       printf("\t%f\n", a[0]);
       scanf("%c", &op);
       if (op == '=') 
       {
          printf("\t%f\n", a[0]);
          break;
       }
       else
       {
           scanf("%lf", &a[1]);
           if (op == '+')
              a[0] += a[1];
           else if (op == '-')
              a[0] -= a[1];
           else if (op == '*')
              a[0] *= a[1];
           else if (op == '/')
           {
               if (a[1] == 0)
               {
                   printf("\t%s\n", "Error.");
                   break;
               }
               else
                  a[0] /= a[1];
           }
       }
    }
    This way the user can do things like

    2+2
    4
    *3
    12
    :
    :

    a[0] always stores the running answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please tell me where my code is incorrect.
    By Sonam Agarwal in forum C Programming
    Replies: 3
    Last Post: 01-09-2014, 10:50 AM
  2. incorrect output
    By Alint808 in forum C Programming
    Replies: 8
    Last Post: 07-17-2011, 06:21 PM
  3. Value is incorrect??
    By Myca in forum C Programming
    Replies: 3
    Last Post: 02-11-2011, 10:20 AM
  4. incorrect initialisation
    By s_siouris in forum C Programming
    Replies: 2
    Last Post: 09-13-2006, 07:26 AM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM

Tags for this Thread