C-Program: Custom Library, Header File, and Implementation Files I have this mai
ID: 3781837 • Letter: C
Question
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:
Explanation / Answer
//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
//min.c
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
#include <stdlib.h>
#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);
}
//histogram.c
#include <stdio.h>
#include <stdlib.h>
#include "stats.h"
void histogram(float grades[], int ArrayCount[], int Size)
{
for (int i = 0; i < Size; ++i)
{
if(grades[i] > 0)
{
if(grades[i] > 10)
{
if(grades[i] > 20)
{
if(grades[i] > 30)
{
if(grades[i] > 40)
{
if(grades[i] > 50)
{
if(grades[i] > 60)
{
if(grades[i] > 70)
{
if(grades[i] > 80)
{
if(grades[i] > 90)
{
ArrayCount[9]++;
}
else
ArrayCount[8]++;
}
else
ArrayCount[7]++;
}
else
ArrayCount[6]++;
}
else
ArrayCount[5]++;
}
else
ArrayCount[4]++;
}
else
ArrayCount[3]++;
}
else
ArrayCount[2]++;
}
else
ArrayCount[1]++;
}
else
ArrayCount[0]++;
}
}
}
// main.c
#include <stdio.h>
#include <stdlib.h>
#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(). ");
}
/*
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
68.4
55.3
42.3
55.4
54.1
65.9
60.2
66.5
67.6
62.1
37.5
73.7
53.4
77.4
52.0
57.6
51.3
47.0
78.5
46.4
58.1
87.7
49.0
32.3
50.4
80.8
75.9
70.7
54.1
74.2
33.5
57.3
41.1
72.7
57.5
55.8
46.4
47.1
47.5
60.7
37.7
54.1
67.0
34.6
76.6
59.4
80.8
72.5
72.2
57.4
71.4
90.5
31.9
66.4
47.5
46.8
77.9
46.9
34.7
50.9
58.3
63.3
50.1
68.4
79.6
69.1
61.4
68.1
75.2
33.4
62.9
64.3
49.2
57.6
41.9
55.7
55.8
35.2
45.1
54.2
43.4
45.6
73.5
57.5
67.2
62.8
45.0
47.9
63.5
80.7
70.9
49.5
49.6
80.9
64.0
60.7
70.4
46.1
59.4
76.9
46.4
68.1
50.8
36.7
58.3
60.5
75.2
71.9
53.9
62.5
54.4
55.3
74.6
63.1
71.9
73.5
69.9
60.2
64.7
63.4
62.3
74.4
66.7
69.9
15.1
39.2
56.6
42.8
55.7
30.2
75.2
76.0
56.2
78.0
73.1
54.0
76.6
94.8
58.5
46.0
73.8
58.1
44.5
56.2
62.8
55.3
78.8
64.7
63.3
83.7
58.4
44.6
83.0
57.0
49.3
72.1
61.0
49.2
73.2
44.1
56.9
40.7
61.9
65.6
86.5
52.5
73.5
48.4
46.1
49.3
46.2
90.2
74.6
71.5
60.4
49.1
67.3
62.9
77.0
66.5
62.7
46.8
69.2
63.9
68.8
82.8
53.1
31.6
35.2
84.9
75.5
77.7
81.7
52.3
38.8
70.9
63.2
84.7
65.0
51.2
54.0
71.9
67.5
67.5
62.0
85.3
59.7
57.0
52.2
56.9
96.7
50.4
57.1
54.4
56.3
62.9
60.2
52.9
29.9
39.7
88.3
69.5
63.3
82.4
51.2
64.1
50.9
64.9
70.3
81.3
50.0
67.5
88.4
40.8
39.8
77.6
100.0
67.6
51.0
51.1
39.9
45.9
74.1
54.0
64.9
62.7
57.3
52.7
62.0
64.1
55.6
54.1
49.8
51.8
69.3
58.0
46.5
66.3
58.3
69.4
55.7
72.6
42.9
44.8
69.6
56.0
60.1
62.1
66.5
72.4
25.2
44.9
56.3
56.7
68.0
52.2
68.0
62.6
37.4
55.3
81.6
60.5
74.6
29.3
57.8
34.6
56.3
79.2
56.1
64.9
43.0
64.3
69.1
67.6
62.9
47.2
44.8
73.9
63.7
73.0
50.5
61.1
61.8
0.8
37.2
67.3
88.1
76.3
70.6
61.7
70.6
78.6
78.3
58.9
46.1
71.6
52.5
56.1
53.6
72.7
48.6
31.4
66.3
59.0
66.6
55.7
62.2
49.2
30.9
37.7
46.3
49.6
51.9
55.8
48.3
72.7
41.3
75.5
67.1
53.7
67.7
55.8
63.2
60.6
83.5
74.5
40.5
59.3
55.5
0.0
49.1
52.1
91.7
51.0
66.9
48.9
64.9
61.7
62.6
51.5
47.8
78.3
61.2
41.4
53.9
59.9
51.5
61.7
53.1
58.9
56.1
84.8
71.9
69.2
64.3
61.0
46.5
79.8
73.3
51.1
74.6
52.4
77.2
57.4
67.3
55.4
81.8
63.0
68.2
62.8
66.0
60.4
83.8
67.5
21.3
57.8
output:
***Welcome to the Program Which Involves a Custom Library***
The program finds the minimum value, maximum value, mean,variance, and a histogram of the grades of data by callingimplementation files min(), max(), mean(), variance(), and histogram().
Enter the data filename: grades.txt
There are 400 grades read
Mean = 60.022758
Variance = 204.935898
Maximum = 100.000000
Minumum = 0.000000
Grade Histogram
0 % - 9 %: 2
10 % - 19 %: 1
20 % - 29 %: 4
30 % - 39 %: 24
40 % - 49 %: 59
50 % - 59 %: 105
60 % - 69 %: 108
70 % - 79 %: 67
80 % - 89 %: 24
90 % - 100 %: 6
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.