Need help on this Thank you

#include<iostream> // required for cin , cout
#include<cmath> // required for sqrt()

using namespace std;

int main()
{
const int size=56;

double R[size]={45.3, 67.8, 34.3, 51.2, 48.5, 61.3, 59.3, 65.1,
49.3, 42.4, 63.5, 69.8, 71.2, 39.8, 55.5, 53.2,
56.7, 48.8, 61.5, 51.2, 58.9, 63.1, 67.5, 62.4,
52.4, 50.2, 49.8, 56.8, 59.7, 60.4, 45.8, 43.8,
51.3, 54.8, 55.1, 52.3, 56.2, 59.7, 63.0, 46.7,
63.1, 58.2, 41.9, 59.2, 57.2, 67.3, 68.2, 38.9,
51.3, 63.8, 53.4, 58.9, 56.3, 58.9, 53.2, 56.8};

double sum=0, sum1=0, mean, temp, max, min, range, std, variance;

// find the mean
for (int count=0; count<=(size-1); count++)
{

sum=sum+R[count];// add up all numbers in the array
}

mean= ;

// Sort the data

for( int i=0; i<=size-2; i++)
{
for( int j=i+1; j<=size-1; j++)
{

if (R[j]>R[i])
// swap R[i] and R[j]
{
temp=R[i];
R[i]=R[j];
R[j]=temp;

}

}
}


for( int k=0; k<=size-1; k++)
{
sum1=sum1+(R[k]-mean)*(R[k]-mean);// add up (xi-mean)^2
cout<<R[k]<<endl;// print the sorted array
}

max= ;

min ;

range= ;

variance=sum1/ ;

std=sqrt(variance);

cout<<"The mean is "<<mean<<endl;
cout<<"The maximum is "<<max<<endl;
cout<<"The minimum is "<<min<<endl;
cout<<"The range is "<<range<<endl;
cout<<"The Variance
cout<<"The standard deviation is

/*double x=3, y=5, sum=0, sub, mul, div;

cout<<"Welcome to EGR120! Today is April 7"<<endl<<endl;
cout<<"Please enter two integers, x and y. The value of y can not be zero."<<endl;
cin>>x>>y;


sum=x+y;
sub=x-y;
mul=x*y;
div=x/y;

cout<<x<<" + "<<y<<" = "<<sum<<endl;
cout<<x<<" - "<<y<<" = "<<sub<<endl;
cout<<x<<" * "<<y<<" = "<<mul<<endl;
cout<<x<<" / "<<y<<" = "<<div<<endl; */

system("pause");

}

This is the questions that have difficulty

The number lists shows the resistances, in ohms, measured for each resistor in the batch. Write a computer program to determine the following:

(a) Maximum
(b) Minimum
(c) Range
(d) Mean
(e ) Standard deviation
(f) Variance
(g) Median

EditReportDelete
Reply
Last edited on
[code] "Please use code tags" [/code]

Maximum/minimum
_ Empty list: undefined.
_ List of one element: that element.
_ General case: compare the first element against the MAX|min of the rest of the list.

Range: MAX-min

Mean: Sum/number_of_elements.
(Keep in mind that if you divide integers the result will be an integer)

Variance: http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance

Median: sorted_{n/2}
Last edited on

I am done with Range, Mean, Variance, Now I want to locate the median.
Can you help with please.
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
#include<iomanip>
using namespace std;
int main()
{

          const int size=56;    

    double sum=0, mean, max=0, min=100, range, variance;
    double R[size]={45.3, 67.8, 34.3, 51.2, 48.5, 61.3, 59.3, 65.1,
                           49.3, 42.4, 63.5, 69.8, 71.2, 39.8, 55.5, 53.2,
                           56.7, 48.8, 61.5, 51.2, 58.9, 63.1, 67.5, 62.4,
                           52.4, 50.2, 49.8, 56.8, 59.7, 60.4, 45.8, 43.8,
                           51.3, 54.8, 55.1, 52.3, 56.2, 59.7, 63.0, 46.7,
                           63.1, 58.2, 41.9, 59.2, 57.2, 67.3, 68.2, 38.9,
                           51.3, 63.8, 53.4, 58.9, 56.3, 58.9, 53.2, 56.8};
  
    for(int i=0;i<size-1;i++)
       sum+=sum+R[i];
//Find mean
    mean=sum/size;
  
    max=R[0];
   
//Find Maximum
      for(int i=1;i<size-1;i++)
    {
       if(R[i]>max)
           max=R[i];
    }
  
//Find Minimum
    min=R[0];
   
//Find Maximum
    for(int i=1;i<size-1;i++)
    {
       if(R[i]<min)
           min=R[i];
    }
  
//Find Range
    range=max-min;
// Find the variance and standard deviation
    double v Sum=0;

    for(int i=0;i<size-1;i++)
    {
       vSum+=pow(R[i]-mean,2);
    }

//finding variance
    variance=vSum/size;

    cout<<"Maximum of R\t :"<<max<<endl;
    cout<<"Minimum of R \t:"<<min<<endl;   
    cout<<"Mean \t"<<mean<<endl;
    cout<<"Range of R:\t"<<range<<endl;   

//standard deviation
    cout<<"Standard Deviation of R: \t"<<setprecision(5)<<sqrt(variance)<<endl;
    cout<<"Variance  of R: \t"<<setprecision(5)<<variance<<endl;

     system("pause");
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
    max=R[0];
   
//Find Maximum
      for(int i=1;i<size-1;i++)
    {
       if(R[i]>max)
           max=R[i];
    }
Your loop is incorrect as you are not checking the last element. It should be for(int i=1; i<size; i++)

To compute the median, first sort the array
1
2
std::sort( R, R+size ); //#include <algorithm>
double median = R[size/2];
Last edited on
THANK YOU GOD BLESS YOU. YOU SAVE MY DAY....
Last edited on
I made this corrections and it works fine.


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
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
int main()
{

    const int size=56;    

    double sum=0, mean, max=0, min=100, range, variance;
    double R[size]={45.3, 67.8, 34.3, 51.2, 48.5, 61.3, 59.3, 65.1,
                           49.3, 42.4, 63.5, 69.8, 71.2, 39.8, 55.5, 53.2,
                           56.7, 48.8, 61.5, 51.2, 58.9, 63.1, 67.5, 62.4,
                           52.4, 50.2, 49.8, 56.8, 59.7, 60.4, 45.8, 43.8,
                           51.3, 54.8, 55.1, 52.3, 56.2, 59.7, 63.0, 46.7,
                           63.1, 58.2, 41.9, 59.2, 57.2, 67.3, 68.2, 38.9,
                           51.3, 63.8, 53.4, 58.9, 56.3, 58.9, 53.2, 56.8};
  
    for(int i=0;i<size-1;i++)
       sum+=sum+R[i];

//Find median
	std::sort( R, R+size ); 
    double median = R[size/2];
	std::cout << "The median of the x array is: " << median << std::endl;

//Find mean
    mean=sum/size;  
    max=R[0];
   
//Find Maximum
      for(int i=1;i<size;i++)
    {
       if(R[i]>max)
           max=R[i];
    }
  
//Find Minimum
    min=R[0];
   
//Find Maximum
    for(int i=1;i<size-1;i++)
    {
       if(R[i]<min)
           min=R[i];
    }
  
//Find Range
    range=max-min;
// Find the variance and standard deviation
    double vSum=0;

    for(int i=0;i<size-1;i++)
    {
       vSum+=pow(R[i]-mean,2);
    }

//finding variance
    variance=vSum/size;

    cout<<"Maximum of R\t :"<<max<<endl;
    cout<<"Minimum of R \t:"<<min<<endl;   
    cout<<"Mean \t"<<mean<<endl;
    cout<<"Range of R:\t"<<range<<endl;  
	

//standard deviation
    cout<<"Standard Deviation of R: \t"<<setprecision(5)<<sqrt(variance)<<endl;
    cout<<"Variance  of R: \t"<<setprecision(5)<<variance<<endl;

     system("pause");
return 0;
}
Last edited on
Your median calculation is incorrect for an array with an even number of elements.
use this:
1
2
3
4
5
6
//Find median
std::sort( R, R+size ); 
double median
if (size%2) median=R[size/2];
else median=double(R[size/2]+R[size/2+1])/2.0
std::cout << "The median of the x array is: " << setprecision(5) << median << std::endl;
Last edited on
That calculation is also incorrect for even numbered arrays.
@viliml: Consider size=2, you are accessing out of bounds.
1
2
3
4
5
6
//Find median
std::sort( R, R+size ); 
double median
if (size%2) median=R[size/2];
else median=double(R[size/2]+R[size/2-1])/2.0
std::cout << "The median of the x array is: " << setprecision(5) << median << std::endl;

You were right, ne555 and cire. This would work.
Last edited on
@viliml: it should be +1 (not -1) on line 5.
Last edited on
@Peter87: Consider size=2, you would access out of bounds. ;)
@ne555: My brain malfunctioned :/
Topic archived. No new replies allowed.