【问题标题】:am trying to sorts the result of an histogram in c我正在尝试对 c 中直方图的结果进行排序
【发布时间】:2022-11-29 03:04:42
【问题描述】:

嗨,我写了下面的代码来给出矩阵中每个数字出现的直方图

#include<stdio.h>

int main() {
  int i, j, existe, k, n, size, tab[10][10], elements[100], histo[100];
  do {
    printf("\n Entrer la taille de la matrice : ");
    scanf("%d", & n);
  } while (n <= 0 || n > 50);
  /* TAKING MATRIX INPUT */
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      printf(" tab[%d][%d]=", i, j);
      scanf("%d", & tab[i][j]);
    }
  }
  /* MATRIX DISPLAY */
  printf("\nla matrice:");
  for (i = 0; i < n; i++) {
    printf("\n");
    for (j = 0; j < n; j++)
      printf("%4d", tab[i][j]);
  }

  /* initialization of the array 'elements' */
  elements[0] = tab[0][0];
  size = 0; //size' is the size of the array 'elements'

  /*filling the array 'elements'*/
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      existe = 0;
      for (k = 0; k <= size; k++)
        if (elements[k] == tab[i][j]) {
          existe = 1;
          break;
        }
        if (!existe) {
        size++;
        elements[size] = tab[i][j];
      }
    }
  }

  /*display of the array 'elements'*/
  printf("\n Affichage du tableau elements \n");
  for (i = 0; i <= size; i++)
    printf("%d ", elements[i]);

  /* initialization of the array 'histo' */
  for (i = 0; i <= size; i++)
    histo[i] = 0;

  /*filling the array 'histo' */
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      for (k = 0; k <= size; k++)
        if (elements[k] == tab[i][j]) {
          histo[k] += 1;
          break;
        }
    }
  }

  /*Affichage du tableau histo */
  printf("\n Affichage du tableau histo \n");
  for (i = 0; i <= size; i++)
    printf("%d ", histo[i]);

  /display of the histogram/
  printf("\n");
  printf("\n Affichage de l\'histogramme \n");
  for (i = 0; i <= size; i++) {
    printf("\n %d : ", elements[i]);
    for (j = 0; j < histo[i]; j++)
      printf("* ");
  }
  return 0;
}

例如,结果是这样给出的


 Entrer la taille de la matrice : 4
 tab[0][0]=3
 tab[0][1]=2
 tab[0][2]=5
 tab[0][3]=3
 tab[1][0]=5
 tab[1][1]=5
 tab[1][2]=2
 tab[1][3]=3
 tab[2][0]=3
 tab[2][1]=2
 tab[2][2]=3
 tab[2][3]=5
 tab[3][0]=5
 tab[3][1]=3
 tab[3][2]=3
 tab[3][3]=3

la matrice:
   3   2   5   3
   5   5   2   3
   3   2   3   5
   5   3   3   3
 Affichage du tableau elements
3 2 5
 Affichage du tableau histo
8 3 5

 Affichage de l'histogramme

 3 : * * * * * * * *
 2 : * * *
 5 : * * * * *

我需要将直方图从最低到最高排序 IE 2:* * * 3:* * * * * * * * 5:* * * * *

我试着像这样对数组元素进行排序

    for (i = 0; i < n; ++i) 
        {
 
            for (j = i + 1; j < n; ++j)
            {
 
                if (elements[i] > elements[j]) 
                {
 
                    a =  elements[i];
                    elements[i] = elements[j];
                    elements[j] = a;
 
                }
 
            }
 
        }

我得到的是元素 ordre 改变了开始没有 2:* * * * * * * * 3:* * * 5:* * * * * 我该怎么办 这不是家庭作业,它只是互联网上的练习,请不要禁止我的问题

【问题讨论】:

  • 为什么不用qsort
  • 旁白:您将 n 作为范围 [1, 50] 中的值,但将其用作索引定义为 int tab[10][10] 的数组的上限。任一维度的有效索引为 [0, 9]。 Undefined Behaviour 的空间很大。 while (n &lt;= 0 || n &gt; 50)应该是while (n &lt;= 0 || n &gt; 10)

标签: arrays c sorting


【解决方案1】:

您需要根据histo中的值同时对histoelements进行排序。

for (i = 0; i <= size; ++i) {
    for (j = i + 1; j <= size; ++j) {    
        if (histo[i] > histo[j]) {    
            a = histo[i];     
            histo[i] = histo[j];    
            histo[j] = a;    
                
            a = elements[i];     
            elements[i] = elements[j];    
            elements[j] = a;    
        }    
    }    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多