Download presentation
Presentation is loading. Please wait.
1
ARRAYS
2
For such type of problems, it is more convenient to use arrays.
1. DATA TYPES Simple data types (int, double, char) use a single memory cell to store a variable. However, it is sometimes more efficient to solve problems by grouping data items together for storage in main memory. For such type of problems, it is more convenient to use arrays. In fact, a string is an array of characters. Dr. Soha S. Zaghloul 2
3
2. DATA TYPES in memory The following declarations of simple data types are reflected in memory as shown in figure 1: int sum = 0; double average = 0.0; char choice = ‘X’; Sum Choice Average X 0.0 Dr. Soha S. Zaghloul 3
4
3. Declaration of arrays An array is a collection of two or more adjacent memory cells, called “array elements”. Elements of the same array should be ALL of the same type: this represents the type of the array. Array elements are associated with a particular symbolic name (identifier name) that obeys all rules previously mentioned about identifiers. Therefore, to set up an array in memory, we must declare the following: The type of the array: which is the type of its elements, The name of the array, The number of elements of the array: which is the number of adjacent cells in memory associated with the name of the array. Dr. Soha S. Zaghloul 4
5
4. Declaration of arrays – example (1)
Consider the following array declaration: double x[8]; The above declaration instructs the compiler to allocate 8 adjacent memory cells with the name x. All cells are of type double. The memory layout is shown in the figure below: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] Dr. Soha S. Zaghloul 5
6
Array subscripts are always of type integer.
To process the data stored in an array, we refer to each individual element by specifying: The array name, The order of the element in the array (or subscript) Array subscripts always start with zero, and end with the array size minus one. For example, if the array contains 8 elements, then the subscript ranges from 0 to 7. Array subscripts are always of type integer. Array subscripts should be constant values. Array subscripts are enclosed between square brackets [ ]. Dr. Soha S. Zaghloul 6
7
6. Array subscripts – example (1) cnt’d
In the previously declared array x (Refer to slide #5), assume we give values to the array elements using the following statements: x[0] = 16.0; x[1] = 12.0; x[2] = 6.0; x[3] = 8.0 x[4] = 2.5; x[5] = 12.0; x[6] = 14.0; x[7]= -54.5; This is reflected in memory as follows: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 14.0 -54.5 Dr. Soha S. Zaghloul 7
8
7. Array manipulation The following are some statements that manipulate the array x, and their explanation: x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 14.0 -54.5 sum 28.0 28.0 28.0 26.0 25.0 Statement Explanation Result printf (“%.1f”, x[0]); Display the value of x[0] 16.0 x[3] = 25.0; Changes the value of x[3] to 25.0 double sum; sum = x[0] + x[1]; Store the sum of x[0] and x[1] (28.0) in sum sum += x[2]; Add x[2] to sum x[3] += 1.0; Add 1 to x[3] x[2] = x[0] + x[1]; Stores the sum of x[0] and x[1] (28.0) in x[2] Dr. Soha S. Zaghloul 8
9
Consider the following example:
8. More about subscripts Array subscripts should be constant. However, these constants may be declared with the directive #define. Consider the following example: #include <stdio.h> #define NUM_STUDENTS 50 Int main (void) { int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; //the rest of the program } // end main Dr. Soha S. Zaghloul 9
10
9. A programming hint – parallel arrays
#include <stdio.h> #define NUM_STUDENTS 50 Int main (void) { int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; //the rest of the program } // end main In the shown example, we have two arrays of the same size (NUM_STUDENTS) This can be useful in problem solving by associating the two arrays using the subscript. In other words, each subscript in both arrays represent the same student. For example, gpa [0] is the gpa of the student of ID = id [0]; gpa [15] is the gpa of the student of ID = id [15], and so on. Dr. Soha S. Zaghloul 10
11
9. A programming hint – parallel arrays (cnt’d)
id[0] 433122 id[1] 433200 id[2] 433333 … id[30] 433818 id[31] 432900 id[49] 433777 gpa[0] 2.71 gpa[1] 3.09 gpa[2] 2.98 … gpa[30] 4.50 gpa[31] 4.03 gpa[49] 4.8 Because the data stored in id[i] and gpa[i] relate to the ith student, the two arrays are called parallel arrays. Dr. Soha S. Zaghloul 11
12
An array can be initialized when we declare it as follows:
10. Array initialization An array can be initialized when we declare it as follows: int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; In the previous declaration, the array size is omitted. The array size is therefore specified by the number of elements listed between braces. In the above example, the array size is 10. The subscripts go from 0 to 9. Another example: char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; This is an array of characters of size 5. The subscripts go from 0 to 4. Dr. Soha S. Zaghloul 12
13
What is the difference in meaning between x3 and x[3]?
11. Quick questions What is the difference in meaning between x3 and x[3]? For the declaration char grades[5]; How many memory cells are allocated for data storage? What type of data can be stored? How does one refer to the initial array element? How does one refer to the final array element? Dr. Soha S. Zaghloul 13
14
12. Example (2) Declare one array for storing the numbers from 0 to 9; another array to store their square, and a third array for storing the cubes of the same integers. Note that the subscripts of the arrays square and cube may be used to recognize the integer. For example, square[2] = 4, square[9] = 81, and the same for the cube array. int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} int square[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; int cube[] = {0, 1, 8, 27, 64, 125, 216, 343, 512, 1000}; Dr. Soha S. Zaghloul 14
15
13. Array initialization using loops
The following example declares an array of integers; then initializes all its elements to zero. Very often, loops are used with arrays to process its elements in sequence. In this case, the loop counter (i) is equal to the array subscript. int i, numbers[100]; for (i = 0; i< 100; i++) numbers [i] = 0; Dr. Soha S. Zaghloul 15
16
14. Example (3) The array square and cube in example 2 can also be initialized as follows: int i, square[10]; for (i = 0; i< 10; i++) { square[i] = i * i; cube[i] = i * i * i; } // end for Dr. Soha S. Zaghloul 16
17
15. Array initialization using scanf
We may also get the values of the array elements from the user as follows: int i, number[10]; for (i = 0; i< 10; i++) { printf (“Enter an integer> “); scanf (“%d”, &number[i]; } // end for Dr. Soha S. Zaghloul 17
18
16. self-check exercises Write a complete programs that calculates the product of the integer of an array of size 100. The array elements are entered by the user. Write a complete programs that accepts 50 array elements of type double, and produces another array that represents the corresponding absolute values of the first array. Dr. Soha S. Zaghloul 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.