-5

Here is the code i currently have. As it stands, it does everything that it needs to, to return the corresponding values necessary. The one thing that I am missing, however, is a function that will return the month that corresponds to the index number associated with the temperature.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

const int NUM_COLS = 2;
const int NUM_ROWS = 12;

void getData(ifstream& tempFile, double temps[][NUM_COLS]);
double averageHigh(double temps[][NUM_COLS], int NUM_ROWS);
double averageLow(double temps[][NUM_COLS], int NUM_ROWS);
double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS);
double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS);
string monthForTemp(double temps[][NUM_COLS], int NUM_ROWS);

/*
 * 
 */
int main(int argc, char** argv) 
{
    ifstream tempFile;
    double temps[NUM_ROWS][NUM_COLS] = {0.0};
    tempFile.open("Ch8_ex9Data.txt");

    if (!tempFile)
    {
        cout << "File does not exist, contact system admin" << endl;
        return 1;
    }
    getData (tempFile, temps);
    double highAverage = averageHigh(temps, NUM_ROWS);
    cout << "Average high is: " << highAverage << endl;

    double lowAverage = averageLow(temps, NUM_ROWS);
    cout << "Average low is: " << lowAverage << endl;

    int indexHiTemp = indexHighTemp(temps, NUM_ROWS);
    cout << "High temp index is: " << indexHiTemp  
         << " which is " << temps[indexHiTemp][0]
         << " degrees F" << endl;

    int indexLoTemp = indexLowTemp(temps, NUM_ROWS);
    cout << "Low temp index is: " << indexLoTemp 
         << " which is " << temps[indexLoTemp][1]
         << " degrees F" << endl;
    return 0;



}

void getData(ifstream& tempFile, double temps[][NUM_COLS])
{
    cout << fixed << showpoint << setprecision(1);
    double hiTemp, loTemp;

    tempFile >> hiTemp >> loTemp;

    int rowIndex = 0;

    while(tempFile)
    {
        temps[rowIndex][0] = hiTemp;
        temps[rowIndex][1] = loTemp;
        rowIndex++;
        tempFile >> hiTemp >> loTemp;
    }
}

double averageHigh(double temps[][NUM_COLS], int NUM_ROWS)
{
    double avgHigh = 0.0;
    double sum = 0.0;
    int i;

    for (i = 0; i < NUM_ROWS; i++)
    {
        sum += temps[i][0];
        //cout << temps[i][0] << endl;
    }
    avgHigh = sum/NUM_ROWS;
    return avgHigh;
}

double averageLow(double temps[][NUM_COLS], int NUM_ROWS)
{
    double avgLow = 0.0;
    double sum = 0.0;
    int i;

    for (i = 0; i < NUM_ROWS; i++)
    {
        sum += temps[i][1];
        //cout << temps[i][1] << endl;
    }
    avgLow = sum/NUM_ROWS;
    return avgLow;
}

double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS)
{
    cout << fixed << showpoint << setprecision(1);
    double index = 0.0;
    double maxHighTemp = 0.0;
    for (int i = 0; i < NUM_ROWS; i++)
    {
        if (temps[i][0] > maxHighTemp)
        {
            maxHighTemp = temps[i][0];
            index = i;
        }
    }
    return index;
}

double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS)
{
    cout << fixed << showpoint << setprecision(1);
    double index = 0.0;
    double maxLowTemp = 0.0;
    string month = " ";
    for (int i = 0; i < NUM_ROWS; i++)
    {
        if (temps[i][0] < maxLowTemp)
        {
            maxLowTemp = temps[i][1];
            index = i;
        }

    }
    return index, month;
}

string monthForTemp(double temps[][NUM_COLS], NUM_ROWS)
{
    string month = " ";
    switch (index)
        {
            case 1 : 
                month = "January";
                break;
            case 2 :
                month = "February";
                break;
            case 3 : 
                month = "March";
                break;
            case 4 : 
                month = "April";
                break;
            case 5 : 
                month = "May";
                break;
            case 6 : 
                month = "June";
                break;
            case 7 : 
                month = "July";
                break;
            case 8 : 
                month = "August";
                break;
            case 9 : 
                month = "September";
                break;
            case 10 : 
                month = "October";
                break;
            case 11 : 
                month = "November";
                break;
            case 12 : 
                month = "December";
                break;
        }

    return month;
}

I need to get the column and row value to be passed to the string function but I can't quite seem to get it right. Any pointers?

Thanks.

Ronave4C
  • 9
  • 1
  • 5
  • Your code doesn't compile, and you don't pass a month `index` into the function. – Ken Y-N Dec 12 '16 at 02:26
  • Right. Without the last function, and in absence of the "return month;" portion of indexLowTemp it will though. I'm trying to find a way to pass the variables into the string function and get a month as a response. – Ronave4C Dec 12 '16 at 02:53
  • Here is the displayed results: Average high is: 61.3 Average low is: 40.7 High temp index is: 6 which is 78.2 degrees F Low temp index is: 0 which is 23.7 degrees F Should I make it where the indexLowTemp and indexHighTemp functions also return a string? Or should it be a separate function? – Ronave4C Dec 12 '16 at 02:59

1 Answers1

0
string monthForTemp(double temps[][NUM_COLS], NUM_ROWS)

is improperly declared and cannot perform its task. It should look something like

string monthForTemp(int index)

But to do this, indexHighTemp and the rest of the index-returning functions need to change to something like

int indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS) //returns int
{
    cout << fixed << showpoint << setprecision(1);
    int index = 0; // index is int
    double maxHighTemp = 0.0;
    for (int i = 0; i < NUM_ROWS; i++)
    {
        if (temps[i][0] > maxHighTemp)
        {
            maxHighTemp = temps[i][0];
            index = i;
        }
    }
    return index;
}

because arrays can't used doubles as indexes. You can't get the item at 3.1459 out of an array. Besides, since i is an int there isn't much point to index being a double. At best it's an integer in floating point representation and at worst (not a problem here since the numbers are small) it gets damaged by floating point imprecision.

And here is a great pplace to Head into X-Y territory and suggest a completely different solution:

Replace the monthForTemp function with an array. Eg:

std::string monthForTemp[] = 
{
    "January", 
    "February", 
    "March", 
    // rest of months go here 
};

Can be used as

cout << "High temp month is: " << monthForTemp[indexHiTemp]  
     << " which is " << temps[indexHiTemp][0]
     << " degrees F" << endl;

assuming indexHighTemp has been modified to return int as suggested above.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54