Sie sind auf Seite 1von 7

CS1325 F16

Homework #6-1
Assigned Nov 10, due Nov 17 at 11:59 PM
This homework assignment gives you the opportunity to practice pointers, arrays, sorting algorithms,
and structures.

HW6-1 (Graded out of 100)


Assume each students information (first name, last name, GPA) is stored in a structure variable, and all
the structures related to the various students are elements of an original array called studentArray.
Write a program that prints the names of the students by order of decreasing GPA, without modifying
the studentArray.
typedef struct
{
char * firstName;
char * lastName;
double GPA;
} Student;
In order to print the names of students by decreasing GPA, your program will have to do a sorting
according to the GPA. Many algorithms to sort the values of an array exist, but normally when an array
of values is sorted, the array is modified, so that the first element of the array is the highest value, the
second element of the array is the second highest value, and so on. An example is the bubble sort
algorithm, whose pseudocode is provided below. Note that in order to sort the array, elements in the
array may be swapped.
In this assignment, we want to leave the original array of structures unchanged. One solution might be
to create a duplicate of the data set, perhaps make a copy of one array into another, and then to sort
the second array. This would allow us to view the data both in the original order and in the sorted order.
A more elegant solution is to sort the array indirectly, by using a second array which contains pointers
pointing to the original array. With this technique we wouldnt change the positions of the data items in
the original array; we would only change the values of the pointers in the second array. When this type
of sorting is performed, we still will be able to access the data both in its original order, by using the
original array, but also in sorted order, by using the array of pointers. This solution is also more efficient
if the elements in the original array are complex, and swapping them would take more time than
swapping the pointers. Here are some diagrams that illustrate the idea.
Fig 1 is an example of studentArray to be sorted according to the GPA. We create an array of
pointers Point that indicates the order in which we view the elements of studentArray. Thus we
view *Point[0], then *Point[1], then *Point[2], etc. in that order, instead of viewing in
the order studentArray[0], then studentArray [1], then studentArray[2], etc.

Point is initialized as illustrated in Fig. 2. We then sort the elements of Point so we view the
elements of studentArray in decreasing order of GPA. That is, after sorting, Point[0]->GPA >=
Point[1]->GPA >= Point[2]->GPA, etc., while the elements of studentArray are
untouched. The result is shown in Fig. 3.

1. Sorting procedure
There are many common sorting algorithms that could be used to sort these data. Most of these are
comparison based sorting algorithms, since they make decisions by comparing each array element
against others. Most often, comparison based sorting algorithms work by interchanging, or swapping,
elements within the array. One common and simple comparison based sorting algorithm is called
Bubble Sort. It works by making multiple passes through the data set and on each pass comparing two
adjacent array elements and swapping them if the right hand element is greater than the left hand
element. In this way, the largest element remaining is bubbled to the top of the array on each pass.
After all the passes are completed, the array is in sorted order. Here is the pseudo-code for one version
of the Bubble Sort. This is an algorithm that is based on two nested loops.
Outer loop index i runs from (n-1) down to 1
Inner loop index j runs from 0 to (i-1)
Compare myArr[j] and myArr[j+1]
if myArr[j] < myArr[j+1], swap the contents of myArr[j] and myArr[j+1]
Here, n refers to the number of elements in the array to be sorted. As given, this algorithm would sort
the original array myArr very well. However, it does modify the array itself, which is what we want to
avoid. In this assignment, we have to adapt the bubble sort to sort the Point array instead of the

original studentArray array. Although the sorting should be done according to the GPA values the
Point array is pointing to, the only values actually changed are the pointers in the Point array.

2. Additional requirements Make sure you meet all the requirements to avoid losing
points
1. Follow the requirements in the Homework Notes.
2. You will need to implement the following. Point and studentArray are arrays of Student *
and Student respectively.

a) A sorting function.
This function should implement a modified version of the above Bubble Sort. There are two arrays
involved, studentArray and Point, instead of the single array myArr, like in the pseudocode. The
pseudo code needs to be adapted as follows. In the comparison step, we compare Point[i]->GPA
with Point[j+1]->GPA, but in the swap step, we swap the contents of Point[j] and
Point[j+1]. The function modifies the Point array, but leaves studentArray unchanged.
void modifiedSort(Student *[], size_t); // Arguments are the array of
pointers and the size of the array

b) A function to display an array.


This function should display the elements of an array of Student.
void display(Student [], size_t); // Arguments are the array to
display and the size of the array

c) A function to display the data pointed to by an array of pointers.


This function should display the structures pointed to by the content of an array of pointers. Note that
this function takes as arguments an array of pointers, and the size of the array. The function displays the
structure pointed to by the first pointer in the array, then the structure pointed to by the second pointer
in the array, and so on.
void displayPointedTo(Student* [],size_t); // Arguments are the array
of pointers, and the size of the array

d) Pseudocode for the main() function


create the studentArray array (size is 4 elements)
Initialize the Val array (copy and paste the code below)
print (Now displaying data in the original order)
call the display function that displays the data in the original order
create the Point array
initialize the Point array
print(Now displaying the pointers array)
display the Point array
call the sorting function to sort the Point array (modified version of bubble sort)
print(Now displaying the pointers array after sorting)
display the Point array
print (Now displaying data in sorted order)

call the display function that displays the data in sorted order.
print (Now displaying data in the original order)
call the display function that displays the data in the original order (to verify the original data has not
been modified)

e) Copy and paste this code to initialize the studentArray:


// Initialize array
studentArray[0].firstName = "Harry";
studentArray[0].lastName = "Potter";
studentArray[0].GPA = 3.5;
studentArray[1].firstName = "Sponge";
studentArray[1].lastName = "Bob";
studentArray[1].GPA = 3.8;
studentArray[2].firstName = "Mr.";
studentArray[2].lastName = "Spock";
studentArray[2].GPA = 4.0;
studentArray[3].firstName = "Captain";
studentArray[3].lastName = "Kirk";
studentArray[3].GPA = 3.9;

3. Suggestions for Implementation


1) Implement the function that prints out the structures in the studentArray array. Use it to test that the
structures were initialized properly. Dont do anything else until this function is written and debugged.
2) Then set up the Point Array and initialize it as described above.
3) Then implement the displayPointedTo function that prints out the data pointed to by the Point Array.
Use it to test the initialization of the Point array. At this point, the data should display in the same order
as in the original array. Dont do anything else until this function is written and debugged.
4) Then implement the Bubble Sort algorithm using the modified sorting algorithm given above. As you
work, use the displayPointedTo function from step 3 to check your work.

4. Expected output of your program

Das könnte Ihnen auch gefallen