1

I'm trying to assign user input using a two-dimensional array that takes five numbers and squares them. The program should calculate and store the squared values for each user in the array.

I'm getting the right output with the current code but I can't figure out how to assign the input to the array, without getting errors in the output.

#include <stdio.h>

int main(void)
{
    int disp[2][5];

    int x, y;
    printf("Please enter 5 integer values:\n");

    for(x=0; x<=2; x++) {

        for (y=0; y<=5; y++) {
            scanf("%d", &x);
            printf("[ %d ][ %d ]\n", x, x*x);
        }
    }

    return 0;
}
3
  • 3
    You're using x as a loop variable, and as the input variable. That's bad. Also, when an array has 5 elements, the indexes into the array are 0,1,2,3,4. So the for loop should be for(y=0;y<5;y++). Commented Jan 17, 2016 at 0:09
  • 1
    Use one variable to loop, and another to do the reading. Then just disp[x][y] = read_variable; Commented Jan 17, 2016 at 0:10
  • I think you're supposed to have three loops. The first loop reads five numbers from the user and stores them in the first row of the array. The second loop computes the square of each number and stores it in the second row of the array. The third loop displays the numbers and their squares. Commented Jan 17, 2016 at 0:20

2 Answers 2

2

The following code scans five numbers, and stores the squares in the array (which is never used again):

#include <stdio.h>

#define NUM_ITEMS 5 // store the number of items in one place

int main()
{
    int disp[2][NUM_ITEMS];
    int i;

    printf("Please enter %d integer values:\n", NUM_ITEMS);

    for (i = 0; i < NUM_ITEMS; ++i) {

        if ( scanf("%d", &disp[0][i]) == 1 ) {
            // always check the return value of scanf()

            disp[1][i] = disp[0][i] * disp[0][i]; // store squares in array
            printf("[ %d ][ %d ]\n", disp[0][i], disp[1][i]);
        }
        else {
            printf("Error in scanf()\n");
            break;
        }
    }

    return 0;
}

I'm trying to assign user input using a two-dimensional array that takes five numbers and squares them. The program should calculate and store the squared values for each user in the array.

Some issues with your code:

for(x=0; x<=2; x++) { // overflows array (0, 1 and 2)
                      // should be x < 2
    for (y=0; y<=5; y++){ // idem -> y < 5
        scanf("%d", &x);  // scanf() is called 10 times
                  // x is already used as a loop counter!
        printf("[ %d ][ %d ]\n", x, x*x);
             // squares are never stored in the array
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you intend to fill this array disp[2][5]; you can enter 2*5=10 integers. I assume that is what you intend. Please note you have to change for loop conditions to avoid index overflow of array.

You have to change as follows:

printf("Please enter 10 integer values:\n");
for(x=0; x<2; x++)
    {
        for (y=0; y<5; y++){
            scanf("%d", &disp[x][y]);
            printf("[ %d ][ %d ]\n", disp[x][y], disp[x][y]*disp[x][y]);
        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.