//-----------------------------------------------------------------------------------------
//
// 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);
And I need to Write the complete definition of the function tempAvg () making sure the post condition is met.
void tempAvg (double tempArray [maxDays] [maxTimes], int days, int times) {
if (days > maxDays || days < 28 || times >= maxTimes) exit (1);
double sum = 0;
for (short d = 0; d < days; d++) {
for (short t = 0; t < times; t++) {
sum += tempArray [d] [t];
}
tempArray [d] [maxTimes] = sum / times;
sum = 0;
}
}
#include <iostream>
usingnamespace std;
constint maxDays = 31;
constint minDays = 28;
constint maxTimes = 31; // here is one more then allowed which is reserved for last column!!
void tempAvg (double [maxDays] [maxTimes], int, int);
int main () {
//example:
double test [maxDays] [maxTimes]; //regardles of how many days there relay are we must use universal arrays which are compatible with function
short days = 3, times = 2;
double temperature;
for (short d = 0; d < days; d++)
for (short t = 0; t < times; t++) {
cout << "enter temperature for day nr. : " << d + 1 << " and time nr. : " << t + 1;
cin >> temperature;
test [d] [t] = temperature; // let's initilize array with some temperatures
}
cout << "-----------------------------------------------";
//lets calculate average for each day
tempAvg (test, days, times); //and here we will specife real days and times...user is responsible to enter correct days and times
// now let's display average for each day:
for (short d = 0; d < days; d++)
for (short t = 0; t < times; t++)
cout << "avgTemp day: " << d + 1 << " is: " << test [d] [maxTimes] << endl;
system ("pause");
return 0;
}
void tempAvg (double tempArray [maxDays] [maxTimes], int days, int times) { /*maxDays + 1 is the last column reserved for average temperature
array second dim. must be maxDays + 1 regardles of real days but argument days will tell us how wide array is (until where members are active values ---other are inactive so skiped in inside for!!!*/
if (days > maxDays || days < 28 || times >= maxTimes) exit (1); //check if rules are broken
double sum = 0; //hold sum of temperature here for each day then reset
for (short d = 0; d < days; d++) {
for (short t = 0; t < times; t++) {
sum += tempArray [d] [t]; //add to the sum for each measure in this day ---> note d are days and t is times
}
tempArray [d] [maxTimes] = sum / times; // exiting inside for so last column for this day hold average for day of outside for (sum / times)
sum = 0; //reset sum for this day! --> day D :)
}
}
it would be much beter to do this with pointers and dynamic memory but that was not the question :)
@sasanet: Don't just give complete solutions, try to give hints instead. You learn more if you solve for the solution yourself than if you read something someone else gave you.
@sasanet: Avoid giving out complete solutions in the future. There are many who will simply take your code and turn it in as their own, learning nothing.
You might have learned more if you had been told how to do it rather than given it. In this instance, I think sasanet explained it well, though. Some people, though, are just lazy and ask for complete solutions, copy them, and then don't understand what the code actually does.