【问题标题】:Incomplete User Input Histogram C不完整的用户输入直方图 C
【发布时间】:2015-10-30 20:54:07
【问题描述】:

得到一个不完整的直方图,需要为项目完成。

   #include <stdio.h>



/**
    * Prints a histogram to the screen using horizontal bar chart.
    * Parameters:
    *   list - a list of integers
    *   n - the number of values in the list
    */
    void printHistogram ( int *hist, int n );

   /**
 * This program requests data from the user and then
 * computes and prints a histogram. You may assume that
     * the data values entered are in the range of 0 to 9.
    */
   int main ( void )
   {


int i, n;


// Data entry
//
printf ("How many values in your data set? ");
scanf ("%d", &n);

int list[n];
for (i=0; i < n; i++) {
    printf ("Enter a value: ");
    scanf ("%d", &list[i]);
}

// Processing data to compute histogram

int hist[10];    


// Printing histogram
printHistogram ( hist, 10);

return 0;
}



void printHistogram ( int *list, int n )
{
int i, j;

for (i=0; i < n; i++) {
    printf ("[%d] ", i);
    for (j = 0; j < list[i]; j++)
        printf ("*");
    printf ("\n");
  }
 }

【问题讨论】:

  • 好的。祝你好运
  • 处理数据以计算直方图

标签: c histogram


【解决方案1】:

您为实际解决方案提供的信息太少,但无论如何。
因此,我假设您要打印一个直方图,其中包含 1-9 之间的整数出现次数(至少,这是我所理解的)。
一种可能的方法是创建一个 ingeter 数组,该数组将保留每个整数的出现次数。它显然有 10 个项目。当您到达输入时,对于您遇到的每个整数,您将递增数组中的相应项目。请注意,您不需要在数组中搜索每个整数。
如果要计算单词字符串的出现次数,这会稍微复杂一些,因为它需要使用结构体,但它是基于相同的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2020-01-30
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多