0

I wrote this little code just to start learning some if statements and C coding in general. However, there is an issue. When running it, if the largest element is the last one, the code won't recognize it. Why is that?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(){
int num[100];
int max;
int i;

    printf("Enter 10 numbers: \n");
    for(i = 1; i < 10; i++){
        scanf("%d\n", &num[i]);
    }
max = num[0];
    for(i = 0; i < 10; i++){

        if(max < num[i]){
            max = num[i];
        }

    }


printf("The biggest nr is: %d", max);


return 0;
}

3 Answers3

2

Your first loop should start from 0, not 1.

for(i = 0; i < 10; i++){
    scanf("%d\n", &num[i]);
}

max already starts with an uninitialized value, here be dragons.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • Thank you for your anwser. However, if i change i=0, the program asks me for 11 inputs before moving on. And among those 11 inputs, the program still won't count the last one, if it is the largest. – Siim Hendrik Jul 20 '20 at 06:01
  • That is an impossibility. Perhaps the code you posted vs what you are actually running has even more differences. Check whether your condition says `i < 10` or `i <= 10`. It should use `<` operator. – Tanveer Badar Jul 20 '20 at 06:18
  • 1
    Your second problem is the scanf function, which often keeps garbage in the buffer. Therefore on the first run of the read-in loop you read a garbage value. Simply print the index of your current loop and the value of the cell in the array to debug this behaviour. – BallisticTomato Jul 20 '20 at 06:20
  • There are lots of SO questions discussing the many issues of scanf and how to solve them by input checking, buffer flushing and what not. Often the recommandation is to use a different function, which is better usable for recovery after wrong input. Maybe you can start here: https://stackoverflow.com/questions/32393392/when-do-we-need-to-clear-the-scanf-buffer – BallisticTomato Jul 20 '20 at 06:22
  • Mhh, interesting. Here I have the IDE and terminal showing the code and the results. Maybe this will shed some light on the problem. https://imgur.com/a/ScXphJG As for using a different function, I will try it, however I still would like to know what causes this issue. Is it just, that scanf is a bad function? – Siim Hendrik Jul 20 '20 at 06:33
  • Like I said, you should try to print your index "i" with each iteration to get a better grasp of the issue. Personally I do not consider functions good or bad, they just have different purposes and handling. You probably can achieve your goal with scanf as well for example by clearing out the buffer and sanity checking your input. On the other hand you could maybe achieve the same in a more convenient way with functions like fgets(). At the end you need to decide, what fits best. Often it makes sense to stick with best practice approaches, which is not scanf in this case. – BallisticTomato Jul 20 '20 at 06:54
0

Inside of:

for (i = 1; i < 10; i++) {
    scanf("%d\n", &num[i]);
}

max = num[0];

max has an indeterminate value because the loop's counter variable i starts at 0, not 1 which gives the result that the first element of the array wasn't assigned inside of the loop. So you end up assigning this indeterminate value to max.

To use an indeterminate value in the following code:

if (max < num[i]) {
     max = num[i];
}

invokes undefined behavior.


"However, if I change i=0, the program asks me for 11 inputs before moving on. And among those 11 inputs, the program still won't count the last one, if it is the largest."

"When running it, if the largest element is the last one, the code won't recognize it. Why is that?"

It doesn't actually ask you for an 11th input for any presumed 11th array element as you think it does and the last in the loops1 treated element of the array is not the one you think it is. That is just an impression to you.

This behavior is caused by the newline character in the format string of the scanf() call:

scanf("%d\n", &num[i]);

The newline character (\n ) is equal to any white space and with this directive, scanf() reads an unlimited amount of white space characters until it finds any-non white space character in the input to stop consuming and the control flow continues to the next statement.

Why does scanf ask twice for input when there's a newline at the end of the format string?

It doesn't ask for the input of the 11th element of the array (as you think it does). It simply needs any non-white space character that the directive fails.

The last element of the array (which is treated inside of the loops1) is still the 10th (num[9]), not the 11th (num[10]) and so is the output correct when you initialize the counter to 0 and it prints:

The biggest nr is: 10

because 10 is the value of the last treated element num[9].


1) Note that you made a typo at the declaration of num -> int num[100];. With this you define an array of one hundred elements, but you actually only need one of 10 elements -> int num[10];.


Side Note:

  • Also always check the return value of scanf()!

    if (scanf("%d\n", &num[i]) != 1)
    {
       // Error routine.
    }
    
0

There are two problems in the code one after another:

  1. The loop should begin from 0 instead of 1:

    for (int i = 0; i < 10; i++)
    
  2. The main problem is here:

    scanf("%d\n", &num[i]);
    _________^^____________
    

    Remove the \n and your problem will be fixed.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34