How would I write a definition of the function?

This is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//-----------------------------------------------------------------------------------------
//
//  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.

This is what I have so far.

1
2
3
4
5
6
void tempAvg (double tempArray[][MaxTimes], const int days, const int times)
{
    for(int i = 0; i < days; i++)
    {
        for(int j = 0; j < times; j++)
}
Last edited on
hello!
here is the function definition:
1
2
3
4
5
6
7
8
9
10
11
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;
	}
}


and here is example and explanation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

const int maxDays = 31;
const int minDays = 28;
const int 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 :)
Thanks alot.

You explained it to me perfectly.

Once again thanks. I now understand it.
@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.
Yeah but this was very informative. I turned this code into my own.
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.
Topic archived. No new replies allowed.