| | |
First post, homework help.... (me != sorting array)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
Need some help.
I have an assignment that calls for the sorting of two arrays. One is an array of doubles, the other chars (two dimensional). I cannot figure it out for the life of me.
Here is the whole program.
The function that I am sorting the arrays is
I have some stuff commented because I was just trying to get the thing to compile...
What I have figured so far is I cannot assign in a char array the same that I can in a 9int or double array...
Any pointers would be great!
Jay
I have an assignment that calls for the sorting of two arrays. One is an array of doubles, the other chars (two dimensional). I cannot figure it out for the life of me.
Here is the whole program.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> using namespace std; //Function Prototypes double sumRain(double[], int); double getHighest(double[], int); double getLowest(double[], int); void sortString(double[], char[][10], int); void displaySort(double[], char[][10], int); void displayResults(double total, double average, double lowest, double highest); int main() { const int MONTHS = 12; double rainfall[MONTHS] = {0}, total = 0, average = 0, highest = 0, lowest = 0; char month[MONTHS][10] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; //Collect the rainfall for each month and store the data in an array for (int count = 0; count < MONTHS; count++) { cout << "Please enter the the rainfall for " << month[count] << endl; cin >> rainfall[count]; if (rainfall[count] < 0) { cout << "You MUST enter an amount greater or equal to zero!\n"; cout << "Please enter the rainfall for the " << month[count] << " month.\n"; cin >> rainfall[count]; } } //Calculate the total rainfall total = sumRain(rainfall, MONTHS); //Calculate the average average = total / MONTHS; //Find the month with the highest rainfall highest = getHighest(rainfall, MONTHS); //Find the month with the lowest rainfall lowest = getLowest(rainfall, MONTHS); //Display results displayResults(lowest, highest, average, total); //Sort The strings sortString(rainfall, month, MONTHS); //display the sorted strings displaySort(rainfall, month, MONTHS); return 0; } void displayResults(double total, double average, double lowest, double highest) { //Format output cout << fixed << showpoint << setprecision(2); //Display results cout << "The total rainfall for the year is " << total << endl; cout << "The average rainfall is " << average << endl; cout << "The the lowest amount of rain for any given month was " << lowest << endl; cout << "The the highest amount of rain for any given month was " << highest << endl; } double sumRain(double rainfall[], int MONTHS) { double total = 0; for (int count = 0; count < MONTHS; count++) { total += rainfall[count]; } return total; } double getHighest(double rainfall[], int MONTHS) { double highest = rainfall[0]; for (int count = 0; count < MONTHS; count++) { if (rainfall[count] > highest) highest = rainfall[count]; } return highest; } double getLowest(double rainfall[], int MONTHS) { double lowest = rainfall[0]; for (int count = 0; count < MONTHS; count++) { if (rainfall[count] < lowest) lowest = rainfall[count]; } return lowest; } void sortString(double rainfall[], char month[][10], int MONTHS) { double swapRain = 0, highest = 0; char swapMonth[12][10], highestMonth[12][10]; for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++) { highest = rainfall[startIndex]; swapRain = rainfall[startIndex]; for (int count = startIndex + 1; count < MONTHS; count++) { if (rainfall[count] > highest) { highest = rainfall[count]; swapRain = rainfall[startIndex]; swapMonth[startIndex] = month[startIndex]; rainfall[count] = swapRain; //month[count] = swapMonth[12][10]; rainfall[startIndex] = highest; //month[startIndex] = highestMonth[12][10]; } } } } void displaySort(double rainfall[], char month[][10], int MONTHS) { for (int count = 0; count < MONTHS; count++) { cout << "The total rain for " << month[count] << " is " << rainfall[count] << endl; } }
The function that I am sorting the arrays is
C++ Syntax (Toggle Plain Text)
void sortString(double rainfall[], char month[][10], int MONTHS) { double swapRain = 0, highest = 0; char swapMonth[12][10], highestMonth[12][10]; for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++) { highest = rainfall[startIndex]; swapRain = rainfall[startIndex]; for (int count = startIndex + 1; count < MONTHS; count++) { if (rainfall[count] > highest) { highest = rainfall[count]; swapRain = rainfall[startIndex]; swapMonth[startIndex] = month[startIndex]; rainfall[count] = swapRain; //month[count] = swapMonth[12][10]; rainfall[startIndex] = highest; //month[startIndex] = highestMonth[12][10]; } } } }
I have some stuff commented because I was just trying to get the thing to compile...
What I have figured so far is I cannot assign in a char array the same that I can in a 9int or double array...
Any pointers would be great!
Jay
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Lets do some sorting.
First of all the sorting technique you are using is sort by swapping. famous as a bubble sort.
before sorting lets improve your code. which is more strutured
Why are you keeping the two related things separate? why don't keep these two things rainfall & month as an aggregate. here comes structure.
how to sort this.
is simple
Hope you understand.
where you lack can be seen by comparing my sort function with yours.
like these.
strcpy is required to copy char array to other char array.
Hope this helps...
if you don't know references, 'RainFallInfo&' you can replace this by RainFallInfo* and do the same.
First of all the sorting technique you are using is sort by swapping. famous as a bubble sort.
before sorting lets improve your code. which is more strutured
Why are you keeping the two related things separate? why don't keep these two things rainfall & month as an aggregate. here comes structure.
C++ Syntax (Toggle Plain Text)
struct RainFallInfo { double rainFall; // stores information about rain char month[10]; // stores information about month. };
how to sort this.
is simple
C++ Syntax (Toggle Plain Text)
void Swap (RainFallInfo& first, RainFallInfo& second) { // swapping technique... RainFallInfo temp; temp.rainFall = first.rainFall; strcpy (temp.month, first.month); first.rainFall = second.rainFall; strcpy (first.month, second.month); second.rainFall = temp.rainFall; strcpy (second.month, temp.month); } void Sort (RainFallInfo rInfo[], int MONTHS) { for (int i=0; i<MONTHS; ++i) { for (int j=i+1; j<MONTHS; ++j) { if (rInfo[i].rainFall> rInfo[j].rainFall) { Swap(rInfo[i], rInfo[j]); } } } }
where you lack can be seen by comparing my sort function with yours.
like these.
C++ Syntax (Toggle Plain Text)
startIndex < (MONTHS - 1); // should be replaced by MONTHS
strcpy is required to copy char array to other char array.
Hope this helps...
if you don't know references, 'RainFallInfo&' you can replace this by RainFallInfo* and do the same.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
strcpy is not required as you're copying the whole structure. So the swap function could be as simple as:
C++ Syntax (Toggle Plain Text)
void Swap(RainFallInfo& first, RainFallInfo& second){ // swapping technique... RainFallInfo temp = first; first = second; second = temp; }
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
I have some stuff commented because I was just trying to get the thing to compile...
What I have figured so far is I cannot assign in a char array the same that I can in a 9int or double array...
You can use strcpy/strncpy for copying the contents of the char arrays:
C++ Syntax (Toggle Plain Text)
strcpy(swapMonth, month[startIndex]); strcpy(month[startIndex], month[count]); strcpy(month[count], swapMonth);
•
•
•
•
char swapMonth[12][10],
highestMonth[12][10];
C++ Syntax (Toggle Plain Text)
char swapMonth[10], highestMonth[10];
•
•
•
•
Any pointers would be great!
Last edited by dougy83; Apr 1st, 2008 at 2:56 am.
You can also use template if it is allowed for your homework however.
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
Thanks guys!
I used the strcpy command like dougy83 suggested!
works like a charm!
I used the strcpy command like dougy83 suggested!
C++ Syntax (Toggle Plain Text)
void sortString(double rainfall[], char month[][10], int MONTHS) { double swapRain = 0, highest = 0; char swapMonth[10], highestMonth[10]; for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++) { highest = rainfall[startIndex]; swapRain = rainfall[startIndex]; for (int count = startIndex + 1; count < MONTHS; count++) { if (rainfall[count] > highest) { highest = rainfall[count]; strcpy(highestMonth, month[count]); swapRain = rainfall[startIndex]; strcpy(swapMonth, month[startIndex]); rainfall[count] = swapRain; strcpy(month[count], swapMonth); rainfall[startIndex] = highest; strcpy(month[startIndex], highestMonth); } } } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Error Msg: ) expected
- Next Thread: C++ Challenge
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






