Question
lab5.h /*---------------------------------------------------------------*/ /* Name */ /* lab5.h */ #include #include #include #define MAX_SIZE 40 /* max length of a grade file */ #define DIST_SIZE 11
lab5.h
/*---------------------------------------------------------------*/
/* Name */
/* lab5.h */
#include
#include
#include
#define MAX_SIZE 40 /* max length of a grade file */
#define DIST_SIZE 11 /* max length of distribution array */
// All the needed function prototypes
/* function to get the data and return filesize */
int get_data(const char *filename, /* input, current file name */
int grades[]); /* output, the filled array */
/* function to return average & distributed grade count */
void class_stats(int grades[], /* input, array that holds data */
int number_of_grades, /* input, actual size of the file */
double *average, /* output, average of the array */
int distribution[]); /* output, distribution count */
void print_all( const char *filename, /* input, the current filename */
double *average, /* input, the class average */
int distribution[]); /* input, the grade distribution */
lab5.c
/*---------------------------------------------------------------*/
/* Name . */
/* LAB 5 1-dimensional arrays */
#include "lab5.h"
const char *FILENAME[] = /* array of the data file names */
{"lab5a.dat", "lab5b.dat", NULL };
/*---------------------------------------------------------------*/
int main(void)
{
int file_count = 0; /* keeps track of which file we are on */
int number_of_grades = 0; /* actual size of current grade file */
int grades[MAX_SIZE]; /* the array */
double average; /* average of the array list */
int distribution[DIST_SIZE]; /* distribution count of the grades */
printf(" Name. Lab 5. ");
/* loop through each file, reading it, and getting stats on it */
for (file_count=0; FILENAME[file_count] != NULL; file_count++)
{
number_of_grades = get_data( FILENAME[file_count], grades);
class_stats(grades, number_of_grades, &average, distribution);
print_all(FILENAME[file_count], &average, distribution);
}
return EXIT_SUCCESS;
}
/*-----------------------------------------------------------------*/
/* This function will open the input file, read the data into */
/* array k, and return filesize. */
int get_data(const char *filename, /* input, current file name */
int grades[]) /* output, the filled array */
{
FILE * input_file;
int number_of_grades =0;
input_file = fopen(filename, "r");
if (input_file == NULL)
{
printf("Error on file-open of file %s", filename);
exit(EXIT_FAILURE);
}
while ((fscanf(input_file, "%i", &grades[number_of_grades])) == 1)
number_of_grades += 1;
return number_of_grades;
}
/*-----------------------------------------------------------------*/
/* This function will print all but your name which is at line 21 */
void print_all( const char *filename, /* input, the current filename */
double *average, /* input, the class average */
int distribution[]) /* input, the grade distribution */
{
int d;
printf(" Stats for %s: ", filename);
printf(" Average grade = %f ", *average);
printf(" Grade Distribution: ");
for (d = DIST_SIZE; d >0; d--)
printf(" Category %3i - %2i ", (d-1)*10, distribution[d-1]);
printf(" ");
return;
}
/*-----------------------------------------------------------------*/
CSC 60. Spring 2019. Lab 5. Arrays, Functions, make Page 1 of 5 PROBLEM: Write a function that has four arguments: a one dimensional array (input) the length of the array (input) average of the array (output) distribution of grades array (output) * Microsoft Word Lab5.docx * This function will be in its own file separate from the other code The function prototype is /* function to return average & distributed grade count void class_stats(int grades], */ /* input, array that holds data */ int number_of_grades, input, actual size of the file */ /* output, average of the array */ / output, distribution count */ double *average, int distribution]) You will need the file lab5.c as your main/driver program for the function. This main program will set things up, read the values from the file, and print the output sentences. You will also need the file lab5.h TO GET THE FILES YOU NEED First move to your class folder by typing: cd csc60 The following command will create a directory named lab5 and put all the needed files into it below your csc60 directory Type: cp -R /gaia/home/faculty/bielr/files_csc60/lab5 Spaces needed: (1) After the cp T Don't miss the space & dot (2) After the -R (3) After the directory name at the end & before the dot. After the files are in your account and you are still in csc60, you need to type: chmod 755 lab5 This will give permissions to the directory. Next move into lab5 directory, and type: chmod 644 lab5* This will give permissions to the files Your new lab5 directory should now contain: lab5.c, lab5.h, lab5a.dat, lab5b.dat INPUT/OUTPUT DESCRIPTION: The input is two lists of unknown length of integer values in the file lab5a.dat and lab5b.dat. They represent test grades. The print output statements are provided. ALGORITHM DEVELOPMENT Pseudocode: int main(void) *given to you */ Print your name & assignment number. Loop through each file Call get_data function and get the number_of_grades Call class_stats. Call print_all and print out the average and the distribution. int get_data (const char * filename, int grades(]) /* sub-function given to you / Open the data file and check for error on open Read and count the values, putting them into an array, returning the number_of_grades void print all (const char *filename, double *average, int distribution[I) /* sub-function given to you / Print to the screen, rather than to a file Print the headers, Average, and then the Grade Distribution #include "lab5.h" void class_stats( int grades[l, int number_of_grades, double *average, int distribution[I) /*A sub-function for you to write. Place it in a separate file. */ set *average to zero for loop from zero to more on next page> CSC 60. Spring 2019. Lab 5. Arrays, Functions, make. Page 4 of 5. Turn in your files. Go to Canvas and turn in four files: 1. lab5.h 2. class_stats.c 3. makefile 4. StudentName_lab5.txt er akiile:) Slide 13: /*Second pass at a makefile:* /*Look at its contents. We have no p2.h but it is included in light italics to show where it would be placed. *NOTE: In lab5, we will need the lab5.h file */ vim makefile >cat makefile power2: power2.o compute.o p2.h gcc power2.o compute.o -o power2 -Im gcc -c power2.c gcc-c compute.c power2.o: power2.c p2.h compute.o: compute.c p2.h /*Run make using our new makefile / [bielr@athena /csc60]68> make gcc -c power2.c gcc-c compute.c gcc power2.o compute.o -o power2 -Im [bielr@athena /csc60]69> Slide 14: # Third and last pass at a makefile t power2.h is not needed but included in light italics to # show where it would be located if it was needed # >cat makefile power2: power2.o compute.o power2.h gcc power2.o compute.o -o power2 -Inm power2.o: power2.h compute.o: power2.h /* Helpful Comments * .Start by opening vim, and typing in the commands to a file named makefile. o vim makefile Close vim and then at the prompt, type: make .When you enter vim, type: :set list o This will show the non-printable characters Itab end of line To reverse the setting, type: :set list! To create a tab on athena, you may have to hit the tab key twice in a row. CSC 60. Spring 2019. Lab 5. Arrays, Functions, make Page 1 of 5 PROBLEM: Write a function that has four arguments: a one dimensional array (input) the length of the array (input) average of the array (output) distribution of grades array (output) * Microsoft Word Lab5.docx * This function will be in its own file separate from the other code The function prototype is /* function to return average & distributed grade count void class_stats(int grades], */ /* input, array that holds data */ int number_of_grades, input, actual size of the file */ /* output, average of the array */ / output, distribution count */ double *average, int distribution]) You will need the file lab5.c as your main/driver program for the function. This main program will set things up, read the values from the file, and print the output sentences. You will also need the file lab5.h TO GET THE FILES YOU NEED First move to your class folder by typing: cd csc60 The following command will create a directory named lab5 and put all the needed files into it below your csc60 directory Type: cp -R /gaia/home/faculty/bielr/files_csc60/lab5 Spaces needed: (1) After the cp T Don't miss the space & dot (2) After the -R (3) After the directory name at the end & before the dot. After the files are in your account and you are still in csc60, you need to type: chmod 755 lab5 This will give permissions to the directory. Next move into lab5 directory, and type: chmod 644 lab5* This will give permissions to the files Your new lab5 directory should now contain: lab5.c, lab5.h, lab5a.dat, lab5b.dat INPUT/OUTPUT DESCRIPTION: The input is two lists of unknown length of integer values in the file lab5a.dat and lab5b.dat. They represent test grades. The print output statements are provided. ALGORITHM DEVELOPMENT Pseudocode: int main(void) *given to you */ Print your name & assignment number. Loop through each file Call get_data function and get the number_of_grades Call class_stats. Call print_all and print out the average and the distribution. int get_data (const char * filename, int grades(]) /* sub-function given to you / Open the data file and check for error on open Read and count the values, putting them into an array, returning the number_of_grades void print all (const char *filename, double *average, int distribution[I) /* sub-function given to you / Print to the screen, rather than to a file Print the headers, Average, and then the Grade Distribution #include "lab5.h" void class_stats( int grades[l, int number_of_grades, double *average, int distribution[I) /*A sub-function for you to write. Place it in a separate file. */ set *average to zero for loop from zero to more on next page> CSC 60. Spring 2019. Lab 5. Arrays, Functions, make. Page 4 of 5. Turn in your files. Go to Canvas and turn in four files: 1. lab5.h 2. class_stats.c 3. makefile 4. StudentName_lab5.txt er akiile:) Slide 13: /*Second pass at a makefile:* /*Look at its contents. We have no p2.h but it is included in light italics to show where it would be placed. *NOTE: In lab5, we will need the lab5.h file */ vim makefile >cat makefile power2: power2.o compute.o p2.h gcc power2.o compute.o -o power2 -Im gcc -c power2.c gcc-c compute.c power2.o: power2.c p2.h compute.o: compute.c p2.h /*Run make using our new makefile / [bielr@athena /csc60]68> make gcc -c power2.c gcc-c compute.c gcc power2.o compute.o -o power2 -Im [bielr@athena /csc60]69> Slide 14: # Third and last pass at a makefile t power2.h is not needed but included in light italics to # show where it would be located if it was needed # >cat makefile power2: power2.o compute.o power2.h gcc power2.o compute.o -o power2 -Inm power2.o: power2.h compute.o: power2.h /* Helpful Comments * .Start by opening vim, and typing in the commands to a file named makefile. o vim makefile Close vim and then at the prompt, type: make .When you enter vim, type: :set list o This will show the non-printable characters Itab end of line To reverse the setting, type: :set list! To create a tab on athena, you may have to hit the tab key twice in a rowStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started