Possible Logic Error

This is a discussion on Possible Logic Error within the C Programming forums, part of the General Programming Boards category; Hi all, I'm new to these boards, and programming in general. In fact, C is my first language. Earlier today, ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Possible Logic Error

    Hi all,

    I'm new to these boards, and programming in general. In fact, C is my first language. Earlier today, my instructor gave an assignment that called for a program that, upon the input of 4 integers, outputs the sum, product, and average of those integers, along with displaying the lowest of the four, second lowest of the four, and the ratio of the second lowest to the lowest. I believe my issue resides in the logic for determining the second lowest of the four integers. I tried to use the same logic process as I did to compute the smallest integer (my instructor demonstrated this part in class). In addition, I think that I need to have the smallest integer defined before I compute the second lowest, but I'm not sure how to call down the smallest or what form of logic to use. My code is below. Any suggestions? Thanks for any help that can be given.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main()
    {
       int a; /*user input 1*/
       int b; /*user input 2*/
       int c; /*user input 3*/
       int d; /*user input 4*/
       int sum; /*sum a, b, c, & d*/
       int product; /*product a, b, c, & d*/
       int average; /*average a, b, c, & d*/
       int secsmall; /*second smallest among 4 integers*/
       int small; /*second smallest among 4 integers*/
       int ratio; /*ratio of second smallest to smallest*/
    
       printf("\nPlease enter four different integers: ");
       scanf("%d", &a);
       scanf("%d", &b);
       scanf("%d", &c);
       scanf("%d", &d);
    
       sum=a+b+c+d; /*compute sum*/
       printf("\nSum is: %d\n\n", sum);
    
       product=a*b*c*d; /*compute product*/
       printf("Product is: %d\n\n", product);
    
       average=((a+b+c+d)/4); /*compute average*/
       printf("Average is: %d\n\n", average);
    
       small=a;
       if(small>b)
       small=b;
       if(small>c)
       small=c;
       if(small>d)
       small=d;        /*compute smallest*/
    
        if(small<a)
       secsmall=a;
       if(secsmall>b)
       secsmall=b;
       if(secsmall>c)
       secsmall=c;
       if(secsmall>d)
       secsmall=d;		/*compute second smallest*/
    
       printf("Second Smallest is: %d\n\n", secsmall);   
    
       printf("Smallest is: %d\n\n", small);
    
       ratio=secsmall/small;
       printf("Second Smallest / Smallest is: %d\n\n", ratio);
    
       printf("*****Program Terminated*****\n\n");
    
       return (EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    I take it you haven't learned arrays yet...

    If the smallest one is a, secsmall will be the smallest of b, c or d. Likewise, if the smallest is b, secsmall will be the smallest of a, c or d, etc.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by anduril462 View Post
    I take it you haven't learned arrays yet...

    If the smallest one is a, secsmall will be the smallest of b, c or d. Likewise, if the smallest is b, secsmall will be the smallest of a, c or d, etc.
    Correct... still only in week 2 of classes. I figure that we will probably go over something to that effect sometime this week (assignment isn't due until next week). I just figured I'd be proactive.

    So if I understand correctly, I'll need to use a number of arrays (separate, larger conditional statements, if that is correct?) to account for each possible situation?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Sorry, I think I confused you. An array is not "separate, larger conditional statements". It's like a list or collection of things, say 100 numbers or 50 characters. Your program would be much easier if you were using arrays (read ahead in your book or take a quick glance here to see what arrays are). You do need to use more conditional statements to determine this, specifically you need to nest them (one inside another), like so:
    Code:
    if a is the smallest
        secsmall = b
        if c < secsmall
            secsmall = c
        if d < secsmal.
            ...
    else if b is the smallest
        secsmall = a
        ...
    Hope that makes sense.
    Last edited by anduril462; 03-14-2011 at 04:36 PM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Thanks for the help! My roommate tried to explain nesting to me (he's far more versed in programming than I, but doesn't know C). We just couldn't figure out off the bat how to do that. I'll try that out and see what happens. Thanks again for the help and the quick response!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Okay...but be careful, I gave you pseudo code, not valid C code. You will need to translate a tiny bit. Some ( ) and some { } for grouping, and a few semicolons should get you close.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    I looked into else...if statements (next section in my book)... and developed the following code. However, I'm still having some issues.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main()
    {
       int a; /*user input 1*/
       int b; /*user input 2*/
       int c; /*user input 3*/
       int d; /*user input 4*/
       int sum; /*sum a, b, c, & d*/
       int product; /*product a, b, c, & d*/
       int average; /*average a, b, c, & d*/
       int secsmall; /*second smallest among 4 integers*/
       int small; /*second smallest among 4 integers*/
       int ratio; /*ratio of second smallest to smallest*/
    
       printf("\nPlease enter four different integers: ");
       scanf("%d", &a);
       scanf("%d", &b);
       scanf("%d", &c);
       scanf("%d", &d);
    
       sum=a+b+c+d; /*compute sum*/
       printf("\nSum is: %d\n\n", sum);
    
       product=a*b*c*d; /*compute product*/
       printf("Product is: %d\n\n", product);
    
       average=((a+b+c+d)/4); /*compute average*/
       printf("Average is: %d\n\n", average);
    
       small=a;
       if(small>b)
       small=b;
       if(small>c)
       small=c;
       if(small>d)
       small=d;        /*compute smallest*/
    
       if(small=a)
          secsmall=b;
       else if(c<secsmall)
          secsmall=c;
       else
          secsmall=d;
       
       if(small=b)
          secsmall=a;
       else if(c<secsmall)
          secsmall=c;
       else if(d<secsmall)
          secsmall=d;
       
       if(small=c)
          secsmall=a;
       else if(b<secsmall)
          secsmall=b;
       else if(d<secsmall)
          secsmall=d;
       
       if(small=d)
          secsmall=a;
       else if(b<secsmall)
          secsmall=b;
       else if(c<secsmall)
          secsmall=c;	   /*compute second smallest*/
    
       printf("Second Smallest is: %d\n\n", secsmall);   
    
       printf("Smallest is: %d\n\n", small);
    
       ratio=secsmall/small;
       printf("Second Smallest / Smallest is: %d\n\n", ratio);
    
       printf("*****Program Terminated*****\n\n");
    
       return (EXIT_SUCCESS);
    }
    Here's the issue: after appending the program, it does not calculate the smallest integer correctly. This is probably an error in the way I set up the else...if statements, but I'm pretty sure I followed the format exactly out of the book.

    Thanks again for all the help. I hate to continue to ask questions, but I really appreciate it.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Code:
    if(small==a)
    You need a second = in there. One = is for assignment (yep, you can assign inside an if-condition). Two == is for comparison. The nested structure you're after is:
    Code:
    if (small == a) {
        secsmall = b;
        if ...
    }
    else if (small == b)

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Brilliant!

    It's all working now. Thanks for all the help! I can't thank you enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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