return to tranceaddict TranceAddict Forums Archive > Main Forums > Chill Out Room

 
morte code for you topcoders out there if you can help
View this Thread in Original format
Trancealot
Write a program that reads a list of floating point numbers from a file and prints out the number of elements, the sum, average, standard deviation, minimum value, maximum value, and median value. Your program should use, at a minimum, the following functions:

double *max( double array[], int length ); //returns a pointer to the largets element in the array
double *min( double array[], int length); //returns a pointer to the smallest element in the array|
double mean( double array[], int length); //returns the average value of the elements in the array
double median( double array[], int length); //returns the median value of the array
double std_dev( double array[], int length); // returns the standard deviation of the elements in the array


I will do some work obviously in an hour or 2 but when I post can you give me some input for help
k.k.d.
*yawn*

back to coding my webserver


But seriously, methinks you guys need stop asking for ing answers, get off your lazy butts, and start working... I mean, those "program my homework for me" threads annoy me deeply... Life is not about someone doing everything for you (unless you are a crafty manipulative bastard, that is)...

That being said, your problem description does't even mentions how the aforementionned numbers are stored in the file... :(
tranceaholic
well i am not gonna right the code for ya..but i can help u with ideas of u want..u read the input into the array..for the max set the first element of the array to max and go through the array using a for loop to see if a number is greater than the number u have set if it is then max will be equal that number..same for min..for mean just do a counter that goes through the array and gets the number of elements in it..then divide this number by the sum of array elements..for median set an element to be the median and use a left and right counter..if the number of left elements equal to right elements then this is the median..if not increment by 1...there is more efficient ways for median but thats off the top of my head right now...good luck
Trancealot
my bad..You are right. I am really going to use my brain and let this thread go away..question: are there any really good C++ sites out there to give me some help you know of about arrays pointers..etc..
tranceaholic
try www.cplusplus.com good site
Trancealot
quote:
Originally posted by tranceaholic
well i am not gonna right the code for ya..but i can help u with ideas of u want..u read the input into the array..for the max set the first element of the array to max and go through the array using a for loop to see if a number is greater than the number u have set if it is then max will be equal that number..same for min..for mean just do a counter that goes through the array and gets the number of elements in it..then divide this number by the sum of array elements..for median set an element to be the median and use a left and right counter..if the number of left elements equal to right elements then this is the median..if not increment by 1...there is more efficient ways for median but thats off the top of my head right now...good luck


thnak you for this help..I am coding right now and in a few hours I will show what I got
Photo_bot_2k1
lets open a "get help on your cs hw" forum!!!!!!!

rofl!:rolleyes: :rolleyes: :D
Trancealot
ok..here is the finished product
only things: you need to create a file called new.txt and insert the amount of numbers you have set(#define MAX __)

when you change the amount of numbers inside the txt file you must change #define MAX __

thanks trancealholic and whoever else helped me on my quest. :rolleyes:

[code]
#include
#include
#include
#include //allow use of pow and sqrt function
using namespace std; //rids the program of std::
void sort( double array[], int legnth); //sorts #'s
void inFile(double array[]); //prints numbers from the .txt. file
double *max(double array[],int legnth); //prints out max value in the array
double *min(double array[],int legnth); //prints out min value in the array
double mean(double array[],int legnth); //prints out mean value of array
double median(double array[],int legnth); //prints out median value of the array
double std_dev(double array[],int legnth); //prints out standard deviation of the array
#define MAX 4 //amount of values inside txt program

int main() //main
{
double *MaxValue;
double *MinValue;
cout<<"inputting data from a file called new.txt\n\n";
double array[MAX];
cout<<"1) the numbers in the text file that are put into an array are:\n";
inFile(array); // print function
cout< MaxValue = max(array,MAX);
MinValue = min(array,MAX);
cout<<"3) the maximum value of the array is: "<<*MaxValue;
cout< cout< cout<<"4) the minimum value of the array is: "<<*MinValue;
cout< cout< cout<<"5) the mean is : "< cout< cout< cout<<"6) the median is: "< cout< cout< cout<<"7) standard Deviation: ";
cout<< std_dev(array,MAX)< return 0;
}

void inFile(double array[]) //file print function
{
double num;
int i=0;
ifstream inFileNew;
inFileNew.open("new.txt");
if(!inFileNew){
cerr<<"file could not be opened"< exit(1);
}

while (! inFileNew.eof())
{
inFileNew>>num;
array[i]=num;
i++;
cout< }
inFileNew.close();
}

double *max(double array[],int legnth) //max function
{
double *maximum;
if(legnth>0){
maximum=&array[legnth-1];
}
else if(legnth<=0){
maximum=0;
}
return maximum;
}

double *min(double array[],int legnth) //min function
{
double *minimum;
if(legnth>0){
sort(array,legnth);
minimum=&array[0];
}
else if(legnth<=0){
minimum=0;
}
return minimum;


}

double mean(double array[],int legnth) //mean function
{
double sum=0,mean=0;
for(int i=0;i<=legnth;i++)
{
sum=sum+array[i];
}
mean=(sum/MAX);

return mean;

}

void sort( double array[], int legnth) //sort function
{
double hold;
for (int pass = 0; pass {
for (int j = 0; j {
if (array[j] > array[j + 1])
{
hold = array[j];
array[j] = array[j + 1];
array[j + 1] = hold;
}
}
}

cout<<"\n\n2) Array after sorting...\n";
for(int l = 0; l cout< cout< cout<

}

double median(double array[],int legnth) //mean function
{
double median;
median=array[legnth/2];
return median;
}


double std_dev(double array[],int legnth) //standard deviation function
{
double std_dev=0,average;
average = mean(array, MAX);
for(int i=0;i std_dev+=pow((array [i]-average),2);
std_dev/=(legnth-1);
std_dev=sqrt(std_dev);
return std_dev;
}




[code] :rolleyes:
tranceaholic
well what if the file contains less than 10 numbers..then ur code is ..ur code seems to work..didnt test it..if the file contains only 10 numbers other than that ur ed..if u want u can do error checking to see if there is 10 numbers and tell the user that the file is incorrect..but i think the main aim of the program is to make it flexible enough to work with any file..u can add a function that counts the number of elements in the file and use that in the rest of ur code...good luck ;)
CLICK TO RETURN TO TOP OF PAGE
 
Privacy Statement