### 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.
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 #include #include #include #include #include #include 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<MAX) { MAX=arr[i]; } } return MAX; } // Find Minimum int findMin(int arr[]) { int MIN=arr[0]; for (int i=0;i(sum)/SIZE; } return average; } // print all elements in array void printArray(const int arr[]) { int i=0; int j=0; cout <<"Oroginal array: " <

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.

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.