# Thread: accessing a two dimensional array

1. ## accessing a two dimensional array

Code:
```#include <iostream>
using namespace std;

int main()
{
float array[12];

char  months[12] [10] = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};

int   sum = 0;

int   largest = 0;
int   smallest = INT_MAX;

cout << "Enter the total rainfall for each month" << endl;

for (int i = 0; i < 12; i++)
{
cout << months[i] << " : ";
cin  >> array[i];

while (cin.fail() || array[i] < 0)
{

cin.clear();
cin.ignore(INT_MAX, '\n');

cout << "\nOnly enter positive numbers" << endl;

cout << months[i] << " : ";
cin  >> array[i];
}

if (array[i] > largest)
largest = array[i];

if (array[i] < smallest)
smallest = array[i];

sum += array[i];

}

cout << "\nThe total rainfall for the year is " << sum << endl;
cout << "\nThe average monthly rainfall is " << sum/12 << endl;

cout << "\nThe month with the highest rainfall is "
cout << "\nThe month with the smallest rainfall is "

return 0;
}```
I'm having trouble with the last two cout statements. I don't know what to put at the end of those statements.

2. According to your topic, I assume that your problem is to access the right element rather than find the highest/smallest rainfall.
Well, you simply type months[i] where i is the month (0 - 11) (*surprise surprise*).
Code:
```int max = FindHighestRainfall();
int min = FindSmallestRainfall();

cout << "\nThe month with the highest rainfall is " << months[max] << endl;
cout << "\nThe month with the smallest rainfall is " << months[min] << endl;```

3. Originally posted by Magos

According to your topic, I assume that your problem is to access the right element rather than find the highest/smallest rainfall.
Well, you simply type months[i] where i is the month (0 - 11) (*surprise surprise*).
It's really the same thing: to find the highest/smallest rainfall is to access the right element.

Anyway, I didn't understand your explanation on how to find the right element. You stored functions to find the highest/smallest rainfall in variables.

How would the definition of those functions look? Isn't there an easier way to do this without functions?

4. Well, finding the largest element in an array is piece of cake. First have a variable keeping the currently highest value. Loop through all elements and if a new highest value is found update the variable. To save some speed (who will notice a few ns anyway? ) you can implement this in the same loop when you read the values, but I leave that to you.
Code:
```int FindHighestRainfall()
{
int CurrentMax = Array[0];

for(int i=1; i<12; i++)
{
if(Array[i] > CurrentMax) CurrentMax = Array[i];
}

return CurrentMax;
}```
Almost the same with FindSmallestRainfall(), just one modification. Guess what .

5. I wanted to find the largest and smallest values so I can find out which months contain those values.

I know how to find the largest and smallest values, but those functions are confusing me. Isn't there a way to do this without functions?

I just want those last two cout statemtents to print out the month with the largest value and the month with the smallest value (not the actual values).

6. Look at this:
Code:
```string months[12] = a bunch of crap
float rain[12];

int i;

float highestRain = 0;
int highSpot;
float lowestRain = 0;
int lowSpot;

for(i = 0; i < 12; i++)
{
cout << "Enter rain for month " << months[i];

cin >> rain[i];

if(rain[i] > highestRain)
{
highestRain = rain[i];
highSpot = i;
}

if(rain[i] < lowestRain)
{
lowestRain = rain[i];
lowSpot = i;
}

// other crap```
The rain spot will be the address of the array, so to display the highest:

Code:
```int highSpot;

...

cout << rain[highSpot];```
Hope that helps, show your effort at least!

7. Originally posted by volk
I wanted to find the largest and smallest values so I can find out which months contain those values.
And that is what that code do...

Originally posted by volk
I know how to find the largest and smallest values, but those functions are confusing me. Isn't there a way to do this without functions?
Not using functions you say? Why wouldn't you want to use them? They make the code clearer, better structured, easier to read, easier to maintain, easier to get a nice overlook, prevents you from writing the same code over and over etc... etc... I don't see any reason why you shouldn't use them.
If the reason is that you're not familiar with them, then this is a nice oportunity (sp?) to be.

Confused by that code? It's as simple as it gets.

Originally posted by volk
I just want those last two cout statemtents to print out the month with the largest value and the month with the smallest value (not the actual values).
They do print the months. Haven't you tested the code?

Popular pages Recent additions