-4
int main()
{
    int i;
    float number[6];
    for (i=0; i<=2; i++) {
        printf("write number %i\n", i+1);
        scanf("%i",&number[i]);
    }
    printf ("the first number you wrote: %i", number[1]);
    return 0;
}

I am having trouble understanding how to print out the first number in an indexed variable. The answer is always 0, if i type number[1] or number[5] the output is always 0

Cherubim
  • 5,287
  • 3
  • 20
  • 37
Benjamin
  • 43
  • 6

1 Answers1

1

I dont know what to write when im printing out the first number, the is answer always 0.

but from this statement it's clear that you are not printing the first number entered

printf ("the first number you wrote: %i", number[1]);

as number[1] refers to the second number you entered, try using number[0] instead of number[1]

printf ("the first number you wrote: %i", number[0]);

apart from the above one, there is one more problem.you are using the wrong format specifier for float in your scanf() and printf statements

use %f when scanning or printing float's, %i is for int's

generally using wrong format specifiers results in undefined behavior, have a look at this : What can happen if printf is called with a wrong format string?

additionally, try avoiding the use of printf() an scanf() and use std::cout and std::cin as others have pointed out.

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37