Based on my header file. How would I write a complete definition of the function temp ().

const int MaxDays = 31;  // Maximum number of days in a month
const int MaxTimes = 5; // Maximum number of times temperature is measured in 
                         // a day plus one column to hold the average for the day



//-----------------------------------------------------------------------------------------
//
//  Function: tempAvg ()
//
//	Parameters:	
//    input tempArray: double; array of temperatures
//    input days: integer; number of days for that month
//    input times: integer; number of times temperature is measured each day 
//                    for that month
//    
//	Pre-condition: input array must be populated with valid temperatures;
//                 input number of days must be between 28 and MaxDays (inclusive)
//                 input number of times must be at least one less than MaxTimes
//  Post-condition: The average temperature for each day is calculated and 
//                  stored in the last column of the array 
//-----------------------------------------------------------------------------------------

void tempAvg (double [][MaxTimes], int, int);

I think the function prototype I wrote about is correct. Yes I have already started writing some code this is what I have so far.

void tempAvg (double tempArray [], int, double [] , double & avg)

The parameters in function you posted for "what I have so far" must match exactly the parameters of the function prototype. In otherwises, the function prototype are double[], int, and int. Yours is double[], double[] and double; Not the same, one of the two is wrong.

Stick some variable names here. The compiler doesn't care, but anyone reading it trying to decide what parameter goes with what needs to know, particularly for your question.

void tempAvg (double [][MaxTimes], int, int);

This doesn't match the above. Which one are we checking?

void tempAvg (double tempArray [], int, double [] , double & avg)

Parameters...

// Parameters:
// input tempArray: double; array of temperatures
// input days: integer; number of days for that month
// input times: integer; number of times temperature is measured each day
// for that month

I see three parameters from the comment. All inputs, no return type specified, so I guess I'm expecting something like this...

/* some return type */ tempAvg(double[] tempArray, int days, int times)

or perhaps

/* some return type */ tempAvg(double[][/* some constant */] tempArray, int days, int times)

Maybe perhaps a "const" thrown in there.

I'm seeing a definite discrepancy between the comments and this one...

void tempAvg (double tempArray [], int, double [] , double & avg)

So...who wrote the comments? Is this a spec that's given to you or can you change it? To me, it's a bit vague. Are we talking about a 1-Dimensional array or a 2-Dimensional array? Clarify that in the comments. Add a sentence or two to make it crystal clear what the function is expecting. You should add a line saying what the function returns, if anything.

Ok my teacher wrote the comments ((//)) above. It was my job to write the function according to the pre and post conditions.

This is more of the first code from the first part.

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


using namespace std;

const int MaxDays = 31;  // Maximum number of days in a month
const int MaxTimes = 5; // Maximum number of times temperature is measured in 
                         // a day plus one column to hold the average for the day



//-----------------------------------------------------------------------------------------
//
//  Function: tempAvg ()
//
//	Parameters:	
//    input tempArray: double; array of temperatures
//    input days: integer; number of days for that month
//    input times: integer; number of times temperature is measured each day 
//                    for that month
//    
//	Pre-condition: input array must be populated with valid temperatures;
//                 input number of days must be between 28 and MaxDays (inclusive)
//                 input number of times must be at least one less than MaxTimes
//  Post-condition: The average temperature for each day is calculated and 
//                  stored in the last column of the array 
//-----------------------------------------------------------------------------------------

void tempAvg (double [][MaxTimes], int, int);


// DO: Put the prototype for the function tmpAvg () here


//-----------------------------------------------------------------------------------------
//
//  Function: inputFmFile ()
//
//	Parameters:	
//    input tempArray: double; array of temperatures
//    output days: integer; number of days for that month
//    output times: integer; number of times temperature is measured each day 
//                    for that month
//    
//	Post-condition: The temperatures for the month are read in from a 
//                  user-specified file and stored in the array; 
//                  The number of days in the month and number of times 
//                  the temperature is measured are returned to calling function.
//-----------------------------------------------------------------------------------------

void inputFmFile (double [][MaxTimes], int &, int &);


//-----------------------------------------------------------------------------------------
//
//  Function: inputFmFile ()
//
//	Parameters:	
//    input tempArray: double; array of temperatures
//    input days: integer; number of days for that month
//    input times: integer; number of times temperature is measured each day 
//                    for that month
//    
//	Pre-condition: input array must be populated with valid temperatures;
//	Post-condition: The temperatures for the month are displayed
//                  on the screen
//-----------------------------------------------------------------------------------------

void outputData (double [][MaxTimes], int, int);

Do you understand now?

Then I had to use it for this

#include <iostream>
#include <fstream>

#include "tempFns.h"

using namespace std;

// DO: Write the complete definition of the function tempAvg () below
// Defition of the temp tempAvg ()








void inputFmFile (double tempArray [][MaxTimes], int &days, int &times)
{
	bool worked = true;
	string inFileName;
	ifstream inStr;
	
	do 
	{
		cout << "Enter name of file to read from: ";
		cin >> inFileName;
		
		inStr.open (inFileName.c_str());
		if (inStr.fail())
		{
			cerr << "Error opening file. Try again." << endl;
			inStr.clear();
			worked = false;
		}
		else 
			worked = true;
		
	} while (!worked);
	
	inStr >> days >> times;
	
	// Use a for loop to enter data into the array
	
	for (int i=0; i < days; i++)
		for (int j=0; j < times; j++)
			inStr >> tempArray [i][j];
	
	// Initialize daily averages to 0 - set the last column to 0 
	for (int i=0; i < days; i++)
		tempArray [i][times] = 0.0;

}



void outputData (double tempArray [][MaxTimes], int days, int times)
{
	cout << endl << "The temperatures for the month are : " << endl;
	cout.setf(ios::fixed);
	cout.precision (2);
	// Use a for loop to display the temperatures
	
	cout << setw(9) << "Day";
	for (int i=0; i < times-1; i++)
		cout << setw(8) << "Temp" << i+1;
	cout << setw(8) << "Average" << endl;
	
	for (int i=0; i < days; i++)
	{
		cout << setw(9) << i+1;
		for (int j=0; j < times; j++)
			cout << setw(9) << tempArray [i][j];
		cout << endl;
	}
	
}

The prototype seems fine except, as mentioned, you should probably stick some "const" qualifiers and the variable names in there.

In addition, since the array is being mutated, I'd change the spec from...

input tempArray: double; array of temperatures

to

input/output tempArray: double; array of temperatures

Since the parameter is being changed, it's also an output parameter. And I'll reiterate that I'd add a few more comments if you're allowed to, but if your teacher provided it and doesn't want to change it, don't.

Thanks for the help. but would you know how would write the complete definition listed in second code I posted above.

Thanks for the help. but would you know how would write the complete definition listed in second code I posted above.

Just add the variable names I guess. Again, I'd probably add a couple of const qualifiers.

//	Parameters:	
//    input/output tempArray[][]: double; 2-D array of temperatures
//    input days: integer; number of days for that month
//    input times: integer; number of times temperature is measured each day 
//                    for that month
//    
//	Pre-condition: input array must be populated with valid temperatures;
//                 input number of days must be between 28 and MaxDays (inclusive)
//                 input number of times must be at least one less than MaxTimes
//  Post-condition: The average temperature for each day is calculated and 
//                  stored in the last column of the array 
//-----------------------------------------------------------------------------------------
 
void tempAvg (double tempArray[][MaxTimes], const int days, const int times);

Or are you asking how to write the function itself?

Yes or at least help me for this part.

#include <iostream>
#include <fstream>

#include "tempFns.h"

using namespace std;

// DO: Write the complete definition of the function tempAvg () below
// Defition of the temp tempAvg ()
void tempAvg (double tempArray[][MaxTimes], const int days, const int times)
{
    // code
    for(int i = 0; i < days; i++)
    {
        // code
        for(int j = 0; j < times; j++)
        {
            // code
        }
        // code
    }
    // code
}

That should give you a very rough skeleton. You need a nested loop. You'll be calculating averages, which mans you'll need to:

  1. Initialize a "total" variable to 0.
  2. Calculate the total by adding some numbers in a loop.
  3. Dividing the total by the number of elements.

Look at the spec to see what the total variable is.

// Post-condition: The average temperature for each day is calculated and
// stored in the last column of the array

start quote:

void tempAvg (double tempArray[][MaxTimes], const int days, const int times)
{
    // code
    for(int i = 0; i < days; i++)
    {
        // code
        for(int j = 0; j < times; j++)
        {
            // code
        }
        // code
    }
    // code
}

That should give you a very rough skeleton. You need a nested loop. You'll be calculating averages, which mans you'll need to:

  1. Initialize a "total" variable to 0.
  2. Calculate the total by adding some numbers in a loop.
  3. Dividing the total by the number of elements.

Look at the spec to see what the total variable is.

Ok so this is what I have.

void tempAvg (double tempArray[][MaxTimes], const int days, const int times)
{
    // code
    for(int i = 0; i < days; i++)

    {if (i >= MaxDays && i <= 28)

        for(int j = 0; j < times; j++)
        {if (j = MaxTimes - 1)


    for (int i=0; i < n; i++)
    total=0
    total += inputArray [i];
    avg = total / n;}

}

So how does this look? I think it cover all of the preconditions and postconditions.

void tempAvg (double tempArray[][MaxTimes], const int days, const int times)
{
    // code
    for(int i = 0; i < days; i++)
    
    {if (i >= MaxDays && i <= 28)
        
        for(int j = 0; j < times; j++)
        {if (j = MaxTimes - 1)
           
        
    for (int i=0; i < n; i++)
    total=0
    total += inputArray [i];
    avg = total / n;}
           
}

Line 6 - MaxDays is 31, so it's this

if (i >= 31 && i <= 28)

Can this if-condition ever be true? If not, what's the point?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.