c++ - 为什么调试器在排序之前显示问题?

标签 c++

我用 C 编写的程序有问题。我想将 int 数字绘制到 2 大小的数组中,显示它,然后按行对数字进行排序,然后用数组进行转置,但它不起作用。调试器显示与函数 main() 中的 printf 一致的错误,但我完全不知道出了什么问题。 感谢大家的支持。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>//dla swapa
void draw(int **tab, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            tab[m][n] = rand() % (100 + 1); //0-100
        }
    }
}


void sort(int **tab, int m, int n)
{
    m = 0;
    while (m) 
    {
        for (int j = n - 1; j > 0; j--) 
        {
            for (int i = 0; i < n; i++)
            {

                if (tab[i] > tab[i + 1])
                {
                    std:: swap(tab[i], tab[i + 1]);
                }
            }
        }
        m++;
    }
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("|%d||[%d|\n", tab[i][j]);
        }
    }
}
int main()
{
    int w, k;
    int **tab;
    printf("Lines: ");
    scanf(" %d", &w);
    printf("columns: ");
    scanf(" %d", &k);

    tab = (int**)malloc(w*sizeof(int*));
    for (int i = 0; i < k; i++) {
        tab[i] = (int*)malloc(k * sizeof(int));
    }
    printf("\nBefore sorting: \n");
    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < k; i++)
        {
            printf("|%d||%d|", tab[i][j]);
        }
    }
    // lub     tab=(int*)malloc(w*k*sizeof(int));
    srand(time(NULL));
    draw(tab, w, k);
    printf("\nSorted array:\n");
    sort(tab,w,k);

}

最佳答案

  1. 线路 for (int i = 0; i < k; i++) {应该是for (int i = 0; i < w; ++i)

  2. 线路 printf("|%d||%d|", tab[i][j]);应该是printf("tab[%d][%d] = %d\n", i, j, tab[i][j]);

  3. 您的函数sort()是错误的。

  4. 您应该使用 row , col不是w , k

以下code可以工作:

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

void draw(int **tab, int row, int col) {
  for (int i = 0; i < row; ++i)
    for (int j = 0; j < col; ++j)
      tab[i][j] = rand() % (100 + 1);
}

void sort(int **tab, int row, int col) {
  int x = 0;
  int y = 0;
  while (x != row) {
    int temp = tab[x][y];
    for (int j = y + 1; j != col; ++j)
      if (tab[x][j] < tab[x][y])
        std::swap(tab[x][j], tab[x][y]);
    for (int i = x + 1; i != row; ++i)
      for (int j = 0; j != col; ++j)
        if (tab[i][j] < tab[x][y])
          std::swap(tab[i][j], tab[x][y]);
    ++y;
    if (y == col) {
      y = 0;
      ++x;
    }
  }
}

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

int main() {
  int row, col;
  int **tab;
  printf("Lines: ");
  scanf(" %d", &row);
  printf("columns: ");
  scanf(" %d", &col);

  tab = (int **)malloc(row * sizeof(int *));
  for (int i = 0; i < row; ++i)
    tab[i] = (int *)malloc(col * sizeof(int));
  printf("\nBefore sorting: \n");
  srand(time(NULL));
  draw(tab, row, col);
  output(tab, row, col);
  printf("\nSorted array:\n");
  sort(tab, row, col);
  output(tab, row, col);
  return 0;
}

关于c++ - 为什么调试器在排序之前显示问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54063475/

相关文章:

具有函数指针替换的 C++ 正则表达式库

c++ - 内存分配小题C++

c++ - 将命令行参数传递给从 C++ 程序调用的 bash shell 脚本

c++ - 通过检查复制/移动省略

c++ - 结构对象析构函数

c++ - 如何以编程方式判断两个变量是否位于同一个堆栈上? (在 Windows 中)

python - struct.error : unpack requires a buffer of 288 bytes when unpacking c++ structure

c++ - 你怎么能 emplace_back 错误的类型呢?

c++ - 在 Cppcheck 中处理 "incompatibily"重载名称

c# - 用 C++ 开发 Win32 应用程序比用 C# 开发 .NET 应用程序有什么优势?