c - 我正在尝试编写一个有关统计的程序

标签 c

我的程序需要编写一个程序来输入分数并执行其他操作。我已经编写了 2 个函数,其中 1 个用于输入分数(每行 5 个)并对其进行排序。现在我正在尝试计算分数的频率并将其打印为图表。我写了一个函数,但它不起作用。谁能教我如何修复。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
int sort(const void *p, const void *q);
void output(int testscore[], int size);
void frequence(int testscore[], int size);
int main()
{
   int i;
   int testscore[SIZE] = {90, 85, 100, 50, 50, 85, 60, 70, 55, 55, 80, 95,         70, 60, 95, 80, 100, 75, 70, 95, 90, 90, 70, 95, 50, 65, 85, 95, 100, 65};
    for(i = 0; i < SIZE; i++) {
        testscore[SIZE];
    }
    printf("before sort\n");
    output(testscore, SIZE);
    qsort(testscore, SIZE, sizeof(int), sort);
    printf("\nafter sort\n");
    output(testscore, SIZE);
    return 0;
 }
int sort(const void *p, const void *q) {
    if (*(int*)p < *(int*)q) {
        return -1;
    }
    return *(int*)p > *(int*)q;
}

void output(int testscore[], int size) {
    int i;
    for(i = 0; i < size; ++i) {
        printf("%6d", testscore[i]);
        if ( i % 5 == 4 || ( i == size - 1)) {
            printf("%c", '\n');
    }
    else {
        printf("%c", ' ');

    }
   }

}
void frequence(int testscore[], int size) {
    int i, j, count = 0;
    char value;
    char frequency;
    printf("%15c %15c", "value", "frequency");
    printf("%15s %15s", "-----", "---------");
    for(i=0;i<SIZE;i++){
        count=1;
        for(j=i+1;j<=SIZE-1;j++){
            if(testscore[i]==testscore[j] && testscore[i]!='\0'){
                count++;
                testscore[j]='\0';
          }
        }
        if(testscore[i]!='\0'){
            printf("%d is %d times.\n",testscore[i],count);
            }
        }
}

最佳答案

据我所知,当我复制并运行它时,您的代码是正确的,并且您需要在主函数中调用频率。

但是存在一些格式问题。

这是我在函数频率中使用的内容

void frequence(int testscore[], int size) {
int i, j, count = 0;
char value;
char frequency;
printf("\n");
printf("%5s %9s", "value", "frequency");
printf("\n");
printf("%5s %9s", "-----", "---------");
printf("\n");
for(i=0;i<SIZE;i++){
    count=1;
    for(j=i+1;j<=SIZE-1;j++){
        if(testscore[i]==testscore[j] && testscore[i]!='\0'){
            count++;
            testscore[j]='\0';
      }
    }
    if(testscore[i]!='\0'){
        printf("%3d     %d\n",testscore[i],count);
        }
    }
}

这是我得到的o/p..希望这是你接受的

before sort
90     85    100     50     50
85     60     70     55     55
80     95     70     60     95
80    100     75     70     95
90     90     70     95     50
65     85     95    100     65

after sort
50     50     50     55     55
60     60     65     65     70
70     70     70     75     80
80     85     85     85     90
90     90     95     95     95
95     95    100    100    100

value frequency
----- ---------
 50     3
 55     2
 60     2
 65     2
 70     4
 75     1
 80     2
 85     3
 90     3
 95     5
100     3

关于c - 我正在尝试编写一个有关统计的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340160/

相关文章:

c - 合并排序错误: Segmentation Fault

c - 使用 POSIX 定时器后程序不退出

c - 在c中打印所有十六进制数字

c - 如何在C中解析数组

c++ - 构建 libcurl 时如何启用摘要

c - 函数声明 : K&R vs ANSI

c - CUDA 上的二维数组

c - 在 C 中调整二维数组的大小

编译.C 文件 : Undefined symbols for architecture x86_64

c - 函数重载在 C 中有效吗?