Please help with Errors in Program create 10 integer randomly then print average and standard deviation

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cmath>

using namespace std;

void fillArray(int a[], int size);

void printArray(int a[], int size);

int main()
{
const int size = 10;
int array[size];
fillArray(array, size);

void printArray(int a[], int size){

for(int i = 0; i<size; i++)

cout << array[i] << " ";
}

int sum = 0;

int sumSq = 0;

double mean, stdDev;

for (int i = 0; i < size; ++i) {

sum += array[i];

sumSq += array[i]*array[i];
}
mean = (double)sum/size;

cout << fixed << "Mean is " << setprecision(2) << mean << endl;

stdDev = sqrt((double)sumSq/size - mean*mean);

cout << fixed << "Std dev is " << setprecision(2) << stdDev << endl;

rturn 0;

}
Last edited on
You should edit your post to put code tags around your code.
http://www.cplusplus.com/articles/jEywvCM9/

> void printArray(int a[], int size){
You haven't finished declaring main yet.
C++ doesn't have nested functions.
Here's a start:

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

using namespace std;

void fillArray(int a[], int size);
void printArray(int a[], int size);

int main()
{
    const int size = 10;
    int array[size];
    
    fillArray(array, size);
    printArray(array, size);
    
    int sum = 0;
    int sumSq = 0;
    double mean, stdDev;
    
    for (int i = 0; i < size; ++i) {
        sum += array[i];
        sumSq += array[i]*array[i];
    }
    
    mean = (double)sum/size;
    cout << "Mean is: " << mean << '\n';
    
    stdDev = sqrt( (double)sumSq/size - mean * mean);
    cout << "Std dev is: " << stdDev << '\n';
    
    return 0;
    
}

void printArray(int array[], int size)
{
    for(int i = 0; i<size; i++)
        cout << array[i] << " ";
    
    cout << '\n';
    
    return;
}

void fillArray(int array[], int size)
{
    for(int i = 0; i<size; i++)
    {
        array[i] = 3 * i; // replace with a random number here
    }
    
    return;
}
1
2
3
4
5
6
7
8
9
void fillArray(int array[], int size)
{
    srand (time(NULL));
    
    for( int i = 0; i<size; i++)
        array[i] = rand() % 10 + 1;
    
    return;
}
Thank you, It works well with my rand() :
void fillArray(int array[], int size)
{
for(int i = 0; i<size; i++)
{
srand(time(0));
for(int i = 0; i<size; i++)
array[i] = (rand() % 99) + 1;
}
Topic archived. No new replies allowed.