# 2d Array Problems

• (2 Pages)
• 1
• 2

## 16 Replies - 10873 Views - Last Post: 01 July 2009 - 11:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=112230&amp;s=624a0d60dbd437a4d73f6a2c1d693664&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

# 2d Array Problems

Posted 28 June 2009 - 01:29 PM

This program uses a 3 X 30 array and for the row is (0 for June, 1 for July and 2 for August) and it is to get data from a file (which is also attached) and then display a weather report showing the total rainy, sunny, and cloudy days for each month and also the total for all 3 months. Also report which month has the largest amount of rain.

I am having problems with the array because it crashes and it shows 0 for all months.

Below is my code

```#include <iostream>		// Setup standard output.
#include <fstream>		// Required for input / outputting files.
#include <iomanip>		// Required for numeric formatting function.
#include <string>		// Required for strings.
using namespace std;

const int MONTH = 3;
const int DAY = 30;

int main()
{
string weather[MONTH][DAY] = {"June", "July", "August"};

int totalRainy,
totalSunny,
totalCloudy;

ifstream datafile;

// Name of program.
cout << " Rain or Shine\n";
cout << "---------------\n\n";

datafile.open("RainOrShine.dat");
if (!datafile)
cout << "Error opening data file.\n";
else
{
cout << "Weather Report\n\n";

for (int months = 0; months <= MONTH; months++)
{	totalRainy = 0,
totalSunny = 0,
totalCloudy = 0;

cout << weather[months][DAY];
datafile >> weather[months][DAY];

if ('R')
totalRainy += weather[months][DAY];
if ('S')
totalSunny += weather[months][DAY];
if ('C')
totalCloudy += weather[months][DAY];

cout << endl;
cout << "	 Total rainy days: " << totalRainy << endl;
cout << "	 Total sunny days: " << totalSunny << endl;
cout << "	 Total cloudy days: " << totalCloudy << endl << endl;

}

datafile.close();

for (int months = 0; months <= MONTH; months++)
{	totalRainy += weather[months][DAY];
totalSunny += weather[months][DAY];
totalCloudy += weather[months][DAY];
}

int highest = 0;
for (int count = 1; count < weather[MONTH][DAY]; count++)
{
if (weather[count] > weather[highest])
highest = count;
}

// Displays the total of all 3 months.
cout << "Total rainy days for all 3 months: " << totalRainy << endl;
cout << "Total sunny days for all 3 months: " << totalSunny << endl;
cout << "Total cloudy days for all 3 months: " << totalCloudy << endl;
cout << "The month with the most rainy days is " << highest << endl;

}

// Stops the program from closing automatically.
system("pause");
return 0;
}

```

This post has been edited by turner2108: 28 June 2009 - 03:23 PM

Is This A Good Question/Topic? 0

## Replies To: 2d Array Problems

### #2 sherinfee

Reputation: 1
• Posts: 13
• Joined: 27-June 09

## Re: 2d Array Problems

Posted 28 June 2009 - 02:34 PM

turner2108, on 28 Jun, 2009 - 12:29 PM, said:

This program uses a 3 X 30 array and for the row is (0 for June, 1 for July and 2 for August) and it is to get data from a file (which is also attached) and then display a weather report showing the total rainy, sunny, and cloudy days for each month and also the total for all 3 months.

I have the code but it crashes and i think i have the right idea but minor problems or things that might be out of place or need extra code.

Below is my code

```#include <iostream>		// Setup standard output.
#include <fstream>		// Required for input / outputting files.
#include <iomanip>		// Required for numeric formatting function.
#include <string>		// Required for strings.
using namespace std;

int main()
{
const int MONTH = 3;
const int DAY = 30;
string weather[MONTH] = {"June", "July", "August"};
int totalRainy,
totalSunny,
totalCloudy;

ifstream datafile;

// Name of program.
cout << " Rain or Shine\n";
cout << "---------------\n\n";

datafile.open("RainOrShine.dat");
if (!datafile)
cout << "Error opening data file.\n";
else
{
cout << "Weather Report\n\n";

for (int months = 0; months <= MONTH; months++)
{	totalRainy = 0,
totalSunny = 0,
totalCloudy = 0;

cout << weather[months];
datafile >> weather[months];

for (int rainy = 0; rainy == 'R'; rainy++)
totalRainy += weather[months][DAY];
for (int sunny = 0; sunny== 'S'; sunny++)
totalSunny += weather[months][DAY];
for (int cloudy = 0; cloudy == 'C'; cloudy++)
totalCloudy += weather[months][DAY];

cout << endl;

cout << "	 Total rainy days: " << totalRainy << endl;
cout << "	 Total sunny days: " << totalSunny << endl;
cout << "	 Total cloudy days: " << totalCloudy << endl << endl;

}

datafile.close();

for (int months = 0; months <= MONTH; months++)
{	totalRainy += weather[months][DAY];
totalSunny += weather[months][DAY];
totalCloudy += weather[months][DAY];
}

// Displays the total of all 3 months.
cout << "Total rainy days for all 3 months: " << totalRainy << endl;
cout << "Total sunny days for all 3 months: " << totalSunny << endl;
cout << "Total cloudy days for all 3 months: " << totalCloudy << endl;

}

// Stops the program from closing automatically.
system("pause");
return 0;
}

```

```string weather[MONTH] = {"June", "July", "August"};
```

"weather is not a 2D array here,is it?
but :
```totalRainy += weather[months][DAY];
```

...
you have used it as a 2D array!

### #3 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 28 June 2009 - 02:43 PM

```string weather[MONTH] = {"June", "July", "August"};
```

"weather is not a 2D array here,is it?
but :
```totalRainy += weather[months][DAY];
```

...
you have used it as a 2D array!
[/quote]

it is supposed to be a 2D array but i do not understand how to make a the row strings and the columns integers in the same array

Reputation:

## Re: 2d Array Problems

Posted 29 June 2009 - 01:04 AM

```	string weather[MONTH] = {"June", "July", "August"};

```

You can use both, but second is better
```
#include <iostream>

#define MONTH  3
#define DAY	31

using namespace std;

int main(void)
{

#if 0
string weather[MONTH][DAY+1] = {
{ "June", "1", "2", "3" },
{ "July" },
{ "August" }
};

for (int i = 0; i < MONTH; i++) {
for (int j = 0; j < DAY+1; j++)
cout << weather[i][j]
<< " ";
cout << endl;
}
#endif

#if 1
struct {
string month;
int day[DAY];
} weather[MONTH] = {
{ "June", 1, 2, 3 },
{ "July", { 1, 2, 3, 4, 5 } },
{ "August", 1 }
};

for (int i = 0; i < MONTH; i++) {
cout << weather[i].month
<< ": ";
for (int j = 0; j < DAY; j++)
cout << weather[i].day[j]
<< " ";
cout << endl;
}
#endif

return 0;
}

```

### #5 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 29 June 2009 - 04:33 PM

erased.

This post has been edited by turner2108: 29 June 2009 - 07:26 PM

### #6 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: 2d Array Problems

Posted 29 June 2009 - 04:51 PM

Can you provide what you have for the text file?

``` if ('R')
totalRainy += weather[months][DAY];
if ('S')
totalSunny += weather[months][DAY];
if ('C')
totalCloudy += weather[months][DAY];

```

Take note, to this. The variable totalRainy is an int, and you are assigning a string value to it because weather is a string array. The array weather[months][DAY] only tells the row and coloumn to look for a string.
Also you say that if('R'). This doesn't make sense, instead you should have something that says if if(variable == 'R').

Does the code you gave compile properly? , because it doesn't for me. It gives 13 errors starting at the above if statement.

This post has been edited by Zerobu: 29 June 2009 - 05:16 PM

### #7 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 29 June 2009 - 05:11 PM

Zerobu, on 29 Jun, 2009 - 03:51 PM, said:

Can you provide what you have for the text file?

That is the information in the file....
I can get the program to output June July and August through a loop just not the information that goes with it

RRCSSSCSCRRRCSSSCSSRSCCRCRRCSS
SSSCCSSSCCSSSCCSSSCRCRCCSSSSSS
SSSSCSSSCSSSCRRCCCSSSSSCSSSSCS

### #8 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: 2d Array Problems

Posted 29 June 2009 - 05:16 PM

Above post edited.

### #9 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 29 June 2009 - 05:27 PM

erased.

This post has been edited by turner2108: 29 June 2009 - 07:26 PM

### #10 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: 2d Array Problems

Posted 29 June 2009 - 06:35 PM

```  if (name[months][day] == 'R')
rainy = 0;
rainy = rainy + rain;
if (name[months][day] == 'S')
sunny = 0;
sunny = sunny + sun;
if (name[months][day] == "C')
cloudy = 0;
cloudy = cloudy + cloud;

```

You are adding an int to a character which would give a weird value. Instead just increment cloudy, sunny or rainy by saying ++. For example cloudy++, rainy++.

Ok, this should resolve your problem. This code here,

```if (name[months][day] == 'R')
rainy++;

else if (name[months][day] == 'S')
sunny++;

else if (name[months][day] == 'C')
cloudy++;

```

Should be in the second for loop. Also you should set cloudy, rainy, and sunny to zero when you declare them This is always a good programming practice, its called intialization. Here it is fixed up.
```#include <iostream>		// Setup standard output.
#include <fstream>		// Required for input / outputting files.
#include <iomanip>		// Required for numeric formatting function.
#include <string>		// Required for strings.
using namespace std;

int main()
{
const int MONTH = 3;
const int DAY = 30;
char name[MONTH][DAY] = {"June", "July", "August"};
char rain = 'R';
char sun = 'S';
char cloud = 'C';
int day = 0;
int months = 0;
int rainy = 0;
int cloudy = 0;
int sunny = 0;

ifstream datafile;

// Name of program.
cout << " Rain or Shine\n";
cout << "---------------\n\n";

datafile.open("file.txt");

if (!datafile)
cout << "Error opening data file.\n";
else
{	cout << "Weather Report\n\n";

for (months = 0; months < MONTH; months++)
{
for (day = 0; day < DAY; day++)
{
cout  << name[months][day];
datafile >> name[months][day];

if (name[months][day] == 'R')
rainy++;

else if (name[months][day] == 'S')
sunny++;

else if (name[months][day] == 'C')
cloudy++;

}
cout << endl;

cout << "	 Total rainy days: " << rainy << endl;
cout << "	 Total sunny days: " << sunny << endl;
cout << "	 Total cloudy days: " << cloudy << endl << endl;
}
datafile.close();

}

}

```

This post has been edited by Zerobu: 29 June 2009 - 06:47 PM

### #11 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: 2d Array Problems

Posted 29 June 2009 - 06:50 PM

Also you might want to add system("pause") and return 0 if your compiler requires it.

### #12 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 29 June 2009 - 06:59 PM

well thanks that worked I know I had the right idea i just wasnt sure how to set up the code since it was bringing in a character instead of an integer!

Zerobu, on 29 Jun, 2009 - 05:50 PM, said:

Also you might want to add system("pause") and return 0 if your compiler requires it.

yea I do that to all my programs I have to because I use Visual Studio 2008

Reputation:

## Re: 2d Array Problems

Posted 29 June 2009 - 07:19 PM

take only this part if you have already learned structs
```	struct {
string month;
int day[DAY];
} weather[MONTH] = {
{ "June", 1, 2, 3 },
{ "July", { 1, 2, 3, 4, 5 } },
{ "August", 1 }
};

for (int i = 0; i < MONTH; i++) {
cout << weather[i].month
<< ": ";
for (int j = 0; j < DAY; j++)
cout << weather[i].day[j]
<< " ";
cout << endl;
}

```

turner2108 said:

it is supposed to be a 2D array but i do not understand how to make a the row strings and the columns integers in the same array

if you want to hold different types in one array it must be array of structures, every element of this array has month name and all of its days

maybe you want to take numbers like a strings, then you can hold it in one type array

This post has been edited by c.user: 29 June 2009 - 07:17 PM

### #14 turner2108

Reputation: 0
• Posts: 25
• Joined: 28-January 09

## Re: 2d Array Problems

Posted 30 June 2009 - 10:29 PM

I have fixed everything with my above code except getting it to display the month with the largest amount of rainy days.... it should be June but my code is not correct

```//***********************************************
// Definition of function 'getHighest'			*
// Calculates and returns the projected			*
// population.																 *
//***********************************************
int getHighest(char array[][DAY])
{
int highest = array[0][0];
for (int months = 0; months < MONTH; months++)
for (int day = 0; day < DAY; day++)

{
{if (array[months][day] > highest)
highest = array[months][day];
}
}
return highest;
}

```

Reputation:

## Re: 2d Array Problems

Posted 30 June 2009 - 11:43 PM

```#include <stdio.h>

#define MONTH 3
#define DAY   5

int getHighest(char array[][DAY]);

main()
{
char words[MONTH][DAY] = {
"RRRDF",
"RARRR",
"CBFAE"
};
int res;

if ((res = getHighest(words)) != EOF)
printf("%d\n", res+1);

return 0;
}

int getHighest(char array[][DAY])
{
int month, day;
int r, maxr, highest;

highest = EOF;
maxr = 0;
for (month = 0; month < MONTH; month++) {
r = 0;
for (day = 0; day < DAY; day++)
if (array[month][day] == 'R')
r++;
if (maxr < r) {
highest = month;
maxr = r;
}
}
return highest;
}

```