How can I return both values in my function? PLEASE HELP!!!

HERE IS MY CODE:
Im having problems problems returning both values from my functions and writing them to a file

// INCLUDES:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>

// USING
using namespace std;

// FUNCTION PROTOTYPES:
double max_min_values (double value[], int N);
double sum_average (double value[], int N);
double std_deviation ( double value[], int N,double avg);

//////////////////////////////////////////////////////////
int main()
{
ifstream readstream;

double data[1000];
readstream.open("randomdata.txt");

if(!readstream){
cout << "\nCould not find file";

return(0);
}

for(int i=0; i<1000; i++){
readstream >> data[i];
cout << "data [ " << i << " ] " << data[i] << endl;

}
readstream.close();


double max_min = max_min_values(data,1000);

double avg = sum_average(data,1000);

double deviation = std_deviation(data,1000,avg);



ofstream writestream;

writestream.open("statistics_info.txt");

writestream << max_min << endl;



writestream << avg << endl;


writestream << deviation << endl;

writestream.close();






system("PAUSE");
return(0);

} // end main





//////////////////////////////////////////////////////////
// FUNCTION: functionname
// PURPOSE:
// INPUT PARAMETERS (include: 1. Type, 2.Ref or Val, 3.Description):
// RETURNS:
// MODIFIES(for globals or parameters passed by reference):
//////////////////////////////////////////////////////////
double max_min_values (double value [], int N)
{
double values;
double maxi = value[0];
double mini = value[0];
for(int i=0; i<N; i++){
if ( maxi < value[i] )
maxi = value[i];
if( mini > value[i] )
mini = value[i];


}

cout << "\nThe maximum value is " << maxi << endl;
cout << "\nThe minimum value is " << mini << endl;
values = maxi, mini;

return(values);





} // max_min_values






//////////////////////////////////////////////////////////
// FUNCTION: functionname
// PURPOSE:
// INPUT PARAMETERS (include: 1. Type, 2.Ref or Val, 3.Description):
// RETURNS:
// MODIFIES(for globals or parameters passed by reference):
//////////////////////////////////////////////////////////
double sum_average(double value[], int N)
{
double sum = 0;
double average;
for(int i=0; i<N; i++){
sum = sum + value[i];

average = sum/1000;

}
cout <<"\nThe sum of the values is " << sum << endl;
cout <<"\nThe average of the values is " << average << endl;
return (average);
return (sum);


} // end functionname





//////////////////////////////////////////////////////////
// FUNCTION: functionname
// PURPOSE:
// INPUT PARAMETERS (include: 1. Type, 2.Ref or Val, 3.Description):
// RETURNS:
// MODIFIES(for globals or parameters passed by reference):
//////////////////////////////////////////////////////////
double std_deviation(double value[], int N, double avg)
{


double sum = 0;
double deviation;

for(int i=0; i < N; i++){
sum = sum + pow((value[i]- avg),2);

}
deviation = sqrt(sum/(1000-1));

cout << "std deviation is " << deviation << endl;

return(deviation);



} // end functionname
Last edited on
closed account (10oTURfi)
1
2
return (average);
return (sum);

Function is terminated with first return program execution encounters, therfore in this case it will not return sum.
It actually doesn't return both max and min values either. I think im supposed to use pass by reference but im having trouble doing it.

can you please help
closed account (D80DSL3A)
Yes, pass by reference will allow you to modify the values of as many variables as you wish. For your max_min_values() function try:
void max_min_values (double value [], int N, double& mini, double& maxi)
Declare two variables in main then pass them to the function when you call it:
1
2
3
double max;
double min;
max_min_values (data, 1000, min, max);

This will work for your sum_average() function also.
closed account (zb0S216C)
What about a POD structure?

1
2
3
4
5
6
struct ReturnedData
{
    double Max, Min;
};

ReturnedData max_min_values( );


Wazzak
THANK YOU VERY MUCH "fun2code" that really helped a lot I really appreciate it. I was on this for hours but I understand it now.
thanks again
@Framework thanks for the help but we havent learned that yet so I dont think we would be allowed to use it for the assignment
thanks again
You've learned references but not structures? That seems astonishingly unlikely. Perhaps you just missed it.
Topic archived. No new replies allowed.