c - 当我读完所有分数时我的程序崩溃了
问题描述
这个程序在我读完所有评委的分数并且编译器没有给出任何警告后关闭/崩溃。当它读入所有分数时,它应该打印出所有分数以及最高分数、平均分数和最低分数。
#include <stdio.h>
/*
Prints out info to the user.
*/
void printInfo(){
printf("Program information\n");
printf("The program reads in the number of judges and the score from each judge.\n");
printf("Then it calculates the average score without regard to the lowest and\n");
printf("highest judge score. Finally it prints the results (the highest, the\n");
printf("lowest and the final average score).\n\n");
}
/*
Reads in how many judges the user wants to have.
returns the number of judges.
*/
int readJudge(){
int tempNumbJudge;
do{
printf("Number of judges (min 3 and max 10 judges)? ");
scanf("%d", &tempNumbJudge);
}while(3 > tempNumbJudge || tempNumbJudge > 10);
return tempNumbJudge;
}
/*
Reads in the score from each judge
Parameters:
arr - the array where to put the scores
judges - the number of judges
*/
void readJudgeScore(double arr[], int judges){
printf("\n");
double temp = 0;
int i = 0;
while(i <= judges - 1){
printf("Score from judge %d? ", i+1);
scanf("%lf", &temp);
arr[i] = temp;
i++;
}
}
/*
Prints of the score from each judge.
Parameters:
arr - the array with the scores
judges - the number of judges
*/
void printJudgeScore(double arr[], int judges){
printf("\n");
printf("Loaded scores:\n");
for (int i = 0; i < judges; i++) {
printf("Judge %d: %.1f\n", i+1 , arr[i]);
}
}
/*
Finds the highest score.
Parameters:
arr - the array with the scores
judges - the number of judges
returns the highest score
*/
double findMaxValue(double arr[], int judges){
double maxValue = arr[0];
for(int i = 0; i < judges; i++){
if(arr[i] > maxValue){
maxValue = arr[i];
}
}
return maxValue;
}
/*
Finds the lowest score.
Parameters:
arr - the array with the scores
judges - the number of judges
returns the lowest score
*/
double findMinValue(double arr[], int judges){
double minValue = arr[0];
for(int j = 0; j < judges; j++){
if(arr[j] < minValue){
minValue = arr[j];
}
}
return minValue;
}
/*
Calculates the average score.
Parameters:
arr - the array with the scores
judges - the number of judges
max - the highest score
min - the lowest score
returns the average score
*/
double findAverage(double arr[], int judges, double max, double min){
double sum = 0;
double average = 0;
for(int k = 0; k < judges; k++){
sum = sum + arr[k];
}
average = (sum - max - min) / (judges - 2);
return average;
}
/*
Prints out the final results
Parameters:
max - the highest score
min - the lowest score
average - the average score
*/
void printFinalResult(double max, double min, double average){
printf("\n");
printf("Final result:\n");
printf("Highest judge score: %.1f\n", max);
printf("Lowest judge score: %.1f\n", min);
printf("Final average score: %.1f", average);
}
int main(){
int numbJudge;
double maxValue;
double minValue;
double average;
printInfo();
numbJudge = readJudge();
double points[numbJudge];
readJudgeScore(points, numbJudge);
printJudgeScore(points, numbJudge);
maxValue = findMaxValue(points, numbJudge);
minValue = findMinValue(points, numbJudge);
average = findAverage(points, numbJudge, maxValue, minValue);
printFinalResult(maxValue, minValue, average);
return 0;
}
我使用的编译器是带有标志 std=c99 和 -Wall 的 gcc
解决方案
您需要传递变量的地址,否则scanf()
应该如何写入?
你需要:
scanf("%lf", &temp);
还要检查返回值,I/O 可能会失败。
推荐阅读
- c# - 此 SqlParameterCollection 不包含具有 ParameterName 'server_name' 的 SqlParameter
- php - 如何在php中的两个多维数组之间找到不同的数组
- javascript - 如何以正确的形式编写 if else 条件
- python - 如何使用张量流解码图像 roi?
- nginx - 为什么nginx有3个进程?
- c# - UWP 中 ComboBox 的奇怪行为 - SelectedItem 和 ItemsSource
- python - python中的adaBoost投票数据和目标表单
- performance - WebGl 不相交的计时器查询峰值
- vba - 如何获取动态数组中的最后一个值
- java - 如何从 XSD 迭代字典键创建动态 XML?