The primary purpose of this lab is to implement a sorting algorithm on parallel arrays. There will be some sample code with the sort function left unimplemented. This sort function is to implement bubble sort on 3 parallel arrays. There will also be some code missing for reading input data and printing the sorted output.
The program will read names, ages and weights for up to 100 people. This means that you need 3 arrays of 100 elements each. Then it will sort the data based on decreasing weights and finally print a report.
The reason bubble sort works is that the for loop compares each pair of adjacent values of the x array. If any of the pairs are out of order, then these values are swapped with the generic swap function and the swapped variable is set to true. Eventually there will be a time when the for loop completes without swapping any values of x. At this point the array is sorted and the outer do-while loop will end since swapped will be false.
In your code for this lab you will adapt the bubble sort code to sort 3 arrays. The testing will be done only for the weights and swapping will be done on the weght values and corresponding values in the other 2 arrays.
Given appropriate input data you should produce a report like this one:
Name Age Weight
----------- --- ------
Fred 35 250.5
Barney 31 165.0
Betty 28 110.5
Wilma 33 115.0
Right now it crashes after it compiles and i enter a persons name. If someone could just help fix that ill try the rest on my own for now.
int read_data ( string names[], int ages[], double weights[], int max )
{
int n;
//string input = "";
//
// Write this function first.
// Below here is the loop to read the input data.
//
Arrays are not 'bad'. They just aren't really convienient. Take for example a variable data structure. If that structure could contain 100 or 1000 items, then to use an array would be a waste of memory if we didnt fill every data block. In C++, it is better to use vectors, because the number of data blocks it can hold can be modified while the data is being given to the program. This means we arent taking up memory space that we aren't necessarily going to use. The reason your program is crashing could be one of these reasons:
- An array item is being called that does not exist. This means that your program is trying to access a memory address that isn't there.
- There is not enough memory to store 300kb of data (i hope that's not true)
- you are trying to pass an array to another function.
I did this... I ended up useing vectors in the end (so much easier that the memory properties of arrays) and dumping arrays. You need to use malloc to make the arrays work, and declare your functions as such. I can not tell you how, arrays and memory allocation are a bit too complex for me to learn on my own (i teach myself) and you are just a beginner.