0

Hello,

I have this probelm posted below. I pretty much have it figured out and it is still in rough draft form. I know it could be optimized better but, I'm not quite sure how to get it to look better. Although, the main reason I am looking for help is for the input of the current month and the previous rainfall data. I have to have the months go backwards while the program receives the data. I tried changing the for loop and all I got was a screen filled with programing characters. I'm not sure what I have to do to get it operational. The getMonth function starts on line 36 and ends on 55. The Actual Rainfall function starts on line 69 and ends on 80. These two functions are where I need the help on.

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

const int MONTHS = 12;//Constant for number of months.
void getMonth(string months[]);// Asks user for current month.
void getAvg(double avgRain[],string months[], int& i);
void getAct(double actRain[],string months[], int& j);
void printTable(double avgRain[], double actRain[],string months[], double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26);
void totals(double avgRain[], double actRain[],string months[], int& i, int& j, double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26);
const int ZERO = 0;
const int WIDTH1 = 20;
const int WIDTH2 = 22;

int main()
{

     double avgRain[MONTHS];
     double actRain[MONTHS];
     double totalAvg,totalAct,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10,sum11,sum12,sum13,sum14,sum15,sum16,sum17,sum18,sum19,sum20,sum21,sum22,sum23,sum24,sum25,sum26;
     int i,j;
     string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

     getAvg(avgRain,months,i);
     getMonth(months);//Calls getMonth function.
     getAct(actRain,months,j);
     totals(avgRain,actRain,months,i,j,totalAvg,totalAct,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10,sum11,sum12,sum13,sum14,sum15,sum16,sum17,sum18,sum19,sum20,sum21,sum22,sum23,sum24,sum25,sum26);
     printTable(avgRain,actRain,months,totalAvg,totalAct,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10,sum11,sum12,sum13,sum14,sum15,sum16,sum17,sum18,sum19,sum20,sum21,sum22,sum23,sum24,sum25,sum26);
    return 0;
}
 
     
void getMonth(string months[])// Asks user for current month.
{
     int month = 1;

     cout << "\nPlease enter the current month. "
          << "Use 1 for January, 2 for February, "
          << "and so on." << endl;
     cin  >> month;
     
    if (month > ZERO && month <= MONTHS)
	{
		cout << "\nThe current month is " << months[month - 1] << endl;
	}
	else
	{
		cout << "You must enter a number between 1 and 12." << endl;
		getMonth(months);
	}
    return;
}

void getAvg(double avgRain[],string months[], int& i)
{
     int month = 1;

     cout << "\nPlease enter the average rainfall for each month" << endl;
     for (int i = ZERO; i < MONTHS; i++)
     {
	cout << months[i]<<" : ";
	cin  >> avgRain[i];
     }
     return;
}
void getAct(double actRain[],string months[], int& j)
{
     int month = 1;
 
     cout << "\nPlease enter the actual monthly rainfall for the previous 12 months. Begin with the current month: "<< months[month - 1] << endl;
     for (int j = ZERO; j < MONTHS; j++)
     {
         cout << months[j] << " : ";
         cin  >> actRain[j];
     }
     return;
}

void totals(double avgRain[], double actRain[],string months[], int& i, int& j, double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26)
{
     double sum = 0, sum2 = 0;
     

     if ( *avgRain > *actRain )
     {
          sum3 = *avgRain - *actRain;
          sum4 = ZERO;
     }     
     if ( *(avgRain + 1) > *(actRain + 1) )
     {     
          sum5 = *(avgRain + 1) - *(actRain + 1);
          sum6 = ZERO;
     }
     if ( *(avgRain + 2) > *(actRain + 2) )
     {
          sum7 = *(avgRain + 2) - *(actRain + 2);
          sum8 = ZERO;
     }
     if ( *(avgRain + 3) > *(actRain + 3) )
     {
          sum9 = *(avgRain + 3) - *(actRain + 3);
          sum10 = ZERO;
     }
     if ( *(avgRain + 4) > *(actRain + 4) )
     {
          sum11 = *(avgRain + 4) - *(actRain + 4);
          sum12 = ZERO;
     }
     if ( *(avgRain + 5) > *(actRain + 5) )
     {
          sum13 = *(avgRain + 5) - *(actRain + 5);
          sum14 = ZERO;
     }
     if ( *(avgRain + 6) > *(actRain + 6 ))
     {
          sum15 = *(avgRain + 6) - *(actRain + 6);
          sum16 = ZERO;
     }
     if ( *(avgRain + 7) > *(actRain + 7) )
     {
          sum17 = *(avgRain + 7) - *(actRain + 7);
          sum18 = ZERO;
     }
     if ( *(avgRain + 8) > *(actRain + 8) )
     {
          sum19 = *(avgRain + 8) - *(actRain + 8);
          sum20 = ZERO;
     }
     if ( *(avgRain + 9) > *(actRain + 9) )
     {
          sum21 = *(avgRain + 9) - *(actRain + 9);
          sum22 = ZERO;
     }
     if ( *(avgRain + 10) > *(actRain + 10) )
     {     
          sum23 = *(avgRain + 10) - *(actRain + 10);
          sum24 = ZERO;
     }
     if ( *(avgRain + 11) > *(actRain + 11) )
     {         
          sum25 = *(avgRain + 11) - *(actRain + 11);
          sum26 = ZERO;
     }
     if ( *actRain > *avgRain )
     {
          sum4 = *actRain - *avgRain;
          sum3 = ZERO;
     }
     if ( *(actRain + 1) > *(avgRain + 1) )
     {          
          sum6 = *(actRain + 1) - *(avgRain + 1);
          sum5 = ZERO;
     }
     if ( *(actRain + 2) > *(avgRain + 2) )
     {          
          sum8 = *(actRain + 2) - *(avgRain + 2);
          sum7 = ZERO;
      }
     if ( *(actRain + 3) > *(avgRain + 3) )
     {         
          sum10 = *(actRain + 3) - *(avgRain + 3);
          sum9 = ZERO;
     }
     if ( *(actRain + 4) > *(avgRain + 4) )
     {          
          sum12 = *(actRain + 4) - *(avgRain + 4);
          sum11 = ZERO;
     }
     if ( *(actRain + 5) > *(avgRain + 5) )
     {         
          sum14 = *(actRain + 5) - *(avgRain + 5);
          sum13 = ZERO;
     }
     if ( *(actRain + 6) > *(avgRain + 6) )
     {         
          sum16 = *(actRain + 6) - *(avgRain + 6);
          sum15 = ZERO;
     }
     if ( *(actRain + 7) > *(avgRain + 7) )
     {          
          sum18 = *(actRain + 7) - *(avgRain + 7);
          sum17 = ZERO;
     }
     if ( *(actRain + 8) > *(avgRain + 8) )
     {          
          sum20 = *(actRain + 8) - *(avgRain + 8);
          sum19 = ZERO;
     }
     if ( *(actRain + 9) > *(avgRain + 9) )
     {          
          sum22 = *(actRain + 9) - *(avgRain + 9);
          sum21 = ZERO;
     }
     if ( *(actRain + 10) > *(avgRain + 10) )
     {         
          sum24 = *(actRain + 10) - *(avgRain + 10);
          sum23 = ZERO;
     }
     if ( *(actRain + 11) > *(avgRain + 11) )
     {         
          sum26 = *(actRain + 11) - *(avgRain + 11);
          sum25 = ZERO;
     }
     

         
     sum = sum + *avgRain + *(avgRain + 1) + *(avgRain + 2) + *(avgRain + 3) + *(avgRain + 4) + *(avgRain + 5) + *(avgRain + 6) + *(avgRain + 7) + *(avgRain + 8) + *(avgRain + 9) + *(avgRain + 10) + *(avgRain + 11);
     //cout << sum << " is the test" << endl;
     totalAvg = sum;
     //cout << totalAvg << " is the test" << endl;
     sum2 = sum2 + *actRain + *(actRain + 1) + *(actRain + 2) + *(actRain + 3) + *(actRain + 4) + *(actRain + 5) + *(actRain + 6) + *(actRain + 7) + *(actRain + 8) + *(actRain + 9) + *(actRain + 10) + *(actRain + 11);
     //cout << sum2 << " is the test" << endl;
     totalAct = sum2;
     //cout << totalAct << " is the test" << endl;

     return;     
}

void printTable(double avgRain[],double actRain[],string months[], double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26)
{

     double totalBelow, totalAbove;

cout << endl;
cout << endl;
cout << "Month" << setw(23) << "AvgRain" << setw(WIDTH1) << "ActRain" << setw(WIDTH2) << "Total Below Avg" << setw(WIDTH2) << "Total Above Avg" << endl;
cout << endl;
cout << months[0] << setw(18) << *avgRain << setw(WIDTH1) << *actRain << setw(WIDTH2)  << sum3 << setw(WIDTH2)   << sum4 << endl
     << months[1] << setw(17) << *(avgRain + 1) << setw(WIDTH1) << *(actRain + 1) << setw(WIDTH2) << sum5 << setw(WIDTH2) << sum6 << endl
     << months[2] << setw(WIDTH1) << *(avgRain + 2) << setw(WIDTH1) << *(actRain + 2) << setw(WIDTH2) << sum7 << setw(WIDTH2) << sum8 << endl
     << months[3] << setw(WIDTH1) << *(avgRain + 3) << setw(WIDTH1) << *(actRain + 3) << setw(WIDTH2) << sum9 << setw(WIDTH2) << sum10 << endl
     << months[4] << setw(22) << *(avgRain + 4) << setw(WIDTH1) << *(actRain + 4) << setw(WIDTH2) << sum11 << setw(WIDTH2) << sum12 << endl
     << months[5] << setw(21) << *(avgRain + 5) << setw(WIDTH1) << *(actRain + 5) << setw(WIDTH2) << sum13 << setw(WIDTH2) << sum14 << endl
     << months[6] << setw(21) << *(avgRain + 6) << setw(WIDTH1) << *(actRain + 6) << setw(WIDTH2) << sum15 << setw(WIDTH2) << sum16 << endl
     << months[7] << setw(19) << *(avgRain + 7) << setw(WIDTH1) << *(actRain + 7) << setw(WIDTH2) << sum17 << setw(WIDTH2) << sum18 << endl
     << months[8] << setw(16) << *(avgRain + 8) << setw(WIDTH1) << *(actRain + 8) << setw(WIDTH2) << sum19 << setw(WIDTH2) << sum20 << endl
     << months[9] << setw(18) << *(avgRain + 9) << setw(WIDTH1) << *(actRain + 9) << setw(WIDTH2) << sum21 << setw(WIDTH2) << sum22 << endl
     << months[10] << setw(17) << *(avgRain + 10) << setw(WIDTH1) << *(actRain + 10) << setw(WIDTH2) << sum23 << setw(WIDTH2) << sum24 << endl
     << months[11] << setw(17) << *(avgRain + 11) << setw(WIDTH1) << *(actRain + 11) << setw(WIDTH2) << sum25 << setw(WIDTH2) << sum26 << endl;
     
cout << endl;     
cout << setw(27) << "totalAvg" << setw(WIDTH1) << "totalAct" << endl; 
cout << setw(26) << totalAvg << setw(WIDTH1) << totalAct << endl;


return;

}

Edited by jmcorpse: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Fbody
0

Um... I think it will make your life much simpler if you make better use of loops.

A prime example:

void getMonth(string months[])// Asks user for current month.
{
     int month = 1;
 
     cout << "\nPlease enter the current month. "
          << "Use 1 for January, 2 for February, "
          << "and so on." << endl;
     cin  >> month;
 
    if (month > ZERO && month <= MONTHS)
	{
		cout << "\nThe current month is " << months[month - 1] << endl;
	}
	else
	{
		cout << "You must enter a number between 1 and 12." << endl;
		getMonth(months);
	}
    return;
}

Why would you do this as a recursive function instead of just putting it in a loop? That's crazy!

Also:

void printTable(double avgRain[], double actRain[],string months[], double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26);

:-O Why so many parameters? I sure don't want to keep track of 30+ arguments when I'm calling a function, too many chances for a problem. Can't you cut this down by using another array or putting the function call in a loop?

To answer your question though, to make a loop run "backward" all you have to do is start the index at a "high" value and decrement it instead of starting "low" and incrementing it.

//"forward" running loop:
for (int i = 0; i < limit; ++i) {
  cout << array[i] << " ";
}

//"backward" running loop, Version 1:
for (int j = (limit-1); j >= 0; --j) {
  cout << array[j] << " ";
}

//"backward" running loop, Version 2:
for (int j = limit; j > 0; --j) {
  cout << array[j-1] << " ";
}

The net result of "backward" versions 1 and 2 is the same. The only difference is where the math is applied to the index and how the end condition is written because of the different math.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.