C-Program: Custom Library, Header File, and Implementation Files
I have this main.c file:
#include
#include
#include \"stats.h\"
int readGrades(char fileName[], float grades[]);
//Include this function prototype
void WelcomeToTheProgram();
int main()
{
///Declare Variables
char fileName[30];
float grades[1000]; //to store grades
int i, ArrayCount[10] = {0}; //to store histogram counts
/* Rewrite this line */
int num_grades; //for # of grades in file
///Call Intro Comment
WelcomeToTheProgram();
///Enter Filename
printf(\"\ \ \ Enter the data filename: \");
scanf(\"%s\", &fileName);
//Get # of grades in file
num_grades = readGrades(fileName, grades); //get # of grades in file
///Print Results
printf(\"There are %d grades read\ \", num_grades);
printf(\"Mean = %f\ \", mean(grades, num_grades));
printf(\"Variance = %f \ \", variance(grades, num_grades));
printf(\"Maximum = %f \ \", maximum(grades, num_grades));
printf(\"Minumum = %f \ \", minimum(grades, num_grades));
//Call histogram
histogram(grades, ArrayCount, num_grades);
//Print the histogram
printf(\"Grade Histogram\ \");
for (i = 0; i < 10; i++)
{
if (i != 9)
{
printf(\"\ %d %% - %d %%: %d \", i * 10, ((i + 1) * 10) - 1, ArrayCount[i]);
}
else
{
printf(\"\ 90 %% - 100 %%: %d\ \", ArrayCount[i]);
}
}
return 0;
}
///Reads all grades from the file to array
int readGrades(char fileName[], float grades[])
{
FILE *fpin = fopen(fileName, \"r\");
int num_scores = 0;
float value;
if (fpin == NULL)
{
printf(\"Cannot open the file\ \");
exit(0);
}
while ((fscanf(fpin, \"%f\", &value)) != EOF)
{
grades[num_scores] = value;
num_scores++;
}
return num_scores;
}
void WelcomeToTheProgram()
{
printf(\"***Welcome to the Program Which Involves a Custom Library***\");
printf(\"\ The program finds the minimum value, maximum value, mean,\");
printf(\"variance, and a histogram of the grades of data by calling\");
printf(\"implementation files min(), max(), mean(), variance(), and histogram().\ \");
}
But, the only thing that prints is
Enter the data file name: grades.txt
There are 400 grades read.
Then the program just stops, it does not print the min, max, mean, variance, or histogram. Any
ideas? Am I using the custom library wrong, the implementation files (for min, max, mean,
variance, and histogram) all check out with no warnings or errors.
Here is stats.h:
#ifndef STATS_H
#define STATS_H
float minimum(float *grades[], int Size); // NOTE: You need to complete the prototypes
float maximum(float *grades[], int Size);
float mean(float *grades[], int Size);
float variance(float *grades[], int Size);
void histogram(float *grades[], int *ArrayCount, int Size);
#endif // STATS_H
And here is the grades file:
Solution
//stats.h
#ifndef STATS_H
#define STATS_H
float minimum(float grades[], int Size); // NOTE: You need to complete the prototypes
float maximum(float grades[], int Size);
float mean(float grades[], int Size);
float variance(float grades[], int Size);
void histogram(float grades[], int ArrayCount[], int S.
C-Program Custom Library, Header File, and Implementation FilesI .pdf
1. C-Program: Custom Library, Header File, and Implementation Files
I have this main.c file:
#include
#include
#include "stats.h"
int readGrades(char fileName[], float grades[]);
//Include this function prototype
void WelcomeToTheProgram();
int main()
{
///Declare Variables
char fileName[30];
float grades[1000]; //to store grades
int i, ArrayCount[10] = {0}; //to store histogram counts
/* Rewrite this line */
int num_grades; //for # of grades in file
///Call Intro Comment
WelcomeToTheProgram();
///Enter Filename
printf(" Enter the data filename: ");
scanf("%s", &fileName);
//Get # of grades in file
num_grades = readGrades(fileName, grades); //get # of grades in file
///Print Results
printf("There are %d grades read ", num_grades);
printf("Mean = %f ", mean(grades, num_grades));
printf("Variance = %f ", variance(grades, num_grades));
printf("Maximum = %f ", maximum(grades, num_grades));
printf("Minumum = %f ", minimum(grades, num_grades));
//Call histogram
2. histogram(grades, ArrayCount, num_grades);
//Print the histogram
printf("Grade Histogram ");
for (i = 0; i < 10; i++)
{
if (i != 9)
{
printf(" %d %% - %d %%: %d ", i * 10, ((i + 1) * 10) - 1, ArrayCount[i]);
}
else
{
printf(" 90 %% - 100 %%: %d ", ArrayCount[i]);
}
}
return 0;
}
///Reads all grades from the file to array
int readGrades(char fileName[], float grades[])
{
FILE *fpin = fopen(fileName, "r");
int num_scores = 0;
float value;
if (fpin == NULL)
{
printf("Cannot open the file ");
exit(0);
}
while ((fscanf(fpin, "%f", &value)) != EOF)
{
grades[num_scores] = value;
num_scores++;
}
return num_scores;
}
void WelcomeToTheProgram()
{
3. printf("***Welcome to the Program Which Involves a Custom Library***");
printf(" The program finds the minimum value, maximum value, mean,");
printf("variance, and a histogram of the grades of data by calling");
printf("implementation files min(), max(), mean(), variance(), and histogram(). ");
}
But, the only thing that prints is
Enter the data file name: grades.txt
There are 400 grades read.
Then the program just stops, it does not print the min, max, mean, variance, or histogram. Any
ideas? Am I using the custom library wrong, the implementation files (for min, max, mean,
variance, and histogram) all check out with no warnings or errors.
Here is stats.h:
#ifndef STATS_H
#define STATS_H
float minimum(float *grades[], int Size); // NOTE: You need to complete the prototypes
float maximum(float *grades[], int Size);
float mean(float *grades[], int Size);
float variance(float *grades[], int Size);
void histogram(float *grades[], int *ArrayCount, int Size);
#endif // STATS_H
And here is the grades file:
Solution
//stats.h
#ifndef STATS_H
#define STATS_H
float minimum(float grades[], int Size); // NOTE: You need to complete the prototypes
float maximum(float grades[], int Size);
float mean(float grades[], int Size);
float variance(float grades[], int Size);
void histogram(float grades[], int ArrayCount[], int Size);
#endif // STATS_H
4. //min.c
#include
#include
#include "stats.h"
float minimum(float grades[], int Size)
{
int min = grades[0];
for (int i = 0; i < Size; ++i)
{
if(grades[i] < min)
min = grades[i];
}
return min;
}
//max.c
#include
#include
#include "stats.h"
float maximum(float grades[], int Size)
{
int max = grades[0];
for (int i = 0; i < Size; ++i)
{
if(grades[i] > max)
max = grades[i];
}
return max;
}
//mean.c
#include
5. #include
#include "stats.h"
float mean(float grades[], int Size)
{
float sum = 0;
for (int i = 0; i < Size; ++i)
{
sum = sum + grades[i];
}
return sum/Size;
}
//variance.c
#include
#include
#include "stats.h"
float variance(float grades[], int Size)
{
float sum = 0;
for (int i = 0; i < Size; ++i)
{
sum = sum + grades[i];
}
float avg = sum/Size;
sum = 0;
for (int i = 0; i < Size; ++i)
{
sum = sum + (grades[i] - avg)*(grades[i] - avg);
}
return (sum/Size);
}
8. float grades[1000]; //to store grades
int i, ArrayCount[10] = {0}; //to store histogram counts
/* Rewrite this line */
int num_grades; //for # of grades in file
///Call Intro Comment
WelcomeToTheProgram();
///Enter Filename
printf(" Enter the data filename: ");
scanf("%s", fileName);
//Get # of grades in file
num_grades = readGrades(fileName, grades); //get # of grades in file
///Print Results
printf("There are %d grades read ", num_grades);
printf("Mean = %f ", mean(grades, num_grades));
printf("Variance = %f ", variance(grades, num_grades));
printf("Maximum = %f ", maximum(grades, num_grades));
printf("Minumum = %f ", minimum(grades, num_grades));
//Call histogram
histogram(grades, ArrayCount, num_grades);
//Print the histogram
printf("Grade Histogram ");
for (i = 0; i < 10; i++)
{
if (i != 9)
{
printf(" %d %% - %d %%: %d ", i * 10, ((i + 1) * 10) - 1, ArrayCount[i]);
}
else
{
printf(" 90 %% - 100 %%: %d ", ArrayCount[i]);
}
9. }
return 0;
}
///Reads all grades from the file to array
int readGrades(char fileName[], float grades[])
{
FILE *fpin = fopen(fileName, "r");
int num_scores = 0;
float value;
if (fpin == NULL)
{
printf("Cannot open the file ");
exit(0);
}
while ((fscanf(fpin, "%f", &value)) != EOF)
{
grades[num_scores] = value;
num_scores++;
}
return num_scores;
}
void WelcomeToTheProgram()
{
printf("***Welcome to the Program Which Involves a Custom Library***");
printf(" The program finds the minimum value, maximum value, mean,");
printf("variance, and a histogram of the grades of data by calling");
printf("implementation files min(), max(), mean(), variance(), and histogram(). ");
}
/*
compile: cc main.c min.c max.c variance.c mean.c histogram.c
run ./a.out
grades.txt
60.2
84.9
57.4
70.2