Need help splitting a program into functions

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>
using namespace std;

int main ()
{
	const int size = 50;
	double temp [size] = {0};								                	double sum = 0;
	double avg, high;

	for (int i = 0; i < size ; i++)								 
	{
		cout << "Enter temperature number " << i + 1 << ":" << endl;	
		cin >> temp[i];

		sum = sum + temp[i];
	}

	avg = sum / size;

	high = temp[0];

	for (int i = 1; i < size; i++)
	{
		if (temp[1] < high)
			high = temp[1];
	}

	cout << "The average temperature is: " << avg << endl;
	cout << "The highest temperature is: " << high << endl;



	return 0;
}


The purpose of the program is to ask the user for a series of temperatures (no more than 50) using an array. That is what the first function should do. The second function should calculate the average of all the scores. The third function should find the highest temperature. The average and highest temperature should then be displayed to the user.

I'm not entirely sure my variables are entirely correct (meaning, I don't know that I have enough) but I know for sure that I keep trying to split this up into the functions and it isn't working for me.

Thanks, guys.
Last edited on
That's it:
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
#include <iostream>
using namespace std;
void read();
double averagef();
double highcalc();                      // those are the prototypes
void print(double x, double y);
const int size = 10;
double temp [size] = {0};
double sum = 0;
double avg, high;
int main ()
{
	read();
	print(averagef(), highcalc());

	return 0;
}
void read()
{
    for (int i = 0; i < size ; i++)
	{
		cout << "Enter temperature number " << i + 1 << ":" << endl;
		cin >> temp[i];

		sum = sum + temp[i];
	}
}
double averagef()
{
    avg = sum / size;
    return (avg);
}
double highcalc()
{   high=temp[0];
    for (int i = 1; i < size; i++)
	{
		if (temp[i] > high)
			high = temp[i];
	}
	return (high);
}
void print(double x, double y)
{   cout << "The average temperature is: " << x << endl;
    cout << "The highest temperature is: " << y << endl;
}
I've found a lot of mistakes, for instance here:
1
2
3
4
5
for (int i = 1; i < size; i++)
	{
		if (temp[1] < high)
			high = temp[1];
	}
Is not correct temp[1] because it will compare the high-variable just with one component of that array, it must be temp[i]. Pay attention next time.
Try this dear... :)

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
#include <iostream>
using namespace std;

double func_sum (double temp[], int size);
double func_high (double temp[], int size);

int main ()
{
	const int size = 50;
	double temp [size] = {0};
	double avg, high, sum = 0;

	sum = func_sum (temp, size);
	avg = sum / size;

	high = func_high (temp, size);

	cout << "The average temperature is: " << avg << endl;
	cout << "The highest temperature is: " << high << endl;

	return 0;
}

double func_sum (double temp[], int size)
{
	double sum;
	for (int i = 0; i < size ; i++)								 
	{
		cout << "Enter temperature number " << i + 1 << ":" << endl;	
		cin >> temp[i];

		sum = sum + temp[i];
	}
	return sum;
}

double func_high (double temp[], int size)
{
	double high = temp[0];
	for (int i = 1; i < size; i++)
	{
		if (temp[i] > high)
			high = temp[i];
	}
	return high;
}
For 50 number of entries use a counter and after every entry make an increment in it like counter++; then make a check that if (counter == 50) print "Memory is full."
Thank you, both. This was very helpful :)
Topic archived. No new replies allowed.