| | |
help with sorting arrays from hightest to lowest
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
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.
what does that mean here is another copy of my code now.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; // Function prototypes double sumArray(double[], int); double getHighest(double[], int); double getLowest(double[], int); double sortArray(double [], int); double showArray(double [], int); int main() { const int MONTHS = 12; int values[MONTHS] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; double amount[MONTHS] = {12, 34, 3, 6, 5, 7, 4, 44, 43, 40, 41, 9 }; double total, // To hold the total rainfall average, // To hold the average rainfall highest, // To hold the highest rainfall amount lowest; // To hold the lowest rainfall amount cout << "Enter the rainfall for each month.\n"; for (int count = 0; count < MONTHS; count++) { cout << "Month " << (count + 1) << ": "; cin >> amount[count]; } // Get the total monthly rainfall total = sumArray(amount, MONTHS); // Calculate the average rainfall average = total / MONTHS; // Find the highest monthly rainfall highest = getHighest(amount, MONTHS); // Find the lowest monthly rainfall lowest = getLowest(amount, MONTHS); // Display the results cout << fixed << showpoint << setprecision(2); cout << "The total monthly rainfall is " << total << " inches" << endl; cout << "The average monthly rainfall is " << average << " inches" << endl; cout << "The highest monthly rainfall is " << highest << " inches" << endl; cout << "The lowest monthly rainfall is " << lowest << " inches " << endl; cout << "The monthly rainfall in descending order:\n"; sortArray(values, MONTHS); showArray(values, MONTHS); return 0; } //****************************************************************** // Definition of sumArray * // This function accepts a double array and its size * // as arguments. The sum of the array's elements * // is returned as an double. * //****************************************************************** double sumArray(double array[], int size) { double total = 0; for (int count = 0; count < size; count++) total += array[count]; return total; } //****************************************************************** // Definition of getHighest * // This function accepts a double array and its size * // as arguments. The highest value in the array is * // returned as an double. * //****************************************************************** double getHighest(double array[], int size) { double highest; highest = array[0]; for (int count = 1; count < size; count++) { if (array[count] > highest) highest = array[count]; } return highest; } //******************************************************************* // Definition of getLowest * // This function accepts a double array and its size * // as arguments. The lowest value in the array is * // returned as an double * //******************************************************************* double getLowest(double array[], int size) { double lowest; lowest = array[0]; for (int count = 1; count < size; count++) { if (array[count] < lowest) lowest = array[count]; } return lowest; } //******************************************************************* // Definition of function sortArray * // This function performs an descending order buble sort on * // array. elems is the number of elements in the array. * //******************************************************************* void sortArray(int array[], int elems) { bool swap; int temp; do { swap = false; for (int count = 0; count < (elems - 1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); } //******************************************************************** // Definition of function showArray. * // This function displays the contents of array. elems is the * // number of elements. * //******************************************************************** void showArray(int array[], int elems) { for (int count = 0; count < elems; count++) cout << array[count] << " "; cout << endl; }
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.
>>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.
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
ok i changed that to match and now its giving me some more issues heres my code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; // Function prototypes double sumArray(double array[], int size); double getHighest(double array[], int size); double getLowest(double array[], int size); double sortArray(double array[], int size); double showArray(double array[], int size); int main() { const int MONTHS = 12; int values[MONTHS] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; double amount[MONTHS] = {12, 34, 3, 6, 5, 7, 4, 44, 43, 40, 41, 9 }; double total, // To hold the total rainfall average, // To hold the average rainfall highest, // To hold the highest rainfall amount lowest; // To hold the lowest rainfall amount cout << "Enter the rainfall for each month.\n"; for (int count = 0; count < MONTHS; count++) { cout << "Month " << (count + 1) << ": "; cin >> amount[count]; } // Get the total monthly rainfall total = sumArray(amount, MONTHS); // Calculate the average rainfall average = total / MONTHS; // Find the highest monthly rainfall highest = getHighest(amount, MONTHS); // Find the lowest monthly rainfall lowest = getLowest(amount, MONTHS); // Display the results cout << fixed << showpoint << setprecision(2); cout << "The total monthly rainfall is " << total << " inches" << endl; cout << "The average monthly rainfall is " << average << " inches" << endl; cout << "The highest monthly rainfall is " << highest << " inches" << endl; cout << "The lowest monthly rainfall is " << lowest << " inches " << endl; cout << "The monthly rainfall in descending order:\n"; sortArray(amount, MONTHS); showArray(amount, MONTHS); return 0; } //****************************************************************** // Definition of sumArray * // This function accepts a double array and its size * // as arguments. The sum of the array's elements * // is returned as an double. * //****************************************************************** double sumArray(double array[], int size) { double total = 0; for (int count = 0; count < size; count++) total += array[count]; return total; } //****************************************************************** // Definition of getHighest * // This function accepts a double array and its size * // as arguments. The highest value in the array is * // returned as an double. * //****************************************************************** double getHighest(double array[], int size) { double highest; highest = array[0]; for (int count = 1; count < size; count++) { if (array[count] > highest) highest = array[count]; } return highest; } //******************************************************************* // Definition of getLowest * // This function accepts a double array and its size * // as arguments. The lowest value in the array is * // returned as an double * //******************************************************************* double getLowest(double array[], int size) { double lowest; lowest = array[0]; for (int count = 1; count < size; count++) { if (array[count] < lowest) lowest = array[count]; } return lowest; } //******************************************************************* // Definition of function sortArray * // This function performs an descending order buble sort on * // array. elems is the number of elements in the array. * //******************************************************************* double sortArray(double array[], int elems) { bool swap; int temp; do { swap = false; for (int count = 0; count < (elems - 1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); } //******************************************************************** // Definition of function showArray. * // This function displays the contents of array. elems is the * // number of elements. * //******************************************************************** double showArray(double array[], int elems) { for (int count = 0; count < elems; count++) cout << array[count] << " "; cout << endl; }
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
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. 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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; // Function prototypes double sumArray(double array[], int size); double getHighest(double array[], int size); double getLowest(double array[], int size); void sortArray(double (&array)[12], int size); // send array by reference void showArray(double array[], int size); int main() { const int MONTHS = 12; int values[MONTHS] = {12,11,10,9,8,7,6, 5, 4, 3, 2,1 }; double amount[MONTHS] = {12,34,3, 6,5,7,4,44,43,40,41,9 }; double total; // To hold the total rainfall double average; // To hold the average rainfall double highest; // To hold the highest rainfall amount double lowest; // To hold the lowest rainfall amount cout << "Enter the rainfall for each month.\n"; for (int count=1; count<=MONTHS; count++) { cout << "Month " << count << ": "; cin >> amount[count]; } // Get the total monthly rainfall total = sumArray(amount, MONTHS); // Calculate the average rainfall average = total / MONTHS; // Find the highest monthly rainfall highest = getHighest(amount, MONTHS); // Find the lowest monthly rainfall lowest = getLowest(amount, MONTHS); // Display the results cout << fixed << showpoint << setprecision(2); cout << "The total monthly rainfall is " << total << " inches" << endl; cout << "The average monthly rainfall is " << average << " inches" << endl; cout << "The highest monthly rainfall is " << highest << " inches" << endl; cout << "The lowest monthly rainfall is " << lowest << " inches " << endl; cout << "The monthly rainfall in descending order:\n"; sortArray(amount, MONTHS); showArray(amount, MONTHS); return 0; } //****************************************************************** // Definition of sumArray * // This function accepts a double array and its size * // as arguments. The sum of the array's elements * // is returned as an double. * //****************************************************************** double sumArray(double array[], int size) { double total = 0; for (int count = 0; count < size; count++) total += array[count]; return total; } //****************************************************************** // Definition of getHighest * // This function accepts a double array and its size * // as arguments. The highest value in the array is * // returned as an double. * //****************************************************************** double getHighest(double array[], int size) { double highest; highest = array[0]; for (int count = 1; count < size; count++) { if (array[count] > highest) highest = array[count]; } return highest; } //******************************************************************* // Definition of getLowest * // This function accepts a double array and its size * // as arguments. The lowest value in the array is * // returned as an double * //******************************************************************* double getLowest(double array[], int size) { double lowest; lowest = array[0]; for (int count = 1; count < size; count++) { if (array[count] < lowest) lowest = array[count]; } return lowest; } //******************************************************************* // Definition of function sortArray * // This function performs an descending order selection sort on * // array. elems is the number of elements in the array. * //******************************************************************* void sortArray(double (&array)[12], int elems) { int first, temp; for (int i=elems-1; i>0; i--) { first = 0; for (int j=1; j<=i; j++) { if (array[j] < array[first]) first = j; } temp = array[first]; array[first] = array[i]; array[i] = temp; } } //******************************************************************** // Definition of function showArray. * // This function displays the contents of array. elems is the * // number of elements. * //******************************************************************** void showArray(double array[], int elems) { for (int count = 0; count < elems; count++) cout << array[count] << " "; cout << endl; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: RegQueryValueEx and ÌÌÌÌÌÌÌÌÌÌÌÌÌ
- Next Thread: please help me with this error
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






