Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

trying to calculate the mean median and range of an array. input output should l

ID: 3552415 • Letter: T

Question

trying to calculate the mean median and range of an array. input output should look like this

1 2 4 6 7 8 9 7 4 2

5 5 8


Here is the code I have. takes in the numbers put does not give correct outputs,not even close actually, not sure where I am going wrong but i believe my sort is not working properly. Still this doesnt explain the large numbers I am getting from my outputs so I must be doing more wrong.

#include<iostream>


using namespace std;

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


int smallerToSmall(const int a[], int startindex, int numUsed);


void numSwap(int& v1,int& v2);

//swap numbers

void sortNum(int a[],int numUsed);


void numMean(int a[],int);


void numMedian(int a[],int);


void Range(int a[]);


int main()

{

int values[10],numUsed;

numUsed=10;

fillArray(values,10,numUsed);

sortNum(values,numUsed);

numMean(values,numUsed);

numMedian(values,numUsed);

Range(values);

return 0;

}

void fillArray(int a[],int size, int& numUsed)

{

int nums,index(0);

cin>>nums;

while ((nums>=0)&&(index<9))

{

a[index]=nums;

index++;

cin>>nums;

}

numUsed=index;

}


void sortNum(int a[],int )

{

int smallToBig,numUsed(10);

for(int index = 0;index < 9; index++)

{

smallToBig=smallerToSmall(a,index,numUsed);

numSwap(a[index],a[smallToBig]);

}

}

int smallerToSmall(const int a[], int startindex, int numUsed)

{

int min=a[0];

int indexOfMin=0;

for(int index=1;index<10;index++)

if(a[index]<min)

{

min=a[index];

indexOfMin = index;

}

return indexOfMin;

}

void numSwap(int& v1,int& v2)

{

int temp;

if(v1>v2)

temp=v1;

v1=v2;

v2=temp;

}

void numMean(int a[],int )

{

cout<<((a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9])/10.0)<<" ";

}


void numMedian(int a[], int)

{

double median;

median=((a[4]+a[5])/2.0);

cout<<median<<" ";

}

void Range(int a[])

{

int range;

range=a[9]-a[0];

cout<<range<<endl;

}

Explanation / Answer

//Please find the indented code at http://pastebin.com/R7MjUt3R


#include<iostream>


using namespace std;


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


void sortNum(int a[],int numUsed);


void numMean(int a[],int);


void numMedian(int a[],int);


void Range(int a[]);


int main()

{

int values[10],numUsed;

numUsed=10;

fillArray(values,10,numUsed);

sortNum(values,numUsed);

for(int i=0;i<10;i++) cout<<values[i]<<" ";

cout<<endl;

numMean(values,numUsed);

numMedian(values,numUsed);

Range(values);

return 0;

}


void fillArray(int a[],int size, int& numUsed)

{

int nums,index(0);

while ((nums>=0)&&(index<10))

{

cin>>nums;

a[index]=nums;

index++;

}

numUsed=index;

}


void sortNum(int array[],int numUsed)

{

int c,d, n= numUsed,swap;

for (c = 0 ; c < ( n - 1 ); c++)

{

for (d = 0 ; d < n - c - 1; d++)

{

if (array[d] > array[d+1]) /* For decreasing order use < */

{

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

}

}

}

}


void numMean(int a[],int )

{

cout<<((a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9])/10.0)<<" ";

}


void numMedian(int a[], int)

{

double median;

median=((a[4]+a[5])/2.0);

cout<<median<<" ";

}


void Range(int a[])

{

int range;

range=a[9]-a[0];

cout<<range<<endl;

}