C++ Sorting an array -
i'm following set of beginner assignments found on forum c++, i'm totally stuck @ task now. task follows:
write program asks user enter number of pancakes eaten breakfast 10 different people (person 1, person 2, ..., person 10) once data has been entered program must analyze data , output person ate pancakes breakfast.
★ modify program outputs person ate least number of pancakes breakfast.
★★★★ modify program outputs list in order of number of pancakes eaten of 10 people.
now i've sorted out original bit , 1 star bit, chose make bit more difficult myself , not go "person 1, 2, 3, 4 , on, instead i've assigned names characters, , print out switch
. here's current code, i'd love suggestions on how sort array without messing order of numbers in array, mess naming code guess.
here's code, know it's not prettiest code out there it's functional now.
#include "stdafx.h" #include <iostream> using namespace std; int main() { int nmostpancakesname; int nleastpancakesname; enum breakfastnames { ned, // 0 arya, // 1 jon, // 2 robb, // 3 sansa, // 4 catelyn, // 5 bran, // 6 theon, // 7 hodor, // 8 ghost // 9 }; int anarray[10]; cout << "enter number of pancakes ned ate breakfast: " << endl; cin >> anarray[ned]; cout << "how many did arya eat?" << endl; cin >> anarray[arya]; cout << "and jon?" << endl; cin >> anarray[jon]; cout << "what robb?" << endl; cin >> anarray[robb]; cout << "did sansa have any?" << endl; cin >> anarray[sansa]; cout << "catelyn?" << endl; cin >> anarray[catelyn]; cout << "crippleboy aka bran?" << endl; cin >> anarray[bran]; cout << "the traitor didn't any, did he?" << endl; cin >> anarray[theon]; cout << "hodor?" << endl; cin >> anarray[hodor]; cout << "no pets @ dining table, ghost." << endl; cin >> anarray[ghost]; int nmaxpancakes = 0; (int npancakes = 0; npancakes < 10; npancakes++) if (anarray[npancakes] > nmaxpancakes) { nmostpancakesname = npancakes; nmaxpancakes = anarray[npancakes]; } int nleastpancakes = 100; (int npancakes2 = 0; npancakes2 < 10; npancakes2++) if (anarray[npancakes2] < nleastpancakes) { nleastpancakesname = npancakes2; nleastpancakes = anarray[npancakes2]; } (int nstartindex = 0; nstartindex < 10; nstartindex++) { int nsmallestindex = nstartindex; (int ncurrentindex = nstartindex + 1; ncurrentindex < 10; ncurrentindex++) { if (anarray[ncurrentindex] < anarray[nsmallestindex]) nsmallestindex = ncurrentindex; } } switch(nmostpancakesname) { case 0: cout << "ned had " << nmaxpancakes << endl; break; case 1: cout << "arya had " << nmaxpancakes << endl; break; case 2: cout << "jon had " << nmaxpancakes << endl; break; case 3: cout << "robb had " << nmaxpancakes << endl; break; case 4: cout << "sansa had " << nmaxpancakes << endl; break; case 5: cout << "catelyn had " << nmaxpancakes << endl; break; case 6: cout << "bran had " << nmaxpancakes << endl; break; case 7: cout << "theon had " << nmaxpancakes << endl; break; case 8: cout << "hodor had " << nmaxpancakes << endl; break; case 9: cout << "ghost had " << nmaxpancakes << endl; break; } switch(nleastpancakesname) { case 0: cout << "ned had " << nleastpancakes << endl; break; case 1: cout << "arya had " << nleastpancakes << endl; break; case 2: cout << "jon had " << nleastpancakes << endl; break; case 3: cout << "robb had " << nleastpancakes << endl; break; case 4: cout << "sansa had " << nleastpancakes << endl; break; case 5: cout << "catelyn had " << nleastpancakes << endl; break; case 6: cout << "bran had " << nleastpancakes << endl; break; case 7: cout << "theon had " << nleastpancakes << endl; break; case 8: cout << "hodor had " << nleastpancakes << endl; break; case 9: cout << "ghost had " << nleastpancakes << endl; break; } return 0; }
here's quick example. there better way's this, should give basic idea of 1 of way's this.
int score[10]; int scorecheck = 0; // initalize array "score" (scorecheck = 0; scorecheck < 10; scorecheck++) { score[scorecheck] = scorecheck; // put everone somewhere } scorecheck = 0; while ( scorecheck < 9 ) // check see if we've reached last person { // check see if person lower on chart ate more if ( anarray[score[scorecheck]] < anarray[score[scorecheck+1]] ) { // swap person 1 , 2, since 2 ate more 1 int tmp = score[scorecheck]; score[scorecheck] = score[scorecheck+1]; score[scorecheck+1] = tmp; // go beggining make // sure in order begining end scorecheck = 0; continue; } // nope, it's in order far // increment next person on chart scorecheck++ }
Comments
Post a Comment