Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "courses.h"
/*Here is printTableTitle's prototype*/
void printTableTitle( const char *theTitle );
/*Here is printColumnLable's prototype*/
void printColumnLabels( unsigned short quiz_count );
/*Here is calculateStudentAverage's prototype*/
float calculateStudentAverage(
unsigned short **theScores,
unsigned short studentRoNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount);
/*Here is printStudentScoresAndAverage's prototype*/
void printStudentScoresAndAverage(
const unsigned short **theScores,
unsigned short studentid[TOTAL_ROWS],
unsigned short studentRoNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount);
/*Here is calculateQuizAverage's prototype*/
float calculateQuizAverage(
const unsigned short **theScores,
unsigned short quizColumnNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount);
/*Here is printQuizAverages prototype*/
void printQuizAverages(
const unsigned short **theScores,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short studentCount,
unsigned short quizCount );
/*We begin the definitions of all functions here*/
/**********************************************************************************************
printTableTitle()
Description: Function to display the title of the table being printed
Returns: NONE
Parameters:
theTitle[]: Contains the title for the array being printed. Not modifyable.
Procedure/Algorithm: Uses a printf statement to print the array's title (specified by theTitle[])
************************************************************************************************/
void printTableTitle( const char *theTitle)
{
printf ("\n%s", theTitle);
}
/************************************************************************************************
printColumnLabels()
Description: Function to print the column labels on the array.
Returns: NONE
Parameters:
quiz_count: acts as a placeholder for the column label value (i.e. 1, 2, 3, ect.)
Procedure/Algorithm: Uses a for loop in order to print labels on each students quiz score row.
*************************************************************************************************/
void printColumnLabels(unsigned short quiz_count )
{
int variable;
printf ("\n\n%10s", "Student");
for (variable = 1; variable <= quiz_count; variable++)
{
printf("%5d",variable);
}
printf("%10s\n", "Average");
}
/************************************************************************************************
calculateStudentAverage()
Description: Function that calculates the students quiz adverage for a single row.
Returns:
average = the average calculated by the function
Parameters:
theScores [][TOTAL_COLUMNS]: Array that stores students test scores.
studentRoNumber: Current row number that the function is currently working with.
totalRowsInArray: The total number of rows in our student grades array
totalColumnsInArray: The total number of columns (tests) in the array.
valCount: total number of filled columns in the student array.
Procedure/Algorithm: The function takes a single row of student quiz scores,
adds them up and then calculates their average.
**************************************************************************************************/
float calculateStudentAverage(
unsigned short **theScores,
unsigned short studentRoNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount)
{
float rowAverage;
unsigned short ColumnsInArray;
rowAverage = 0;
for (ColumnsInArray = 0; ColumnsInArray < valCount; ColumnsInArray++)
{
rowAverage += theScores[studentRoNumber][totalColumnsInArray];
}
rowAverage /= (float) valCount;
totalColumnsInArray == ColumnsInArray;
return rowAverage;
}
/***********************************************************************************************************
printStudentScoresAndAverage()
Description: Function that prints out the previously calculated student scores and averages
Returns: NONE - simply prints out results
Parameters:
theScores: Array that stores the list of student test scores.
studentRoNumber: Current row number that the function is currently working with.
totalRowsInArray: The total number of rows in our student grades array.
totalColumnsInArray: The total number of columns (tests) in the array.
valCount: Number of filled columns in the student grades array.
Procedure/Algorithm: Input from main() is taken and put into the function. The function prints out
the unadulterated test scores. Once they are fully printed, the function calls the calculateStudentAdverage
function and prints out the student's average on the test scores.
************************************************************************************************************/
void printStudentScoresAndAverage(
const unsigned short **theScores,
unsigned short studentid[TOTAL_ROWS],
unsigned short studentRoNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount)
{
unsigned short ColumnsInArray;
printf ("%10.03hu", studentid[studentRoNumber]);
for (totalColumnsInArray = 0; totalColumnsInArray < valCount; totalColumnsInArray++)
{
printf("%5hu", theScores[studentRoNumber][totalColumnsInArray]);
}
printf("%10.2f\n", calculateStudentAverage(theScores, studentRoNumber, totalRowsInArray, totalColumnsInArray, valCount));
}
/***************************************************************************************************************
calculateQuizAverage()
Description: Function that calculates averages for all quizzes in a column
Returns:
quizAverage = average for the quiz column at hand
Parameters:
theScores: Array that stores the list of student test scores.
quizColumnNumber: Value sent from printQuizAverages() - indicates current column
totalRowsInArray: The total number of filled rows in our students grade array
totalColumnsInArray: The total number of columns (tests) in the array.
valCount: number of filled columns in the student grades array.
Procedure/Algorithm: Input from printQuizAverages() is sent to the function. Function then calculates the
average for all tests in a particular test column and sends results back to printQuizAverages() as quizAverage.
****************************************************************************************************************/
float calculateQuizAverage(
const unsigned short **theScores,
unsigned short quizColumnNumber,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short valCount)
{
float quizAverage;
quizAverage = 0.0;
for (totalRowsInArray = 0; totalRowsInArray <= valCount; totalRowsInArray++)
{
quizAverage += theScores[totalRowsInArray][quizColumnNumber];
}
quizAverage /= valCount;
return quizAverage;
}
/**********************************************************************************************************
printQuizAverages()
Description: Function that prints out quiz averages calculated by calculateQuizAverage()
Returns: None - prints outputquizColumnNumber
Parameters
theScores: Array that stores the list of student test scores.
totalRowsInArray: The total number of filled rows in our students grade array
totalColumnsInArray: The total number of columns (tests) in the array.
studentCount: Number of filled rows in our student grades array.
quizCount: Number of filled columns in our student grades array.
Procedure/Algorithm: Function executes after main body of program (introduction, printing of student grades)
has completed. Function takes input from main() and utilizes seperate function calculateQuizAverage() to
calculate the adverages for each quiz column. It then prints these at the bottom of the quiz array.
************************************************************************************************************/
void printQuizAverages(
const unsigned short **theScores,
unsigned short totalRowsInArray,
unsigned short totalColumnsInArray,
unsigned short studentCount,
unsigned short quizCount)
{
float quizaverage; //Value used to pass the average without changing value of quizCount
unsigned short incrementalvalue; //Value used in for loop below to advance the column to be printed
printf( "%10s", "Average" );
for (incrementalvalue = 0; incrementalvalue < quizCount; incrementalvalue++)
{
printf ("%5.1f", calculateQuizAverage(theScores, incrementalvalue, studentCount, quizCount, studentCount));
}
quizaverage = 0.0f;
for (incrementalvalue = 0; incrementalvalue <= studentCount; incrementalvalue++)
quizaverage += calculateQuizAverage(theScores, incrementalvalue, studentCount, quizCount, studentCount);
quizaverage /= quizCount;
printf("%10.2f", quizaverage);
}
/************************************************************************************************
CourseCreateCourse()
Description: Function that creates a new structure of type Class_t to hold course info
Returns: One, returns structure "newcourse"
Parameters:
coursename - Array that holds the name of the course.
deptnum - The department number of the course in question.
coursenum - The course number of the course in question.
sectionnum - The section number of the course in question.
Procedure/Algorithm: The function is sent info from main reguarding the course name, and
numbers. The function initializes the structure for the course and inserts this data into it.
The data that is not added at the time of initialization, such as student numbers, is set to 0.
The function then returns the structure to main.
************************************************************************************************/
//CourseCreateCourse starts here
Class_t CourseCreateCourse(
char coursename[25],
unsigned short deptnum,
unsigned short coursenum,
unsigned short sectionnum
)
{
Class_t newcourse = { 0 };
int i, j;
unsigned short **quizzesp;
//Copies the char array of coursename to the structure elements
strcpy(newcourse.coursename, coursename);
//Copies the various numbers to the structure elements
newcourse.deptnum = deptnum;
newcourse.coursenum = coursenum;
newcourse.sectionnum = sectionnum;
quizzesp = (unsigned short**) calloc (sizeof (unsigned short), TOTAL_ROWS);
for (i = 0; i <= TOTAL_ROWS; i++)
quizzesp[i] = (unsigned short*) calloc (sizeof (unsigned short), TOTAL_COLUMNS);
quizzesp = &newcourse.quizzes;
//Returns structure.
return newcourse;
}
/*************************************************************************************************
CourseAddStudent()
Description: Adds a student to the array elements of structure newcourse, repeating until the
user-defined counter is complete
Returns: Updated version of the newcourse structure with student info within.
Parameters:
newcourse - Structure occurance from main that was from CourseCreateCourse.
Procedure: Based on the number of students that was entered by the user, CourseAddStudent runs a
for loop that asks for various student and quiz related information. It takes this information and stores it within the newcourse structure. At the same time, it keeps track of how many quizzes
there are and enters that info into newcourse as well. Once completed, it returns the updated
structure value to main.
*************************************************************************************************/
//CourseAddStudent Starts here
Class_t CourseAddStudent(Class_t newcourse)
{
unsigned short counter;
unsigned short studentquiz = 0;
//Start of student-adding loop
printf("\nPlease enter the student's ID number now:");
scanf("%hu", &newcourse.studentid[counter]);
//Start of sub-loop to add student's quiz scores.
while(studentquiz < newcourse.scorecount)
{
printf("\nPlease enter the student's quiz score %hu:", studentquiz+1);
scanf("%hu", &newcourse.quizzes[counter][studentquiz]);
studentquiz++;
}
studentquiz = 0;
//Return newcourse to main.
return newcourse;
}
/*************************************************************************************************
CourseAddScore()
Description: Function that adds the final quiz score onto the end of the array for each student
Returns: Updated version of the newcourse structure occurance
Parameters:
newcourse - Structure occurance sent from main.
Procedure: The function adds one last test to the end of each student row. This enables the user
to have one last grade added before the adverages are calculated. Once the grade is added, the
newcourse.scorecount value is updated to reflect the extra score, and the function returns the
updated version of the newcourse structure occurance.
*************************************************************************************************/
//CourseAddScore Begins here
Class_t CourseAddScore(Class_t newcourse)
{
unsigned short student;
//Run down the list of students to add the extra grade
for (student = 0; student < newcourse.studentcount; student++)
{
printf("\nPlease enter the quiz score for student %hu:", student+1);
scanf("%hu", &newcourse.quizzes[student][newcourse.scorecount]);
}
//Update the scorecount value
newcourse.scorecount++;
return newcourse;
}
/*************************************************************************************************
CoursePrintScores()
Description: Function that handles the printing of the table title, the student numbers, the
student grades, and the adverages to everything.
Returns: NONE
Parameters:
newcourse - Structure occurance sent from main.
Procedure: This function is responsible for printing all of the scores and averages, plus the
table title. It does this with the existing functions printTableTitle, printColumnLables,
printStudentScoresAndAverage, and printQuizAverages. The function sends the data to these various
functions in the order that the require, having broken the structure down to its component parts.
*************************************************************************************************/
//CoursePrintScores
Class_t CoursePrintScores(Class_t newcourse)
{
unsigned short i;
//Print the table title
printTableTitle(newcourse.coursename);
//Print the quiz numbers
printColumnLabels(newcourse.scorecount);
//Print the student scores and adverage for each student
for (i = 0; i < newcourse.studentcount; i++)
printStudentScoresAndAverage(newcourse.quizzes, newcourse.studentid, i, newcourse.studentcount, newcourse.scorecount, newcourse.scorecount);
//Print the quiz adverages
printQuizAverages(newcourse.quizzes, TOTAL_ROWS, TOTAL_COLUMNS, newcourse.studentcount, newcourse.scorecount);
printf("\n");
}
/*Start of main function*/
int main( void )
{
/* *************************************************************************
* DEFINE arrays: *
************************************************************************/
unsigned short deptnum, coursenum, sectionnum, counter, quizcount, i;
char coursename[25],tempcoursename1[25], tempcoursename2[25];
//Initialization of the newcourse structure.
Class_t newcourse;
/*************************************************************************
* PRINT Introduction to user *
************************************************************************/
printf("Please specify a Course Name:");
scanf ("%s %s", tempcoursename1, tempcoursename2);
//Appending the 2nd part of the course name onto the same array
strcat(tempcoursename1, " ");
strcat(tempcoursename1, tempcoursename2);
printf("\nPlease enter the Department number:");
scanf ("%hu", &deptnum);
printf("\nPlease enter the Course number:");
scanf ("%hu", &coursenum);
printf("\nPlease enter the Section number:");
scanf("%hu", §ionnum);
//Defining how the title will be printed through sprintf
sprintf(coursename, "%s # %hu.%hu-%.03hu", tempcoursename1, deptnum, coursenum, sectionnum);
//Execute CourseCreateCourse
newcourse = CourseCreateCourse(coursename, deptnum, coursenum, sectionnum);
//More User prompts
printf("\nPlease enter the number of students in the course:");
scanf("%hu", &newcourse.studentcount);
printf("\nNow you will be allowed to enter in your students and their scores.");
//Begin student-adding loop.
for (counter = 0; counter < newcourse.studentcount; counter++)
{
printf("\nPlease enter the number of quiz scores:");
scanf("%hu", &quizcount);
//If the number of quizzes is greater than that currently on file...
if(quizcount >= newcourse.scorecount)
//newcourse.scorecount gets updated
newcourse.scorecount = quizcount;
else
quizcount = 0;
//Execute CourseAddStudent
newcourse = CourseAddStudent(newcourse);
}
printf("\nNow you will be allowed to enter the student's final quiz score.");
//Execute CourseAddScore
newcourse = CourseAddScore(newcourse);
//Execute CoursePrintScores
newcourse = CoursePrintScores(newcourse);
return EXIT_SUCCESS;
}
Thanks to anyone who feels like reading through this beast :-)