why do i get overflow?

Discussion in 'C' started by SaKaMoToSaN15, Jan 12, 2018.

  1. SaKaMoToSaN15

    SaKaMoToSaN15 New Member

    Joined:
    Jan 7, 2018
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Here is my code:
    Code:
       
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ROWS_LENGHT 10
    
    void inputNums(int timesTable[][ROWS_LENGHT]);
    void printTable(int timesTable[][ROWS_LENGHT]);
    
    int main()
    {
        int timesTable[ROWS_LENGHT][ROWS_LENGHT] = {0};
        inputNums(timesTable);
        printTable(timesTable);
        return 0;
    }
    
    void inputNums(int timesTable[][ROWS_LENGHT])
    {
        int i = 0;
        int j = 0;
        for(i = 1 ; i <= ROWS_LENGHT ; i++)
        {
            for(j = 1 ; j <= ROWS_LENGHT ; j++)
            {
                    timesTable[j] = i*j;
            }
        }
     
    }
    
    void printTable(int timesTable[][ROWS_LENGHT])
    {
        int i = 0;
        int j = 0;
        int number = 1;
        for(i = 1 ; i <= ROWS_LENGHT ; i++)
        {
            for(j = 1 ; j <= ROWS_LENGHT ; j++ )
            {
                if(number % 11 == 0)
                {
                    printf("\n");
                    printf("%d ",timesTable[j]);
                }
                else
                {
                    printf("%d ",timesTable[j]);
                }
            }
        }
    }

    Im almost sure that i get overflow, even thou i dont use more the 11 arrays 11 places in every array.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Code:
    for(i = 1 ; i <= ROWS_LENGHT ; i++)
    isnt right.

    It should be 0 to ROWS_LENGHT
     
  3. vishaljamdagni

    vishaljamdagni New Member

    Joined:
    Feb 14, 2018
    Messages:
    2
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Male
    Hey there,
    Well in the first loop you can change the value of "i" to 0 as indicated by shabbir, but there is one more thing that might be coming in your way which is the initial value of "j" which you've initialized the same as that of "i".
    I'd recommend try initializing the value of "j=i+1" this should solve your issue.
    Hope it helps.
    Cheers
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice