gpt4 book ai didi

c - 从 C 中的另一个文件读取输入的家庭作业程序

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

For this lab, you are to write a a program that will first read in the heights and weights of a series of people from a file named values.dat. Create this file using your editor so that each line contains a height and corresponding weight. For instance, it might look likea:

60.0  125.0
48.0 100.0

等等。

接下来创建一个名为 stats.h 的文件,其中包含以下内容:

#define MAXNUM 100
typedef struct person
{
double height;
double weight;
} Person;

//prototypes follow:
numPeople = getData(File *input, Person[] people, int MAXNUM);
getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeople)
getStandardDevs(Person[] people, double aveHeight, double aveWeight,
double *stdHeight, double *stdWeight, int numPeople);


主程序将如下所示:

#include <stdlib.h>
#include <stdio.h>
#include "stats.h"

void main(void)
{
char filename[] = "values.dat";
FILE *input;
Person people[MAXNUM];
int numPeople;
double aveHeight, aveWeight, stdHeight, stdWeight;

numPeople = getData(input, people, MAXNUM)
fclose(input);
getAverages(people, &aveHeight, &aveWeight, numPeople)
getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeople)

printf("The average height is %lf\n", aveHeight);
printf("The average weight is %lf\n", aveWeight);
printf("The standard deviation of the heights is %lf\n", stdHeight);
printf("The standard deviation of the weights is %lf\n", stdWeight);
}

现在,编写函数 getData , getAverages ,和getStandardDevs 。将它们放在主程序之后。

一系列数字 x[1], x[2] ... x[n] 的标准差公式为

std = sqrt( sum( (x[i] - xbar)**2 ) / (n-1) )

其中xbar是x的平均值,n是样本数,和 **2 均方。

请记住,要使用 sqrt,您必须 #include <math.h>并且将 -lm 添加到编译行。

上面的文本是实验室,当我尝试创建 3 个文件(values.dat、stats.h 和主程序)时,在尝试使用 -gcc -o 进行编译时,我立即收到错误lab6 lab6.c -lm

我现在的主程序代码是

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "stats.h"

void main(void)
{
char filename[] = "values.dat";
FILE *input;
Person people[MAXNUM];
int numPeople;
double aveHeight, aveWeight, stdHeight, stdWeight;

numPeople = getData(input, people, MAXNUM)
fclose(input);
getAverages(people, &aveHeight, &aveWeight, numPeople)
getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeople)

printf("The average height is %lf\n", aveHeight);
printf("The average weight is %lf\n", aveWeight);
printf("The standard deviation of the heights is %lf\n", stdHeight);
printf("The standard deviation of the weights is %lf\n", stdWeight);
}

int getData(File *input, Person[] people, int MAXNUM)
{

}

double getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeople)
{

}

double getStandardDevs(Person[] people, double aveHeight, double aveWeight, double *stdHeight, double *stdWeight, int numPeople)
{


}

我添加了函数并尝试编译。

我在行尾使用 -lm 编译的行是否正确。
如果有人可以帮助我使用这三个文件进行编译,那就太棒了,或者如果有人可以帮助解决任何代码。

我遇到的错误

In file included from lab6.c:4:0:
stats.h:9:9: error: unknown type name ‘File’
getData(File *input, Person[] people, int MAXNUM);
^
stats.h:9:31: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
getData(File *input, Person[] people, int MAXNUM);
^
stats.h:10:22: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
getAverages(Person[] people, double *aveHeight, double *aveWeight, int numPeopl
^
stats.h:11:26: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
getStandardDevs(Person[] people, double aveHeight, double aveWeight,
^
lab6.c: In function ‘main’:
lab6.c:14:15: warning: implicit declaration of function ‘getData’ [-Wimplicit-function-declaration]
numPeople = getData(input, people, MAXNUM);
^
lab6.c:16:3: warning: implicit declaration of function ‘getAverages’ [-Wimplicit-function-declaration]
getAverages(people, &aveHeight, &aveWeight, numPeople);
^
lab6.c:17:3: warning: implicit declaration of function ‘getStandardDevs’ [-Wimplicit-function-declaration]
getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeop
^
lab6.c: At top level:
lab6.c:25:13: error: unknown type name ‘File’
int getData(File *input, Person[] people, int MAXNUM)
^
lab6.c:25:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
int getData(File *input, Person[] people, int MAXNUM)
^
lab6.c:30:29: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
double getAverages(Person[] people, double *aveHeight, double *aveWeight, int n
^
lab6.c:35:33: error: expected ‘;’, ‘,’ or ‘)’ before ‘people’
double getStandardDevs(Person[] people, double aveHeight, double aveWeight, dou
^

谢谢

最佳答案

FileFILE 不同。 <stdio.h>定义FILE这就是你应该使用的。

我不知道修复此错误(将您的 File 引用更改为 FILE )是否能让您进行编译,因为可能还有其他错误被此错误隐藏。然而,这将是编译代码时需要修复的第一个错误。

关于c - 从 C 中的另一个文件读取输入的家庭作业程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58533591/

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