Average, Minimum and Maximum temperature using functions.

Pages: 12
Same error ?

Don't worry min and max is much simpler :D you'll just need to sort that array and get min and max in easy way
Ok let's go.

My main function now is:

1
2
3
4
	double tempNumbers[14];

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


What is right I hope.

Then we go to my functions.cpp file that is:

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

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

	average = double (sum) / size;

	return average;

}


What looks right to me too.

And then we get the line from functions.h that is:

int getAverage (double*, int);

So everything looks fine, but when I run it I still get 2 errors that are:

Error 1 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 2 error LNK1120: 1 unresolved externals E:\C++\Assigment2-Right\Debug\Assigment2-Right.exe 1 1 Assigment2-Right
You've got to use both
1
2
double getAverage (double[], int);
double getArverge (double tempNumbers[], int size);
according to what keene said change your getAverage first param in function.h to double[]
Wow, just wow, you guys are awesome :D

It runs now!! However, at the end my result is 16 and not 16.21 but that's ok I can fix that later heheh going to work on the max and min values now...

So I need a for loop to sort the arrays and then print the first array for the min temp and the last array for max temp right?

Thank you very much :)
Change int getAverage to double getAverage in both function.h and functon.cpp, then change sum and average var to double :)

For min and max, yes it is :D
Last edited on
closed account (48T7M4Gy)
Sorting is a bit complicated. There's an easy way. All you need is something like:

double getMaximum( double arrayOfSameStuff, sameSize )
{
double max = ...;

for (int i; i < sameSize; i++)
{
is arrayOfStuff[i] bigger than max? If it is then that value becomes max.
}
return max;
}

Then do the same sort of thing for min

Did this one for sorting for minimum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double minTemp (double tempNumbers[], int size){

	int minTemp1,minTemp2;

	for (minTemp1 = size - 1; minTemp1 > 0; minTemp1--)
	for (minTemp2 = 0; minTemp2 < minTemp1; minTemp2++)
	
		if (tempNumbers[minTemp2] > tempNumbers[minTemp2 + 1]){
			std::swap (tempNumbers[minTemp2],tempNumbers[minTemp2+1]);
		}

	for (minTemp1 = 0; minTemp1 < size; minTemp1++)
		cout << tempNumbers[minTemp1] << endl;

}


But then I don't really know how to put it in my main, it's like this but it's wrong it doesn't run :s

1
2
	double minimumT = minTemp (tempNumbers, 14);
	cout << "The minimum temperature is: " << minimumT << endl;
closed account (48T7M4Gy)
The idea for the 2 lines with cout is OK and they go in as the last two lines in main, before return.

Generally the functions go in after main withtheir prototype (pattern) put in before main starts.

Do that and I think u need to show us the complete code so far.
Ok my complete code so far is:

Main function:

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
#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 << "The average temperature is: " << theAverage << endl;

	double minimumT = minTemp (tempNumbers, 14);
	cout << "The minimum temperature is: " << minimumT << endl;

	system("pause");

}


Functions.cpp:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//Task 1 Functions
int sum (int sumNum1, int sumNum2)
{
	int resultSum;
	resultSum = sumNum1 + sumNum2;
	return resultSum;
}

int subtract (int subNum1, int subNum2)
{
	int resultSub;
	resultSub = subNum1 - subNum2;
	return resultSub;
}

int multiply (int multNum1, int multNum2)
{
	int resultMult;
	resultMult = multNum1 * multNum2;
	return resultMult;
}

int divide (int divNum1, int divNum2)
{
	int resultDiv;
	resultDiv = divNum1 / divNum2;
	return resultDiv;
}

int modulus (int modNum1, int modNum2)
{
	int resultMod;
	resultMod = modNum1 % modNum2;
	return resultMod;
}

//Task 4 Functions

double getAverage (double tempNumbers[], int size)
{
	int i, sum = 0;
	double average;

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

	average = (sum) / size;

	return average;

}

double minTemp (double tempNumbers[], int size){

	int minTemp1,minTemp2;

	for (minTemp1 = size - 1; minTemp1 > 0; minTemp1--)
	for (minTemp2 = 0; minTemp2 < minTemp1; minTemp2++)
	
		if (tempNumbers[minTemp2] > tempNumbers[minTemp2 + 1]){
			std::swap (tempNumbers[minTemp2],tempNumbers[minTemp2+1]);
		}

	//for (minTemp1 = 0; minTemp1 < size; minTemp1++)
		/*cout << tempNumbers[minTemp1] << endl;*/

	return minTemp1;
}


Functions.h:

1
2
3
4
5
// TASK 4 FUNCTION

double getAverage (double[], int);

double minTemp (double[], int);


But now it returns 0 for minimum temperature :s
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
double minTemp(double tempNumbers[], int size)
{
	double minTemp = tempNumbers[0];

	for (int i = 0; i < size; i++)
	{
		if (tempNumbers[i] < minTemp)
			minTemp = tempNumbers[i];
	}
	return minTemp;
}


Try this and modify it to get maxTemp. I'd suggest you have two funcyions, one for max and one for min. You can do them together but it is clearer if they are separate.
Last edited on
Ahh I get it now, yeah done for maxTemp too... Thanks for your help me guys.

The only thing is that now my average is not 16.21 but only 16 for some reason hehe but that's ok I'll figure that out.

Thank you very much!!

Cheers :)
Topic archived. No new replies allowed.
Pages: 12