Arrays Lab
Project
- Test Scores
Project will have
three functions: main, round, sort_Array
- Declare an
array to store a maximum of 100 integer test scores.
- Use a loop
to permit operator entry of the scores into the array.
- Scores will
be positive, so use a negative number as a sentinel.
- Display the
following information:
- The test
scores in descending order - (sort the array using a modification
of bubble sort function below)
- The total
of all test scores.
- The average
score rounded to the nearest whole number.
(Code a generic
function to receive a double and return the nearest integer -
for use here and in future)
Test Data:
82, 77, 90, 89, 63,
71, 75, 86, 45, 78, -1
Submit:
Source code
Drawings of array
with subscripts (before & after sorting)
Output and Proofs
of calculations
Bubble
Sort Function - Ascending Order
Prototype:
void sortArray(int[
], int);
Call:
sortArray(nums, 20);
// pass array of 20 ints
Implementation:
void
sortArray(intnums[],int count) //store array addr & size
{
int hold, a, b; //
a and b are subscripts
for(a = 0; a < count-1; a++) //start first loop with
0
{
for(b = a+1; b < count; b++) //start second loop with 1
{
if(nums[a] > nums[b]) //compare nums[0]
to nums[1]
{
hold = nums[a]; //
if first is bigger
nums[a] = nums[b]; // swap the two
nums[b] = hold;
}
}
}
}
Return
to TOP
|