This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Arrays - Declaration and Initialization 1 Arrays - Declaration and Initialization Suppose that we want to write a program to do the following. 1. Read in five exam scores (integer values). 2. Find the highest score among these five scores. 3. Output the amount by which each score is less than the highest. Obviously, the highest score is not known until all five scores are read in. Therefore we have to keep all five scores so that after the highest score is computed each score can be compared to it. To keep track of the five scores, we need five variables of type int . We could use five individual variables of type int . What if, our program has to handle 100 scores instead of 5? Then keeping track of 100 variables becomes cumbersome and impractical. An array is a good solution to this problem. An array is a group of variables with a uniform naming mechanism that can be declared in a single statement. For example, the names for the five individual variables we need might be score, score, score, score and score . Declaring and using arrays Suppose we want to declare an array consisting of five variables of some type, say Type . We can declared the array as follows. Type score; For example an array of 5 variables of type int is declared as int score; By this statement, we now have five variables of type int , namely score, score, score, score and score . Each variable in the array is called a subscripted variable or an element of the array. The number in square brackets is called the index (or subscript ). Arrays - Declaration and Initialization 2 Note that the indices are numbered starting with 0, not starting with 1. The number of indexed variables (elements) in an array is the size of the array. When an array is declared, the size of the array is given in square braces after the array name. The indexed variables are then numbered (also using square braces), starting with 0 and ending with the integer that is one less than the size of the array. In our example, the indexed variables are of type int , but an array can have indexed variables of any type. All the indexed variables for one array are, however, of the same type. This type is called the base type of the array. Thus, in our example, for the array score , the base type is int . An indexed variable like score can be used any place that an ordinary variable of type int can be used. Do not confuse the two ways to use the square brackets [ ] with an array name. When used in a declaration, such as int score; the number enclosed in the square brackets specifies how many indexed variables the array must have. After the declaration of the array score , to access the i th element, we use the syntax score[i] . For example, score, score, score, score and score ....
View Full Document
This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.
- Fall '08