Brian
11-20-2002, 05:36 PM
Man programming doesn't seem to be my thing here. I've been working on this problem for a couple days and have no idea where to go with it. I've been reading like crazy and I think my brain has shut down completely. Here is the code I have so far
// INCLUDE FILES
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//================================================== ===========================
// CONSTANT DEFINITIONS
//
//================================================== ===========================
// GLOBAL CONSTANTS/OBJECTS
//
//================================================== ===========================
// FUNCTION PROTOTYPES
//
int GetTemperatures(int Temperatures[], int Num_Temperatures); //fills the array with data from user
int Compute_Average_Temp(int Temperatures[], int Num_Temperatures);//calculates average temperature
void Display_Temperatures(int Temperatures[], int Num_Temperatures, int Average_Temp);
const int size = 24;
//************************************************** ***************************
// BEGINNING OF PROGRAM CODE
//************************************************** ***************************
int main()
{
//Declaration/Initialization/Instantiation Section
int Hourly_Temperatures[size] = {-51};
int temp[24];
int average;
int result = 0;
result = GetTemperatures(temp, size);
cout<<result;
result = Compute_Average_Temp(temp, size);
cout<<result;
Display_Temperatures(temp, size, average);
return 0;
}
//Processing Section
// Fill the array
int GetTemperatures(int Temperatures[], int Num_Temperatures)
{
int local;
for(int x = 0; x < Num_Temperatures; x++)
{
cout <<"Enter a temperature between -15 and 130 degrees\n";
cin >> local;
if ((local < -50)||(local > 130))
{
cout <<"Bad input. Try again\n";
x--;
int temp[24];
return temp[24] = local;
}
}
}
//computes the average temp
int Compute_Average_Temp(int Temperatures[], int Num_Temperatures)
{
int average = 0;
int sum = 0;
for (int i = 0; i < Num_Temperatures; i++)
sum += Temperatures[i];
average = int(sum)/ Num_Temperatures;
return average;
}
//Displays the results
void Display_Temperatures(int Temperatures[], int Num_Temperatures, int Average_Temp)
{
}
Here is the homework problem
1) In function main declare an array int Hourly_Temperatures[24] that will be used to hold one temperature for each hour of the day.
2)Pass the hourly_Temperature array to a function GetTemperatures(int Temperatures[], int Num_Temperatures) This function must prompt for each temperature then verify that it lies between minus 50 degrees and 130 degrees.
3)Pass the filled array to a function Compute_Average_Temp(int Temperatures[], int Num_Temperatures). This function should compute the average temp then returns it to the calling function.
4)finally, pass your array and the computed average temp to another function, Display_Temperatures[], int Num_Temperatures, int Average_Temp), which displays the values for the high temp, low temp, and average temp of the day.
main should look something like this.
void main()
{
do{
//call all three functions
}while // user wants to process more days of temps
}
I guess my main problem is figuring out how to pass the average temp from one function to another, and bringing everything together.
// INCLUDE FILES
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//================================================== ===========================
// CONSTANT DEFINITIONS
//
//================================================== ===========================
// GLOBAL CONSTANTS/OBJECTS
//
//================================================== ===========================
// FUNCTION PROTOTYPES
//
int GetTemperatures(int Temperatures[], int Num_Temperatures); //fills the array with data from user
int Compute_Average_Temp(int Temperatures[], int Num_Temperatures);//calculates average temperature
void Display_Temperatures(int Temperatures[], int Num_Temperatures, int Average_Temp);
const int size = 24;
//************************************************** ***************************
// BEGINNING OF PROGRAM CODE
//************************************************** ***************************
int main()
{
//Declaration/Initialization/Instantiation Section
int Hourly_Temperatures[size] = {-51};
int temp[24];
int average;
int result = 0;
result = GetTemperatures(temp, size);
cout<<result;
result = Compute_Average_Temp(temp, size);
cout<<result;
Display_Temperatures(temp, size, average);
return 0;
}
//Processing Section
// Fill the array
int GetTemperatures(int Temperatures[], int Num_Temperatures)
{
int local;
for(int x = 0; x < Num_Temperatures; x++)
{
cout <<"Enter a temperature between -15 and 130 degrees\n";
cin >> local;
if ((local < -50)||(local > 130))
{
cout <<"Bad input. Try again\n";
x--;
int temp[24];
return temp[24] = local;
}
}
}
//computes the average temp
int Compute_Average_Temp(int Temperatures[], int Num_Temperatures)
{
int average = 0;
int sum = 0;
for (int i = 0; i < Num_Temperatures; i++)
sum += Temperatures[i];
average = int(sum)/ Num_Temperatures;
return average;
}
//Displays the results
void Display_Temperatures(int Temperatures[], int Num_Temperatures, int Average_Temp)
{
}
Here is the homework problem
1) In function main declare an array int Hourly_Temperatures[24] that will be used to hold one temperature for each hour of the day.
2)Pass the hourly_Temperature array to a function GetTemperatures(int Temperatures[], int Num_Temperatures) This function must prompt for each temperature then verify that it lies between minus 50 degrees and 130 degrees.
3)Pass the filled array to a function Compute_Average_Temp(int Temperatures[], int Num_Temperatures). This function should compute the average temp then returns it to the calling function.
4)finally, pass your array and the computed average temp to another function, Display_Temperatures[], int Num_Temperatures, int Average_Temp), which displays the values for the high temp, low temp, and average temp of the day.
main should look something like this.
void main()
{
do{
//call all three functions
}while // user wants to process more days of temps
}
I guess my main problem is figuring out how to pass the average temp from one function to another, and bringing everything together.