# Thread: How to access different things in two dimensional arrays

1. ## How to access different things in two dimensional arrays

So I started taking a C++ class two weeks ago and we just got to arrays. I am confused on how to pull data from two dimensional arrays. The assignment for this topic reads as follows.
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a. Function getData: This function reads and stores data in the two dimensional array.
b. Function averageHigh: This function calculates and returns the average high temperature for the year.
c. Function averageLow: This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp: this function returns the index of the highest high temperature in the array.
e. Function indexLowTemp: this function returns the index of the lowest low temperature in the array.

I made the getData function. I do know that i am supposed to more then likely use nested loops on each function to iterate through each column. I have been trying to figure this out. I first saw in my book that the array could be variables so naturally I tryed to make the array loop and increment up the column and row each time a item is stored to make it easier to loop in different functions but then i found out that they have to have a constant value and that doesn't work. Here is the code for index high temp that i have been working on. I honestly feel like i'm over thinking this and its easier then what i'm making it out to be. Here is all of the code. I don't necessarily want you guys to give me the answer. Just point me in the right direction so i'm actually learning and not copying.

Code:
```#include <iostream>
#include <string>

using namespace std;

void getData(int month);
void averageHigh(int month);
void averageLow(int month);
void indexHighTemp(int month,int averagehigh);
void indexLowtemp(int month);
void monthData(int month);

int main()
{

int month;
getData(month);
averageHigh(month);
averageLow(month);
indexHighTemp(month);
indexLowtemp(month);

}
void getData(int month)
{

int month;

for(int i; i < 13 && i!=0; i++)
{
cout << "Enter the high temperature for " << i << endl;
cin >> month;
cout << "Enter the low temparature for " << i << endl;
cin >> month;

switch (i)
{
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
case 3:
cout << "March" << endl;
break;
case 4:
cout << "April" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "June" << endl;
break;
case 7:
cout << "July" << endl;
break;
case 8:
cout << "August" << endl;
break;
case 9:
cout << "September" << endl;
break;
case 10:
cout << "October" << endl;
break;
case 11:
cout << "November" << endl;
break;
case 12:
cout << "December" << endl;
break;

}

}

}
void indexHighTemp(int month)
{
int smallestValue = 100;
double month_length = month.length;
for(int i = 0; i < 24; i++)
{
if(month

}

void averageHigh()

{
averagehigh =
}``` 2. Okay here is a update of the GetDate
Code:
```
for(row = 0; row < 12; row++)
for(col = 0; col < 2; col++)
int month[row][col] = 0;
for(int i; i < 13 && i!=0; i++)
switch statement```
I got this from the book and it is telling me row and col have to have a constant value. But i don't see how i can finish this program without making row and col a variable. Otherwise im going to be typing out 500 statements since i can't loop. Please help! 3. You should look at the difference between
Code:
`int month[row][col] = 0;`
(which makes no sense on two different levels: one because you can't declare an array with variable amounts of indices, and second because you can't assign the value 0 to an array)
and
Code:
`month[row][col] = 0;`
which only deals with one single item in your array, specifically the one at (row, col).

Also the fact that you have written this:
Code:
`for(int i; i < 13 && i!=0; i++)`
suggests you didn't really get past for-loops yet either. (What values do you think i will take on during this loop? How will it get them?) 4. Originally Posted by tabstop You should look at the difference between
Code:
`int month[row][col] = 0;`
(which makes no sense on two different levels: one because you can't declare an array with variable amounts of indices, and second because you can't assign the value 0 to an array)
and
Code:
`month[row][col] = 0;`
which only deals with one single item in your array, specifically the one at (row, col).

Also the fact that you have written this:
Code:
`for(int i; i < 13 && i!=0; i++)`
suggests you didn't really get past for-loops yet either. (What values do you think i will take on during this loop? How will it get them?)
Thanks for the help! honestly i just scrapped the whole program and studied more about arrays and for loops. After i redid the program I got it functioning. after reading some on arrays i saw everything i did wrong. Here is the new complete code.
Code:
```#include <iostream>
using namespace std;

const int months = 12;

void getData( double A[], int B);
double averageHigh(double A[], int B);
double averageLow(double A[], int B);
int indexHighTemp(double A[], int B);
int indexLowTemp( double A[], int B);

int main()
{
double temperatures [months];

getData(temperatures, months);

cout << "\n\n\tThe average high temperature for the year is "
<< averageHigh(temperatures, months);
cout << "\n\n\tThe average low temperature for the year is "
<< averageLow(temperatures, months);

cout << "\n\n\tIndex of the highest temperature for the year is "
<< indexHighTemp(temperatures, months);
cout << "\n\n\tIndex of the lowest temperature for the year is "
<< indexLowTemp(temperatures, months);
return 0;
}

void getData(double A[], int B)
{
for (int i = 0; i < B; i++)
{
cout << "\n\tEnter the highest temperature for the month"
<< (i+1) << ": " ;
cin >> A[i];
cout << "\n\tEnter the lowest temperature for the month"
<< (i+1) << ": " ;
cin >> A[i];

}
}

double averageHigh(double A[], int B)
{
double sum = 0;

for (int i = 0; i <B; i++)
sum +=A[i];
return (sum/B);
}

double averageLow(double A[], int B)
{
double sum = 0;

for( int i = 0; i < B; i++)
sum +=A[i];
return (sum/B);
}

int indexHighTemp(double A[], int B)
{
int index = 0;
double highest = A;

for(int i =1; i <B; i++)
if (A[i] > highest)
{
highest = A[i];
index =i;
}
return index;
}

int indexLowTemp(double A[], int B)
{
int index = 0;
double lowest = A;

for (int i = 1; i < B; i++)
if (A[i] < lowest)
{
lowest = A[i];
index = i;
}

return index;
}``` 5. But i need to study more. I hastily read a chapter and me looking dumb ensued. Popular pages Recent additions 