sorting array C++

Use functional decomposition to calculate the statistic for a set of 30 data scores (integers between 1 to 100), that will generated randomly.

Find: MAX,MIN,Average,standard deviation.

Here is the function.
1 - function to randomly generate score and assign to array
2 - function to calculate max and min
3 - function to calculate average
4 - function to calculate standard deviation
5 - function to sort array in ascending order.
6 - function to display all elements in array
7 - function to display max,min,average and standard deviation.

Here is my code.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <string>
using namespace std;

#define SIZE 30
//Function prototype
void getData(int arr[]);
int findMax(int arr[]);
int findMin(int arr[]);
double findAverage(int arr[]);
void printArray(const int arr[]);
void sortArray(const int arr[]);
void printResult(int MAX,int MIN, double average);

int main ()
{
    int arr[SIZE];
    int MAX,MIN;
    double average,deviation;
    
    //Call functions
    getData(arr);                       
    MAX=findMax(arr);                // assign max
    MIN=findMin(arr);                // assign min
    average = findAverage(arr);      // assign average
    printArray(arr);                 // print all element in array
    sortArray(arr);                  // print sorting array
    printResult(MAX,MIN,average);    // print max,min,average
    
    cout<<endl<<endl;
    system("PAUSE");
    return 0;
}
// Randomly generate value and assign to array
void getData(int arr[])
{
     unsigned seed = time(0);
     for (int i=0;i<SIZE;i++)
     {
         arr[i] = rand()%100;
     }
}

// Find Maximum
int findMax(int arr[])
{
    int MAX=arr[0];
    for (int i=0;i<SIZE;i++)
    {
        if( arr[i]>MAX)
        {
            MAX=arr[i];
        }
    }
    return MAX;
}
// Find Minimum
int findMin(int arr[])
{
    int MIN=arr[0];
    for (int i=0;i<SIZE;i++)
    {
        if( arr[i]<MIN)
        {
            MIN=arr[i];
        }
    }
    return MIN;
}
// Find average
double findAverage( int arr[])
{
       int sum=0;
       double average;
       for( int i=0;i<SIZE;i++)
       {
            sum +=arr[i];
            average = static_cast<double>(sum)/SIZE;
       }
       return average;
}
// print all elements in array
void printArray(const int arr[])
{
     int i=0;
     int j=0;
     
     cout <<"Oroginal array: " <<endl;
     for (i=0,j=0;i<SIZE;i++,j++)
     {
         if (j==5){
             j=0;
             cout<<endl;
         }
         cout<<setw(5)<<arr[i];
     }
}
// sorting array
void sortArray(const int arr[])
{
     int i,j,temp;
     
     for (i=0;i<SIZE; i++)
     {
         for (j=0;j<SIZE; j++)
         {
             if( arr[i]< arr[j])
             {
                 temp = arr[i];
                 arr[i] = arr[j];
                 arr[j] = temp;
             }
         }
     }  
     cout <<"Array in ascending orders : "<<endl;
     for ( i=0;i<SIZE;i++
     {
         cout<<arr[i]<<endl;
     }
}
// print the results
void printResult(int MAX,int MIN, double average)
{
     cout<< "Maximum is "<<MAX<<endl;
     cout<< "Minimum is "<<MIN<<endl;
     cout<< "Average is "<<average<<endl;
}


My sorting function doesn't run.
error on line : 114

it looks ok, but i dont know why it doesnt run.
Any help or suggestion?

would be easier if i combine function 2,3,4?

also, i left out the function for standard deviation. Not sure how i should approve it. Any suggestion on the function?

Thanks in advance for any ideas or suggestions.

(1)
Your bubble sort is not correct.
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/

The main problem is that there is no boundary between 'yet to be sorted' and 'already sorted', so your values keep getting swapped from sorted and not sorted. Don't swap sorted things back into the unsorted stuff.

[edit]
Either get rid of one of the loops, or make sure to make j start at i instead of zero and change the condition on line 111 to arr[i] > arr[j].
[/edit]

Also, I hope lines 119 through 123 are there to help you figure out what is going wrong, and won't be there when you are done.

(2)
No, it would not be easier. More efficient, but that is not important here. Keep them separate.

(3)
Google "population standard deviation" for how to calculate. (It's pretty simple, and you'll need your average value calculated in step 3 to compute it. You'll also need to #include <cmath> and use std::sqrt().

Good luck!
Last edited on
Thanks for the help.
instead of for loop, i do do-while loop and it work.

Thanks!
Last edited on
Topic archived. No new replies allowed.