Function/Array problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Trev17
    New Member
    • Feb 2007
    • 22

    Function/Array problems

    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.

    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;
    //}
    any help is greatly appreciated. Thank you.
    Last edited by sicarie; Apr 23 '07, 03:04 PM. Reason: Please use [code] and [/code] tags around your code.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Trev17-

    I see a few problems with this - your if (input != 1) statements aren't put in a manner that you will be able to do anything except for the first. If the first fails, 0 is returned, which means the program terminates normally. I think you want more of a menu to call your functions, have you considered a while loop to read in a line, and a switch statement to call the proper function?

    Have you printed out the values in the function, to make sure there is data there?

    Comment

    • Trev17
      New Member
      • Feb 2007
      • 22

      #3
      Originally posted by sicarie
      Have you printed out the values in the function, to make sure there is data there?
      I have both functions (averageHigh, averageLow) being called in the program and the output for both is 0. If thats what you meant?

      for the if (!File) how would you put that in so that it does something?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by Trev17
        I have both functions (averageHigh, averageLow) being called in the program and the output for both is 0. If thats what you meant?

        for the if (!File) how would you put that in so that it does something?
        Ok, after looking at it a bit more, I'm pretty sure you have no data in your arrays. (I was talking about printin out the full arrays - a 'cout << temp[i][j]' type of action), but take a look at the for loop you have where you assign temperatures to the array.

        You open the file, make sure it's open, but then look at what you declare in the comment as "array initialization" . You set everything to 0. Your functions are probably working right, because there's no info in them.

        You have File declared, you just need to read the elements in from file into the proper place. I'll give you a hint, you'll need to wrap the for loops in a while loop, and after that, instead fo assigning a place in the array 0, you need to assign it the next value from the file.

        Does that help?
        Last edited by sicarie; Apr 23 '07, 04:00 PM. Reason: I shouldn't log on before I drink my coffee - it interferes with spelling properly...

        Comment

        • Trev17
          New Member
          • Feb 2007
          • 22

          #5
          Originally posted by sicarie
          You have File declared, you just need to read the elements in from file into the proper place. I'll give you a hint, you'll need to wrap the for loops in a while loop, and after that, instead fo assigning a place in the array 0, you need to assign it the next value from the file.

          Does that help?
          I changed it to this but now it sort of freezes after it calls the getData function.

          while (File)
          {
          for (int row = 0; row < 12; row++)
          for (int col = 0; col < 2; col++)
          temps[row][col]++;
          }
          I think this is what you meant but not sure.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Trev17
            I changed it to this but now it sort of freezes after it calls the getData function.

            while (File)
            {
            for (int row = 0; row < 12; row++)
            for (int col = 0; col < 2; col++)
            temps[row][col]++;
            }
            I think this is what you meant but not sure.
            Ok, so you want to pull temperatures from File, correct? So I would change the condition in your while to be (File != 'EOF') - the EOF character stands for 'End of File' and will let the while loop know that is has reached the end. Right now it's freezing because the file exists, so it's open, so it always returns true (there is no condition in which File will return false, unless you delete/move it yourself, which isn't what you want).

            Second, code tags ( [ code] and [ /code] without the spaces inside) help a great deal with readability and spacing.

            Third, you have

            temps[row][col]++;

            What do you want this to do? The ++ postincrement operator is working on the values of each, but you're not saving those values, and that's not what you really want to do anyway. Right here is where you want to read in a value from File.

            Check out this tutorial from cplusplus.com, it will help with File IO.

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              For the high, low functions: just assign a variable to one of the temps and check to see if any of the other temps are larger (or smaller) than it. Like this:
              Code:
              variable = first temp
              if variable < second temp
                  variable = second temp
              if variable < third temp
                  variable = third temp
              and so on and so on

              Comment

              Working...