# Chapter 7-p - Example 7.1 Write a program using a...

This preview shows pages 1–4. Sign up to view the full content.

Example 7.1 Write a program using a single-subscripted variable to evaluate the following expressions: 10 Total = Σ x i 2 i=1 The values of x1,x2,. ...are read from the terminal. Program in Fig.7.1 uses a one-dimensional array x to read the values and compute the sum of their squares. PROGRAM SHOWING ONE-DIMENSIONAL ARRAY Program : main() { int i ; float x[10], value, total ; /* . . . . . .READING VALUES INTO ARRAY . . . . . . */ printf("ENTER 10 REAL NUMBERS\n") ; for( i = 0 ; i < 10 ; i++ ) { scanf("%f", &value) ; x[i] = value ; } /* . . . . . . .COMPUTATION OF TOTAL . . . . . . .*/ total = 0.0 ; for( i = 0 ; i < 10 ; i++ ) total = total + x[i] * x[i] ; /*. . . . PRINTING OF x[i] VALUES AND TOTAL . . . */ printf("\n"); for( i = 0 ; i < 10 ; i++ ) printf("x[%2d] = %5.2f\n", i+1, x[i]) ; printf("\ntotal = %.2f\n", total) ; } Output ENTER 10 REAL NUMBERS 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
x[ 1] = 1.10 x[ 2] = 2.20 x[ 3] = 3.30 x[ 4] = 4.40 x[ 5] = 5.50 x[ 6] = 6.60 x[ 7] = 7.70 x[ 8] = 8.80 x[ 9] = 9.90 x[10] = 10.10 Total = 446.86 Fig.7.1 Program to illustrate one-dimensional array Example 7.2 Given below is the list of marks obtained by a class of 50 students in an annual examination. 43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78 65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59 Write a program to count the number of students belonging to each of following groups of marks: 0-9, 10-19, 20-29,. ....,100. The program coded in Fig.7.2 uses the array group containing 11 elements, one for each range of marks. Each element counts those values falling within the range of values it represents. For any value, we can determine the correct group element by dividing the value by 10. For example, consider the value 59. The integer division of 59 by 10 yields 5. This is the element into which 59 is counted. PROGRAM FOR FREQUENCY COUNTING Program #define MAXVAL 50 #define COUNTER 11 main() { float value[MAXVAL]; int i, low, high; int group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0}; /* . . . . . . . .READING AND COUNTING . . . . . .*/ for( i = 0 ; i < MAXVAL ; i++ ) { /*. . . . . . . .READING OF VALUES . . . . . . . . */ scanf("%f", &value[i]) ; /*. . . . . .COUNTING FREQUENCY OF GROUPS. . . . . */ ++ group[ (int) ( value[i] + 0.5 ) / 10] ;
} /* . . . .PRINTING OF FREQUENCY TABLE . . . . . . .*/

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 9

Chapter 7-p - Example 7.1 Write a program using a...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online