0

I'm trying to make a program which allows the user to insert 5 numbers, define if they are even or odd, store them to an array and print the numbers that the user insert into the array.

It's a pretty simple program that I'm doing just to learn how to use the arrays. The problem is that it doesn't print out the values of the array. Why?

int main () {
int list [5];
int i = 0;

while (i < 5) {
    printf("Insert the number\n");
    scanf("%d", &list[i]);
    
    if (i % 2 == 0) {
        printf("Even\n");
    }
    else{
        printf("Odd\n");
    }
    i++;
}

i=0;
while (i < 5){
    printf("%d\n", &list[i]);
    i++;
}
return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Nov 22 '20 at 21:02
  • Just as a side note: It is unsafe to use `scanf` without checking the return value. See this page for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) – Andreas Wenzel Nov 22 '20 at 21:03
  • Just as a side note: If you write `for ( i = 0; i < 5; i++ )`, then you don't need the lines `i = 0` and `i++`. This is a much more compact way of writing it. But the way you are doing it is not wrong. If you feel more comfortable doing it your way, there is nothing wrong with that while learning the language. – Andreas Wenzel Nov 22 '20 at 21:07
  • Regarding the last comment, I know it would be easier, but my teacher did not make that topic and therefore does not allow us to do something that he hasn't already explained. Anyway thanks for the tips – Sub0Zero1990 Nov 22 '20 at 21:13
  • Because you are new to Stack Overflow and were replying to my comment without notifying me, I would like to point out the following: Since you are commenting on a question that does not belong to me, if you want me to be notified of your comment, it must contain `@AndreasWenzel`. You just have to type `@` and the first few characters, autocomplete (TAB key) will type the rest for you. Otherwise, I may not notice your reply (I did notice this time). – Andreas Wenzel Nov 22 '20 at 21:25

2 Answers2

3

You are using an incorrect argument in the call of the function printf

printf("%d\n", &list[i]);
               ^^^^^^^^

You have to write

printf("%d\n", list[i]);
               ^^^^^^^

That is when you use the function scanf you have to pass the argument by reference

scanf("%d", &list[i]);

In C passing by reference means passing the argument indirectly through a pointer to it.

When you use the function printf it is enough to pass the argument by value because it is the value that is outputted.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Don't use the address of operator in your printf statement.

printf("%d\n", &list[i]);

It should be:

printf("%d\n", list[i]);
LEF
  • 188
  • 1
  • 9
  • Although your answer is correct, I am not upvoting it, because it contains no more information than in the answer that was posted 7 minutes before your answer. – Andreas Wenzel Nov 22 '20 at 21:17