Standard Deviation Function

This is a discussion on Standard Deviation Function within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <fstream> #include <cmath> using namespace std; const int N = 1000; void pass_array(double array[N]); ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    Standard Deviation Function

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    const int N = 1000;
    void pass_array(double array[N]);
    
    
    int main()
    {
    	double x_bar, average();
    	double s_array[N], sam(0), s;
    	pass_array(s_array);
    
    	x_bar = average();
    
        for (int i=0; i<=5; i++)
    	{
    		sam += pow((s_array[i] - x_bar),2);
    	}
    
    	s = sqrt(sam/(i-1));
    
    	cout << "sam=:" << sam << endl;
    	cout << "s=" << s << endl;
    	cout << "i=" << i <<endl;
    
    	return 0;
    }
    
    double average()
    {
    	double av_array[N], sum(0), average;
    	pass_array(av_array);
    
    	for (int i=0; i<=5; i++)
    	{
    		cout << av_array[i] << endl;
    	}
    	for (int j=0; j<=(i-1); j++)
    	{
    		sum += av_array[j];
    	}
    
    	average = sum/j;
    	return average;
    }
    
    void pass_array(double array[N])
    {
    	string filename;
    	ifstream data;
    	int count(0);	
        	
        cout << "What is the filename where the data is located?" << endl;
    	cin >> filename;
    
    	data.open(filename.c_str());
    	if (data.fail())
    	{
    		cout << "Error opening file, check file status and try again." << endl;
    	}
    	else
    		while(count < (N-1) && !data.eof())
    		{
    			data >> array[count];
    			count++;
    		}
    	       
    }
    I need to call both the average and the standard deviation functions at different times, can anyone tell me how to write this so it doesn't ask me the name of the file twice.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Pass the filename as a parameter.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    How do I do that, I'm unfortunately terrible at this

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Like
    void pass_array(double array[N], string filename)

    Move the code you presently have in pass_array to prompt for a filename, and move it to main.
    Then pass that as a parameter to whatever functions need it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21