How to put the sum, average, and deviation in functions

So I wrote this code to find the highest value, lowest value, sum, average, and standard deviation for a file but I don't know how to put them into functions instead of just everything in the main. (The numbers in the file are doubles.) Help me out please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream> 
using namespace std;
int main()
{

	
	ifstream data_stream;
	
	data_stream.open("data.txt");
	
	double data[1000];
	int size = 1000;
		
	for(int i = 0; i<1000; i++){
		data_stream >> data[i];
	}

	
    double max = data[0]; 
    int index1 = 0;
    for (int i = 1; i < size; i++)
    {
		if (data[i] > max)
		{
            max = data[i];
            index1 = i;
          }
	}
	cout << endl << "The largest value in this file is " << max << '.' << endl;
         
	
    double min = data[0]; 
    int index2 = 0;
    for (int i = 1; i < size; i++)
    {
		if (data[i] < min)
		{
            min = data[i];
            index2 = i;
          }
	}
	cout << "The smallest value in this file is " << min << '.' << endl;
    

	//////To find sum//////////
	double sum = 0;
	double avg;
	for (int i = 0; i<1000 != '\0'; i++)
	{
		sum = sum + data[i];
		avg = (double)sum/1000;
	}
	

	cout << "The sum of the numbers is " << sum  << '.' << endl;
	cout << "The average of the numbers is " << avg << '.' << endl;

	
	////Standard Deviation////
	double variance = (double)sum_squares / n - (mean * mean);
	double deviation = sqrt(variance);
	cout << "The standard deviation of the numbers in the file is "
		<< deviation << endl;


	data_stream.close();
        
                           
                            
	system("PAUSE");
	return(0);

} // end main
you define functions with a return type, the function name, its arguments and its internal logic.

An example would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double max (double* data, int size)
{
    int index1 = 0;
    double max;
    
    for (int i = 1; i < size; i++)
    {
	if (data[i] > max)
	{
            max = data[i];
            index1 = i;
          }
    }
    cout << endl << "The largest value in this file is " << max << '.' << endl;
    return max; 
}


There's a couple of issues with this, though:

1. you're comparing doubles to ints; I don't think your compiler will like that.
2. you need to initialize max to some arbitrarily small number, so that you can be sure that at least one number in your list will be bigger; otherwise, you'll just return max.
3. you want to begin your loop counter (i) at 0, not 1.
4. the index1 value just goes away at the end of the function.

I didn't bother trying to compile this, so there may be some errors of my own, but hopefully this can get you started. Let us know if you need more help.
How do I fix the first problem you stated? I initialized max and everything, but idk how to fix the first part. I tried changing all the ints to doubles but that didn't work.
My code for standard deviation was:
in main
 
double StandardDeviation = StandardDeviation(data, Total, Mean);


outside of main:
double StandardDeviation(int data[], int Total, float Mean);

function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double StandardDeviation(int data[], int Total, float Mean)
{
	double StandardDeviation = 0.0;
	int Sum2 = 0;
        int Total = 1000;

 
	for (int i = 0; i < Total; ++i)
	{
		Sum2 += pow((data[i] - Mean), 2);
	}

	StandardDeviation = sqrt(Sum2 / (Total - 1));
	return StandardDeviation;
}


I changed the code a bit from the original but that compiles and gives the right answer as well.

I know one reason this doesnt work is because I didn't declare Mean in this function, but that's supposed to be in its own function so idk what to do now.
It looks like I was wrong; the comparison of data[i] to max is OK. Sorry about that.

You do have some undefined variables in the standard deviation part of your code, though. You'll need to get that fixed before it will compile.

Also: remember that you need to either define your subroutines before main(), or do a forward declaration (again before main). The simplest (though not necessarily recommended) way is to make sure that your main() function is last in the file (after all the subroutines).
First off, it would be easier to follow the dialogue in the thread if you'd reply to posts, rather than edit yours. The scientific method does not erase errors; it draws a line through them and moves on.

Now, as far as your SD function, if it needs a mean, then you might want to create a mean() function and use it within the SD function. You can create an empty function if you wish to test with.
I didn't edit my posts, I replied.

So I tried to use the mean function in the standard deviation function but it's giving me the same error of "cannot convert parameter 1 from 'int []' to 'double []' " that I got in the main for the standard deviation function that I don't know how to fix.
OK...can you post your code again? I want to look at the mean function.
Topic archived. No new replies allowed.