You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言代码输出格式不符期望,直方图未达标,求技术帮助

Fixing Your Histogram Output Issue

Hey there! Let's get your code to produce the histogram format you're looking for. First, let's break down what's off with your current code, then walk through the fixes.

What's Wrong With the Current Code?

  • You wrote a handy printStars function to visualize counts with asterisks, but you never actually call it in main()
  • The current output prints each number and its count on a new line, while you want everything in a single line, formatted like 0-*** 1-** 2- 3-***... (or the number-count format you mentioned)
  • There's no validation for input values—if someone enters a number outside 0-9, it'll cause an array out-of-bounds error (we'll add a quick check for that too)

Modified Code That Meets Your Requirements

Here's the adjusted code that outputs the visual histogram in the format you want:

#include<stdio.h> 

void printStars(int n){ 
    int i; 
    for(i = 0; i < n; i++){ 
        printf("*"); 
    } 
} 

int main() { 
    int i, n; 
    int input[100]; 
    int hist[10] = {0}; // Initialize all counts to 0

    printf("Input the number of values: "); 
    scanf("%d", &n); 

    printf("Input the values between 0 and 9 (separated by space): "); 
    for(i = 0; i < n; i++){ 
        scanf("%d", &input[i]); 
        // Validate input to prevent array out-of-bounds
        if(input[i] < 0 || input[i] > 9){
            printf("\nWarning: Value %d is outside 0-9, ignoring it!\n", input[i]);
            i--; // Decrement i so we don't skip the next input
            continue;
        }
        hist[input[i]]++; 
    } 

    printf("\n\nYour histogram: "); 
    for(i = 0; i < 10; i++){ 
        printf("%d-", i); // Print number followed by dash
        printStars(hist[i]); // Call your star-printing function
        printf(" "); // Add space between each entry
    } 
    printf("\n"); // New line at the end for clean formatting

    // If you prefer the number-count format (like your example "0-3 1-2 2-0") instead of stars:
    // printf("\n\nAlternate histogram (number-count): ");
    // for(i = 0; i < 10; i++){ 
    //     printf("%d-%d ", i, hist[i]); 
    // }
    // printf("\n");

    return 0; 
}

Key Changes Explained

  1. Used the printStars function: We now call it inside the final loop to display asterisks equal to each number's count, which makes the histogram visual.
  2. Single-line output: Instead of printing each entry on a new line, we print all entries in one line with spaces between them, matching your desired format structure.
  3. Input validation: Added a check to make sure only values 0-9 are counted—this prevents crashes from invalid inputs that would mess with the hist array.
  4. Optional number-count format: Included a commented-out section if you want to stick strictly to the 0-3 1-2 format you mentioned (just uncomment those lines if you prefer that over asterisks).

Example Output

If you input 5 values: 0 0 1 3 3, the visual histogram will look like:

Your histogram: 0-** 1-* 2- 3-** 4- 5- 6- 7- 8- 9- 

And the alternate number-count format would be:

Alternate histogram (number-count): 0-2 1-1 2-0 3-2 4-0 5-0 6-0 7-0 8-0 9-0 

内容的提问来源于stack exchange,提问作者thisthat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:47:59