我正在编写用于从文本文件中读取成绩的代码并将它们存储在数组中。我的完整代码相当大,所以我只发布我遇到问题的部分;我的while循环没有改变数组中的值。所有样本数据均为65 99 87 76 89 37 -999。我的输出显示,在函数应该更改其值之后,数组值保持为零。其他while循环都是在同一任务中失败的尝试。
int readGrades(double grades[]){
FILE* gd;
int count = 0;
double ind;
gd = fopen("sample.txt", "r");
while(count < MAX){ //MAX is defined as 100/array is grades[MAX]
fscanf(gd, "%lf", &ind); //Should scan through items in the file
if(ind < 0) //End of sample data is -999 this breaks out of the loop
break;
grades[count] = ind; //changes values in the array (it doesn't change them from 0)
count++; } //increments the index of the array
/*while(fscanf(gd, "%lf", &ind)>0){
grades[count] = ind;
count++; }*/
/*do {
fscanf(gd, "%lf", &grades[count]);
printf("%.0lf", grades[count]);
if(fscanf(gd, "%lf", &grades[count])== -999){
break;
}
count++;
}while(fscanf(gd, "%lf", &grades[count])> 0);*/
fclose(gd);
return count+1;
}
一些额外的信息:grade数组必须初始化为双填充w / 0s。我只需要将grade []中的零替换为文本文件中的数据。 我已经在这一天工作了一天以上,仍然有0个进步。我只是不明白它为什么不改变数组中的值。
编辑:这是我用数组数据调用readGrades的地方。
int numGrades;
numGrades = readGrades(grades);
在主内部。 #define MAX 100和双等级[MAX]在主外部进行。我返回count + 1,因为函数需要返回读取的数据项数。
有人要求完整的计划:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int readGrades(double []);
void frequency(double [], int);
int maximum(double [], int);
int minimum(double [], int);
int deleteElement(double [], int, int);
double mean(double [], int);
double standardDeviation(double [], int);
#define MAX 100
double grades[MAX];
int loc;
int main(){
int numGrades;
printf("%lf", grades);
numGrades = readGrades(grades);
loc = minimum(grades, numGrades);
numGrades = deleteElement(grades, numGrades, loc);
printf("The data has been adjusted by removing the minimum %.2lf\n", grades[loc]);
loc = maximum(grades, numGrades);
numGrades = deleteElement(grades, numGrades, loc);
printf("The data has been adjusted by removing the maximum %.2lf\n", grades[loc]);
printf("The adjusted mean is %.2lf\n", mean(grades, numGrades));
printf("The adjusted standard deviation is %.2lf\n", standardDeviation(grades, numGrades));
printf("Here is a histogram of the adjusted data:\n");
frequency(grades, numGrades);
return 0;
}
int readGrades(double grades[]){
FILE* gd;
int count = 0;
double ind;
gd = fopen("sample.txt", "r");
while(count < MAX){
fscanf(gd, "%lf", &ind);
//double atod(ind);
if(ind < 0)
break;
grades[count] = ind;
count++;
}
/*while(fscanf(gd, "%lf", &ind)>0){
grades[count] = ind;
count++;
}*/
/*do {
fscanf(gd, "%lf", &grades[count]);
printf("%.0lf", grades[count]);
if(fscanf(gd, "%lf", &grades[count])== -999){
break;
}
count++;
}while(fscanf(gd, "%lf", &grades[count])> 0);*/
printf("%.0lf", grades[1]);
fclose(gd);
return count+1;
}
void frequency(double grades[], int numGrades){
int j, i, a=0, b=a+4;
for(j=0; j<numGrades-1; j++){
printf("%d - %d|", a, b);
for(i=0; i<numGrades; i++){
if(grades[i]<=b && grades[i]>=a){
printf("*");
}
}
printf("\n");
a+=5;
if(a==100){
break;
}
}
printf("%d|", a);
for(j=0; j<numGrades; j++){
if(grades[i]==100)
printf("*");
}
printf("\n");
}
int maximum(double grades[], int numGrades){
int i, h = 0;
for(i=1; i<numGrades; i++){
/*if(grades[i] == grades[h])
continue;*/
if(grades[i] > grades[h])
h = i;
}
return h;
}
int minimum(double grades[], int numGrades){
int i, h = 0;
for(i=1; i<numGrades; i++){
/*if(grades[i] == grades[h])
continue;*/
if(grades[i] < grades[h])
h = i;
}
return h;
}
int deleteElement(double grades[], int numGrades, int loc){
int i;
for(i=loc; i<numGrades-1; i++){
grades[i] = grades[i+1];
}
return numGrades-=1;
}
double mean(double grades[], int numGrades){
int i;
double ans, sum;
for(i = 0; i<numGrades; i++){
sum += grades[i];
}
ans = sum/numGrades;
return ans;
}
double standardDeviation(double grades[], int numGrades){
int i;
double avg, sd, ans;
avg = mean(grades, numGrades);
for(i=0; i<numGrades; i++){
sd += pow((avg - grades[i]), 2);
}
ans = sqrt(sd/numGrades);
return ans;
}
我感谢你们帮助我。
答案 0 :(得分:0)
检查fopen
和fscanf
来电的结果:
gd = fopen( "sample.txt", "r" );
if ( !gd )
{
fprintf( stderr, "error opening input file\n" );
return 0;
}
while( count < max && fscanf( gd, "%lf", &ind ) == 1 )
{
if ( ind < 0 )
break;
grades[count++] = ind;
}
if ( feof( gd ))
fprintf( stderr, "hit end of file\n" );
else if ( ferror( gd ))
fprintf( stderr, "error during read\n" );
答案 1 :(得分:0)
程序完全完美。我确信printf中存在问题,就像你使用它一样。您粘贴打印值的行。我希望你一定要做这样的事情
printf("%lf\n",grades[i]);
只是出于改善的原因,你甚至不需要将成绩[]作为参数传递,因为这是全局变量。仅供参考,在readGrades()中,当您作为参考传递时,它会创建名称grade []的局部变量。
另外,在readGrades()函数&#34;返回计数&#34;应该没问题,因为你已经在while循环中进行了计数递增。
答案 2 :(得分:0)
你的代码完全没问题,因为@Pawan说。主要的第二行printf("%lf"s, grades);
不需要并且运行错误(对我而言)。如果你在同一个“EXE”路径中有sample.txt就不会有任何问题。如果sample.txt在另一个文件夹中,则必须使用完整路径引用它。
这是当前的输出:
来自循环的Printint值:
65
99个
87.000000
76.000000
89.000000
37.000000
99个
通过删除最小0.00
来调整数据
通过删除最大值87.00来调整数据
调整后的平均值为70.80
调整后的标准差为18.96
以下是调整后数据的直方图:
0 - 4 |
5 - 4 |
10 - 4 |
15 - 4 |
20 |
如果您无法打开文件,请尝试
if ( !gd ) {
fprintf( stderr, "error opening input file\n", strerror(errno));
return 0;
}
查看错误。我认为另一个进程已打开该文件。