0

Taking a class on C, we have learned if/else statements, repetition, operators, loops, and switch.

The purpose of this assignment is to have a user input 5 numbers and back output following:

Display the smallest number entered

Display the largest number entered

Display the sum of the five numbers entered

Display the average of the five numbers entered

I know its long and probably bogged down but it runs and works for all but the 'minim' which repeatedly outputs zero instead of the actual smallest number. Not sure where i went wrong on this. Any tips would help.

int choice = 0, number, sum = 0, count = 0, minim = 0, max = 0;
float average;

printf("Please enter your first number: ");
scanf("%i", &number);
if (number < minim) {
    minim = number;  }
if (max < number) {
    max = number;  }
   count += number;

printf("Please enter your second number: ");
scanf("%i", &number);
if (number < minim) {
    minim = number;  }
if (max < number) {
    max = number;  }
count += number;

printf("Please enter your third number: ");
scanf("%i", &number);
if (number < minim) {
    minim = number;  }
if (max < number) {
    max = number;  }
count += number;

printf("Please enter your fourth number: ");
scanf("%i", &number);
if (number < minim) {
    minim = number;  }
if (max < number) {
    max = number;  }
count += number;

printf("Please enter your fifth number: ");
scanf("%i", &number);
if (number < minim) {
    minim = number;  }
if (max < number) {
    max = number;  }
count += number;

sum += count;
average = sum / 5;

while (choice != -1) {
    printf("\n\nChoose an option from the menu below to see the results (1,2,3,4) or use (-1) to exit:\n");
    printf("1.  Smallest number entered\n");
    printf("2.  Largest number entered\n");
    printf("3.  Sum of the five numbers entered\n");
    printf("4.  Average of the five numbers entered\n\n");
    printf("\nEnter your selection: ");
    scanf("%i", &choice);

    switch (choice) {
        case 1:
            printf("\n The smallest number entered is %i\n", minim);
            break;
        case 2:
            printf("\n The largest number entered is %i\n", max);
            break;
        case 3:
            printf("\n The sum of the five numbers entered is %i\n", sum);
            break;
        case 4:
            printf("\n The average of the five numbers entered is %.2lf\n", average);
            break;
        default:
        printf("Incorrect menu option selected.\n");
    }
}

printf("\nThank you for your time. Exiting program.\n");

}

iAm Bob
  • 1
  • 1
  • 5
    Initialize `minim` to a really big number. Ex: `INT_MAX` in limits.h. (Also, you say you've learned loops. This would be a really good place to use one.) – 001 Mar 26 '18 at 21:56
  • The issue (without digging much into the code) appears to be that you initialize the `minim` variable to `0`. When this is compared to user inputs, it almost always will fail because checking (userInput < minim) is really checking (userInput < 0), which is likely not what you want. – MrHappyAsthma Mar 26 '18 at 21:58
  • ... and `max = INT_MIN;` – Weather Vane Mar 26 '18 at 21:58
  • Aside I am intrigued as to why you have `count += number;` for each entry and finally `sum += count;` when you could do `sum += number` and get rid of `count`. – Weather Vane Mar 26 '18 at 22:06
  • For ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* (consider th closing brace '}' as a separate statement. 2) separate code blocks ( `for` `if` `else` `while` `do...while` `switch` `case` `default` ) via a single blank line – user3629249 Mar 27 '18 at 14:06
  • Per your question, you need to process 5 inputs, performing the same processing for each input Then finally, output 4 bits of data. This 'screams' for a loop that iterates 5 times, suggests 4 variables for the 4 data to be output and one variable to share the inputs – user3629249 Mar 27 '18 at 14:09
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 Mar 27 '18 at 14:14
  • the statement of the problem does NOT indicate the user should be selecting from a menu, so why is a menu implemented? – user3629249 Mar 27 '18 at 14:40

4 Answers4

2

As John3136 mentioned, you could assign the initial minimum value to the constant INT_MAX and the initial maximum value to INT_MIN, and you probably should do this as it will make transitioning to loops easier, but an easier alternative with your current code would be to simply assign the initial value that the user inputs to minim. It would also help if you did this with max to because it would allow your program to handle negative numbers.

Aiden Grossman
  • 337
  • 5
  • 13
1

minim starts at 0 and only gets reset if a number is less than 0 so unless you enter a negative number...

2 approaches:

  1. Set minim to INT_MAX at the start (everything will be less than INT_MAX)
  2. Set minim to some "impossible" sentinel value (e.g. -1) so you know on the first time though if (minim == sentinel) then you set minim

INT_MAX is in limits.h - see this qustion for some more details Is there a LARGEST_INTEGER macro or something similar? (C)

no 1 is simplest and works in all cases.

As pointed out in the comments: the code as written is straight sequential and so you could just say minim = number; in the "Enter first number block". The reason I don't advise this is because if/when OP puts the data entry into a loop you need special case handling on the i=0 case. I guess it is not all that different to using a sentinel so it follows that INT_MAX is the only sensible answer.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • 2
    You could just assign the first value to minim. – Aiden Grossman Mar 26 '18 at 21:58
  • 1
    @AidenGrossman true in this case, but when OP realises there really should be a loop then that becomes special case handling inside the loop. – John3136 Mar 26 '18 at 22:00
  • Yes, it's a special case to _unconditionally_ set the first value when using a loop (e.g. `for (int i = 0; i < ...; ++i) { if (i == 0) { minim = number; max = number; } if (number < minim) minim = number; if (number > max) max = number; }` but it's low overhead and avoids the need for `limits.h`. Also, if the problem were slightly different [with a function `get_minmax(int *arr,int count)`], the unconditional set could be done _before_ the loop within the function. Particularly, if the raw values need to be kept to do other operations later. So, 50/50 as to approach ... – Craig Estey Mar 26 '18 at 22:18
0

Just make first number as minim like below code:

printf("Please enter your first number: "); scanf("%i", &number); **minim=number;**

0

Instead of taking 5 input in number use loop and set the value minim and max to first input.

I have changed the code little bit so I am giving the full code below.

int main() {
int choice , number, sum = 0, count = 0, minim = 0, max = 0;
float average;
for(int i=0;i<5;i++){
        printf("Enter %d number\n",i+1);
        scanf("%d", &number);
        if(i==0){
            max=number;
            minim=number;
        }
        else{
        if(number>max){
            max=number;
        }
        if(number<minim){
            minim=number;
        }
        }
        sum+=number;
    }
average=sum/5;
while (1) {       // infinite loop
    printf("\n\nChoose an option from the menu below to see the results (1,2,3,4) or use (-1) to exit:\n");
    printf("1.  Smallest number entered\n");
    printf("2.  Largest number entered\n");
    printf("3.  Sum of the five numbers entered\n");
    printf("4.  Average of the five numbers entered\n\n");
    printf("\nEnter your selection: ");
    scanf("%i", &choice);

    switch (choice) {
        case 1:
            printf("\n The smallest number entered is %i\n", minim);
            break;
        case 2:
            printf("\n The largest number entered is %i\n", max);
            break;
        case 3:
            printf("\n The sum of the five numbers entered is %i\n", sum);
            break;
        case 4:
            printf("\n The average of the five numbers entered is %.2lf\n", average);
            break;
        case -1:
            printf("\n Program Exit\n");
            exit(0);
        default: 
            printf("\nIncorrect menu option selected.\n");
    }
}

printf("\nThank you for your time. Exiting program.\n");
    return 0;
}

Hopefully that help.

Lakshraj
  • 291
  • 2
  • 18
  • it is a poor programming practice to include header files those content is not used. For instance, the contents of `math.h` are not being used, so that statement should be removed. – user3629249 Mar 27 '18 at 14:39