Originally Posted by
Kayla Hoyte
That is a brilliant idea. I did so and I see that 'i' does not add one to itself. Do I need to add 'i += 1'?
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
int main()
{
int value = 0, arr[SIZE] = {0}, i = 0;
printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
scanf("%d", &value);
i+=1; // get rid of that it does not belong
// printf("%i \n", i); i is 1 but turns to 0?
// need i to start at 0 and add one
while (i < 50 && value != -999){
printf("arr[%i] = %i \n", arr[i], value);
printf("Enter another number. Type -999 to stop the loop: ");
scanf("%i", &value);
i++; / / this belongs
// printf("%i \n", i);
// i staying as 0l
}
return 0;
}
with your i on top it sets i = 1 so your array [ 0 ] never gets a value inside of it.
whence you got that corrected leave it alone .
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
int main()
{
int value = 0, arr[SIZE] = {0}, i = 0; // Don't forget to initialize all of your variables.
printf("Enter up to 50 numbers. Type and enter -999 to stop the loop: ");
// You need to get the first value from the user prior to the while loop.
scanf("%d", &value);
while (i < 50 && value != -999) // Notice the change here.
{
printf("loop i set to 0 at start\non top of loop i = %d\n", i);
arr[i] = value;
printf("arr [ %i ] = value = %i i = %d\n", arr[i], value, i);
i++;
printf("on bottom of of loop i = %d\n", i);
printf("Enter another number. Type -999 to stop the loop: "); // keeps being printed with the other printf
scanf("%i", &value);
}
int j = 0;
for ( ; j < 50; j++)
printf("element number %d\n count = %d \n arr = %d\n", j,j+1, arr[j]);
return 0;
}