943,892 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1660
  • C++ RSS
Mar 30th, 2008
0

First post, homework help.... (me != sorting array)

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5. //Function Prototypes
  6. double sumRain(double[], int);
  7. double getHighest(double[], int);
  8. double getLowest(double[], int);
  9. void sortString(double[], char[][10], int);
  10. void displaySort(double[], char[][10], int);
  11. void displayResults(double total, double average, double lowest, double highest);
  12.  
  13. int main()
  14. {
  15. const int MONTHS = 12;
  16. double rainfall[MONTHS] = {0},
  17. total = 0,
  18. average = 0,
  19. highest = 0,
  20. lowest = 0;
  21. char month[MONTHS][10] = {"January",
  22. "Febuary",
  23. "March",
  24. "April",
  25. "May",
  26. "June",
  27. "July",
  28. "August",
  29. "September",
  30. "October",
  31. "November",
  32. "December"};
  33.  
  34. //Collect the rainfall for each month and store the data in an array
  35. for (int count = 0; count < MONTHS; count++)
  36. {
  37. cout << "Please enter the the rainfall for " << month[count] << endl;
  38. cin >> rainfall[count];
  39.  
  40. if (rainfall[count] < 0)
  41. {
  42. cout << "You MUST enter an amount greater or equal to zero!\n";
  43. cout << "Please enter the rainfall for the " << month[count] << " month.\n";
  44. cin >> rainfall[count];
  45. }
  46. }
  47.  
  48. //Calculate the total rainfall
  49. total = sumRain(rainfall, MONTHS);
  50.  
  51. //Calculate the average
  52. average = total / MONTHS;
  53.  
  54. //Find the month with the highest rainfall
  55. highest = getHighest(rainfall, MONTHS);
  56.  
  57. //Find the month with the lowest rainfall
  58. lowest = getLowest(rainfall, MONTHS);
  59.  
  60. //Display results
  61. displayResults(lowest, highest, average, total);
  62.  
  63. //Sort The strings
  64. sortString(rainfall, month, MONTHS);
  65.  
  66. //display the sorted strings
  67. displaySort(rainfall, month, MONTHS);
  68.  
  69.  
  70. return 0;
  71.  
  72. }
  73. void displayResults(double total, double average, double lowest, double highest)
  74. {
  75. //Format output
  76. cout << fixed << showpoint << setprecision(2);
  77.  
  78. //Display results
  79. cout << "The total rainfall for the year is " << total << endl;
  80. cout << "The average rainfall is " << average << endl;
  81. cout << "The the lowest amount of rain for any given month was " << lowest << endl;
  82. cout << "The the highest amount of rain for any given month was " << highest << endl;
  83. }
  84. double sumRain(double rainfall[], int MONTHS)
  85. {
  86. double total = 0;
  87. for (int count = 0; count < MONTHS; count++)
  88. {
  89. total += rainfall[count];
  90. }
  91. return total;
  92. }
  93. double getHighest(double rainfall[], int MONTHS)
  94. {
  95. double highest = rainfall[0];
  96. for (int count = 0; count < MONTHS; count++)
  97. {
  98. if (rainfall[count] > highest)
  99. highest = rainfall[count];
  100. }
  101. return highest;
  102. }
  103. double getLowest(double rainfall[], int MONTHS)
  104. {
  105. double lowest = rainfall[0];
  106. for (int count = 0; count < MONTHS; count++)
  107. {
  108. if (rainfall[count] < lowest)
  109. lowest = rainfall[count];
  110. }
  111. return lowest;
  112. }
  113. void sortString(double rainfall[], char month[][10], int MONTHS)
  114. {
  115. double swapRain = 0,
  116. highest = 0;
  117. char swapMonth[12][10],
  118. highestMonth[12][10];
  119.  
  120. for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++)
  121. {
  122. highest = rainfall[startIndex];
  123. swapRain = rainfall[startIndex];
  124.  
  125. for (int count = startIndex + 1; count < MONTHS; count++)
  126. {
  127.  
  128. if (rainfall[count] > highest)
  129. {
  130. highest = rainfall[count];
  131. swapRain = rainfall[startIndex];
  132. swapMonth[startIndex] = month[startIndex];
  133. rainfall[count] = swapRain;
  134. //month[count] = swapMonth[12][10];
  135. rainfall[startIndex] = highest;
  136. //month[startIndex] = highestMonth[12][10];
  137. }
  138. }
  139. }
  140. }
  141. void displaySort(double rainfall[], char month[][10], int MONTHS)
  142. {
  143. for (int count = 0; count < MONTHS; count++)
  144. {
  145. cout << "The total rain for " << month[count] << " is " << rainfall[count] << endl;
  146. }
  147. }

The function that I am sorting the arrays is
C++ Syntax (Toggle Plain Text)
  1. void sortString(double rainfall[], char month[][10], int MONTHS)
  2. {
  3. double swapRain = 0,
  4. highest = 0;
  5. char swapMonth[12][10],
  6. highestMonth[12][10];
  7.  
  8. for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++)
  9. {
  10. highest = rainfall[startIndex];
  11. swapRain = rainfall[startIndex];
  12.  
  13. for (int count = startIndex + 1; count < MONTHS; count++)
  14. {
  15.  
  16. if (rainfall[count] > highest)
  17. {
  18. highest = rainfall[count];
  19. swapRain = rainfall[startIndex];
  20. swapMonth[startIndex] = month[startIndex];
  21. rainfall[count] = swapRain;
  22. //month[count] = swapMonth[12][10];
  23. rainfall[startIndex] = highest;
  24. //month[startIndex] = highestMonth[12][10];
  25. }
  26. }
  27. }
  28. }

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
Reputation Points: 27
Solved Threads: 0
Light Poster
Yellowdog428 is offline Offline
25 posts
since Mar 2008
Mar 31st, 2008
0

Re: First post, homework help.... (me != sorting array)

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.
C++ Syntax (Toggle Plain Text)
  1. struct RainFallInfo {
  2. double rainFall; // stores information about rain
  3. char month[10]; // stores information about month.
  4. };

how to sort this.
is simple

C++ Syntax (Toggle Plain Text)
  1. void Swap (RainFallInfo& first, RainFallInfo& second)
  2. {
  3. // swapping technique...
  4. RainFallInfo temp;
  5. temp.rainFall = first.rainFall;
  6. strcpy (temp.month, first.month);
  7.  
  8. first.rainFall = second.rainFall;
  9. strcpy (first.month, second.month);
  10.  
  11.  
  12. second.rainFall = temp.rainFall;
  13. strcpy (second.month, temp.month);
  14. }
  15.  
  16.  
  17. void Sort (RainFallInfo rInfo[], int MONTHS)
  18. {
  19. for (int i=0; i<MONTHS; ++i)
  20. {
  21. for (int j=i+1; j<MONTHS; ++j)
  22. {
  23. if (rInfo[i].rainFall> rInfo[j].rainFall)
  24. {
  25. Swap(rInfo[i], rInfo[j]);
  26. }
  27. }
  28. }
  29. }
Hope you understand.

where you lack can be seen by comparing my sort function with yours.
like these.
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Mar 31st, 2008
0

Re: First post, homework help.... (me != sorting array)

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)
  1. void Swap(RainFallInfo& first, RainFallInfo& second){
  2. // swapping technique...
  3. RainFallInfo temp = first;
  4. first = second;
  5. second = temp;
  6. }
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Mar 31st, 2008
0

Re: First post, homework help.... (me != sorting array)

dougy83
thanks, I know this is the portable and most suitable way to swap, there exists several other swapping techniques, but I was concentrating on technique adopted by Yellowdog428. I want him to find out this technique himself.

Thanks anyway.
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Mar 31st, 2008
0

Re: First post, homework help.... (me != sorting array)

Thank you for your help!

Problem is I have not learned structures yet....

is there another way to do it without them?

Thanks again
Reputation Points: 27
Solved Threads: 0
Light Poster
Yellowdog428 is offline Offline
25 posts
since Mar 2008
Apr 1st, 2008
0

Re: First post, homework help.... (me != sorting array)

Quote ...
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't assign arrays in the same way that you can assign an int, long, double, etc.

You can use strcpy/strncpy for copying the contents of the char arrays:
C++ Syntax (Toggle Plain Text)
  1. strcpy(swapMonth, month[startIndex]);
  2. strcpy(month[startIndex], month[count]);
  3. strcpy(month[count], swapMonth);

Quote ...
char swapMonth[12][10],
highestMonth[12][10];
You should realise that swapMonth and highestMonth only needs to hold a single month, so use:
C++ Syntax (Toggle Plain Text)
  1. char swapMonth[10],
  2. highestMonth[10];


Quote ...
Any pointers would be great!
So you like pointers then?? The strcpy can be avoided if we swap pointers instead. I have a feeling that this will confuse more than it's worth so I won't include any info, unless you want it -- so stick to the strcpy method shown above.
Last edited by dougy83; Apr 1st, 2008 at 2:56 am.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Apr 2nd, 2008
0

Re: First post, homework help.... (me != sorting array)

You can also use template if it is allowed for your homework however.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Apr 2nd, 2008
0

Re: First post, homework help.... (me != sorting array)

Thanks guys!

I used the strcpy command like dougy83 suggested!

C++ Syntax (Toggle Plain Text)
  1. void sortString(double rainfall[], char month[][10], int MONTHS)
  2. {
  3. double swapRain = 0,
  4. highest = 0;
  5. char swapMonth[10],
  6. highestMonth[10];
  7.  
  8. for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++)
  9. {
  10. highest = rainfall[startIndex];
  11. swapRain = rainfall[startIndex];
  12.  
  13. for (int count = startIndex + 1; count < MONTHS; count++)
  14. {
  15.  
  16. if (rainfall[count] > highest)
  17. {
  18. highest = rainfall[count];
  19. strcpy(highestMonth, month[count]);
  20. swapRain = rainfall[startIndex];
  21. strcpy(swapMonth, month[startIndex]);
  22. rainfall[count] = swapRain;
  23. strcpy(month[count], swapMonth);
  24. rainfall[startIndex] = highest;
  25. strcpy(month[startIndex], highestMonth);
  26. }
  27. }
  28. }
  29. }
works like a charm!
Reputation Points: 27
Solved Threads: 0
Light Poster
Yellowdog428 is offline Offline
25 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error Msg: ) expected
Next Thread in C++ Forum Timeline: C++ Challenge





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC