How to make function from this coding

I have coding that needs to find the many values such as highest, smallest, standard deviation, and many more. I was thinking that would the coding be easier to understand if I do it by function? This is my coding :

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
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string input; 
	ifstream nameFile("data.txt"); 
	if (!nameFile)
		return (cout << " ERROR : cannot open file.\n"), 1; 

	while (getline(nameFile, input)) 
			cout << input << endl;

	nameFile.clear();   
	nameFile.seekg(0);  

	int a {}, b {}, c {}, d {}, e {};
    nameFile >> a >> b >> c >> d >> e;

    int highest_radius = c , smallest_radius = c;
    int highest_area = d , smallest_area = d;
    int highest_diameter = e , smallest_diameter = e;

    for (int a {}, b {}, c {}, d {}, e {}; nameFile >> a >> b >> c >> d >> e; )
    {

		if (c > highest_radius)
			highest_radius = c;

		if (d > highest_area)
			highest_area = d;

		if (e > highest_diameter)
			highest_diameter = e;


        if (c < smallest_radius )
			smallest_radius = c;

		if (d < smallest_area)
			smallest_area = d;

		if (e < smallest_diameter)
			smallest_diameter = e;

    }

        std::cout << " The highest value for radius is " << highest_radius << '\n';
        std::cout << " The highest value for area is " << highest_area << '\n';
        std::cout << " The highest value for diameter is " << highest_diameter << '\n';

        std::cout << " The smallest value for radius is " << smallest_radius << '\n';
        std::cout << " The smallest value for area is " << smallest_area << '\n';
        std::cout << " The smallest value for diameter is " << smallest_diameter << '\n';


        nameFile.clear();   
        nameFile.seekg(0);  

        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int quantity = 0;

        while (nameFile >> a >> b >> c >> d >> e)
    {
        sum1 += c ;
        sum2 += d ;
        sum3 += e ;
        ++quantity;
    }
        if (quantity > 0)
    {
        const double average_radius = (double) sum1 / quantity;
        const double average_area = (double) sum2 / quantity;
        const double average_diameter = (double) sum3 / quantity;

        std::cout << " The average for radius is : " << average_radius << '\n';
        std::cout << " The average for area is : " << average_area << '\n';
        std::cout << " The average for diameter is : " << average_diameter << '\n';
    }

}

Should I do it in function? If I do it in function, do I have to make the for loop and while loop for every function ?
Last edited on
I was thinking that would the coding be easier to understand if I do it by function?
It would make sense to put the whole loop into a function but not the calculations.

Creating function is good when you feel like repeating the same code with just small changes. Like:
1
2
3
        std::cout << " The highest value for radius is " << highest_radius << '\n';
        std::cout << " The highest value for area is " << highest_area << '\n';
        std::cout << " The highest value for diameter is " << highest_diameter << '\n';
But then the question is: Does the gain justify effort?
If you know radius then you can immediately infer diameter and area. You don't need to find the minimum, maximum, average or standard deviation separately.

The most efficient method wouldn't effectively duplicate information.
to find the many values such as highest, smallest, standard deviation, and many more


I think you're falling into the trap of 'design as code' rather than 'design before code'.

When programming, the first thing you should do is the program design. Once you have the design, then you start coding from the design - in small chunks so that you compile frequently and test before coding another chunk of say no more than about 20 lines. Once you're completed the code as per the design, then the program should work OK. It it doesn't then either there's a design issue or in your last chunk of code.

In the overall time taken to produce a working program, a 'rule of thumb estimate' is:

design - 40% - 50%
testing/debugging 10% - 20%
coding - no more than 50% of the overall time

Many people learning to program think that design time is either wasted time or not needed. For anything other than trivial programs, this is not right. Design time is often the most valuable productive time. When I did my degree many, many, many years ago, we had to first produce a program design - which was marked - before we wrote the code.
for this, one function actually makes more sense to me, because you would otherwise sort the data a bajillion times or have a bunch of functions that only work right when the input is sorted.

here is a rough cut at it, taken from some C I wrote in college. I changed the pointers to vectors but did not clean it up fully, and hopefully I did not break it, but you get the idea...
result should use the enum, eg result[low], but the enum was added afterwards for the consumer to use..
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
enum {mean, median, stddev, high, low};
vector<double> stats(const vector<double> &m)
{
	/*mean, median, stddev, high, low */
	int dx;
	vector<double> tmp{m};
	vector<double> result(5);	
	sort(tmp.begin(), tmp.end()); 	
	//mean
	result[0] = 0; 
	for(dx = 0; dx < m.size(); dx++)
		result[0] += m[dx];
	result[0] /= m.size();	
	//median 
	result[1] = tmp[m.size()/2.0];	
	//stddev 
	result[2] = 0;
	for(dx = 0; dx < m.size(); dx++)
		result[2] += ((m[dx]- result[0])*(m[dx]- result[0]));
	result[2] /= (m.size()-1);
	result[2] = sqrt(result[2]);
	//high and low
	result[3] = tmp[m.size()-1];
	result[4] = tmp[0];	
	return(result);	
}


now, if you have more stuff that isn't tied to picking off the low hanging fruit (the above is really the most basic stuff and easy to get info) additional functions will become necessary.
Last edited on
Topic archived. No new replies allowed.