Average, Minimum and Maximum temperature using functions.

Pages: 12
So there comes the task:

Write a program that takes a maximum and minimum temperature of the city from Monday to Sunday, and three functions that calculate on average temperature, maximum temperature and minimum temperature of that week.

It should look like this:


Please enter this week temperature,
. Monday: 19.0 14.0
. Tuesday: 24.5 12.5
. Wednesday: 22.0 9.5
. Thursday: 17.0 9.0
. Friday: 17.0 10.0
. Saturday: 23.0 12.0
. Sunday: 22.0 15.5

This week:
minimum temperature is: 9.0
maximum temperature is: 24.5
average temperature is: 16.21


So I have to ask the user for 14 numbers, and then create a function that does minimum, maximum and average temperature. The thing is, we haven't been taught arrays so I was wondering if I need to declare 14 integers and then divide them by 14 to get the average, without using a function I got this:

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
#include <iostream>

using namespace std;

void main ()
{
	float monTemp1, monTemp2;
	float tueTemp1, tueTemp2;
	float wedTemp1, wedTemp2;
	float thuTemp1, thuTemp2;
	float friTemp1, friTemp2;
	float satTemp1, satTemp2;
	float sunTemp1, sunTemp2;
	float average;


	cout << "Please enter this week's temperature: \n";
	cout << "	Monday: ";
	cin >> monTemp1 >> monTemp2;
	cout << "	Tuesday: ";
	cin >> tueTemp1 >> tueTemp2;
	cout << "	Wednesday: ";
	cin >> wedTemp1 >> wedTemp2;
	cout << "	Thursday: ";
	cin >> thuTemp1 >> thuTemp2;
	cout << "	Friday: ";
	cin >> friTemp1 >> friTemp2;
	cout << "	Saturday: ";
	cin >> satTemp1 >> satTemp2;
	cout << "	Sunday: ";
	cin >> sunTemp1 >> sunTemp2;

	float averageNumber = (monTemp1 + monTemp2 + tueTemp1 + tueTemp2 + wedTemp1 + wedTemp2 + thuTemp1 + thuTemp2 + friTemp1 + friTemp2 + satTemp1 + satTemp1 + sunTemp1 + sunTem2) / 14;

	cout << "The average is: " << averageNumber << endl;

	system("pause");

}


So I'm wondering how I could put this into a function considering that I have only done functions with 2 integers, and to get the the minimum and maximum temperature I would have to do a BubbleSort right?

Thanks for helping guys.

Cheers
You say you haven't been taught arrays, but do you know how to use them and are you allowed to use them? That would make this problem 10x easier to code as you could pass the whole array as a function parameter.
You'll need to learn about array and loop :) to get a min and max value you can use sequencial search, or sort the array of temp and get array at 0 as min and the last as max
I don't know how to use array 100% yet, I know just a little bit however I don't use it that often and I have never used an array as a function parameter and yes I guess we are allowed to use them but I would have to research and find out because I have no idea hehe.

I have another topic that is fixed: http://www.cplusplus.com/forum/beginner/143879

In that task I did a sort using loops and array, but that would be only to sort numbers I don't know how I would go for minimum, maximum and average numbers from that, and that was using function overloaded so I'm completely lost :(.

P.s: This is only my first semester of cpp :o
Min = array[first]
Max = array[last]
sum = array[0]+array[1]+...+array[last]
Average = sum/amount

._.
The reason we ask is because your assignment says to create three functions. Without using an array, that means each function would require you use 14 parameters as opposed to requiring no more than two (an array and its size). I'd suggest researching if you're up for it. :P
closed account (48T7M4Gy)
You could do it with one parameter and calling the function 14 times. :)
Ok this is being very hard and I have already spent 6 hours in front of this computer trying to find a solution.

I DON'T know how to declare the function for average, mine is:

1
2
3
4
5
6
7
8
9
10
11
12
13
int getAverage (int tempNumbers[], int size)
{
	int i, sum = 0;
	int average;

	for (i = 0; i < size; i++)
		sum +- tempNumbers[i];

	average = int(sum) / size;

	return average;

}


But then I don't know what to put in my void main()...

Thanks
closed account (48T7M4Gy)
In main you would put something like:
1
2
3
 
double theAverage = getAverage ( yourArray, sizeOfIt);
cout << theAverage << endl;


should be sum += tempNumbers[i];
Last edited on
It doesn't work.

My main function now is:

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
#include <iostream>
#include "functions.h"

using namespace std;

void main()
{
	double tempNumbers[14];

	cout << "Please enter this week's temperature: \n";
	cout << "	Monday: ";
	cin >> tempNumbers[0] >> tempNumbers[1];
	cout << "	Tuesday: ";
	cin >> tempNumbers[2] >> tempNumbers[4];
	cout << "	Wednesday: ";
	cin >> tempNumbers[5] >> tempNumbers[6];
	cout << "	Thursday: ";
	cin >> tempNumbers[7] >> tempNumbers[8];
	cout << "	Friday: ";
	cin >> tempNumbers[9] >> tempNumbers[10];
	cout << "	Saturday: ";
	cin >> tempNumbers[11] >> tempNumbers[12];
	cout << "	Sunday: ";
	cin >> tempNumbers[13] >> tempNumbers[14];
	

	cout << "The average is: " << endl;

	double tempAverage = getAverage(tempNumbers,14);
	cout << tempAverage << endl;

	system("pause");

}


And my function is the same as I posted before but it doesn't work.

P.s: All my functions need to be in different library, so I get confused because I have to keep going back to my functions.cpp file :s
Last edited on
Your function looks fine. It might be because you're not inputting numbers correctly. Your main function has you inputting values into the indexes 0 through 14, excluding 3 (line 14). So you're not putting them in sequence and you're going beyond the bounds of the array.

Also, your syntax is wrong from main. You're defining it as void main() when it should be int main (void)
Last edited on
What did yu mean by doesn't work ? Is it not compiling ? Or get unexpected result ?
Oh wow that's right I missed number 3... Fixed it already

It is not compiling because in this line at the end of my main function:

1
2
3
4
double tempAverage = getAverage(tempNumbers,14);
	cout << tempAverage << endl;

	system("pause");


it says that (tempNumbers,14) = Error: argument of type "float*" is incompatible with parameter of type "int".

So I'll post everything again, right NOW my main is:

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
#include <iostream>
#include "functions.h"

using namespace std;

void main()
{
	double tempNumbers[14];

	cout << "Please enter this week's temperature: \n";
	cout << "	Monday: ";
	cin >> tempNumbers[0] >> tempNumbers[1];
	cout << "	Tuesday: ";
	cin >> tempNumbers[2] >> tempNumbers[3];
	cout << "	Wednesday: ";
	cin >> tempNumbers[4] >> tempNumbers[5];
	cout << "	Thursday: ";
	cin >> tempNumbers[6] >> tempNumbers[7];
	cout << "	Friday: ";
	cin >> tempNumbers[8] >> tempNumbers[9];
	cout << "	Saturday: ";
	cin >> tempNumbers[10] >> tempNumbers[11];
	cout << "	Sunday: ";
	cin >> tempNumbers[12] >> tempNumbers[13];
	
	double theAverage = getAverage (tempNumbers, 14);
	cout << theAverage << endl;

	system("pause");

}


My functions.cpp is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getAverage (int tempNumbers[], int size)
{
	int i, sum = 0;
	int average;

	for (i = 0; i < size; i++)
		sum += tempNumbers[i];

	average = int(sum) / size;

	return average;

}


And my functions.h is:

 
int getAverage (int, int);
Last edited on
Inside main tempNumbers is double but parameter for getAverage is int

Just change your tempNumbers parameter in getAverage to double
Last edited on
Done, but now I get:

Error: argument of type "double*" is incompatible with parameter of type "double".

In the same tempNumbers.
Not sure but maybe
In function.h first parameter should be double* or double[]

1 more thing are you sure that getAverage returning int ?
Last edited on
Yeah you were right, but it still doesn't compile, I get 2 error and 1 warning:

Error 2 error LNK2019: unresolved external symbol "int __cdecl getAverage(double *,int)" (?getAverage@@YAHPANH@Z) referenced in function _main E:\C++\Assigment2-Right\Assigment2-Right\Task4.obj Assigment2-Right

Error 3 error LNK1120: 1 unresolved externals E:\C++\Assigment2-Right\Debug\Assigment2-Right.exe 1 1 Assigment2-Right

Warning 1 warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data e:\c++\assigment2-right\assigment2-right\functions.cpp 51 1 Assigment2-Right

What @LendraDwi said. Also, since you're using doubles, it's important to preserve your data.

tempNumbers, theAverage in main and sum, average in the fuction should all be of type double. Without that, you'd lose significant precision. The average of temperatures of 2.5 and 3.5 would result in 2 the way your program is currently written instead of 3 as it should be.
Use double[]. That way...
1
2
double getAverage (double[], int)
double getAverage (double tempNumbers[], int size)

...will match.
They are like that, the tempNumbers, theAverage in main are double, and sum in the function is double but I still can't compile it.

This is giving me such a headache can't fix this :(. And this is only the average don't even want to think when I go for min and max :s
Pages: 12