Home > Software engineering >  How do you use a loop to enter values into an array of a specific size in C?
How do you use a loop to enter values into an array of a specific size in C?

Time:10-17

I'm making a simple program that asks users to choose between a 3x3 and 4x4 array. When they choose the array that they want, it will initialize an array of that size and the program will ask the user to input the values one by one (going up by column first before going up by rows). After they input all the values for the array, it will display the values in the array in a "table-like" structure and display the sum of all the values in that array. The code is very repetitive and it might be hard to debug. I don't have the logic yet to use loops for this, but I want a loop that repeatedly asks the users to input the values into the 4x4 array starting from row 1 column 1 to row 4 column 4 (and when it reaches the 4th column of the 1st row, it then asks the users to input for the 2nd row 1st column, and so on until the end of the array).

#include <stdio.h>

int main(){
    
    int choice;
    while(choice != 1 && choice !=2){
        printf("Choose Matrix Size:\n");
        printf("1. 3x3\n");
        printf("2. 4x4\n");
        printf("Choice: ");
        scanf("%d", &choice);
    }
    
    int sumRow1, sumRow2, sumRow3, sumRow4, sumMatrix3, sumMatrix4;
    switch(choice){
        case 1:
            int square3[3][3];
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square3[1][1]);
            printf("Enter value for row 1 column 2: ");
            scanf("%d", &square3[1][2]);
            printf("Enter value for row 1 column 3: ");
            scanf("%d", &square3[1][3]);
            printf("Enter value for row 2 column 1: ");
            scanf("%d", &square3[2][1]);
            printf("Enter value for row 2 column 2: ");
            scanf("%d", &square3[2][2]);
            printf("Enter value for row 2 column 3: ");
            scanf("%d", &square3[2][3]);
            printf("Enter value for row 3 column 1: ");
            scanf("%d", &square3[3][1]);
            printf("Enter value for row 3 column 2: ");
            scanf("%d", &square3[3][2]);
            printf("Enter value for row 3 column 3: ");
            scanf("%d", &square3[3][3]);
            printf("\n");
            
            printf("Matrix Results:\n");
            printf("| %d %d %d |\n", square3[1][1], square3[1][2], square3[1][3]);
            printf("| %d %d %d |\n", square3[2][1], square3[2][2], square3[2][3]);
            printf("| %d %d %d |\n", square3[3][1], square3[3][2], square3[3][3]);
            
            sumRow1 = square3[1][1]   square3[1][2]   square3[1][3];
            sumRow2 = square3[2][1]   square3[2][2]   square3[2][3];
            sumRow3 = square3[3][1]   square3[3][2]   square3[3][3];
            sumMatrix3 = sumRow1   sumRow2   sumRow3;
            
            printf("Matrix Sum: %d", sumMatrix3);
            break;
            
        case 2:
            int square4[4][4];
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[1][1]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[1][2]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[1][3]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[1][4]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[2][1]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[2][2]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[2][3]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[2][4]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[3][1]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[3][2]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[3][3]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[3][4]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[4][1]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[4][2]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[4][3]);
            printf("Enter value for row 1 column 1: ");
            scanf("%d", &square4[4][4]);
            printf("\n");
            
            printf("Matrix Result:\n");
            printf("| %d %d %d %d |\n", square4[1][1], square4[1][2], square4[1][3], square4[1][4]);
            printf("| %d %d %d %d |\n", square4[2][1], square4[2][2], square4[2][3], square4[2][4]);
            printf("| %d %d %d %d |\n", square4[3][1], square4[3][2], square4[3][3], square4[3][4]);
            printf("| %d %d %d %d |\n", square4[4][1], square4[4][2], square4[4][3], square4[4][4]);
            
            sumRow1 = square4[1][1]   square4[1][2]   square4[1][3]   square4[1][4];
            sumRow2 = square4[2][1]   square4[2][2]   square4[2][3]   square4[2][4];
            sumRow3 = square4[3][1]   square4[3][2]   square4[3][3]   square4[3][4];
            sumRow4 = square4[4][1]   square4[4][2]   square4[4][3]   square4[4][4];
            sumMatrix4 = sumRow1   sumRow2   sumRow3   sumRow4;
            
            printf("Matrix Sum: %d", sumMatrix4);
            break;
    }
    
    return 0;
}

CodePudding user response:

Since you can store a 3x3 matrix inside a 4x4 matrix then you don’t need to declare 2 matrices.

int square[4][4];

Declare a variable call size to store the size of the square.

int size;

if (choice == 1) 
{
   size = 3;
}
else 
{
   size = 4;
}

Now you can loops through the array:

for (int row = 0; row < size; row  )
{
   for (int column = 0; column < size; column  ) 
   {
      printf(“Square[%d][%d]=“);
      scanf(“%d”, &square[row][column]);
    }
 }

Use similar loop for printing

CodePudding user response:

If you know about pointers, it will make life easy. But if you don’t, an easy way would just be to take n as an input, then iterate through it:

int main(){
        int n, i, j;
        scanf(“%d”, &n);
        int arr[n][n];
        for(i = 0; i < n; i  ){
                for(j = 0; j < n; j  ){
                        printf(“Enter the value at [%d][%d]”, i, j);
                        scanf(“%d”, &arr[i][j]);
                }
        }
}

P.S: Please ignore the indentation as I am typing this from my phone.

Also you don’t require switch case since you can directly assign the size from user input, unless you specifically want 1 to correspond to size 3 and 2 to size 4.

  • Related