0% found this document useful (0 votes)
4 views15 pages

Median and Standard Deviation Calculation

1. The document describes algorithms for calculating the median, standard deviation, and evaluating test scores for multiple students. 2. It provides pseudocode and sample programs to find the median of a list by sorting it, calculate standard deviation by finding the mean and variance of a data set, and evaluate student test responses by comparing them to correct answers. 3. The algorithms and programs demonstrate common statistical analysis techniques.

Uploaded by

Ankit Gautam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Median and Standard Deviation Calculation

1. The document describes algorithms for calculating the median, standard deviation, and evaluating test scores for multiple students. 2. It provides pseudocode and sample programs to find the median of a list by sorting it, calculate standard deviation by finding the mean and variance of a data set, and evaluate student test responses by comparing them to correct answers. 3. The algorithms and programs demonstrate common statistical analysis techniques.

Uploaded by

Ankit Gautam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

CASE STUDIES

7.1. Median of a List of Numbers

When all the items in a list are arranged in order, the middle value which divides the items into
two parts with equal number of items on either side is called the median. Odd number of items
have just one middle value while even number of items have two middle values. The median for
even number of items is therefore designated as the average of the two middle values.

The major steps for finding the median are as follows:


1. Read the items into an array while keeping a count of the items.
2. Sort the items in increasing order.
3. Compute median.

The program and sample output are shown in Fig.7.7. The sorting algorithm used is as follows:
1. Compare the first two elements in the list, say a[1], and a[2]. If a[2] is
smaller than a[1], then interchange their values.
2. Compare a[2] and a[3]; interchange them if a[3] is smaller than a[2].
3. Continue this process till the last two elements are compared and interchanged.
4. Repeat the above steps n-1 times.

In repeated trips through the array, the smallest elements 'bubble up' to the top. Because of this
bubbling up effect, this algorithm is called bubble sorting. The bubbling effect is illustrated below
for four items.

Initial After After After


values step 1 step 2 step 3

80 35 35 35

35 80 65 65
Trip-1

65 65 80 15

10 10 10 80

35 35 35

65 80 80
Trip-2
10 65 65

80 10 10
35 10
Trip-3
10 35

65 65

80 80

During the first trip, three pairs of items are compared and interchanged whenever needed. It
should be noted that the number 80, the largest among the items, has been moved to the bottom
at the end of the first trip. This means that the element 80 (the last item in the new list) need not
be considered any further. Therefore, trip-2 requires only two pairs to be compared. This time, the
number 65 (the second largest value) has been moved down the list. Notice that each trip brings
the smallest value 10 up by one level.

The number of steps required in a trip is reduced by one for each trip made. The entire process
will be over when a trip contains only one step. If the list contains n elements, then the number of
comparisons involved would be n(n-1)/2.

PROGRAM TO SORT A LIST AND FIND ITS MEDIAN


Program
#define N 10

main( )
{
int i,j,n;
float median,a[N],t;

printf("Enter the number of items\n");


scanf("%d", &n);

/* Reading items into array a */

printf("Input %d values \n",n);


for (i = 1; i <= n ; i++)
scanf("%f", &a[i]);
/* Sorting begins */
for (i = 1 ; i <= n-1 ; i++)
{ /* Trip-i begins */

for (j = 1 ; j <= n-i ; j++)


{
if (a[j] <= a[j+1])
{ /* Interchanging values */
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
else
continue ;
}
} /* sorting ends */

/* calculation of median */

if ( n % 2 == 0)
median = (a[n/2] + a[n/2+1])/2.0 ;
else
median = a[n/2 + 1];

/* Printing */

for (i = 1 ; i <= n ; i++)


printf("%f ", a[i]);
printf("\n\nMedian is %f\n", median);

Output

Enter the number of items


5
Input 5 values
1.111 2.222 3.333 4.444 5.555
5.555000 4.444000 3.333000 2.222000 1.111000

Median is 3.333000

Enter the number of items


6
Input 6 values
3 5 8 9 4 6
9.000000 8.000000 6.000000 5.000000 4.000000 3.000000

Median is 5.500000
Fig.7.7 Program to sort a list of numbers and to determine median

2. Calculation of Standard Deviation

In statistics, standard deviation is used to measure deviation of data from its mean. The formula
for calculating standard deviation of n items is
__________
s =  variance

where
1 n

variance = -----  (xi-m)2


i=1
n

and 1 n
m = mean = ------  xi
i=1
n

The algorithm for calculating the standard deviation is as follows:

1. Read n items.
2. Calculate sum and mean of the items.
3. Calculate variance.
4. Calculate standard deviation.

Complete program with sample output is shown in Fig.7.8.

PROGRAM TO CALCULATE STANDARD DEVIATION


Program
#include <math.h>
#define MAXSIZE 100

main( )
{
int i,n;
float value [MAXSIZE], deviation,
sum,sumsqr,mean,variance,stddeviation;

sum = sumsqr = n = 0 ;

printf("Input values: input -1 to end \n");


for (i=1; i< MAXSIZE ; i++)
{
scanf("%f", &value[i]);
if (value[i] == -1)
break;
sum += value[i];
n += 1;
}
mean = sum/(float)n;

for (i = 1 ; i<= n; i++)


{
deviation = value[i] - mean;
sumsqr += deviation * deviation;
}
variance = sumsqr/(float)n ;
stddeviation = sqrt(variance) ;

printf("\nNumber of items : %d\n",n);


printf("Mean : %f\n", mean);
printf("Standard deviation : %f\n", stddeviation);
}

Output

Input values: input -1 to end


65 9 27 78 12 20 33 49 -1

Number of items : 8
Mean : 36.625000
Standard deviation : 23.510303

Fig 7.8 Program to calculate standard deviation

3. Evaluating a Test

A test consisting of 25 multiple-choice items is administered to a batch of 3 students. Correct


answers and student responses are tabulated as shown below:
Items

1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

Correct
answers

Student 1
Student 2
Student 3

The algorithm for evaluating the answers of students is as follows:

1. Read correct answers into an array.


2. Read the responses of a student and count the correct ones.
3. Repeat step-2 for each student.
4. Print the results.

A program to implement this algorithm is given in Fig.7.9. The program uses the following arrays:
key[i] - To store correct answers of items
response[i] - To store responses of students
correct[i] - To identify items that are answered correctly.

PROGRAM TO EVALUATE A MULTIPLE-CHOICE TEST


Program
#define STUDENTS 3
#define ITEMS 25
main( )
{
char key[ITEMS+1],response[ITEMS+1];
int count, i, student,n,
correct[ITEMS+1];
/* Reading of Correct answers */
printf("Input key to the items\n");

for(i=0; i < ITEMS; i++)


scanf("%c",&key[i]);

scanf("%c",&key[i]);
key[i] = '\0';

/* Evaluation begins */

for(student = 1; student <= STUDENTS ; student++)


{
/*Reading student responses and counting correct ones*/
count = 0;
printf("\n");
printf("Input responses of student-%d\n",student);

for(i=0; i < ITEMS ; i++)


scanf("%c",&response[i]);
scanf("%c",&response[i]);
response[i] = '\0';

for(i=0; i < ITEMS; i++)


correct[i] = 0;

for(i=0; i < ITEMS ; i++)


if(response[i] == key[i])
{
count = count +1 ;
correct[i] = 1 ;
}

/* printing of results */

printf("\n");
printf("Student-%d\n", student);
printf("Score is %d out of %d\n",count, ITEMS);
printf("Response to the items below are wrong\n");

n = 0;
for(i=0; i < ITEMS ; i++)
if(correct[i] == 0)
{
printf("%d ",i+1);
n = n+1;
}
if(n == 0)
printf("NIL\n");
printf("\n");

} /* Go to next student */

/* Evaluation and printing ends */


}

Output

Input key to the items


abcdabcdabcdabcdabcdabcda

Input responses of student-1


abcdabcdabcdabcdabcdabcda

Student-1
Score is 25 out of 25
Response to the following items are wrong
NIL

Input responses of student-2


abcddcbaabcdabcdddddddddd

Student-2
Score is 14 out of 25
Response to the following items are wrong
5 6 7 8 17 18 19 21 22 23 25

Input responses of student-3


aaaaaaaaaaaaaaaaaaaaaaaaa

Student-3
Score is 7 out of 25
Response to the following items are wrong
2 3 4 6 7 8 10 11 12 14 15 16 18 19 20 22 23 24

Fig 7.9 Program to evaluate responses to a multiple-choice test

4. Production and Sales Analysis

A company manufactures five categories of products and the number of items manufactured and
sold are recorded product-wise every week in a month. The company reviews its production
schedule at every month-end. The review may require one or more of the following information:

(a) Value of weekly production and sales.


(b) Total value of all the products manufactured.
(c) Total value of all the products sold.
(d) Total value of each product, manufactured and sold.

Let us represent the products manufactured and sold by two two-dimensional arrays M and S
respectively. Then,

M11 M12 M13 M14 M15

M21 M22 M23 M24 M25

M31 M32 M33 M34 M35


M=
M41 M42 M43 M44 M45

S11 S12 S13 S14 S15

S21 S22 S23 S24 S25

S31 S32 S33 S34 S35

S41 S42 S43 S44 S45

S =
where Mij represents the number of jth type product manufactured in ith week and Sij the number
of jth product sold in ith week. We may also represent the cost of each product by a single
dimensional array C as follows:

C = C1 C2 C3 C4 C5

th
where C is the cost of j type product.
j

We shall represent the value of products manufactured and sold by two value arrays, namely,
Mvalue and Svalue. Then,

Mvalue[i][j] = Mij x Cj
Svalue[i][j] = Sij x Cj

A program to generate the required outputs for the review meeting is shown in Fig.7.10. The
following additional variables are used:

Mweek[i] = Value of all the products manufactured in week i.

5
=  Mvalue[i][j]
j=1

Sweek[i] = Value of all the products sold in week i

5
=  Svalue[i][j]
j=1

Mproduct[j] = Value of jth type product manufactured during the month

4
=  Mvalue[i][j]
i=1

Sproduct[j] = Value of jth type product sold during the month

4
=  Svalue[i][j]
i=1

Mtotal = Total value of all the products sold during the month

4 5
=  Mweek[i] =  Mproduct[j]
i=1 j=1

Stotal = Total value of all the products sold during the month

4 5
=  Sweek[i] =  Sproduct[j]
i=1 j=1
PRODUCTION AND SALES ANALYSIS
Program
main( )
{
int M[5][6],S[5][6],C[6],
Mvalue[5][6],Svalue[5][6],
Mweek[5], Sweek[5],
Mproduct[6], Sproduct[6],
Mtotal, Stotal, i,j,number;

/* Input data */

printf (" Enter products manufactured week_wise \n");


printf (" M11,M12,----, M21,M22,---- etc\n");

for(i=1; i<=4; i++)


for(j=1;j<=5; j++)
scanf("%d",&M[i][j]);

printf (" Enter products sold week_wise\n");


printf (" S11,S12,----, S21,S22,---- etc\n");

for(i=1; i<=4; i++)


for(j=1; j<=5; j++)
scanf("%d", &S[i][j]);

printf(" Enter cost of each product\n");


for(j=1; j <=5; j++)
scanf("%d",&C[j]);
/* Value matrices of production and sales */
for(i=1; i<=4; i++)
for(j=1; j<=5; j++)
{
Mvalue[i][j] = M[i][j] * C[j];
Svalue[i][j] = S[i][j] * C[j];
}
/* Total value of weekly production and sales */
for(i=1; i<=4; i++)
{
Mweek[i] = 0 ;
Sweek[i] = 0 ;
for(j=1; j<=5; j++)
{
Mweek[i] += Mvalue[i][j];
Sweek[i] += Svalue[i][j];
}
}
/* Monthly value of product_wise production and sales */
for(j=1; j<=5; j++)
{
Mproduct[j] = 0 ;
Sproduct[j] = 0 ;
for(i=1; i<=4; i++)
{
Mproduct[j] += Mvalue[i][j];
Sproduct[j] += Svalue[i][j];
}
}

/* Grand total of production and sales values */

Mtotal = Stotal = 0;
for(i=1; i<=4; i++)
{
Mtotal += Mweek[i];
Stotal += Sweek[i];
}

/***********************************************
Selection and printing of information required
***********************************************/
printf("\n\n");
printf(" Following is the list of things you can\n");
printf(" request for. Enter appropriate item number\n");
printf(" and press RETURN Key\n\n");

printf(" [Link] matrices of production & sales\n");

printf(" [Link] value of weekly production & sales\n");

printf(" 3.Product_wise monthly value of production &");


printf(" sales\n");

printf(" [Link] total value of production & sales\n");

printf(" [Link]\n");

number = 0;
while(1)
{ /* Beginning of while loop */
printf("\n\n ENTER YOUR CHOICE:");
scanf("%d",&number);
printf("\n");

if(number == 5)
{
printf(" G O O D B Y E\n\n");
break;
}
switch(number)
{ /* Beginning of switch */

/* V A L U E M A T R I C E S */

case 1:
printf(" VALUE MATRIX OF PRODUCTION\n\n");
for(i=1; i<=4; i++)
{
printf(" Week(%d)\t",i);
for(j=1; j <=5; j++)
printf("%7d", Mvalue[i][j]);
printf("\n");
}
printf("\n VALUE MATRIX OF SALES\n\n");
for(i=1; i <=4; i++)
{
printf(" Week(%d)\t",i);
for(j=1; j <=5; j++)
printf("%7d", Svalue[i][j]);
printf("\n");
}
break;

/* W E E K L Y A N A L Y S I S */

case 2:
printf(" TOTAL WEEKLY PRODUCTION & SALES\n\n");
printf(" PRODUCTION SALES\n");
printf(" ---------- -----\n");
for(i=1; i <=4; i++)
{
printf(" Week(%d)\t", i);
printf("%7d\t%7d\n", Mweek[i], Sweek[i]);
}
break;

/* P R O D U C T W I S E A N A L Y S I S */

case 3:
printf(" PRODUCT_WISE TOTAL PRODUCTION &");
printf(" SALES\n\n");
printf(" PRODUCTION SALES\n");
printf(" ---------- -----\n");
for(j=1; j <=5; j++)
{
printf(" Product(%d)\t", j);
printf("%7d\t%7d\n",Mproduct[j],Sproduct[j]);
}
break;

/* G R A N D T O T A L S */

case 4:
printf(" GRAND TOTAL OF PRODUCTION & SALES\n");
printf("\n Total production = %d\n", Mtotal);
printf(" Total sales = %d\n", Stotal);
break;

/* D E F A U L T */

default :
printf(" Wrong choice, select again\n\n");
break;

} /* End of switch */

} /* End of while loop */

printf(" Exit from the program\n\n");

} /* End of main */

Output

Enter products manufactured week_wise


M11,M12,----, M21,M22,---- etc
11 15 12 14 13
13 13 14 15 12
12 16 10 15 14
14 11 15 13 12

Enter products sold week_wise


S11,S12,----, S21,S22,---- etc
10 13 9 12 11
12 10 12 14 10
11 14 10 14 12
12 10 13 11 10
Enter cost of each product
10 20 30 15 25

Following is the list of things you can


request for. Enter appropriate item number
and press RETURN key

[Link] matrices of production & sales


[Link] value of weekly production & sales
3.Product_wise monthly value of production & sales
[Link] total value of production & sales
[Link]
ENTER YOUR CHOICE:1
VALUE MATRIX OF PRODUCTION
Week(1) 110 300 360 210 325
Week(2) 130 260 420 225 300
Week(3) 120 320 300 225 350
Week(4) 140 220 450 185 300
VALUE MATRIX OF SALES
Week(1) 100 260 270 180 275
Week(2) 120 200 360 210 250
Week(3) 110 280 300 210 300
Week(4) 120 200 390 165 250

ENTER YOUR CHOICE:2


TOTAL WEEKLY PRODUCTION & SALES
PRODUCTION SALES
---------- -----
Week(1) 1305 1085
Week(2) 1335 1140
Week(3) 1315 1200
Week(4) 1305 1125

ENTER YOUR CHOICE:3


PRODUCT_WISE TOTAL PRODUCTION & SALES

PRODUCTION SALES
---------- -----
Product(1) 500 450
Product(2) 1100 940
Product(3) 1530 1320
Product(4) 855 765
Product(5) 1275 1075

ENTER YOUR CHOICE:4


GRAND TOTAL OF PRODUCTION & SALES

Total production = 5260


Total sales = 4550

ENTER YOUR CHOICE:5


G O O D B Y E
Exit from the program

Fig.7.10 Program for production and sales analysis

You might also like