有关矩阵的循环实现问题

你想要的未来

我是C语言的新手,我正在此程序中学习矩阵。下面的程序旨在从用户那里读取行和列(我将通过选择数字来实现其他任务)。矩阵是根据用户给定的大小随机给出的。给定1时,程序应再次询问行和列,但它不起作用,它仅更改矩阵,而不更改其大小。这可能与指针有关,但我无法弄清楚。以下是可编译的代码。

#include<stdio.h>
#include <time.h>
#include <stdlib.h>

#define max_row 20
#define max_col 20

typedef int Matrix[max_row][max_col];

void Read_Matrix(int *row, int *col)
{
    while(*row > max_row || *row < 1 || *col > max_col || *col < 1)
    {
        printf("\n***** Matrix *****");
        printf("\nEnter rows >> ");
        scanf(" %d", row);
        printf("\nEnter columns >> ");
        scanf(" %d", col);
    }
}

void Random_Matrix(Matrix M, int row, int col)
{
    time_t seg = time(NULL);
    srand(seg);
    int i, j;

    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            M[i][j] = rand()%10;
        }
    }
}

void Show_Matrix(Matrix M, int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
       printf("\n");
       for (j = 0; j < col; j++)
       {
           printf(" %d ", M[i][j]);
       }
   }
   printf("\n");
}

int main()
{
    int choice, row, col;
    Matrix M;

    while(1)
    {
        /*

        Tasks concerning the Matrix

        */

        printf("\n\nEnter the task >> ");
        scanf(" %d", &choice);

        switch(choice)
        {
            case 1:
               Read_Matrix(&row, &col);
               Random_Matrix(M, row, col);
               Show_Matrix(M, row, col);
               break;
           case 2:
               printf("\nExit\n");
               exit(0);
               break;
        }
    }
}
乔纳森·莱夫勒

正如我在评论中指出的:

  1. 您需要每次测试scanf()的返回值,以确保它不会失败。
  2. srand()-为什么叫它只有一次?当然,如果有用户在键入,那么您可能会得到不同的种子,但是最好养成不要srand()多次调用的习惯
  3. 主要的问题是,当你调用Read_Matrix()第二次,在价值观*row*col已经达到循环的标准,所以它永远不会重新进入循环。您需要*row = *col = 0;在功能顶部添加或等效功能。此外,这是可以想象的是,在价值*col*row符合条件,你首先调用函数之前-您没有初始化row,并colmain()让你不知道他们的初始值。

将这些更改放在一起产生(源mat17.c,程序mat17):

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define max_row 20
#define max_col 20

typedef int Matrix[max_row][max_col];

static
void Read_Matrix(int *row, int *col)
{
    *row = *col = 0;
    while (*row > max_row || *row < 1 || *col > max_col || *col < 1)
    {
        printf("\n***** Matrix *****");
        printf("\nEnter rows >> ");
        if (scanf(" %d", row) != 1)
        {
            fprintf(stderr, "Unexpected error or EOF\n");
            exit(1);
        }
        printf("\nEnter columns >> ");
        if (scanf(" %d", col) != 1)
        {
            fprintf(stderr, "Unexpected error or EOF\n");
            exit(1);
        }
    }
}

static
void Random_Matrix(Matrix M, int row, int col)
{
    time_t seg = time(NULL);
    srand(seg);
    int i, j;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            M[i][j] = rand() % 10;
        }
    }
}

static
void Show_Matrix(Matrix M, int row, int col)
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        printf("\n");
        for (j = 0; j < col; j++)
        {
            printf(" %d ", M[i][j]);
        }
    }
    printf("\n");
}

int main(void)
{
    int choice, row, col;
    Matrix M;

    while (1)
    {
        printf("\n\nEnter the task >> ");
        if (scanf("%d", &choice) != 1)
        {
            fprintf(stderr, "Unexpected error or EOF\n");
            exit(1);
        }

        switch (choice)
        {
        case 1:
            Read_Matrix(&row, &col);
            Random_Matrix(M, row, col);
            Show_Matrix(M, row, col);
            break;
        default:
            printf("\nExit\n");
            exit(0);
            break;
        }
    }
}

样品输出

$ mat17


Enter the task >> 1 2 2 1 3 3 1 4 4 1 2 2 2

***** Matrix *****
Enter rows >> 
Enter columns >> 
 4  5 
 0  3 


Enter the task >> 
***** Matrix *****
Enter rows >> 
Enter columns >> 
 4  5  0 
 3  2  0 
 1  3  8 


Enter the task >> 
***** Matrix *****
Enter rows >> 
Enter columns >> 
 4  5  0  3 
 2  0  1  3 
 8  4  1  3 
 4  3  7  2 


Enter the task >> 
***** Matrix *****
Enter rows >> 
Enter columns >> 
 4  5 
 0  3 


Enter the task >> 
Exit
$

请注意,此运行在一行输入中输入描述4个矩阵的日期。它表明调用srand()太多意味着对4个矩阵中的每个矩阵都生成相同的数据序列。有几种可能的修复:

  1. 只能拨打srand()一次。
  2. 读取一行(例如fgets())并解析一次(例如使用sscanf()),如果它包含多个数字,则可能拒绝输入,或者可能在循环中处理输入(请参见如何sscanf()在循环中使用

同时应用选项1和2可能是最好的。应用选项1几乎是强制性的,或者您必须设计一种机制来确保每次调用的种子都不同,即使调用在一秒钟内多次发生。例如,捕获unsigned seed = time(0);一次并srand(seed++)用于每个呼叫。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章