PDA

View Full Version : C++ Guru's I have another one


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.

jojo
11-20-2002, 08:58 PM
Let's see if I can get this to display.



// INCLUDE FILES
#include <iostream>
#include <iomanip>
#include <cmath>


using namespace std;

// ================================================== ===========================
// CONSTANT DEFINITIONS
//
// ================================================== ===========================
// GLOBAL CONSTANTS/OBJECTS
//
// ================================================== ===========================
// FUNCTION PROTOTYPES
//
void 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];
int average;

GetTemperatures(Hourly_Temperatures, size);

average = Compute_Average_Temp(Hourly_Temperatures, size);

Display_Temperatures(Hourly_Temperatures, size, average);

return 0;
}




//Processing Section

// Fill the array
void 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;
while ((local < -50)||(local > 130))
{
cout <<"Bad input. Try again\n";
cout <<"Enter a temperature between -15 and 130 degrees\n";
cin >> local;
}
Temperatures[x] = 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 = sum/Num_Temperatures;

return average;
}

//Displays the results
void Display_Temperatures(int Temperatures[], int Num_Temperatures, int Average_Temp)
{

for (int i = 0; i < Num_Temperatures; i++)
{
cout<<"Hour "<<(i+1)<<" temp = "<<Temperatures[i]<<endl;
}

cout<<"\nThe average temp is "<<Average_Temp<<endl;

}

jojo
11-20-2002, 09:09 PM
That should be close. It's tough to write code in this little box.

It looks like your biggest problem was handling the return of the array. You probably haven't covered pointers yet but in C++ arrays are effectively passed by reference. What this means to you is that when you pass an array to a function, then modify the array in that function, you are actually modifying the original array outside of the called function eliminating the need to return it. Take a look at the code and see if that makes sense to you.

Brian
11-21-2002, 12:26 AM
That does make sense. Arrays reference the memory space rather than a pass by value that just points to the actual data. I didn't know it was automatic with arrays. I had been searching all over the last couple days trying to figure out how to get the data back and forth. It just didn't click in my head at all. Thanks a ton, that helped a bunch.


Sounds like i'm going to owe a couple of you guys a few beers when I get this class over with ;)

Jai SI
11-21-2002, 12:38 AM
God I wish I still remember C++. Looks real familiar though haha.

Pang
11-21-2002, 12:50 AM
i still think vb is harder than c++ but they both are confusing shiet

exciv2000
11-21-2002, 06:12 AM
Originally posted by TurboSi
i still think vb is harder than c++ but they both are confusing shiet

I'm totally the opposite, I think C++ is harder.

Genesis
11-21-2002, 06:49 AM
Originally posted by jojo
That should be close. It's tough to write code in this little box.

It looks like your biggest problem was handling the return of the array. You probably haven't covered pointers yet but in C++ arrays are effectively passed by reference. What this means to you is that when you pass an array to a function, then modify the array in that function, you are actually modifying the original array outside of the called function eliminating the need to return it. Take a look at the code and see if that makes sense to you.

good explanation. I have, I mean had, a great book on C++. I need to track my friend to get the author and title. it's a textbook worth having.

all programming languages are pure logic. what makes them distinct is the anotation. C++ is much more mathematical as vB is linguistic. I prefer the mathematical anotations. if you've taken basic (I did more than a decade ago) it was much more mathematical than vB nowadays.