I was assigned to create a program that lets the user enter the total rainfall for each of the 12 months, into an array of doubles. Then it needed to calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Then sort and display the months based on their descending rainfall amount.

Here's what I'm really having trouble with. How do I tell what element position the data came out of from my double array? I need to know that, at least I think I do to tell my char array which element to display for the name of the month with the right rainfall amount.

Can anyone help me please? I'm new to this and am really stuck. Any info would be greatly appreciated!!

// This program allows the user to enter the amount of rainfall for each month.
// It can then calculate the total rainfall for the year as well as the average monthly rainfall,
// and the months with the highest and lowest amounts of rain.
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(double [], int);
void showArray(double [], int);

int main()
{
  double total;
  double average;
  double highest;
  double lowest;
 const int MONTHS = 12;
 const int cols = 2;
 const int string_SIZE = 10;
 double values[MONTHS]; // An Array for the values
  
// The rain Array contains the months for the data to be entered
 char rain[MONTHS][string_SIZE] =
 { "January", "February", "March", "April",
   "May", "June", "July", "August", "September",
   "October", "November", "December" };
 
   // dynamically ask the user for input.
   for (int count = 0; count < MONTHS; count++)
   {
      cout << "Enter the amount of rainfall for "<<rain[count]<< " in inches."<<endl; 
  cin>>values[count];
   }
   
   cout<<"\n";
   cout<<"\n";
   // Get the total rainfall.
   total = sumArray(values, MONTHS);
   // Calculate the average.
   average = total / MONTHS;
   // Find the highest sales amount.
   highest = getHighest(values, MONTHS);
   // Find the lowest sales amount.
   lowest = getLowest(values, MONTHS);
      // Display the total
   cout<<"The total amount of rainfall for the year is: "<<setprecision(3)<<total<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the average
   cout<<"The average amount of rainfall for the year is: "<<setprecision(3)<<average<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the highest
 cout<<"The highest amount of rain was in "<<" with: "<<highest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the lowest
   cout<<"The lowest amount of rain is: "<<lowest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
      // Display the values.
   cout << "The unsorted values are:\n";
   showArray(values, MONTHS);
   // Sort the values.
   sortArray(values, MONTHS);
   // Display them again.
   cout << "The sorted values are:\n";
   showArray(values, MONTHS);
 
   return 0;
}

double sumArray(double array[], int size)
{
   double total = 0; // Accumulator
   for (int count = 0; count < size; count++)
      total += array[count];
   return total;
}
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;
}
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 ascending order bubble sort on *
// array. size is the number of elements in the array.     *
//***********************************************************
void sortArray(double array[], int size)
{
   bool swap;
   int temp;
   do
   {
      swap = false;
      for (int count = 0; count < (size - 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. size is the *
// number of elements.                                        *
//*************************************************************
void showArray(double array[], int size)
{
   for (int count = 0; count < size; count++)
      cout << array[count] << " ";
   cout << endl;
}

I didn't read your code, but what I would do is put the rainfall amount and month in a structure to keep them together. Then sort the structures by rainfall amounts. That makes it pretty easy to then just print the sorted array of structures and associated months.

struct rainfall
{
    double amount;
    int month;
};

Here's what I'm really having trouble with. How do I tell what element position the data came out of from my double array? I need to know that, at least I think I do to tell my char array which element to display for the name of the month with the right rainfall amount.

Simply return the index rather than the array contents:

double getLowest(double array[], int size)
{
   double lowest;
   lowest = 0;
   for (int count = 1; count < size; count++)
   {
      if (array[count] < array[lowest])
         lowest = count;
   }
   return lowest;
}

Ok Ancient Dragon, here's what I got from your help so far but I'm still confused. Here's my code.

// This program allows the user to enter the amount of rainfall for each month.
// It can then calculate the total rainfall for the year as well as the average monthly rainfall,
// and the months with the highest and lowest amounts of rain.
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(double [], int);
void showArray(double [], int);
int getindex(int array[], int);
const int MONTHS = 12;
const int string_SIZE = 10;
 
struct rainfall // structure for the rainfall amounts, months, and position of the elements.
{
    double values[MONTHS];
 char rain[MONTHS][string_SIZE];
 int position;
 
};
int main()
{
    char inrain[MONTHS][string_SIZE] =  { "January", "February", "March", "April",
   "May", "June", "July", "August", "September",
   "October", "November", "December" };
 
 rainfall yearly;
  double total;
  double average;
  double highest;
  double lowest;
 
 int i,j;
 for (i = 0; i < MONTHS; i++)
  for (j = 0; j < string_SIZE; j ++)
   yearly.rain[i][j] = inrain[i][j];
 
 
 
   // dynamically ask the user for input.
   for (int count = 0; count < MONTHS; count++)
   {
      cout << "Enter the amount of rainfall for "<<yearly.rain[count]<< " in inches."<<endl; 
   cin>>yearly.values[count];
   }
 
   cout<<"\n";
   cout<<"\n";
   // Get the total rainfall.
   total = sumArray(yearly.values, MONTHS);
   // Calculate the average.
   average = total / MONTHS;
   // Find the highest rainfall amount.
   highest = getHighest(yearly.values, MONTHS);
   // Find the lowest rainfall amount.
   lowest = getLowest(yearly.values, MONTHS);
   // Find the position of the lowest amount
 
      // Display the total
   cout<<"The total amount of rainfall for the year is: "<<setprecision(3)<<total<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the average
   cout<<"The average amount of rainfall for the year is: "<<setprecision(3)<<average<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the highest
   cout<<"The highest amount of rain was in "<<" with: "<<highest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the lowest
   cout<<"The lowest amount of rain was in "<<" with: "<<lowest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
      // Display the values.
   cout << "The unsorted values are:\n";
   showArray(yearly.values, MONTHS);
   // Sort the values.
   sortArray(yearly.values, MONTHS);
   // Display them again.
   cout << "The sorted values are:\n";
   showArray(yearly.values, MONTHS);
 
   return 0;
}
 
double sumArray(double array[], int size)
{
   double total = 0; // Accumulator
   for (int count = 0; count < size; count++)
      total += array[count];
   return total;
 
}
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;
 
}
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 ascending order bubble sort on *
// array. size is the number of elements in the array.     *
//***********************************************************
void sortArray(double array[], int size)
{
   bool swap;
   int temp;
   int count;
   do
   {
      swap = false;
      for (count = 0; count < (size - 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. size is the *
// number of elements.                                        *
//*************************************************************
void showArray(double array[], int size)
{
   for (int count = 0; count < size; count++)
      cout << array[count] << " ";
   cout << endl;
}

I took your advise and made a structure but I am still not sure on how to tell what element the values came from.

I want it to work like this:

Say it ask you how much did it rain in January and you put 1 inch (that being the highest) It loops through all the months and ask you how much it rained in say August and you put in 0.1 inches(that being the lowest.)

So what I want it to be able to output is this:

The month that it rained the most was January with 1 inch.

The month that it rained the least was August with 0.1 inches.

Then it should sort the months in the order of their rainfall in decsending order.

This would start with January and end with August.

I have the sorting code at the bottom of my cpp file.
I'm thinking I could maybe make the array 2 dimensional for the values and put in the actual rainfall in the first column and then the element number in the 2nd column. Would that work??

Anyways thanks for your help!!! I hope you aren't totally confused by me. I know I am...:confused:

The structure should only hold the data for one month. Then create an array of these structures.

struct rainfall 
{
   double amount;
   int month; // 1 = Jan, 2 = Feb ... 12 = Dec
 } values[MONTHS];

Now after populating the array all you have to do is sort the array in descending order by amount.

void sortArray(struct rainfall array[MONTHS])
{
   bool swap;
   struct rainfall temp;
   int count;
   do
   {
      swap = false;
      for (count = 0; count < (MONTHS-1); count++)
      {
         if (array[count].amount < array[count + 1].amount)
         {
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
            swap = true;
         }
      } 
   } while (swap);
 
}

Well, I tried that idea but I am really not understanding this very well. Here's the error I'm getting:

rain_fall.obj : error LNK2001: unresolved external symbol "void __cdecl sortArray(double * const,int)" ([EMAIL="?sortArray@@YAXQANH@Z"]?sortArray@@YAXQANH@Z[/EMAIL])
Debug/rain_fall.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
rain_fall.exe - 2 error(s), 0 warning(s)

Here's the latest code:

// This program allows the user to enter the amount of rainfall for each month.
// It can then calculate the total rainfall for the year as well as the average monthly rainfall,
// and the months with the highest and lowest amounts of rain.
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(double [], int);
void showArray(double [], int);
int getindex(int array[], int);
const int MONTHS = 12;
const int string_SIZE = 10;

struct rainfall // structure for the rainfall amounts, months, and position of the elements.
{
 double amount;
    double values[MONTHS];
 char rain[MONTHS][string_SIZE];
 
};
int main()
{
    char inrain[MONTHS][string_SIZE] =  { "January", "February", "March", "April",
   "May", "June", "July", "August", "September",
   "October", "November", "December" };
 
 rainfall yearly;
  double total;
  double average;
  double highest;
  double lowest;
  
 int i,j;
 for (i = 0; i < MONTHS; i++)
  for (j = 0; j < string_SIZE; j ++)
   yearly.rain[i][j] = inrain[i][j];
 
 

   // dynamically ask the user for input.
   for (int count = 0; count < MONTHS; count++)
   {
      cout << "Enter the amount of rainfall for "<<yearly.rain[count]<< " in inches."<<endl; 
   cin>>yearly.values[count];
   }
   
   cout<<"\n";
   cout<<"\n";
   // Get the total rainfall.
   total = sumArray(yearly.values, MONTHS);
   // Calculate the average.
   average = total / MONTHS;
   // Find the highest rainfall amount.
   highest = getHighest(yearly.values, MONTHS);
   // Find the lowest rainfall amount.
   lowest = getLowest(yearly.values, MONTHS);
   // Find the position of the lowest amount
 
      // Display the total
   cout<<"The total amount of rainfall for the year is: "<<setprecision(3)<<total<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the average
   cout<<"The average amount of rainfall for the year is: "<<setprecision(3)<<average<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the highest
   cout<<"The highest amount of rain was in "<<" with: "<<highest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
   // Display the lowest
   cout<<"The lowest amount of rain was in "<<" with: "<<lowest<<" inches."<<endl;
   cout<<"\n";
   cout<<"\n";
      // Display the values.
   cout << "The unsorted values are:\n";
   showArray(yearly.values, MONTHS);
   // Sort the values.
   sortArray(yearly.values, MONTHS);
   // Display them again.
   cout << "The sorted values are:\n";
   showArray(yearly.values, MONTHS);
 
   return 0;
}

double sumArray(double array[], int size)
{
   double total = 0; // Accumulator
   for (int count = 0; count < size; count++)
      total += array[count];
   return total;
   
}
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;
   
}
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 ascending order bubble sort on *
// array. size is the number of elements in the array.     *
//***********************************************************
void sortArray(struct rainfall array[MONTHS], int size)
{
   bool swap;
   struct rainfall temp;
   do
   {
      swap = false;
      for (int count = 0; count < size; count++)
      {
         if (array[count].amount < array[count + 1].amount)
         {
            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. size is the *
// number of elements.                                        *
//*************************************************************
void showArray(double array[], int size)
{
   for (int count = 0; count < size; count++)
      cout << array[count] << " ";
   cout << endl;
}

Look at the prototype of the function and compare it to the definition of that function. Look for anything that doesn't match.

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.