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: Yellowdog428 is an unknown quantity at this point 
Solved Threads: 0
Yellowdog428 Yellowdog428 is offline Offline
Newbie Poster

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

 
0
  #1
Mar 30th, 2008
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.

  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
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

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

 
0
  #2
Mar 31st, 2008
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.
  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

  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

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

 
0
  #3
Mar 31st, 2008
strcpy is not required as you're copying the whole structure. So the swap function could be as simple as:
  1. void Swap(RainFallInfo& first, RainFallInfo& second){
  2. // swapping technique...
  3. RainFallInfo temp = first;
  4. first = second;
  5. second = temp;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

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

 
0
  #4
Mar 31st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: Yellowdog428 is an unknown quantity at this point 
Solved Threads: 0
Yellowdog428 Yellowdog428 is offline Offline
Newbie Poster

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

 
0
  #5
Mar 31st, 2008
Thank you for your help!

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

is there another way to do it without them?

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

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

 
0
  #6
Apr 1st, 2008
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:
  1. strcpy(swapMonth, month[startIndex]);
  2. strcpy(month[startIndex], month[count]);
  3. strcpy(month[count], swapMonth);

char swapMonth[12][10],
highestMonth[12][10];
You should realise that swapMonth and highestMonth only needs to hold a single month, so use:
  1. char swapMonth[10],
  2. highestMonth[10];


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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,845
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

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

 
0
  #7
Apr 2nd, 2008
You can also use template if it is allowed for your homework however.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 22
Reputation: Yellowdog428 is an unknown quantity at this point 
Solved Threads: 0
Yellowdog428 Yellowdog428 is offline Offline
Newbie Poster

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

 
0
  #8
Apr 2nd, 2008
Thanks guys!

I used the strcpy command like dougy83 suggested!

  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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC