Two Dimensional Array with function

Write a program that uses two dimensional array to store the highest and lowest temperatures of each month of the year. The program should output the average high, average low, and the highest and lowest temperatures of the year. Your program must consist of the following functions:

a. Function getData: This function reads and store data in the two dimensional array.
b. Function averageLow: This function calculates and returns the average low temperature of the year.
c.Function averageHigh: This function calculates and returns the average high temperature of the year.
d. Function indexHighTemp: This function returns the index of the highest high temperature in the array.
e.d. Function indexHighTemp: This function returns the index of the lowest low temperature in the array.

(This function must have all the appropriate parameters)

Here is my code guys. I don't know how to convert this to function. I hope someone can help with this. I badly need it. Thank you.

#include<iostream>

using namespace std;

main()
{
int highest, lowest;
int temp[12][2] =
{
{33,15},
{32,14},
{37,17},
{37,16},
{38,18},
{36,17},
{39,19},
{42,21},
{44,22},
{39,19},
{37,14},
{35,12}
};

puts("Temperatures for each month of the year");
cout << endl;
cout << "Highest: " << "Lowest: " << endl;

for(int a = 0; a < 12; a++)
{
for(int b = 0; b < 2; b++)
{
cout << " ";
cout << temp[a][b];
cout << "\t";

}
if(a == 0)
{cout << " - January";}
else if(a == 1)
{cout << " - February";}
else if(a == 2)
{cout << " - March";}
else if(a == 3)
{cout << " - April";}
else if(a == 4)
{cout << " - May";}
else if(a == 5)
{cout << " - June";}
else if(a == 6)
{cout << " - July";}
else if(a == 7)
{cout << " - August";}
else if(a == 8)
{cout << " - September";}
else if(a == 9)
{cout << " - October";}
else if(a == 10)
{cout << " - November";}
else if(a == 11)
{cout << " - December";}
cout << endl;
}

for(int c = 0; c < 12; c++)
{
if(temp[c][0] > temp[c-1][0])
{
highest = temp[c][0];
}
if(temp[c][1] < temp[c-1][0])
{
lowest = temp[c][1];
}
}

cout << endl;
cout << "The highest temperature of the year is: " << highest << endl;
cout << "The lowest temperature of the year is: " << lowest << endl;


return 0;
}
Last edited on
Line 3: main() must be type int.

Line 24: Why are you using puts() if you're writing C++? That is a C function.

Line 66,70: temp[c-1][0] and temp[c-1][1] are going to cause an out of bounds reference when c = 0.

Line 70: Your second subscript on the right hand side of the comparison is wrong. Should be: if(temp[c][1] < temp[c-1][1])

As for converting this to use functions, do you know how to write functions?
Start by writing a function declaration for each of the required functions. Think about the arguments you will need to pass to the function and what you need to return from each function.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Topic archived. No new replies allowed.