Passing data onto another array

I just learned C++ for 3 weeks and I have the basics down, and I can do some simple coding. But I'm stuck here on arrays when my lecturer taught this. Basically it is a program that needs me to calculate the average temperatures of 5 days, and later on compare the average temperatures that are gathered by the program in order to do a comparison and finally make an output at the last. I've configured Atom text editor for my C++ work, I do have Visual Studio, but it takes quite some time to start so I've not been using it for some time. Below is what I have right now:

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

float dayTemp[5][2];
float averageTemp[5];

int main()
{
for (int day = 0; day < 5; day++)
{
	for (int temp = 0; temp < 2; temp++)
	{
	cout << "Please enter the highest temperature of the day: ";
	cin >> dayTemp[day][temp];
	cout << "Please enter the lowest temperature of the day: ";
	cin >> dayTemp[day][temp+1];
	}
}
	for (int z = 0; z < 5; z++)
	{
		cout << "The highest average temperature is: " << Highest(temperature)
		
	}
}

void Highest(temperature[5])
{
	for (int )
}



The problem I have now is passing the data from one array to another, as I will need the second array to help me compare the temperatures. Please excuse my bad coding, I really have no idea what I am doing on this one. If needed the full question is below:

Exercise 2

A weather researcher records high and low temperatures for five consecutive days. Write a C++ program which computes the average of the high and low temperatures for each day and determine the day on which the average is largest in a function Highest().
Last edited on
1
2
float dayTemp[5][2];
float averageTemp[5];

It's a bad practice to use global variables and may make your code a pain to debug. Also, since they're global you don't need to pass them as a parameter as you can access them from anywhere. But that doesn't mean I'm encouraging global variables. Please declare them inside main().

1
2
3
4
5
6
7
	for (int temp = 0; temp < 2; temp++)
	{
	cout << "Please enter the highest temperature of the day: ";
	cin >> dayTemp[day][temp];
	cout << "Please enter the lowest temperature of the day: ";
	cin >> dayTemp[day][temp+1];
	}

Consider when temp == 1.
1
2
3
4
	cout << "Please enter the highest temperature of the day: ";
	cin >> dayTemp[day][1]; // fine
	cout << "Please enter the lowest temperature of the day: ";
	cin >> dayTemp[day][1+1]; // woops! array access out of bounds 


As for how to pass arrays to functions
1
2
3
4
5
6
7
void Highest(int temperature[5][2])
{
	for (int )
}

// call it like so:
Highest( dayTemp );
Last edited on
Alright, Ill make some changes and get back.
Last edited on
Its not exactly a function I want it to pass to, but another array, to store the 5 average temperatures that the user keyed in above to compare and get the highest average temperature among them.

Its not exactly a function I want it to pass to, but another array, to store the 5 average temperatures that the user keyed in above to compare and get the highest average temperature among them.

That's actually much easier than you think it is.
1
2
3
for( int i{}; i < 5; i++ ) {
    averageTemp[i] = /* average */
}
i see, ill make some changes then, thanks a lot for your time.
Thanks, done a bit more research on my own and got it done, below is my full program, in case anyone comes up with the same problem, I included some comments to make it easier to understand I guess? Hope it helps in some way. Here it 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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <iostream>
using namespace std;

float dayTemp[5][2]; // 5 days, 2 temperatures to be entered
float averagedaytemp[5]; // 5 datas to be passed from dayTemp to averagedaytemp

float temperature = 0; // initialise temperature variable
float Highest(float averagedaytemp[5]); // calls to main function to let it know there is function called Highest()

int main()
{
	for (int day = 1; day < 6; day++) // 5 days to record
	{
		int temp=0 ; // initialise the temp variable

			cout << "Please enter the highest temperature of the day: "; // first temp record
			cin >> dayTemp[day][temp];

			cout << "Please enter the lowest temperature of the day: "; // second temp record
			cin >> dayTemp[day][temp + 1];
			cout << endl;

			averagedaytemp[day] = (dayTemp[day][temp] + dayTemp[day][temp + 1]) / 2; // calculate average of temp
			cout << "The average temperature of day " << day << " is: " << averagedaytemp[day] << endl; // print out the averagetemp of the day
			cout << endl << endl;

			Highest(averagedaytemp); // call function Highest()

	}
	cout << "The highest average temperature between these 5 days are: " << Highest(averagedaytemp) << endl; // print out the data of Highest() function

	system("pause");
	return 0;
}


float Highest(float averagedaytemp[5]) // function to calculate highest averagedaytemp
{
	float max = 0; // initialise max variable to compare maximum averagedaytemp
	for (int i = 1; i < 6; i++)
	{
		if (averagedaytemp[i] >= max) // to overwrite max with the data of averagedaytemp because its > 0 (max)
			max = averagedaytemp[i];
	}
	return max; // return the data that was entered into max
}



Topic archived. No new replies allowed.