help with sorting arrays from hightest to lowest

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

help with sorting arrays from hightest to lowest

 
0
  #1
Nov 16th, 2006
im getting an error cannot convert 'int' to 'double' for argument '1' to 'double sort Array(double'.int)' while trying to debug code
what does that mean here is another copy of my code now.
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. // Function prototypes
  7. double sumArray(double[], int);
  8. double getHighest(double[], int);
  9. double getLowest(double[], int);
  10. double sortArray(double [], int);
  11. double showArray(double [], int);
  12.  
  13. int main()
  14. {
  15. const int MONTHS = 12;
  16. int values[MONTHS] = {12, 11, 10, 9, 8, 7, 6,
  17. 5, 4, 3, 2, 1};
  18.  
  19. double amount[MONTHS] = {12, 34, 3, 6, 5, 7, 4, 44, 43, 40, 41, 9 };
  20. double total, // To hold the total rainfall
  21. average, // To hold the average rainfall
  22. highest, // To hold the highest rainfall amount
  23. lowest; // To hold the lowest rainfall amount
  24.  
  25.  
  26. cout << "Enter the rainfall for each month.\n";
  27. for (int count = 0; count < MONTHS; count++)
  28. {
  29. cout << "Month " << (count + 1) << ": ";
  30. cin >> amount[count];
  31. }
  32.  
  33. // Get the total monthly rainfall
  34. total = sumArray(amount, MONTHS);
  35.  
  36. // Calculate the average rainfall
  37. average = total / MONTHS;
  38.  
  39. // Find the highest monthly rainfall
  40. highest = getHighest(amount, MONTHS);
  41.  
  42. // Find the lowest monthly rainfall
  43. lowest = getLowest(amount, MONTHS);
  44.  
  45. // Display the results
  46. cout << fixed << showpoint << setprecision(2);
  47. cout << "The total monthly rainfall is " << total << " inches" << endl;
  48. cout << "The average monthly rainfall is " << average << " inches" << endl;
  49. cout << "The highest monthly rainfall is " << highest << " inches" << endl;
  50. cout << "The lowest monthly rainfall is " << lowest << " inches " << endl;
  51. cout << "The monthly rainfall in descending order:\n";
  52. sortArray(values, MONTHS);
  53. showArray(values, MONTHS);
  54. return 0;
  55. }
  56.  
  57.  
  58.  
  59. //******************************************************************
  60. // Definition of sumArray *
  61. // This function accepts a double array and its size *
  62. // as arguments. The sum of the array's elements *
  63. // is returned as an double. *
  64. //******************************************************************
  65.  
  66. double sumArray(double array[], int size)
  67. {
  68. double total = 0;
  69.  
  70. for (int count = 0; count < size; count++)
  71. total += array[count];
  72. return total;
  73. }
  74.  
  75.  
  76. //******************************************************************
  77. // Definition of getHighest *
  78. // This function accepts a double array and its size *
  79. // as arguments. The highest value in the array is *
  80. // returned as an double. *
  81. //******************************************************************
  82.  
  83. double getHighest(double array[], int size)
  84. {
  85. double highest;
  86.  
  87. highest = array[0];
  88. for (int count = 1; count < size; count++)
  89. {
  90. if (array[count] > highest)
  91. highest = array[count];
  92. }
  93. return highest;
  94. }
  95.  
  96. //*******************************************************************
  97. // Definition of getLowest *
  98. // This function accepts a double array and its size *
  99. // as arguments. The lowest value in the array is *
  100. // returned as an double *
  101. //*******************************************************************
  102.  
  103. double getLowest(double array[], int size)
  104. {
  105. double lowest;
  106.  
  107. lowest = array[0];
  108. for (int count = 1; count < size; count++)
  109. {
  110. if (array[count] < lowest)
  111. lowest = array[count];
  112. }
  113. return lowest;
  114. }
  115.  
  116.  
  117.  
  118. //*******************************************************************
  119. // Definition of function sortArray *
  120. // This function performs an descending order buble sort on *
  121. // array. elems is the number of elements in the array. *
  122. //*******************************************************************
  123.  
  124. void sortArray(int array[], int elems)
  125. {
  126. bool swap;
  127. int temp;
  128.  
  129. do
  130. {
  131. swap = false;
  132. for (int count = 0; count < (elems - 1); count++)
  133. {
  134. if (array[count] > array[count + 1])
  135. {
  136. temp = array[count];
  137. array[count] = array[count + 1];
  138. array[count + 1] = temp;
  139. swap = true;
  140. }
  141. }
  142. } while (swap);
  143. }
  144.  
  145.  
  146.  
  147.  
  148. //********************************************************************
  149. // Definition of function showArray. *
  150. // This function displays the contents of array. elems is the *
  151. // number of elements. *
  152. //********************************************************************
  153.  
  154. void showArray(int array[], int elems)
  155. {
  156. for (int count = 0; count < elems; count++)
  157. cout << array[count] << " ";
  158. cout << endl;
  159. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with sorting arrays from hightest to lowest

 
0
  #2
Nov 16th, 2006
those functions sort double arrays, not int arrays. Either change your int arrays to double, or change the functions to sort int arrays. The two can not be mixed.

>>double sortArray(double [], int);
this is the wrong prototype. Actual function does not have those parameters.
Last edited by Ancient Dragon; Nov 16th, 2006 at 8:34 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with sorting arrays from hightest to lowest

 
0
  #3
Nov 16th, 2006
ok i changed that to match and now its giving me some more issues heres my code
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. // Function prototypes
  5. double sumArray(double array[], int size);
  6. double getHighest(double array[], int size);
  7. double getLowest(double array[], int size);
  8. double sortArray(double array[], int size);
  9. double showArray(double array[], int size);
  10. int main()
  11. {
  12. const int MONTHS = 12;
  13. int values[MONTHS] = {12, 11, 10, 9, 8, 7, 6,
  14. 5, 4, 3, 2, 1};
  15. double amount[MONTHS] = {12, 34, 3, 6, 5, 7, 4, 44, 43, 40, 41, 9 };
  16. double total, // To hold the total rainfall
  17. average, // To hold the average rainfall
  18. highest, // To hold the highest rainfall amount
  19. lowest; // To hold the lowest rainfall amount
  20.  
  21.  
  22. cout << "Enter the rainfall for each month.\n";
  23. for (int count = 0; count < MONTHS; count++)
  24. {
  25. cout << "Month " << (count + 1) << ": ";
  26. cin >> amount[count];
  27. }
  28.  
  29. // Get the total monthly rainfall
  30. total = sumArray(amount, MONTHS);
  31.  
  32. // Calculate the average rainfall
  33. average = total / MONTHS;
  34.  
  35. // Find the highest monthly rainfall
  36. highest = getHighest(amount, MONTHS);
  37.  
  38. // Find the lowest monthly rainfall
  39. lowest = getLowest(amount, MONTHS);
  40.  
  41. // Display the results
  42. cout << fixed << showpoint << setprecision(2);
  43. cout << "The total monthly rainfall is " << total << " inches" << endl;
  44. cout << "The average monthly rainfall is " << average << " inches" << endl;
  45. cout << "The highest monthly rainfall is " << highest << " inches" << endl;
  46. cout << "The lowest monthly rainfall is " << lowest << " inches " << endl;
  47. cout << "The monthly rainfall in descending order:\n";
  48. sortArray(amount, MONTHS);
  49. showArray(amount, MONTHS);
  50. return 0;
  51. }
  52.  
  53. //******************************************************************
  54. // Definition of sumArray *
  55. // This function accepts a double array and its size *
  56. // as arguments. The sum of the array's elements *
  57. // is returned as an double. *
  58. //******************************************************************
  59. double sumArray(double array[], int size)
  60. {
  61. double total = 0;
  62. for (int count = 0; count < size; count++)
  63. total += array[count];
  64. return total;
  65. }
  66.  
  67. //******************************************************************
  68. // Definition of getHighest *
  69. // This function accepts a double array and its size *
  70. // as arguments. The highest value in the array is *
  71. // returned as an double. *
  72. //******************************************************************
  73. double getHighest(double array[], int size)
  74. {
  75. double highest;
  76.  
  77. highest = array[0];
  78. for (int count = 1; count < size; count++)
  79. {
  80. if (array[count] > highest)
  81. highest = array[count];
  82. }
  83. return highest;
  84. }
  85. //*******************************************************************
  86. // Definition of getLowest *
  87. // This function accepts a double array and its size *
  88. // as arguments. The lowest value in the array is *
  89. // returned as an double *
  90. //*******************************************************************
  91. double getLowest(double array[], int size)
  92. {
  93. double lowest;
  94.  
  95. lowest = array[0];
  96. for (int count = 1; count < size; count++)
  97. {
  98. if (array[count] < lowest)
  99. lowest = array[count];
  100. }
  101. return lowest;
  102. }
  103.  
  104. //*******************************************************************
  105. // Definition of function sortArray *
  106. // This function performs an descending order buble sort on *
  107. // array. elems is the number of elements in the array. *
  108. //*******************************************************************
  109. double sortArray(double array[], int elems)
  110. {
  111. bool swap;
  112. int temp;
  113. do
  114. {
  115. swap = false;
  116. for (int count = 0; count < (elems - 1); count++)
  117. {
  118. if (array[count] > array[count + 1])
  119. {
  120. temp = array[count];
  121. array[count] = array[count + 1];
  122. array[count + 1] = temp;
  123. swap = true;
  124. }
  125. }
  126. } while (swap);
  127. }
  128.  
  129.  
  130. //********************************************************************
  131. // Definition of function showArray. *
  132. // This function displays the contents of array. elems is the *
  133. // number of elements. *
  134. //********************************************************************
  135. double showArray(double array[], int elems)
  136. {
  137. for (int count = 0; count < elems; count++)
  138. cout << array[count] << " ";
  139. cout << endl;
  140. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with sorting arrays from hightest to lowest

 
0
  #4
Nov 16th, 2006
That's nice, have you heard of debugging and finding out what might be wrong yourself?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with sorting arrays from hightest to lowest

 
0
  #5
Nov 16th, 2006
>>ok i changed that to match and now its giving me some more issues heres my code

what where the errors? did you make any attempt to fix them?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with sorting arrays from hightest to lowest

 
0
  #6
Nov 17th, 2006
yeah i changed showValue and sortValue back to a void and changed the int to doubles i guess thats why im getting the error message:

invalid types `double*[double]' for array subscript
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: help with sorting arrays from hightest to lowest

 
0
  #7
Nov 17th, 2006
Here's a working solution. Ok what I did is:
1. Changed the sort algorithm from bubble to selection sort, passing the array in by reference.
2. Changed both sortArray and ShowArray functions to void, the needn't be double, they dont return anything.
3. Changed the count variable from 0 to 1 and used count instead of (count+1) in the loop at the beginning.
4. Changed the spacing and some minor things you prob wont even notice.
Study this code and try to understand it. It might not be exactly what you're looking for but it works fine. If you want, change back the sorting algorithm you had but that's up to you. Hope this helps, byee
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. // Function prototypes
  7. double sumArray(double array[], int size);
  8. double getHighest(double array[], int size);
  9. double getLowest(double array[], int size);
  10. void sortArray(double (&array)[12], int size); // send array by reference
  11. void showArray(double array[], int size);
  12.  
  13. int main()
  14. {
  15. const int MONTHS = 12;
  16. int values[MONTHS] = {12,11,10,9,8,7,6, 5, 4, 3, 2,1 };
  17. double amount[MONTHS] = {12,34,3, 6,5,7,4,44,43,40,41,9 };
  18.  
  19. double total; // To hold the total rainfall
  20. double average; // To hold the average rainfall
  21. double highest; // To hold the highest rainfall amount
  22. double lowest; // To hold the lowest rainfall amount
  23.  
  24. cout << "Enter the rainfall for each month.\n";
  25. for (int count=1; count<=MONTHS; count++)
  26. {
  27. cout << "Month " << count << ": ";
  28. cin >> amount[count];
  29. }
  30.  
  31. // Get the total monthly rainfall
  32. total = sumArray(amount, MONTHS);
  33.  
  34. // Calculate the average rainfall
  35. average = total / MONTHS;
  36.  
  37. // Find the highest monthly rainfall
  38. highest = getHighest(amount, MONTHS);
  39.  
  40. // Find the lowest monthly rainfall
  41. lowest = getLowest(amount, MONTHS);
  42.  
  43. // Display the results
  44. cout << fixed << showpoint << setprecision(2);
  45. cout << "The total monthly rainfall is " << total << " inches" << endl;
  46. cout << "The average monthly rainfall is " << average << " inches" << endl;
  47. cout << "The highest monthly rainfall is " << highest << " inches" << endl;
  48. cout << "The lowest monthly rainfall is " << lowest << " inches " << endl;
  49. cout << "The monthly rainfall in descending order:\n";
  50. sortArray(amount, MONTHS);
  51. showArray(amount, MONTHS);
  52.  
  53. return 0;
  54. }
  55.  
  56. //******************************************************************
  57. // Definition of sumArray *
  58. // This function accepts a double array and its size *
  59. // as arguments. The sum of the array's elements *
  60. // is returned as an double. *
  61. //******************************************************************
  62. double sumArray(double array[], int size)
  63. {
  64. double total = 0;
  65. for (int count = 0; count < size; count++)
  66. total += array[count];
  67. return total;
  68. }
  69.  
  70. //******************************************************************
  71. // Definition of getHighest *
  72. // This function accepts a double array and its size *
  73. // as arguments. The highest value in the array is *
  74. // returned as an double. *
  75. //******************************************************************
  76. double getHighest(double array[], int size)
  77. {
  78. double highest;
  79.  
  80. highest = array[0];
  81. for (int count = 1; count < size; count++)
  82. {
  83. if (array[count] > highest)
  84. highest = array[count];
  85. }
  86. return highest;
  87. }
  88. //*******************************************************************
  89. // Definition of getLowest *
  90. // This function accepts a double array and its size *
  91. // as arguments. The lowest value in the array is *
  92. // returned as an double *
  93. //*******************************************************************
  94. double getLowest(double array[], int size)
  95. {
  96. double lowest;
  97.  
  98. lowest = array[0];
  99. for (int count = 1; count < size; count++)
  100. {
  101. if (array[count] < lowest)
  102. lowest = array[count];
  103. }
  104. return lowest;
  105. }
  106.  
  107. //*******************************************************************
  108. // Definition of function sortArray *
  109. // This function performs an descending order selection sort on *
  110. // array. elems is the number of elements in the array. *
  111. //*******************************************************************
  112. void sortArray(double (&array)[12], int elems)
  113. {
  114. int first, temp;
  115.  
  116. for (int i=elems-1; i>0; i--)
  117. {
  118. first = 0;
  119. for (int j=1; j<=i; j++)
  120. {
  121. if (array[j] < array[first])
  122. first = j;
  123. }
  124. temp = array[first];
  125. array[first] = array[i];
  126. array[i] = temp;
  127. }
  128. }
  129.  
  130.  
  131. //********************************************************************
  132. // Definition of function showArray. *
  133. // This function displays the contents of array. elems is the *
  134. // number of elements. *
  135. //********************************************************************
  136. void showArray(double array[], int elems)
  137. {
  138. for (int count = 0; count < elems; count++)
  139. cout << array[count] << " ";
  140. cout << endl;
  141. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with sorting arrays from hightest to lowest

 
0
  #8
Nov 17th, 2006
thanks that was mighty helpfull.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC