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:
#include <iostream>
usingnamespace 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().
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 );
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 */
}
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:
#include "stdafx.h"
#include <iostream>
usingnamespace 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
}