关于优化这个C程序的建议?

发布于 2024-10-27 14:32:21 字数 1443 浏览 4 评论 0原文

想知道是否有办法可以缩小这个程序。我不喜欢 5 个 printf/scanf 对的外观,但我想不出使用 for 循环来缩小它的方法。我无法将 scanf 调用与 printf 调用相匹配。

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

这部分很简单,但是 scanf 调用和数组赋值让我感到困惑。

//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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最美的太阳 2024-11-03 14:32:21

替换:

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]);

为:

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]);
}

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]);
}
ヤ经典坏疍 2024-11-03 14:32:21

下面的内容如何读取输入:

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]);
}

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]);
}
回眸一遍 2024-11-03 14:32:21
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]);
}
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]);
}
遥远的她 2024-11-03 14:32:21

你可以这样做:

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]);
}

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]);
}
赏烟花じ飞满天 2024-11-03 14:32:21

@Jim Balter 已经在对当前接受的答案的评论中提到了这一点,但它值得作为答案发布,因为它是一个更好、更可维护的解决方案。

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

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

请注意,scanf() 使用任何空格作为分隔符,因此不需要在单个 scanf() 调用中输入“行”。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文