Thursday, 15 September 2011

c++ - Sorting both a string array and a double array from highest to lowest(parallel arrays) and aligning the text -



c++ - Sorting both a string array and a double array from highest to lowest(parallel arrays) and aligning the text -

alright i'll bite, programme sort number highest lowest but it'll sort if numbers arranged in order highest lowest;not if it's sporadic. here 2 illustration of mean.

example 1: 12,11,10,9,8,7,6,5,4,3,2,1.

example 2: 32,25,24,31,10,11,15,16,8,19,18,5.

you see how in illustration 2 of numbers in order 32 25 24 of others not. main problem. secondary problem aligning text vertically looks neat. should utilize setw left, right this? please give feedback.

note 1: ide i'm using codeblocks.

note 2: maintain in mind whatever number user inputs particular month has vertically parallel each.

note 3: i'm pretty sure problem selection sort. should looking @ function void selectionsort since function doing sorting. don't know went wrong sorting though. else seems in order.

/* programme lets user come in total rainfall each month array of doubles. programme calculates , displays total rainfall year, average monthly rainfall, , months highest , lowest amounts. programme displays list of months, sorted in order of rainfall highest lowest.*/ /* programme not take negative numbers monthly rainfall figures.*/ #include <iostream> #include <iomanip> #include <string> using namespace std; void selectionsort(string[], double[], int);// function protoype. int main() { const int size = 12; /* constant integer represent total amount of months in year. */ double totalrainfallpermonth[size]; /* loop array forcefulness user come in variables each element in array. */ double totalrainfallperyear = 0; /* total amount of rainfall (in inches) per year. */ // array of every month. string montharray[size]={"january", "february", "march", "april", "may", "june", "july","august", "september", "october", "november", "december"}; double average; // variable holds average monthly rainfall. int i; // used counter loop. cout << fixed << showpoint << setprecision(2); // set decimal notation. for(i=0; i<=11; i++) { // prompt user come in values. cout << "please come in total rainfall(in inches) "; cout << montharray[i] << ": "; cin >> totalrainfallpermonth[i]; while(totalrainfallpermonth[i] < 0) /* if user enters negative value */ { cerr << "no negative values allowed. "; // display error message. cout << "please seek again. "; cin >> totalrainfallpermonth[i]; } } for(i=0; i<=11; i++) { // calculate total rainfall year. totalrainfallperyear += totalrainfallpermonth[i]; } // display total rainfall year. cout << "\nthe total rainfall year " << totalrainfallperyear; cout << " inches of rain. " << endl; // calculate average monthly rainfall. average = totalrainfallperyear / size; // display average cout << "\nthe average monthly rainfall per month "; cout << average; cout << " inches of rain. " << endl << endl << endl; cout << "\n" << "month " << "\t"; cout << " rainfall(in inches)" << endl; cout << "-----------------------------------"; selectionsort(montharray, totalrainfallpermonth, size); /* phone call in function. */ homecoming 0; } void selectionsort(string month[], double rain[], int size) { int i; int j; int min; (i = 0; < size - 1; i++) { min = i; // intial subscript or first element. (j = + 1; j < size; j++) { if (rain[j] > rain[min]) /* if element greater, new minimum */ { min = j; // swap both variables @ same times double tempdouble = rain[i]; rain[i] = rain[j]; rain[j] = tempdouble; string tempstring = month[i]; month[i] = month[j]; month[j] = tempstring; } } } for(i=0; i<=11; i++) { /* display amount of rainfall per month highest lowest */ cout << "\n" << month[i] << "\t" << rain[i] << endl; } }

there in fact error in selection sort implementation: swapping elements , often.

only after have performed total sweep on whole remaining array , hence have determined global minimum (i maintain term in order consistent variable name , comments in code, although in fact looking maximum) should perform single swap of first element of remaining array minimum element.

the corrected code (only main loop of sorting function shown) should this:

for (i = 0; < size - 1; i++) { min = i; // intial subscript or first element. (j = + 1; j < size; j++) { if (rain[j] > rain[min]) /* if element greater, new minimum */ { min = j; } } // swap both variables @ same times double tempdouble = rain[i]; rain[i] = rain[min]; rain[min] = tempdouble; string tempstring = month[i]; month[i] = month[min]; month[min] = tempstring; }

c++ arrays string sorting double

No comments:

Post a Comment