Cpp issue to get the median. Any idea? #include <iostream> #include <cstdlib> #i
ID: 3792851 • Letter: C
Question
Cpp issue to get the median. Any idea?
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <time.h>
#define MAX 10
using namespace std;
int ranged_rand(int min, int max) {
return min + (int)((double)(max - min) * (rand() / (RAND_MAX + 1.0)));
}
int main() {
int arr[MAX][MAX];
int x, y, z, temp;
int sum=0;
int maxVal=0, minVal=0;
double avg=0.0, median = 0.0;
srand (time(NULL));
// generate random values
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
arr[x][y] = ranged_rand(-MAX, MAX);
arr[x][y] = ranged_rand(-MAX, MAX);
}
}
// sort array from smallest value to the largest
for( x=0; x<MAX; x++){
for( y =0; y<MAX; y++){
for(z = 1; z<MAX; z++){
if(arr[x][y]> arr[y][z-1]){
temp = arr[x][y];
arr[x][y]=arr[y][z-1];
arr[y][z-1]=temp;
}
}
}
}
// print out the array original generated values and sorted values
cout<<"Sn. org: sorted: ";
for( x=0; x<MAX; x++){
for(y=0; y<MAX; y++){
cout<<setw(3)<<x<<": "<<
setw(4)<<arr[x][y]<<" ";
}
}
// calculate the sum of the all elements
for( x = 0; x < MAX; x++){
for( y = 0; y < MAX; y++){
sum = sum + arr[x][y];
}
}
cout<<" Sum is "<< sum<< " ";
avg = double(sum)/MAX;
cout<<"Average is "<< avg << " ";
// get minimum value and maximum value
minVal = arr[0][0];
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
if(arr[x][y]<minVal)
minVal = arr[x][y];
}
}
cout<<"Min is "<<minVal<<" ";
// get maximum value
maxVal = arr[0][0];
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
if(arr[x][y]>maxVal)
maxVal = arr[x][y];
}
}
cout<<"Max is "<<maxVal<<" ";
int aa[MAX*MAX];
int k = 0;
for (x=0;x<MAX;x++){
for (y=0;y<MAX;y++){
aa[k++] = arr[x] [y] ;
}
}
median = arr[k/2];
if (k%2 == 0){
median = (median + arr[k/2-1] )/2;
}
return 0;
}
Explanation / Answer
/*********************Please look into code changes****************/
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <time.h>
#define MAX 10
using namespace std;
int ranged_rand(int min, int max) {
return min + (int)((double)(max - min) * (rand() / (RAND_MAX + 1.0)));
}
int main() {
int arr[MAX][MAX];
int x, y, z, temp;
int sum=0;
int maxVal=0, minVal=0;
double avg=0.0, median = 0.0;
srand (time(NULL));
// generate random values
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
arr[x][y] = ranged_rand(-MAX, MAX);
arr[x][y] = ranged_rand(-MAX, MAX);
}
}
// sort array from smallest value to the largest
for( x=0; x<MAX; x++){
for( y =0; y<MAX; y++){
for(z = 1; z<MAX; z++){
if(arr[x][y]> arr[y][z-1]){
temp = arr[x][y];
arr[x][y]=arr[y][z-1];
arr[y][z-1]=temp;
}
}
}
}
// print out the array original generated values and sorted values
cout<<"Sn. org: sorted: ";
for( x=0; x<MAX; x++){
for(y=0; y<MAX; y++){
cout<<setw(3)<<x<<": "<<
setw(4)<<arr[x][y]<<" ";
}
}
// calculate the sum of the all elements
for( x = 0; x < MAX; x++){
for( y = 0; y < MAX; y++){
sum = sum + arr[x][y];
}
}
cout<<" Sum is "<< sum<< " ";
avg = double(sum)/MAX;
cout<<"Average is "<< avg << " ";
// get minimum value and maximum value
minVal = arr[0][0];
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
if(arr[x][y]<minVal)
minVal = arr[x][y];
}
}
cout<<"Min is "<<minVal<<" ";
// get maximum value
maxVal = arr[0][0];
for( x=0; x<MAX; x++){
for( y=0; y<MAX; y++){
if(arr[x][y]>maxVal)
maxVal = arr[x][y];
}
}
cout<<"Max is "<<maxVal<<" ";
int aa[MAX*MAX];
int k = 0;
for (x=0;x<MAX;x++){
for (y=0;y<MAX;y++){
aa[k++] = arr[x][y];
}
}
/*Code changes here use array aa instead of arr */
median = aa[(k/2)];
if (k%2 == 0){
median = (median + aa[k/2-1] )/2;
}
cout<<"Median is "<<median<<" ";
return 0;
}
/*****************output**********************/
Sn. org: sorted:
0: 9
0: 7
0: 7
0: 7
0: 7
0: 6
0: 6
0: 5
0: 3
0: -9
1: 9
1: 6
1: 6
1: 3
1: 2
1: 1
1: 0
1: -1
1: -2
1: -8
2: 9
2: 9
2: 9
2: 5
2: 4
2: 4
2: 0
2: 0
2: -2
2: -8
3: 8
3: 7
3: 6
3: 6
3: 5
3: 3
3: 0
3: -3
3: -4
3: -5
4: 9
4: 5
4: 3
4: 3
4: 1
4: 0
4: -2
4: 0
4: -3
4: -3
5: 4
5: 3
5: 1
5: 0
5: -1
5: -8
5: -1
5: 1
5: -3
5: -4
6: 5
6: 3
6: 1
6: 0
6: -7
6: -7
6: -9
6: -1
6: -2
6: -2
7: 7
7: 0
7: -1
7: -6
7: -7
7: -7
7: -7
7: -10
7: -2
7: -7
8: 2
8: -2
8: -6
8: -5
8: -6
8: -7
8: -9
8: -9
8: -10
8: -8
9: -6
9: -5
9: -4
9: -4
9: -4
9: -9
9: -9
9: -10
9: -10
9: -10
Sum is -68
Average is -6.8
Min is -10
Max is 9
Median is 0.5
--------------------------------
Process exited after 0.08333 seconds with return value 0
Press any key to continue . . .
Note: Median value was printed. Please check in output
Thanks a lot