unit3 - /* Ranking of 60 students in a class */ int main()...

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

View Full Document Right Arrow Icon
/* 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); * 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/15/2011 for the course ECKOW 123 taught by Professor Riuyt during the Spring '11 term at University of Damascus.

Page1 / 16

unit3 - /* Ranking of 60 students in a class */ int main()...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online