Hello, I am trying to write a program that reads in a file, and uses a 2d array to store the highest and lowest temp from each month. It also outputs the average high and the average low. The file that i am reading in is called homework2.txt and looks like this:
-4 25
6 33
21 50
33 67
50 72
65 80
72 95
65 101
58 82
35 62
34 59
-6 31
the first column being the lows and the second being the highs.
I also need 4 functions: getData(reads in the file and assigns to 2d array), averageHigh(sel f explanatory), averageLow(self explanatory), indexHighTemp(h ighest temp), and indexLowTemp(lo west temp).
I wrote a lot of the program already minus the indexHighTemp and indexLowTemp because i can't seem to figure out where to even start to find them.
Some problems i am having is that the answers for my averageHigh and averageLow are both 0. I dont know if its because i assigned the file to the 2d array wrong or if i just did the two functions wrong.
any help is greatly appreciated. Thank you.
-4 25
6 33
21 50
33 67
50 72
65 80
72 95
65 101
58 82
35 62
34 59
-6 31
the first column being the lows and the second being the highs.
I also need 4 functions: getData(reads in the file and assigns to 2d array), averageHigh(sel f explanatory), averageLow(self explanatory), indexHighTemp(h ighest temp), and indexLowTemp(lo west temp).
I wrote a lot of the program already minus the indexHighTemp and indexLowTemp because i can't seem to figure out where to even start to find them.
Some problems i am having is that the answers for my averageHigh and averageLow are both 0. I dont know if its because i assigned the file to the 2d array wrong or if i just did the two functions wrong.
Code:
#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; //function prototypes void getData(); double averageHigh(); double averageLow(); int indexHighTemp(); int indexLowTemp(); //declared arrays in file int temps[12][2]; //main part of program int _tmain(int argc, _TCHAR* argv[]) { //declared variables int a; int b; int c; int d; int e; cout << "Press 1 to read the temp. file or 2 to exit" << endl; cin >> a; //exit if statement if (a != 1) return 0; //calling the function getData(); cout << "Press 1 if you want to find the average high temp" << endl; cin >> b; if (b != 1) return 0; cout << "The average high is: "<< averageHigh() << endl; cout << "Press 1 if you want to find the average low temp" << endl; cin >> c; if (c != 1) return 0; cout << "The average low is: " << averageLow() << endl; cout << "Press 1 if you want to find the highest temp" << endl; cin >> d; if (d != 1) return 0; //cout << "The Hottest temperature was: " << indexHighTemp() << endl; cout << "Press 1 if you want to find the lowest temp" << endl; cin >> e; if (e != 1) return 0; //cout << "The Coldest temperature was: " << indexLowTemp() << endl; system("pause"); return 0; } //getData function void getData() { //declared file stream variable ifstream File; //opens file File.open("homework2.txt"); if (!File) { cout << "Error cannot open file!" << endl; } //array initialization for (int row = 0; row < 12; row++) for (int col = 0; col < 2; col++) temps[row][col] = 0; } //average high function double averageHigh() { double avgHigh; //calculates the average high(the highs are the 2nd column) avgHigh = (temps[1][2]+ temps[2][2]+temps[3][2]+temps[4][2]+temps[5][2]+temps[6][2]+temps[7][2] +temps[8][2]+temps[9][2]+temps[10][2]+temps[11][2]+temps[12][2])/12; //return the average high return avgHigh; } //average low function double averageLow() { double avgLow; //calculates the average low(the lows are the 1st column) avgLow = (temps[1][1]+ temps[2][1]+temps[3][1]+temps[4][1]+temps[5][1]+temps[6][1]+temps[7][1] +temps[8][1]+temps[9][1]+temps[10][1]+temps[11][1]+temps[12][1])/12; //returns the average low return avgLow; } //highest temp function //int indexHighTemp() //{ //int Hottest; //calculates the hottest temperature //Hottest = //return Hottest; //} //lowest temp function //int indexLowTemp() //{ //int Coldest; //calculates the coldest temperature //Coldest = //return Coldest; //}
Comment