-
Program skips for cycle
Hi, I have the following exercise:
"Write a C language program that reads a sequence of positive numbers from the keyboard
and for each number, print the progressive sum. The program ends when you enter a number less than or equal to zero."
Here's what I made but It seems to skip the for cycles. Help?
Output:
somma_numeri_positivi.c:23:21: warning: format specifies type 'int *' but the
argument has type 'int (*)[i][j]' [-Wformat]
if ( scanf("%d",&numbers) == 0 )
~~ ^~~~~~~
somma_numeri_positivi.c:38:5: warning: expression result unused [-Wunused-value]
getc;
^~~~
2 warnings generated.
Input a sequence of 0 numbers
3 <-- My input
[Program ended]
Code:
int main(void)
{
int i;
int j;
int n;
int sum;
int numbers[i][j];
printf("input a sequence of %d numbers\n", n);
for (j=0; j<n; j++)
{
printf("element %d: ", i);
scanf("%d", &numbers[i][j]);
}
if ( scanf("%d",&numbers) == 0 )
{
for(i=0;i<n;i++)
{
sum = 0;
for(j=0;j<n;j++)
{
sum = sum + numbers[i][j];
}
printf("sum: %d\t", sum);
}
}
printf("Program end");
getc;
}
-
> int numbers[i][j];
Since i and j are uninitialised, you've no idea whether you have a valid array.
> scanf("%d", &numbers[i][j]);
But your loop says j
> if ( scanf("%d",&numbers) == 0 )
So what were you trying to say here?
> getc;
Maybe you were trying to call getc();
-
You don't even need an array for this problem. Here's how I interpret the problem statement:
- Read a number.
- If the number is less than or equal to zero, end.
- Print the running sum.
- Repeat from 1.
Try converting that pseudo-code to C.