GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2007, 13:57
herceca herceca is offline
New Member
 
Join Date: Dec 2007
Posts: 6
herceca is on a distinguished road

Help with program


I am supposed to design a program that calculates average of rainfall over a period of years, using nested loops.

should look like this:
Enter number of years: 3(random number i picked)

Enter amount of rainfall for year 1, month 1: 1(random number again)
Enter amount of rainfall for year 1, month 2: 1
Enter amount of rainfall for year 1, month 3:1
Enter amount of rainfall for year 1, month 4:1
Enter amount of rainfall for year 1, month 5:1
Enter amount of rainfall for year 1, month 6:1
Enter amount of rainfall for year 1, month 7: 1
Enter amount of rainfall for year 1, month 8: 1
Enter amount of rainfall for year 1, month 9:1
Enter amount of rainfall for year 1, month 10:1
Enter amount of rainfall for year 1, month 11:1
Enter amount of rainfall for year 1, month 12:1

Enter amount of rainfall for year 2, month 1: 1(random number again)
Enter amount of rainfall for year 2, month 2: 1
Enter amount of rainfall for year 2, month 3:1
Enter amount of rainfall for year 2, month 4:1
Enter amount of rainfall for year 2, month 5:1
Enter amount of rainfall for year 2, month 6:1
Enter amount of rainfall for year 2, month 7: 1
Enter amount of rainfall for year 2, month 8: 1
Enter amount of rainfall for year 2, month 9:1
Enter amount of rainfall for year 2, month 10:1
Enter amount of rainfall for year 2, month 11:1
Enter amount of rainfall for year 2, month 12:1

Enter amount of rainfall for year 3, month 1: 1(random number again)
Enter amount of rainfall for year 3, month 2: 1
Enter amount of rainfall for year 3, month 3:1
Enter amount of rainfall for year 3, month 4:1
Enter amount of rainfall for year 3, month 5:1
Enter amount of rainfall for year 3, month 6:1
Enter amount of rainfall for year 3, month 7: 1
Enter amount of rainfall for year 3, month 8: 1
Enter amount of rainfall for year 3, month 9:1
Enter amount of rainfall for year 3, month 10:1
Enter amount of rainfall for year 3, month 11:1
Enter amount of rainfall for year 3, month 12:1

Average rain per year:1
Average total: 12


CPP / C++ / C Code:
# include <iostream.h>
# include <iomanip.h>
#include <ctype.h> // needed for toupper
#include <stdlib.h>// needed for rand

using namespace std;

int main ( )
{
int years=0;
int years2;
int months=0;
int inches=0;
int monthcounter=0;
int months2=0;

cout << "Number of years: ";
cin >> years;
cout << endl;
	while (years<0)
		{
		cout <<"Wrong number, try again: ";
		cin >> years;
		}
	while (monthcounter<years)
		{
		cout << "Enter inches of rainfall for month "<<monthcounter+1<< ": ";
		cin >> inches;
		}
	for (years2=1;years2<=years;years2++) 
	months2=years*12;
	for (months2=1;months2<=months;months2++)
	{
	}
return 0;
}

What I have so far and I am currently stuck.

Thanks in advance.
  #2  
Old 10-Dec-2007, 08:19
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help with program


It looks like you will probably want to save all of the data that you read in to an array of some kind. This way, you can separate your program into sections:

1) Input the rainfall data into the array
2) Make any number of calculations from the data

Lets just say that you're going to use a single array for all of this. Then, your code should look more like:

CPP / C++ / C Code:
# include <iostream>
using namespace std;

int main ( )
{ double* months = NULL;
  int years,year,month;

  cout << "Number of years: ";
  cin >> years;
  cout << endl;
  while (years<0)
  { cout <<"Wrong number, try again: ";
    cin >> years;
  }
  
  // Allocate memory for all of the months
  months = new double[12*years];

  for(year=0;year<years;year++)
  { for(month=0;month>12;month++)
    { cout << "Enter inches of rainfall for year " << year+1 << ", month " << month+1<< ": ";
      cin >> months[year*12+month];
    }//end for(month)
  }//end for(year)

  // This is an example of counting all of the rainfall
  int inches = 0;
  for(year=0;year<years;year++)
  { for(month=0;month>12;month++)
    { inches += months[year*12+month];
    }//end for(month)
  }//end for(year)
  cout << "There were a total of " << inches << " inches of rain in that time span.";
  
  // Delete our allocated memory
  delete[] months;
  
  return 0;
}
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Text-Based Roulette Game mfm1983 CPP / C++ Forum 5 29-Nov-2006 12:20
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Pipeline freeze simulation darklightred CPP / C++ Forum 6 27-Jul-2006 19:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.