Here is my heading
#include <iostream> // Standard IO routine #include <cstdlib> // Standard C++ library #include <fstream> // File Stream #include <string> // String Class #include <iomanip> // Formatting using namespace std; // Standard NameSpace
Here is the struct itself
struct soda {
string name;
double price;
int numCans;
};
/* ========================================================================== */
/* TYPE DEFS */
typedef soda SodaMachine[MAX_NUM_DRINKS];
And here is the sort function I am having trouble with.
void SortAndPrint (int n, SodaMachine sodaMachine){
/* The SortAndPrint function sorts the items in the array
based on numer of cans that are for a drink, then it
prints them out
Parameters:
sodaMachine: the array of structs with the data in them.
*/
int // 1st 3: type is index type of array
start,
// index to first value in unsorted portion
largest, // index to smallest value in unsorted
// portion
current, // used to scan array for smallest value
// temp: type is basetype of array
temp; // temporary storage during a swap
// perform N - 1 passes
for ( start = 0; start < (n - 1); start++ ) {
largest = start;
// scan unsorted part of array to find
// smallest value
for ( current = (start + 1); current < n; current++ )
if ( sodaMachine[current].numCans > sodaMachine[largest].numCans )
largest = current;
sodaMachine[largest].name = sodaMachine[current].name;
// perform one exchange of elements if
// necessary
if (largest != start){
temp = sodaMachine[start].numCans;
sodaMachine[start].numCans = sodaMachine[largest].numCans;
sodaMachine[start].name = sodaMachine[largest].name;
sodaMachine[largest].numCans = temp;
}
} // outer for loop
// Print out the names
for (int i = 0; i < MAX_NUM_DRINKS; i++){
cout << i << ". " << sodaMachine[i].name << endl;
cout << "Available: " << sodaMachine[i].numCans;
cout << endl << endl;
} // For Loop
} // SortAndPrint
Any and all help would be appreciated!

New Topic/Question
Reply




MultiQuote






|