CS 208
Computer Programming
for Business with C – I
Spring 2003
Lab #6
Due :
Lab Objective:
Description:
Write
a program that will accept name and scores for a student. Use arrays to store
the scores. Use functions to calculate the average score of the student and
also the grade based it. Use bubble sort to sort the scores. Finally display a
grade report for that student.
Program Requirements:
Your
program should first display a welcome message.
Prompt
the user to enter Last name of Student. Store it in a character array (String).
Prompt
the user to enter number of scores to be stored. Store it in an integer
variable.
Using
a for loop, prompt the user and scan in the scores
into a double array.
Call
a function : get_average to calculate the average
score of the student. Pass to it the array of scores and the number of scores
it holds. The function should return the averages as a double. Store it in a
variable.
Call
a function get_grade to determine the grade. Pass it the average score.
Grade
is a letter grade determined based on the average score as:
Average Score Grade
>= 90 A
>= 80 B
>= 70 C
>= 50 D
< 50 F
The
function should return the grade of the student as a character. Store it in a
variable.
Now
call a function sort_scores to sort the scores stored in the array so that it
is in increasing order. In the function use bubble sort.
(Bubble sort algorithm is explained in the last section of the handout). The
function will not return anything, but it will sort the array.
Finally
call a function : display_report
to display the grade report for the student. The function takes the last name,
scores, number of scores, average and grade as arguments. It does not return
anything but prints out the grade report in the following format:
GRADE REPORT
Last Name : xxxxxx
Sorted Scores :
Score 1 : ##.##
Score 2 : ##.##
Score 3 : ##.##
Average
Score: ##.##
Grade:
X
Now
Stop Program Message and Quit .
Functions Prototypes
double
get_average(double[],int);
char
get_grade(double);
void
sort_scores(double[],int);
void
display_report(char[],double[],int,double,char);
Program Flow:
// comments
// include statement(s)
// function prototypes
void main(void)
{
//
Variable declaration. Different data types to appear on different lines.
//
Display Starting Program Message.
// Prompt
user to enter number of scores
//
Scan in number of scores into a variable
//start
for loop to scan in specific scores
// Prompt user to enter score
//
Scan in the scores and store into array variable
//end for loop
//
Call the get_average function and store result in a variable.
//
Call the get_grade function and store result in a variable.
// Call the sort_scores function.
//
Call the display_report function to
display the report in the specified format.
//
display end of program message
} // end of main
// Function Definitions
double get_average(double scores[], int numscores)
{
// code to calculate the average score
// return the average score
}
char get_grade(double average)
{
// code to calculate the grade based on the average
// return the grade
}
void sort_scores(double scores[], int numscores)
{
//code to sort the array
//use bubble sort
}
void display_report(char name[], double scores[], int
numscores, double avg, char grade)
{
// printf statements to print the score in specified
format
}
Sample Runs
* User Inputs bold and
underlined.
Run 1
Starting Student
Report Program
Please Enter Last
Name of Student :
Enter Number of
Scores to store (less than 10) : 4
Enter Score 1: 98
Enter Score 2: 97
Enter Score 3: 96
Enter Score 4: 95
GRADE REPORT
Last Name :
Sorted Scores :
Score 1 : 95.00
Score 2 : 96.00
Score 3 : 97.00
Score 4 : 98.00
Average Score: 96.50
Grade: A
Stopping Student Report Program
Run 2
Starting Student
Report Program
Please Enter Last
Name of Student :
Enter Number of
Scores to store (less than 10): 5
Enter Score 1: 100
Enter Score 2: 95
Enter Score 3: 90
Enter Score 4: 85
Enter Score 5: 76
GRADE REPORT
Last Name :
Sorted Scores :
Score 1 : 76.00
Score 2 : 85.00
Score 3 : 90.00
Score 4 : 95.00
Score 5 : 100.00
Average Score: 89.20
Grade: B
Stopping Student Report Program
Run 3
Starting Student
Report Program
Please Enter Last
Name of Student : Parker
Enter Number of
Scores to store (less than 10): 6
Enter Score 1: 77
Enter Score 2: 88
Enter Score 3: 95
Enter Score 4: 91
Enter Score
Enter Score 6: 74
GRADE REPORT
Last Name : Parker
Sorted Scores :
Score 1 : 45.00
Score 2 : 74.00
Score 3 : 77.00
Score 4 : 88.00
Score 5 : 91.00
Score 6 : 95.00
Average Score: 78.33
Grade: C
Stopping Student Report Program
Run 4
Starting Student Report Program
Please Enter Last
Name of Student : Ipkiss
Enter Number of
Scores to store (less than 10) : 3
Enter Score
Enter Score
Enter Score
GRADE REPORT
Last Name : Ipkiss
Sorted Scores :
Score 1 : 42.00
Score 2 : 51.00
Score 3 : 55.00
Average Score: 49.33
Grade: F
Stopping Student Report Program
Bubble Sort
This sorting algorithm works by comparing each element of the
array to every element after it. The idea is to compare two elements and swap
them if they are in wrong order.
We use 2 for loops. The first loop is used to traverse
through each element of the array. The second loop traverses through the rest
of the elements in the array for each chosen element in the first loop.
Given an array of numbers say
myarray, with length num here's a snippet of C code for
bubble sort:
for (a=0; a < num-1; a++) {
for (b=a+1; b < num;
b++)
{
if (myarray[a] >
myarray[b])
{
/*
swap myarray[a] and myarray[b] */
temp = myarray[a];
myarray[a] = myarray[b];
myarray[b] = temp;
}
}
}
During the first iteration of the outer loop the first
element of the array is compared with every element after it. That is why the
second loop index b starts for a+1. If there is a
element smaller than the first element they are swapped. Thus the first index
holds the smallest index at the end of first outer loop iteration.
At the second iteration the element at second index is taken
and compared with the rest of the elements. And this process continues.
So, after each iteration of the
outer loop, the next smallest element of the array would have come to the
beginning of the array.
The outer loop goes only until the last but one element of
the array, because by then the array will be sorted. The last element does not
have any other elements after it to compare with!