I was spending a lot of time looking around this site to practice with beginner programs. I found one posted by sniper1. And, I tried to make it work. But, now am stuck! Can someone please tell me what is wrong with my code.
Problem:
1. [SOLO] 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 averageHigh: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.(These functions must all have the appropriate parameters.)
I also wanted the function getData to get the data from a text file which I made. Can't seem to get that to work either.
I don't even know if I am on the right track, so any advice would be beneficial. And I have been trying to post my code the correct way [ / code] but, can't even seem to get that to work. So please bear with me.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
double getData(ifstream inputF, double high, double low);
double averageHigh();
double averageLow();
double indexHighTemp();
double indexLowTemp();
int i, j;
double hsum = 0, lsum = 0;
double avgh, avgl;
double high = 0, low = 0;
int ind;
int const row = 2, col = 12, num = 12;
double temp[row][col];
int main()
{
ifstream inp;
ifstream inputF;
if (!inp)
{
cout << "Unable to open input file!"<<endl;
return 1;
}
inputF.open("c:\Ch9_Ex9Data");
getData(inp);
averageHigh();
averageLow();
indexHighTemp();
indexLowTemp();
system("PAUSE");
return 0;
}
double getData(ifstream inputF, double high, double low)
{
inputF >> high >> low;
}
double averageHigh()
{
for (i = 0; i < col; i++)
{
hsum = (hsum + (temp[0][i]));
}
avgh = hsum / 12;
cout << "Average High Temperature: "<< endl;
cout << avgh;
return 0;
}
double averageLow()
{
for (i = 0; i < col; i++)
{
lsum = (lsum + (temp[1][i]));
}
avgl = lsum / 12;
cout << "Average Low Temperature: "<< endl;
cout << avgl;
return 0;
}
double indexHighTemp()
{
ind = 0;
for (i = 0; i < col; i++)
{
if (high <= (temp[0][i]))
{
high = (temp[0][i]);
ind = i;
}
}
cout << "Highest Temperature: "<< endl;
cout << (temp[0][ind]);
return 0;
}
double indexLowTemp()
{
ind = 0;
for (i = 0; i < col; i++)
{
if (low >= (temp[1][i]))
{
low = (temp[1][i]);
ind = i;
}
}
cout << "Lowest Temperature: "<< endl;
cout << (temp[1][ind]);
return 0;
}