Assignment Guidance

sweenish

Diamond Member
May 21, 2013
3,656
60
91
I'm in a beginning C++ class, and our current assignment was going well enough, until I hit my second function call.

Code:
// File Name: program8.cpp
// Author: 
// Student ID: 
// Assignment Number: 8
// Description: This program reads temperatures from a data file and converts them to Celcius.
// Last Changed: 

#include <iostream>
#include <iomanip>
#include <fstream>

// function declarations. Function format is required
void show_warmer(double temperature[][24], int days, double cutoff);
void show_warmest(double temperature[][24], int days);
void find_daily_mmm(double day_temps[], double &max, double &min, double &mean);

int main(void)
{
	using namespace std;

	// variable declaration
	double target;
	double celcius[7][24];	// required declaration
	int i, j;
	ifstream read_data;

	// Always print the decimal and two decimal places.
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	// Opens temperature.dat, exits if file doesn't open
	read_data.open("temperature.dat");
	if (read_data.fail()) {
		cout << endl << "File failed to open. Exiting..." << endl;
		return -1;
	}

	// reads value from file into array, then converts value to celcius
	for (i = 0; i <= 6; i++) {
		for (j = 0; j <= 23; j++) {
			read_data >> celcius[i][j];
			celcius[i][j] = (celcius[i][j] - 32.0) * (5.0/9.0);
	//		cout << endl << "celcius[" << i << "][" << j << "] = " << celcius[i][j];
		}
	}

	read_data.close();

	cout << "Enter the value for which to find warmer temperatures (°C): ";
	cin >> target;
	cout << endl;

	show_warmer(celcius, 7, target);
	show_warmest(celcius, 7);
	return 0;
}

void show_warmer(double temperature[][24], int days, double cutoff)
{
	using namespace std;

	int i, j;

	cout << "Times at which temperatures warmer than " << cutoff << " were found:" << endl << endl;

	for (i = 0; i < days; i++) {
		for (j = 0; j < 24; j++) {
			if (temperature[i][j] > cutoff) {
				cout << "At day " << i+1 << ", hour " << j+1 << ", the temperature was " 
					  << temperature[i][j] << " °C." << endl;
			}
		}
	}

	cout << endl;

	return;
}

void show_warmest(double temperature[][24], int days)
{
	using namespace std;

	double max = 0;
	int i = 0, j = 0;

	for (i = 0; i < days; i++); {
		for (j= 0; j < 24; j++) {
			cout << endl << "celcius[" << i << "][" << j << "] = " << temperature[i][j];
			if (temperature[i][j] > max) {
				max = temperature[i][j];
			}
		}
	}

	cout << "The warmest temperature was:" << endl << endl
		  << "At day " << i+1 << ", hour " << j+1 << ", the temperature was " 
		  << temperature[i][j] << " °C." << endl << endl;
	return;
}

The issue I'm having is in the function show_warmest. The cout to show where the loop is processing starts at [7][0], even though the variable i was initialized to 0 and the loop explicitly states that i starts at 0.

I would appreciate some guidance. I still have to write another function that parses the entire array, and I imagine I'll have the same kind of trouble there.
 

sweenish

Diamond Member
May 21, 2013
3,656
60
91
Is there something I need to expound on?

I've made a few changes to the function show_warmest; I'll just post that function here.

Code:
void show_warmest(double temperature[][24], int days)
{
	using namespace std;

	double max = temperature[0][0];
	int i, j, day, hour;

	// Finds the warmest temperature in the file
	for (i = 0; i < days; i++); {
		for (j = 0; j < HOURS; j++) {
			cout << endl << "celcius[" << i << "][" << j << "] = " << temperature[i][j];
			if (temperature[i][j] > max) {
				max = temperature[i][j];
				day = i + 1;
				hour = j + 1;
			}
		}
	}

	cout << "The warmest temperature was:" << endl << endl
		  << "At day " << day << ", hour " << hour << ", the temperature was " 
		  << max << " °C." << endl << endl;
	return;
}

I realized that I was making it show the last value regardless.

This function now shows garbage for the day number, hour number, and the max value is never changed because the cout that checks the loop still starts at temperature[7][0] for whatever reason.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
At a glance I don't see anything wrong with the loop in show_warmest (aside from the fact that you use '< boundary' to read it and '<= boundary-1' to initialize it; try to be consistent). Are you sure the array is initialized correctly? When in doubt print it out.
 

sweenish

Diamond Member
May 21, 2013
3,656
60
91
I am printing it out.

Or do you mean to just try and print the array on its own?

the values are there. max initializes correctly. I can poll any value and get the correct number to spit back. It's just that my loop doesn't start at the beginning like it should.

I have gone through the entire program and used constants to define my boundaries. I should be strictly "< boundary" now.

The array works. The first function show_warmer works. And the final function which isn't shown here works as well. It's only show_warmest that's acting up.

An example of the garbage output is:
At day -1100839936, hour 32529, the temperature was 13.33 °C.

I don't know how day and hour are escaping the bounds of the loop. max never changes from the initialized value because temperature[7][0-23] doesn't actually exist. I'm lucky they're just zeros.

EDIT:

Weirdness.

I commented out the loop that's in show_warmest and re-wrote a nested for to print out the array before the loop that is in the function. It printed just fine. I then added the greater than check with assignment, and the max value changed appropriately in the output. So I added the day and hour assignment, and it worked. The loop looks exactly the same, but behaves properly. At least visually, I didn't do a character analysis to see if there was a difference.

Markbnj, thanks for taking the time and chiming in, and at least affirming that I wasn't insane. The function is simple enough, and I don't know what was going on. I find it odd that simply re-writing the loops fixed it. Also, thank you for the consistency tip.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Interesting. Post your final version if you like. Perhaps there is some subtle difference. I used to teach C++ but it has been many years since I used it in any serious way. There are others here with much more recent experience.
 

sweenish

Diamond Member
May 21, 2013
3,656
60
91
Here is the complete code.

It is designed to open a file called temperature.dat, which contains a 7x24 grid of temperature readings in Fahrenheit.

The only input from the user is a target temperature in degrees Celsius. The program finds times when the temperature was higher, then gives general information.

The celcius (just found out it's spelled wrong) array isn't declared with the constants DAYS and HOURS because the array declaration was required as-is.

If you want any more information, please let me know.

Code:
// File Name: program8.cpp
// Author: 
// Student ID:
// Assignment Number: 8
// Description: This program reads temperatures from a data file and converts them to Celcius.
//					 It also shows when the highest recorded temperature occured, and statistics 
//					 for each day.
// Last Changed: Nov. 11, 2014

#include <iostream>
#include <iomanip>
#include <fstream>

const int DAYS = 7;
const int HOURS = 24;

// function declarations. Function format is required
void show_warmer(double temperature[][24], int days, double cutoff);
void show_warmest(double temperature[][24], int days);
void find_daily_mmm(double day_temps[], double &max, double &min, double &mean);

int main(void)
{
	using namespace std;

	// variable declaration
	double target, max, min, mean;
	double celcius[7][24];	// required declaration
	double daily[24];
	int i, j;
	ifstream read_data;

	// Always print the decimal and two decimal places.
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	// Opens temperature.dat, exits if file doesn't open
	read_data.open("temperature.dat");
	if (read_data.fail()) {
		cout << endl << "File failed to open. Exiting..." << endl;
		return -1;
	}

	// reads value from file into array, then converts value to celcius
	for (i = 0; i < DAYS; i++) {
		for (j = 0; j < HOURS; j++) {
			read_data >> celcius[i][j];
			celcius[i][j] = (celcius[i][j] - 32.0) * (5.0/9.0);
		}
	}

	read_data.close();

	// User prompt
	cout << "Enter the value for which to find warmer temperatures (°C): ";
	cin >> target;
	cout << endl;

	// Function calls to show warmer than user provided value, and warmest 
	show_warmer(celcius, DAYS, target);
	show_warmest(celcius, DAYS);

	// Feeds one day's temps into daily[], then calls find_daily_mmm
	// Prints results before moving onto next day
	cout << "The maximum, minimum, and mean temperatures for each day" << endl << endl;
	for (i = 0; i < DAYS; i++) {
		for (j = 0; j < HOURS; j++) {
			daily[j] = celcius[i][j];
			find_daily_mmm(daily, max, min, mean);
		}
		cout << "Day " << i+1 << ":  max " << max << "°C,  min " << min << "°C,  mean "
			  << mean << "°C." << endl;
	}
	return 0;
}

void show_warmer(double temperature[][24], int days, double cutoff)
{
	using namespace std;

	// Variable declaration
	int i, j;

	cout << "Times at which temperatures warmer than " << cutoff << " were found:" << endl << endl;

	// When a temperature is found higher than the user's target, it is printed.
	for (i = 0; i < days; i++) {
		for (j = 0; j < HOURS; j++) {
			if (temperature[i][j] > cutoff) {
				cout << "At day " << i+1 << ", hour " << j+1 << ", the temperature was " 
					  << temperature[i][j] << " °C." << endl;
			}
		}
	}

	cout << endl;

	return;
}

void show_warmest(double temperature[][24], int days)
{
	using namespace std;

	double max = temperature[0][0];
	int i, j, day, hour;

	// Finds the max temperature in the file
	for (i = 0; i < days; i++) {
		for (j = 0; j < HOURS; j++) {
			if (temperature[i][j] > max) {
				max = temperature[i][j];
				day = i + 1;
				hour = j + 1;
			}
		}
	}

	cout << "The warmest temperature was:" << endl << endl
		  << "At day " << day << ", hour " << hour << ", the temperature was " 
		  << max << " °C." << endl << endl;
	return;
}

void find_daily_mmm(double day_temps[], double &max, double &min, double &mean)
{
	using namespace std;

	// Variable declaration
	int i;
	double sum = day_temps[0];
	
	// All variables initialized to first value in array
	max = day_temps[0];
	min = day_temps[0];

	// Checks each array value against max and min to see if they need to be replaced
	// Also adds to sum
	for (i = 1; i < 24; i++) {
		if (day_temps[i] > max) {
			max = day_temps[i];	
		}
		if (day_temps[i] < min) {
			min = day_temps[i];
		}

		sum += day_temps[i];
	}

	mean = sum / 24;
	
	return;
}
 
Last edited: