My Code Feels A Little "Unpolished," Any Suggestions?

Howdy guys,

I just had a project for my first year CompSci class where we had to design a
program that would generate a list of random numbers and then calculate their
standard deviation.

We haven't covered arrays yet, which meant we had to come up with a way to
calculate standard deviation without recording the values.

There were also the following predefined functions we were required to use:

1
2
3
4
5
6
7
8
void getParameters(int& seed, int& n, int& lower, int& upper);
// get the value for seed, number of random integers, lower and upper bound for those random integers.
int getNum(int lower, int upper);
// Generate a random integer between the lower and upper bound and return the integer.
void update(int num, double& sum1, double& sum2);
// update sum of values and sum of value squared
void output(double avg, double std);
//Output the two numbers. 


I'm not sure whether we could add functions or not, but I did.

Anyways, this is the code that I came up with. It works, although the
Standard Deviation it produces is usually .01 or so off from the "real"
STDEV.

However, the code feels a little, unpolished, to me. I feel I could have coded
it cleaner.

Does anyone have any suggestions on how I could have cleaned this up?

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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

void getParameters(int& seed, int& n, int& lower, int& upper);
// get the value for seed, number of random integers, lower and upper bound for those random integers.
int getNum(int lower, int upper);
// Generate a random integer between the lower and upper bound and return the integer.
void update(int num, double& sum1, double& sum2);
// update sum of values and sum of value squared
void output(double avg, double std);
//Output the two numbers.
int average(int n, int sum);
//Calculates the average of a sum of n integers
void average(int n, double sum1, double& avg);
//Calculates the average of the random values
void standardDeviation(double avg, int n, double sum1, double sum2, double& std);
//Calculates the standard deviation
void resetAll(double& avg, double& sum1,double& sum2, double& std, int& count, int& lower, int& upper, int& n, int& seed);
//Resets all variables so the program may be run again

int seed;

int main(){
    cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
    int count = 0, lower, upper, n;
    double avg, sum1, sum2, std;
    char repeat = 'y';
    do {
        srand(seed);
        getParameters(seed, n, lower, upper);
        cout << "The numbers generated are: " << "\n";
        do {
            update(getNum(lower, upper), sum1, sum2);
            }
            while(++count < n);
            average(n, sum1, avg);
            standardDeviation(avg, n, sum1, sum2, std);
            output(avg, std);
            resetAll(avg, sum1, sum2, std, count, lower ,upper ,n, seed);
            cout << "\n" << "Would you like to continue? (y or n): ";
            cin >> repeat; }
        while(repeat == 'y' || repeat == 'Y');
}

void getParameters(int& seed, int& n, int& lower, int& upper) {
     cout << "\n" << "Seed for the random number generator: ";
     cin >> seed;
     cout << "\n" << "The size of the sequence: ";
     cin >> n;
     cout << "\n" << "Lower and upper bound for the random numers: ";
     cin >> lower;
     cout << " ";
     cin >> upper;}
     
int getNum(int lower, int upper){
    int value = ((rand() % (lower - upper + 1)) + lower);
    cout << value << " ";
    return value;  }
    
void update(int num, double& sum1, double& sum2) {
     sum1 = num + sum1;
     sum2 = (num * num) + sum2;  }

void average(int n, double sum1, double& avg) {
    avg = (sum1 / n);  }
    
void standardDeviation(double avg, int n, double sum1, double sum2, double& std) {
     std = sqrt((sum2 -(2*avg*sum1) + (n*avg*avg))/n); }
    
void output(double avg, double std) {
     cout << "\n" << "Average = " << avg 
          << " Standard deviation = " << std; }
          
void resetAll(double& avg, double& sum1,double& sum2, double& std, int& count, int& lower, int& upper, int& n, int& seed) {
     count = 0, lower = 0, upper = 0, n = 0;
     avg = 0, sum1 = 0, sum2 = 0, std = 0, seed = 0;}
Last edited on
Do you know about classes and structs? You could simplify/clean this up by making this a class instead of a series of global functions.

Other than that, the main thing I noticed was that you really like to pass by reference, which I usually try to avoid whenever possible. You should prefer to return variables when you can, as it's a lot more flexible.

That is..

1
2
3
4
5
6
7
// what you have:
void average(int n, double sum1, double& avg) {
    avg = (sum1 / n);  }

// what would be better:
double average(int n,double sum1) {
    return (sum1 / n);  }



EDIT:

I'm also not a fan of your indentation/brace style, but that's just a preference thing I guess.
Last edited on
It works, although the Standard Deviation it produces is usually .01 or so off from the "real" STDEV.
¿How are you calculating the "real" STDEV? It may be dividing by n-1 instead of n

You forgot to initialize some variables, and you are calling srand before obtain the seed.

Do you know about classes and structs? You could simplify/clean this up by making this a class instead of a series of global functions.

Other than that, the main thing I noticed was that you really like to pass by reference, which I usually try to avoid whenever possible. You should prefer to return variables when you can, as it's a lot more flexible.


No, we haven't learned anything about classes and structs yet.

Most of the passing by reference was as a requirement for the assignment
since we just learned passing by reference, but you make a good point. Thank you.


How are you calculating the "real" STDEV? It may be dividing by n-1 instead of n

You forgot to initialize some variables, and you are calling srand before obtain the seed.


Lol, yeah, I realized I called srand too early about 5 minutes after I had emailed
the assignment to my TA :-D.

I'm not sure how the real STDEV was calculated, I found a site online where you put
numbers in and it gives you the avg and stdev, mine was just a hair off of theirs.

As far as initializing variables, I feel like a noob now, what do you mean by that?

Thanks.
1
2
3
4
double avg, sum1, sum2, std; //you should set sum1 and sum2 to 0
//...
update(getNum(lower, upper), sum1, sum2); //because they are used here as accumulators
//sum1 = num + sum1 
well dunno wat seed is but the following program seems to be shorter and more efficiet for calculating the standard deviation for a set of random numbers...
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
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
int k,i=1,n,l;
float s=0,m=0,stdev,mean;
clrscr();
//write a program that genrates 10 random numbers between 0-99 and prints the standard deviation of those numbers.....
randomize();
printf("enter the upper limit for the random function : ");
scanf("%d",&l);
printf("The size of the sequence : ");
scanf("%d",&n);
printf("The random numbers generated are : \n");
while(i<=n)
{k=random(l+1);
printf("%d\t",k);
m+=k;
s+=k*k;
i++;
}
mean=m/n;       //the keypoint here is the apporach or method u use to calculate standarddev
if((s-n*m*m)>0)
stdev=sqrt((s-n*m*m)/n);
else
stdev=sqrt((n*m*m-s)/n);
printf("average : %f\n",mean);
printf("standard deviation : %f\n",stdev);
getch();
}
Last edited on
Topic archived. No new replies allowed.