【问题标题】:Filling a 2D Array in C with random numbers from 0 - 99, then sorting in descending order用 0 - 99 的随机数填充 C 中的 2D 数组,然后按降序排序
【发布时间】:2018-07-12 16:30:20
【问题描述】:

这对我的家庭作业来说是个问题,但我在试图打印出数组时遇到了困难。该程序将提示用户输入“行数”和“列数”,但会立即终止而不打印任何内容,我不确定为什么会发生这种情况。

我的排序功能也不起作用。该代码给了我关于指针的错误,但目前对它们不太熟悉。如果有办法不用指针,我想那会更好。但如果最好的方法是使用指针,请包括它。会有很大帮助的

警告:赋值从没有强制转换的指针生成整数 [-Wint-conversion] 温度 = A[i];

错误:赋值给数组类型的表达式 A[i] = A[j];

错误:赋值给数组类型的表达式 A[j] = 温度;

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

#define N1 100

#define M1 100

void fillMatrix(int A[][M1], int N, int M);
int sortMatrix(int A[][M1],int rowsize, int colsize);
void displayArray(int A[][M1], int N, int M);

int main(void)
{
    int array[N1][M1] = {0};

    fillMatrix(array, N1, M1);

    displayArray(array, N1, M1);

    //sortMatrix(array, N1, M1);

    return  0;
}

void fillMatrix(int A[][M1], int N, int M)
{
    int rows = 0, columns = 0;
//ASK FOR ROWS
    printf("Enter a number of rows: ");
    scanf("%i", &rows);

    if(rows < 0 && rows >= 100)
    {
        printf("Invalid Input.\n");
        printf("Enter a number of rows: ");
        scanf("%i", &rows);
    }
//ASK FOR COLUMNS
    printf("Enter a number of columns: ");
    scanf("%i", &columns);

    if(columns < 0 && columns >= 100)
    {
        printf("Invalid Input.\n");
        printf("Enter a number of columns: ");
        scanf("%i", &columns);
    }
//FILLS ARRAY W/ RANDOM INT VALUES BETWEEN 0 AND 100
    srand(time(NULL));

    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
        {
            A[i][j] = 0 + rand() % 100;
        }
    }
}

/*int sortMatrix(int A[][M1],int rowsize, int colsize)
{
    int temp = 0;
    for(int i = 0; i < rowsize; i++)
    {
        for (int j = 0 ; j < colsize; j++)
        {
            if(A[i] > A[j])
            {
                temp = A[i];
                A[i] = A[j];
                A[j] = temp;
            }
        }
    }
    return 0;
}*/

void displayArray(int A[][M1], int N, int M)
{
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            printf("%i ", A[i][j]);
        }
    }
}

【问题讨论】:

  • A[i]A[j]。您不能分配整行,只能分配元素,例如A[i][j].
  • if(rows &lt; 0 &amp;&amp; rows &gt;= 100) 两者都不可能是真的。使用if(rows &lt; 0 || rows &gt;= 100) 并使用while 如此重复,直到输入正确的数字。
  • 如果不使用int N, int M 作为参数,为什么要传递给fillMatrixmain需要输入实际值,否则无法排序和打印。
  • 您有符号常量 M1N1 用于最大数组维度。使用这些而不是常量100。例如:if (n&lt;1 || n&gt;N) ....` --and-- printf("Rows must be from 1 to %d\n", N1);
  • 此外,您从未说过对二维数组进行排序的顺序是什么。 array[0][0] 是否应该接收第 0 行中的 (a) 最小条目; (b) 第 0 栏中的最小条目;或 (c) 整个数组中的最小条目?您的代码看起来可能意味着(d)交换整行而不重新排序该行中的元素;所有这些选项都有不同的代码来实现它们。

标签: c pointers multidimensional-array


【解决方案1】:

是的,好吧。首先sortMatrix(int A[][M1]...。在这种情况下,您将传递一个指向数组行的指针。在 C++ 中,没有其他方法可以将数组传递给函数,只能通过指针。 Look over there.

其次,当您执行类似A[i] &gt; A[j] 的操作时,您正在比较行的指针(顺便说一下,comparing pointers is UB。但是,在您的情况下,这只是比较行地址,这仍然是,不是你想要对数组进行排序时应该做的)。 我建议这个A[i][M1] &gt; A[j][M1]。这样,您将比较元素。

最后但并非最不重要。

但会立即终止而不打印任何内容

您的意思是控制台窗口正在立即关闭吗?在这种情况下,您可能需要在打印数组之后将cin.get(); 添加到您的代码中,或者system("pause");,如果您使用的是clear C,或者something like this,如果不是。

UPD。

  1. 如果您使用的是干净的 C,那么您应该使用 cin.get(); 而不是 getchar();
  2. system("pause"); 真正只有 Windows 的东西。

附言另外,请阅读 Paul Ogilvie 和 Mike Housky 的其他笔记。他们写的是好东西。

p.p.s.请原谅,如果有任何错误。英语,不是我的母语,所以请随时编辑答案。

如果您有任何问题,请随时与我联系。我会尽量回答。

【讨论】:

  • 比较指针到同一个数组是完全定义的行为。请参阅 ISO C11 标准中的 6.5.8(第 5 项)。
  • 另外,这是C所以cin.get()应该是getchar();你应该提到system("pause"); 是特定于Windows 的。 (Code::Blocks用户不需要,VS用户可以通过创建一个Win32 Console项目,然后使用Ctrl+F5“运行不调试”来避免它。)
  • @Mike 是的,关于我提到的同一个数组。是的,关于cin是真的。在家时会编辑我的帖子。谢谢你。此外,在不调试的情况下运行并不总是有效。
  • 如果项目具体是一个控制台项目,它会这样做;甚至可以捕捉到对exit() 的调用,该调用绕过了main() 中的system() 调用。 (是的,atexit() 应该处理这个问题。在某些时候,启动命令提示符并在命令窗口中运行命令开始有意义。:^)
【解决方案2】:

函数fillMatrix 从用户那里检索两个重要的值rowscolumns,它们在整个程序中都是必需的,但是当函数退出时这些值会丢失。然后继续使用固定值N1M1

您应该将函数更改为void fillMatrix(int A[][M1], int *set_rows, int *set_columns),并通过引用传递rowscolumns,以便保存它们的值。

if(rows < 0 && rows >= 100)
{
    printf("Invalid Input.\n");
    printf("Enter a number of rows: ");
    scanf("%i", &rows);
}

行的有效范围应为 1 到 M1。您还应该做好准备,以防用户再次输入错误的值。

没有对矩阵中的值进行排序的标准方法。你想对每一行进行排序吗?您可以使用qsort 对一维数组进行排序。

#define TOTAL_ROWS 100
#define TOTAL_COLUMNS 100
void fillMatrix(int A[][TOTAL_COLUMNS], int *set_rows, int *set_columns)
{
    int rows = 0, columns = 0;

    //ASK FOR ROWS
    while(1)
    {
        printf("Enter a number of rows: ");
        scanf("%i", &rows);
        if(rows > 0 && rows < TOTAL_ROWS)
            break;
        printf("Invalid Input.\n");
    }

    //ASK FOR COLUMNS
    while(1)
    {
        printf("Enter a number of columns: ");
        scanf("%i", &columns);
        if(columns > 0 && columns < TOTAL_COLUMNS)
            break;
        printf("Invalid Input.\n");
    }

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < columns; j++)
            A[i][j] = rand() % 100;

    *set_rows = rows;
    *set_columns = columns;
}

int compare(const void *a, const void *b)
{
    return (*(int*)a - *(int*)b);
}

void sortMatrix(int A[][TOTAL_COLUMNS], int rowsize, int colsize)
{
    for(int r = 0; r < rowsize; r++)
        qsort(A[r], colsize, sizeof(int), compare);
}

void displayArray(int A[][TOTAL_COLUMNS], int rows, int columns)
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < columns; j++)
            printf("%3i ", A[i][j]);
        printf("\n");
    }
}

int main(void)
{
    srand((unsigned int)time(NULL));
    int array[TOTAL_ROWS][TOTAL_COLUMNS] = { 0 };
    int rows, columns;
    fillMatrix(array, &rows, &columns);
    sortMatrix(array, rows, columns);
    displayArray(array, rows, columns);
    return  0;
}

如果您要使用自己的排序,或者专门使用冒泡排序,请为一维数组编写一个简单的冒泡排序函数,然后一次对矩阵进行 1 行排序:

void bubble_sort(int *arr, int count)
{
    for(int i = 0; i < count - 1; i++)
    {
        for(int j = 0; j < count - i - 1; j++)
        {
            if(arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void sortMatrix(int A[][TOTAL_COLUMNS], int rowsize, int colsize)
{
    for(int r = 0; r < rowsize; r++)
        bubble_sort(A[r], colsize, sizeof(int), compare);
}

最后,如果事先不知道行和列,则声明 int array[100][100] 不是最好的方法。在 gcc 编译器中,您可以使用变量数组。在 Visual Studio 中,您必须使用 malloc 才能根据用户为 rowscolumns 选择的内容动态分配数组。

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多