在本实验中,您将编写一个程序,该程序将首先读取 档案中一系列人的身高和体重 values.dat。使用编辑器创建此文件,以便每一行 包含一个高度和相应的重量。例如,它可能 看起来像:
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
^
谢谢
答案 0 :(得分:-1)
File
与FILE
不同。 <stdio.h>
定义了FILE
,这是您应该使用的。
我不知道解决此错误(将File
引用更改为FILE
)是否会使您编译,因为该错误可能还会隐藏其他错误。但是,这将是使代码易于使用的第一个错误。