Bowlslaw
Bowlslaw

Reputation: 95

Advice on optimizing this C program?

wondering if there is a way I can shrink this program. I don't like the look of the 5 printf/scanf pairs, but I can't think of a way to use a for loop to shrink it. I have trouble with matching the scanf calls to the printf calls.

for(i = 1; i < 6; i++)
    printf("Enter row %d", i);

this part is easy, however, the scanf calls and assignments into the array are baffling me.

//Sums row and column
#include <stdio.h>

int main(void) {

    int a[5][5];
    int row[5] = {0}, col[5] = {0};
    int i, j;

    printf("\t--Enter a 5x5 chart--\n");
    printf("\tEnter row #1: ");
    scanf("%d %d %d %d %d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4]);

    printf("\tEnter row #2: ");
    scanf("%d %d %d %d %d", &a[1][0], &a[1][1], &a[1][2], &a[1][3], &a[1][4]);

    printf("\tEnter row #3: ");
    scanf("%d %d %d %d %d", &a[2][0], &a[2][1], &a[2][2], &a[2][3], &a[2][4]);

    printf("\tEnter row #4: ");
    scanf("%d %d %d %d %d", &a[3][0], &a[3][1], &a[3][2], &a[3][3], &a[3][4]);

    printf("\tEnter row #5: ");
    scanf("%d %d %d %d %d", &a[4][0], &a[4][1], &a[4][2], &a[4][3], &a[4][4]);

    for(i = 0; i < 5; i++) {
            for(j = 0; j < 5; j++) {
                    row[j] += a[j][i];
                    col[j] += a[i][j];
            }
    }

    printf("\tRow sums: %d %d %d %d %d\n", row[0], row[1], row[2], row[3], row[4]);
    printf("\tColumn sums: %d %d %d %d %d\n", col[0], col[1], col[2], col[3], col[4]);

    return 0;
}

Upvotes: 0

Views: 180

Answers (5)

Clifford
Clifford

Reputation: 93476

@Jim Balter already mentioned this in his comment to the currently accepted answer, but it deserves being posted as an answer since it is a better and more maintanable solution.

for (i = 0; i < ROWS; i++) 
{
    printf("\tEnter row #%d: ", i+1);

    for( j = 0; j < COLUMNS; j++ )
    {
        scanf("%d", &a[i][j] ) ;
    }
}

Note that scanf() uses any whitespace as a delimiter so, the 'row' need not be entered in a single scanf() call.

Upvotes: 0

Will Dean
Will Dean

Reputation: 39500

You can do something like this:

int rowIndex;

... 

for(rowIndex = 0; rowIndex < 5; rowIndex++)
{
   printf("\tEnter row #%d: ", rowIndex+1);
    scanf("%d %d %d %d %d", &a[rowIndex][0], &a[rowIndex][1], &a[rowIndex][2], &a[rowIndex][3], &a[rowIndex][4]);
}

Upvotes: 0

Anteru
Anteru

Reputation: 19384

for (int i = 0; i < 5; ++i)
{
    printf ("\tEnter row #%d: ", (i+1));
    scanf("%d %d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]);
}

Upvotes: 0

MacGucky
MacGucky

Reputation: 2504

What about the following to read the input:

for (int i = 0; i < 5; ++i) {
   printf("\tEnter row #%i: ", i+1);
   scanf("%d %d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]);
}

Upvotes: 0

typo.pl
typo.pl

Reputation: 8942

Replace:

printf("\tEnter row #1: ");
scanf("%d %d %d %d %d", &a[0][0], &a[0][1], &a[0][2], &a[0][3], &a[0][4]);         
...
printf("\tEnter row #5: ");
scanf("%d %d %d %d %d", &a[4][0], &a[4][1], &a[4][2], &a[4][3], &a[4][4]);

with:

for (i = 0; i < 5; i++) {
    printf("\tEnter row #%d: ", i+1);
    scanf("%d %d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]);
}

Upvotes: 4

Related Questions