Oct 22, 2011 at 5:23am UTC
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 Oct 22, 2011 at 7:34am UTC
Oct 22, 2011 at 6:07am UTC
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
Oct 22, 2011 at 9:27am UTC
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
Oct 22, 2011 at 9:28am UTC
@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
Oct 22, 2011 at 9:31am UTC
You've learned references but not structures? That seems astonishingly unlikely. Perhaps you just missed it.