Myron
Myron

Reputation: 23

For loop continues beyond limit (C language)

I'm trying to make a console application which displays the multiplication table multiplied by an integer. The integer is determined by the user. It is written in C.

My problem is that if the user enters a value smaller than 10 or greater than 20, the app continues the nested 'for' loop beyond its determined limit (num + 10). Otherwise it works fine.

#define _CRT_SECURE_NO_WARNINGS
#include `<stdio.h>`

void main()
{
    int row, col, num;          // Initialization of 10 x 10 array.
    int arr[10][10];

    printf("Enter a number: "); // User input of integer.
    scanf("%d", &num);

    for (row = num; row < num + 10; row++)         /* Nested for loop. Loop starts at determined
                                                   number and stops ten steps further.*/
    {
        for (col = num; col < num + 10; col++)
        {
            arr[col][row] = row * col;            
            printf("%d\t", arr[col][row]);
        }
        printf("\n");
    }
}

Upvotes: 0

Views: 570

Answers (6)

ryyker
ryyker

Reputation: 23208

The array bounds for int arr[10][10]; require using index values from 0 to 9.

In addition to paying attention to array bounds in your nested for loops, (which you are currently violating) you should do something to verify user input before using it in the for loop:

printf("Enter a number from 0 to 9: "); // User input of integer.
scanf("%d", &num);
while((num < 0) || (num > 9))// legal array indices are 0 through 9
{
     printf("%d is out of bounds, Enter another number: ", num); 
     scanf("%d", &num);
}
...

Upvotes: 0

Luciano
Luciano

Reputation: 2999

You need to keep inside the limits of your array: arr

Suppose the user enters the value 50, your loop will iterate among 50 and 60, but your array has indexes from 0 to 9 (10 as the total). So, you can offset the indexes, subtracting the base: num

Example - the array offset:

int rowindex, colindex;
for (row = num; row < num + 10; row++) {
    rowindex = row - num;
    for (col = num; col < num + 10; col++) {
        colindex = col - num;
        arr[colindex][rowindex] = row * col;            
        printf("%d\t", arr[colindex][rowindex]);
    }
    printf("\n");
}

Upvotes: 0

Sofie Vos
Sofie Vos

Reputation: 383

Like Pablo said, you're writing out of your arr boundaries. arr[col][row] is out of bounds when either col or row is not in the range of 0 to 9. Maybe the easiest fix would be to replace this:

        arr[col][row] = row * col;            
        printf("%d\t", arr[col][row]);

with just this line:

        printf("%d\t", row * col);

Upvotes: 0

Sanjeev S
Sanjeev S

Reputation: 1163

Rearranging the condition inside the for loop will solve the problem.

the for loop condition will always satisfy for the input less than 10 and greater than 20

Upvotes: 1

cdcdcd
cdcdcd

Reputation: 569

As has been stated you're overwriting the bounds of your array which will be overwriting part of the stack - this may be the portion that stores the num value.

Use different counters (i and j for indexing into array):

int main()
{
    int row, col, num;          // Initialization of 10 x 10 array.
    int arr[10][10];
    int i = 0;
    int j;

    printf("Enter a number: "); // User input of integer.
    scanf("%d", &num);

    for (row = num; row < num + 10; row++)         /* Nested for loop. Loop starts at determined
                                                   number and stops ten steps further.*/
    {
        j = 0;
        for (col = num; col < num + 10; col++)
        {
            arr[i][j] = row * col;
            printf("%d\t", arr[i][j]);
            j++;
        }
        i++;
        printf("\n");
    }
    return 0;
}

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

You are writing outside arr boundaries. Check your loop logic.

Since you want to multiply by numbers from 1 to 10, then you'll be better of with conditions like this:

for (row=0; row < 10; row++) {
    for (col=0; col < 10; col++) {
        arr[row][col] = (row+1)*(col+1);
        printf("%d\t", arr[row][col]);
    }
    printf("\n");
}

Upvotes: 2

Related Questions