{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

unit3 - Ranking of 60 students in a class int main...

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

/* Ranking of 60 students in a class */ int main() { /*declaring 60 varialbes */ int score0, score1,score2,……,score59; /* Reading scores for sixty times */ printf(“Enter the score : “); scanf(“%d”, &score0); …. …. …. …. printf(“Enter the score : “); scanf(“%d”, &score59); /* comparing & swapping for 1770 times * to arrange in descending order */ swap( score0, score1); swap( score1, score2); swap( score2, score3); …. …. …. …. swap( score0,score1); swap( score1,score2); swap( score0,score1); /*printing 60 scores after sorting */ printf(“%4d”, score0); printf(“%4d”, score1); } void swap ( int a, int b) { int temp; if( a < b) { temp = a ; a = b ; b = temp; } } score0 score1 score2 score3 . . score59 scores[0] scores[1] scores[2] scores[3] . . scores[59] #include<stdio.h> int main() { int scores[60] , i , j, temp; for(i = 0; i < 60 ;i++) { printf("Enter the score : "); scanf("%d", &scores[i]); } for(i=0;i<(60-1);i++) for( j=0; j <(60 -(i+1)); j++) if(scores[ j ] < scores[ j +1]) { temp = scores[ j ]; scores[ j ] = scores[ j +1]; scores[ j + 1] = temp; } for( i = 0; i < 60; i ++) printf("%4d", scores[i]); } Sixty variables are replaced by one Array Sixty input statements are called by one loop statement 1770 comparing statements are included in one loop statement Array & its Advantage

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

View Full Document
scores[0] scores[1] scores[2] scores[3] scores[4] scores Array . . . start here 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 ( memory addresses) Mean can be calculated only after reading all scores. Each deviation is difference of individual score and mean. To calculate deviations of all scores, scores must be stored in an ARRAY. Initialization of Array Declaration of Array Processing on Array Input to an element Accessing an element
Scalar Variables : A variable represents a data item and it can be used to store a single atomic value at a time. These are also called scalar variables. Integer takes 2 bytes memory as a single unit to store its value. i.e.,the value of a scalar variable cannot be subdivided into a more simpler data items. The address of first byte is the address of a variable . Vector Variables (arrays) : In contrast, an array is multivariable (an aggregate data type), which is also referred to a data structure. It represents a collection of related data items of same type. An individual data item of an array is called as ‘element’. Every element is accessed by index or subscript enclosed in square brackets followed after the array name. All its elements are stored in consecutive memory locations, referred under a common array name. Ex : int marks[10] ; /* declaration of array */ ‘0’ is first number in computer environment. The first element of array marks is marks[0] and last element is marks[9]. (the address of first element is the address of the array) An array is a derived data type. It has additional operations for retrieve and update the individual values. The lowest address corresponds to the first element and the highest address to the last element.

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 ]}