# Possible Logic Error

• 03-14-2011
seanksg
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); }```
• 03-14-2011
anduril462
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.
• 03-14-2011
seanksg
Quote:

Originally Posted by anduril462
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?
• 03-14-2011
anduril462
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.
• 03-14-2011
seanksg
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!
• 03-14-2011
anduril462
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.
• 03-14-2011
seanksg
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.
• 03-14-2011
anduril462
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)```
• 03-14-2011
seanksg
Brilliant!

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