Code:
/* Input: This program is divided into three parts.
Part 1: p5.h : is the header of the program.
Part 2: p5m.c : is the function MAIN of the program.
Part 3: p5f.c : is the series of companion functions.
The program will use data from the file p5.in
Makefile is then used to compile the three together.
*/
/* Output: The output for the program will be sent directly to the monitor.
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROWS 40
#define MAX_COLS 5
// Prototype Declarations
int getStuData (int StdNum[], int table[MAX_ROWS][MAX_COLS],
float StdAverage[MAX_ROWS]);
void printRawData(int size, int StdNum[], int table[][MAX_COLS]);
void testAverage (int table[][MAX_COLS], float testAve[], int size);
void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[],
float testAve[], int size);
int table [MAX_ROWS][MAX_COLS];
int StdNum [MAX_ROWS];
float StdAverage [MAX_ROWS] = {0};
float testAve [MAX_COLS] = {0};
int main(void)
{
//Local Definitions
int size;
//Function Calls
size=getStuData (StdNum, table, StdAverage);
printRawData(size, StdNum, table);
testAverage (table, testAve, size);
printResults (table, StdNum, StdAverage, testAve, size);
return 0;
}
//end of Main
/*------------ getStuData-------------------
This function reads the file p5.in
and then stores the student numbers into a
array.
*/
int getStuData (int StdNum[MAX_ROWS], int table[MAX_ROWS][MAX_COLS],
float StdAverage[MAX_ROWS])
{
// Local Definitions
FILE *fp;
int row=0;
int col=0;
int rowAve;
int Okscan;
if (!(fp = fopen ("p5.in", "r")))
printf("Error opening File\a\a\n") , exit (100);
while(row<MAX_ROWS)
{
rowAve=0;
for(col=0; col<6; col++)
{
if (col==0)
{
Okscan=fscanf(fp, "%d", &StdNum[row]);
if(!Okscan)
break;
}
if (col!=0)
{
fscanf(fp, "%d", &table[row][col-1]);
rowAve+=table[row][col-1];
}
}
if(Okscan)
{
StdAverage[row]=(rowAve/5);
row++;
}
else
break;
}
fclose(fp);
return row;
}
void printRawData(int size, int StdNum[], int table[][MAX_COLS])
{
//Local Definitions
int row=0;
int col=0;
system("clear");
printf("\n\t\t RAW DATA REPORT\n");
printf("Student # Test1 Test2 Test3"
" Test4 Test5\n");
printf("---------------------------------------"
" ----------------");
for(row=0; row < size; row++)
{
printf("\n%7d", StdNum[row]);
for(col = 0; col<MAX_COLS; col++)
printf("%6d", table[row][col]);
}
printf("\n\n\nDepress <enter> key to continue: ");
getc(stdin);
fflush(stdin);
}
/*--------------testAverage--------------
This function will average the students scores
for each test individually.
*/
void testAverage (int table[][MAX_COLS], float testAve[], int size)
{
//Local Definitions
int row=0;
int col=0;
for(col = 0; col<MAX_COLS; col++)
{
for(row=0; row < size; row++)
testAve[col] += table[row][col];
testAve[col] /= size;
}
}
/*-------------printResults---------------
This function will display the Average
data report.
*/
void printResults (int table[][MAX_COLS], int StdNum[], float StdAverage[],
float testAve[], int size)
{
int row=0;
int col=0;
system("clear");
printf("\t\t STATISTICAL REPORT");
printf("\nStudent Test1 Test2 Test3"
" Test4 Test5 Average");
printf("\n---------------------------------------"
" ----------------");
for(row=0; row < size; row++)
{
printf("\n%7d", StdNum[row]);
for(col = 0; col<MAX_COLS; col++)
printf("%6d", table[row][col]);
printf(" | %6.2f", StdAverage[row]);
}
printf("\n ****************************************************\n");
printf("Average: ");
for(col = 0; col<MAX_COLS; col++)
printf("%6.2f", testAve[col]);
printf("\n\n\nDepress <enter> key to continue: ");
getc(stdin);
fflush(stdin);
return;
}