gpt4 book ai didi

c - 将文件中的整数分配给数组

转载 作者:行者123 更新时间:2023-11-30 19:42:37 25 4
gpt4 key购买 nike

编辑:尝试了一些建议,并没有改变太多,但它的工作原理给了我不正确的数据,但至少它们不是零。感谢大家的帮助。

我正在编写用于从文本文件读取成绩并将其存储在数组中的代码。我的完整代码相当大,所以我只发布有问题的部分;我的 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;
}

一些额外信息:等级数组必须初始化为双填充带 0。我只需要用文本文件中的数据替换 Grades[] 中的零。我已经为此工作了一天多了,但仍然取得了 0 进展。我只是不明白为什么它不改变数组中的值。

编辑:这是我使用数组数据调用 readGrades 的地方。

int numGrades;

numGrades = readGrades(grades);

就在main里面。 #define MAX 100 和双等级[MAX] 在 main 外部定义。我返回 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;
}

感谢大家在这方面帮助我。

最佳答案

检查 fopenfscanf 调用的结果:

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" );

关于c - 将文件中的整数分配给数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com