Thread: Please help me with my c++ homework!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25

    Please help me with my c++ homework!

    . Your program must include a main program that opens an input file, a function that reads the content of the file into a partially filled array, and a function that computes and returns the standard deviation of the array’s content. You must use the formula for standard deviation that is presented in the textbook’s statement of the project. The main program must display the resulting standard deviation on the screen.

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    float average (float vec[], int size);
    float deviation( float sum, float average,float N );
    float sum (float vec[], int size);
    
    int main ()
    {
    	ifstream inputfile;
    	float N;
    	int size = 0;
    	const int Num_List = 1000;
    	float summ, avg;
    
    	double List[Num_List];
    
    	inputfile.open("program8.txt");
    	if(inputfile.fail())
    	{
    		cout << "Error: Could'nt open program8.txt" << endl;
    		exit (EXIT_FAILURE);
    	}
    
    	for (int count=0; count < Num_List; count++)
    		inputfile >> List[count];
    
    
    
    	float standDeviation [100];
    
    	inputfile >> N;
    	while(!inputfile.eof())
    	{
    		standDeviation[size] = N;
    		size++;
    		inputfile >> N;
    	}
    
    	summ = sum (standDeviation, size);
    	avg = average(standDeviation, size);
    	
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(3);
     
    	cout << "Standard Deviation: " << deviation(summ, avg, size) << endl;
    
    	inputfile.close();
    	cin.get();
    	cin.get();
    	return 0;
    }
    
    //-----------------------------------------------------------------------------
    // sum function
    //-----------------------------------------------------------------------------
    
    float sum (float vec[], int size)
    {
    	float sum;
    	sum = 0.0;
    	for (int N = 0; N < size; N++)
    	{
    		sum +=vec[N] ;
    	}
    
    
    	return sum;
    }
    
    //-----------------------------------------------------------------------------
    // average function
    //-----------------------------------------------------------------------------
    
    float average (float vec[], int size)
    {
    	float sum, average;
    	sum = 0.0;
    	for (int i = 0; i < size; i++)
    		sum += vec[size];
    
    	if (size == 0)
    		average = 0.0;
    	else
    		average = sum / size;
    
    	return average;
    }
    
    
    //-----------------------------------------------------------------------------
    // deviation function
    //-----------------------------------------------------------------------------
    #include <cmath>
    float deviation(float sum, float average, float N)
    {
    	double S;
    	double U;
    	
    	
    	U =  ( sum -   average ) * ceil (2);
    	S = (sqrt (U / N));
    
    
    	return S;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    So what part are you having problems with?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    it tells me on the cout for standard deviation that it is a conversion from an int to a float?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by dmeyers81 View Post
    it tells me on the cout for standard deviation that it is a conversion from an int to a float?
    So is Size an integer or a float, third parameter todeviation? Your code implies both.

    Tim S.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    i figured it out!! i appreciate it though thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help about homework
    By agathery in forum C Programming
    Replies: 27
    Last Post: 05-19-2010, 09:17 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM