| | |
Rain or Shine
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
An amateur meteorologist wants to keep track of weather conditions during the past year's three month summer
season and has designated each day as either rainy(R), cloudy (C) or sunny (S). Write a program that stores this information in a 3x30 array of characters, where the row indicates the month (0=june,1=july,2=August) and the column indicates the day of the month. Note that data is not being collected for the 31st of any month. The program should begin by reading the weather data in from a file. Then it should create a report that displays for each month and for the whole 3 month period, how many days were rainy, how many were cloudy, and how many were sunny. It should also report which of the three months had the largest number of rainy days. Data for the program can be found in rainorshine.dat file.
The data file says:
RRCSSSCSCRRRCSSSCSSRSCCRCRRCSS
SSSCCSSSCCSSSCCSSSCRCRCCSSSSSS
SSSSCSSSCSSSCRRCCCSSSSSCSSSSCS
season and has designated each day as either rainy(R), cloudy (C) or sunny (S). Write a program that stores this information in a 3x30 array of characters, where the row indicates the month (0=june,1=july,2=August) and the column indicates the day of the month. Note that data is not being collected for the 31st of any month. The program should begin by reading the weather data in from a file. Then it should create a report that displays for each month and for the whole 3 month period, how many days were rainy, how many were cloudy, and how many were sunny. It should also report which of the three months had the largest number of rainy days. Data for the program can be found in rainorshine.dat file.
The data file says:
RRCSSSCSCRRRCSSSCSSRSCCRCRRCSS
SSSCCSSSCCSSSCCSSSCRCRCCSSSSSS
SSSSCSSSCSSSCRRCCCSSSSSCSSSSCS
Which chapter would that be? Of course there is nothing wrong with you reading the chapter yourself.
The instructions seem pretty clear to me
If you don't know how to use ifstream then you can search for example code -- there is lots of it all over the net and even here at DaniWeb. Learn to make google one of your best programming buddies.
The instructions seem pretty clear to me
- Create the starter program with main() function
- Add a character array that has 3 rows and 30 columns
- Create an iostream object, open the data file and read the rows into the array.
- Loop through the character array and, for each month, count the number of R's, S's and C's.
- Display the report on the screen using std::cout.
If you don't know how to use ifstream then you can search for example code -- there is lots of it all over the net and even here at DaniWeb. Learn to make google one of your best programming buddies.
Last edited by Ancient Dragon; Apr 20th, 2009 at 11:41 pm.
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: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { const int NUM_MONTHS = 3; const int NUM_DAYS = 30; const int NUM_TYPES= 3; char forecast[NUM_MONTHS][NUM_DAYS]; int counts [NUM_MONTHS][NUM_TYPES]; int rainyDays = 0; ifstream datafile; datafile.open("RainOrShine.txt"); if (!datafile) cout << "Error opening data file.\n"; else { // Read data and load arrary for(int month = 0; month < NUM_MONTHS; month++) { for(int day = 0; day < NUM_DAYS; day++) { datafile >> forecast[month][day]; } datafile.ignore(); } for(int month = 0; month < NUM_MONTHS; month++) { for(int day = 0; day < NUM_DAYS; day++) { cout << forecast[month][day] << '\t'; } cout << endl; } // intialize counts arrarys to 0 for(int month = 0; month < NUM_MONTHS; month++) { for(int type = 0; type < NUM_TYPES; type++) { counts[month][type] =0; } } // count all data for(int month = 0; month < NUM_MONTHS; month++) { for(int day = 0; day < NUM_DAYS; day++) { if(forecast [month][day] =='R') { counts[month][0]++; } if(forecast [month][day] =='S') { counts[month][1]++; } if(forecast [month][day] =='C') { counts[month][2]++; } } } for(int month = 0; month < NUM_MONTHS; month++) { for(int type = 0; type < NUM_TYPES; type++) { cout << counts[month][type] << '\t'; } cout << endl; } // Determine month with largest number of rainy days // output report cout << "Three-Month Analysis\n\n"; } system("pause"); return 0; }
Last edited by Narue; Apr 29th, 2009 at 4:03 pm. Reason: added code tags
>>How do i find the month with the largest amount of rain?
You already have the array that contains the count of the number of Rs, Ss and Cs. Now all you have to do is iterate through that array and keep track of which month has the greatest number of Rs. How would you do that? Try it with pencil & paper first
You already have the array that contains the count of the number of Rs, Ss and Cs. Now all you have to do is iterate through that array and keep track of which month has the greatest number of Rs. How would you do that? Try it with pencil & paper first
Last edited by Ancient Dragon; Apr 29th, 2009 at 6:06 pm.
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.
![]() |
Similar Threads
- This or that? (Posting Games)
Other Threads in the C++ Forum
- Previous Thread: Binary Files and Seekg
- Next Thread: New Delete Runtime error.
Views: 460 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






