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.

#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.

ok i changed that to match and now its giving me some more issues heres my code

#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;
}
Member Avatar for iamthwee

That's nice, have you heard of debugging and finding out what might be wrong yourself?

>>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?

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

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

#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;
}

thanks that was mighty helpfull.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.