Thread: Program skips for cycle

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    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:
    #include <stdio.h>
    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;
    
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > 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();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    738
    You don't even need an array for this problem. Here's how I interpret the problem statement:

    1. Read a number.
    2. If the number is less than or equal to zero, end.
    3. Print the running sum.
    4. Repeat from 1.

    Try converting that pseudo-code to C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program skips the second scanf. pls help!
    By aske10 in forum C Programming
    Replies: 5
    Last Post: 01-08-2016, 03:39 AM
  2. Program skips scanf
    By nic050 in forum C Programming
    Replies: 2
    Last Post: 06-05-2015, 10:54 PM
  3. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM
  4. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 PM
  5. Program skips condition which uses argv[]
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-12-2002, 02:44 AM

Tags for this Thread