Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)

Similar presentations


Presentation on theme: "CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)"— Presentation transcript:

1 CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)

2 Topics 1.Introduction to Arrays 2.Operations on Arrays: a)Declaring arrays b)Initializing arrays c)Assigning values to array elements d)Reading values from array elements e)Passing arrays to a function 3.2 Dimensional (2D) Arrays 2BS (Feb 2012)

3 INTRODUCTION TO ARRAYS Topic 1 BS (Feb 2012)3

4 What are arrays? In C, a group of items of the same type can be set up using array. An array is a group of consecutive memory locations related by the fact that they all have the same name and the same type. The compiler must reserve storage (space) for each element/item of a declared array. The size of an array is static (fixed) throughout program execution. To refer to a particular location or element in the array, we specify the name of the array (index or subscript) and the position number of the particular element in the array. BS (Feb 2012)4

5 Array: Conceptual View (1) BS (Feb 2012)5 score[0] ? Index (subscript) // C array declarations // An array of 4 elements with // no value. int score[4] A subscript can be an integer or an integer expression. For example if x = 1 and y = 2, then score[x+y] is equal to score[3]. Starts with zero Name of the array ??? score[1]score[2]score[3]

6 Array: Conceptual View (2) BS (Feb 2012)6 score[0] ? Index (subscript) Name of the array ? ? ? score[1] score[2] score[3] // C array declarations // An array of 4 // elements with no value. int score[4]

7 Array: Conceptual View (3) BS (Feb 2012)7 [0] ? Index (subscript) // C array declarations // An array of 4 elements with no value. int score[4] Name of the array ??? [1][2][3] score

8 OPERATIONS ON ARRAYS Topic 2 BS (Feb 2012)8

9 1. Declaring Arrays Array declaration is made by specifying the data type, it’s name and the number of space (size) so that the computer may reserve the appropriate amount of memory. General syntax: Examples: – int my_array[100]; – char name[20]; – int a[27], b[10], c[76]; – double bigval[5*200]; BS (Feb 2012)9 data_type array_name[size]; #define SIZE 20 double test_score[SIZE]; int tax[SIZE+2]; #define SIZE 20 double test_score[SIZE]; int tax[SIZE+2]; expression Constant

10 2. Initializing Arrays (1) After an array is declared, it must be initialized. 2 ways to initialize an array: compile-time and run-time 1.Compile-time, example: int age[ ] = {1, 2, 3, 4, 5}; int age[3] = {90, 21, 22}; BS (Feb 2012)10 Initialize as many elements as we want. The array size is not specified. 91 22 21 age[0] age[1] age[2] 1 3 2 age[0] age[1] age[2] age[3] age[4] 5 4

11 2. Initializing Arrays (2) BS (Feb 2012)11 char init[5] = {‘a’,’d’}; int age[5] = {0}; char ans[4] = {0}; aNULLd init 00000 age [0] [1] [2] [3] [4] NULL ans [0] [1] [2] [3] Initialize all array elements to zero. [0] [1] [2] [3] [4] Initialize the first two elements to the value of a and d respectively, while the other elements are initialized to NULL for char or zero for numeric identifier.

12 2. Initializing Arrays (3) 2. Run-time, example: Using for loop to initialize the array elements BS (Feb 2012)12 ????? salary [0] [1] [2] [3] [4] 0.0 salary [0] [1] [2] [3] [4] double salary[5]; for (i = 0; i < 5; i++) salary[i] = 0.0; // scanf(“%d”, &salary[i] is accepted. /*end for */ double salary[5]; for (i = 0; i < 5; i++) salary[i] = 0.0; // scanf(“%d”, &salary[i] is accepted. /*end for */

13 3. Assigning Values to Array Elements 1.You can assign values to array elements by using the for statement (use the same example of initializing arrays during run-time) 2.You can also assign a value to a specific array element by using its index number. – Example: let’s say we have an array that represent the number of people staying in 5 unit apartments. int apartment[5]={3,2,6,4,5}; – The above initialization indicates that there are 3 people living in apartment[0], 2 people living in apartment[1] and so on. – To assign new value to apartment[3], you may use this statement: apartment[3] = 8; apartment[3] = apartment[3] + 2 BS (Feb 2012)13

14 4. Reading Values of Array Elements To read a value from a specific array element, refer to the index. For example, let’s say we want to know how many people leaving in apartment[3], we could simply do this: Output: BS (Feb 2012)14 int apartment[5] = {3,2,6,4,5}; int nop; nop = apartment[3]; printf(“Apartment 3 has %d people.”, nop); int apartment[5] = {3,2,6,4,5}; int nop; nop = apartment[3]; printf(“Apartment 3 has %d people.”, nop); Apartment[3] has 4 people.

15 Example (1) BS (Feb 2012)15 #include #define SIZE 5 void main(void) { int apartment[SIZE] = {3,2,6,4,5}; int index, total = 0; for (index = 0; index < SIZE; index++) { total = total + apartment[index]; } printf("There are total of %d inhabitants",total); } #include #define SIZE 5 void main(void) { int apartment[SIZE] = {3,2,6,4,5}; int index, total = 0; for (index = 0; index < SIZE; index++) { total = total + apartment[index]; } printf("There are total of %d inhabitants",total); } Output?

16 Example (2) #include void main(void) { int apartment[5] = {3,2,6,4,5}; int index, total = 0; printf("Apt No\t\tNo of people"); for (index = 0; index < 5; index++) { printf(“%d\t\t%d\n",index, apartment[index]); } #include void main(void) { int apartment[5] = {3,2,6,4,5}; int index, total = 0; printf("Apt No\t\tNo of people"); for (index = 0; index < 5; index++) { printf(“%d\t\t%d\n",index, apartment[index]); } BS (Feb 2012)16 Output?

17 5. Passing Arrays to Functions(2) When we pass an array to a function, we are actually passing the pointer to the first element in the array to the function. Therefore, any changes to the array inside the function will also change the actual array inside the calling function. When we want to pass an array to a function, we need to know these 3 things. – How to write the function prototype? – How to do function call? – How does the function header would look like? BS (Feb 2012)17

18 5. Passing Arrays to Functions(2) Assume that we have the following array declaration. int score[3]={45,90,14}; Say for example we want to write a function, called ReadScore(), which will read marks/scores from the user and store the marks inside the score array. Perform these steps: – Function prototype - indicate that the parameter is an array. Example: void ReadScore(int s[]); – Function call: int score[3]={45,90,14}; //Assume we have this ReadScore(score); //declaration – Function definition : void ReadScore(int s[]){ } BS (Feb 2012)18 with NO array size information.

19 Example (1) #include void test(int s[]); void main(void) { int x[3]={9,7,6}; test(x); } void test(int s[]) { printf("%d\n",s[0]); printf("%d\n",s[1]); } #include void test(int s[]); void main(void) { int x[3]={9,7,6}; test(x); } void test(int s[]) { printf("%d\n",s[0]); printf("%d\n",s[1]); } BS (Feb 2012)19 Output?

20 Example(2) BS (Feb 2012)20 #include #define size 5 void getMarks(float s[ ]); float calcAverage(float sc[ ]); void main(void) { float marks[size] = {0.0}; //initializing the array getMarks(marks); /* function call */ printf("Average for marks given is %.2f\n“, calcAverage(marks)); } #include #define size 5 void getMarks(float s[ ]); float calcAverage(float sc[ ]); void main(void) { float marks[size] = {0.0}; //initializing the array getMarks(marks); /* function call */ printf("Average for marks given is %.2f\n“, calcAverage(marks)); } void getMarks(float s[ ]) { int i; for (i = 0; i < size; i++) { printf("Marks student %d:",i + 1); scanf("%f",&s[i]); } void getMarks(float s[ ]) { int i; for (i = 0; i < size; i++) { printf("Marks student %d:",i + 1); scanf("%f",&s[i]); } float calcAverage(float sc[ ]) { float total = 0.0; int i; for (i = 0; i < size; i++) { total = total + sc[i]; } return (total / size); } float calcAverage(float sc[ ]) { float total = 0.0; int i; for (i = 0; i < size; i++) { total = total + sc[i]; } return (total / size); }

21 2 DIMENSIONAL (2D) ARRAYS Topic 3 BS (Feb 2012)21

22 2D Arrays It is possible to create an array which has more than one dimension. In C, we can go up to 12-dimensional arrays. 2D arrays: – Declaration: Example: BS (Feb 2012)22 Data_type array[row][column] int carrymarks[4][2] Row Column

23 2D: Conceptual View (1) BS (Feb 2012)23 int m[4][2] = {10, 12, 33, 14, 25, 56, 77, 85}; Column subscript Row subscript 1012 3314 2556 7785 [0][1] [0] [1] [2] [3] Column Row This 2D array has 4 rows and 2 columns.

24 2D: Conceptual View (2) BS (Feb 2012)24 CSEB134 : BS/2008 int m[4][2] = {10, 12, 33, 14, 25, 56, 77, 85}; m[0][0]m[0][1] 1012 m[1][0]m[1][1] 3314 m[2][0]m[2][1] 2556 m[3][0]m[3][1] 7785 Column 0Column 1 Row 0 Row 1 Row 2 Row 3 1012331425567785 Row 0Row 1 m[0][0] m[0][1] m[1][0] m[1][1] m[2][0] m[2][1] m[3][0] m[3][1] Storage structure Row 2Row 3 This 2D array has 4 rows and 2 columns.

25 Initializing 2D Arrays (1) 2 ways to initialize an array: compile-time and run-time 1. Compile-time – Variable initialization can also be done this way: – This method is less confusing since we can see the rows and columns division more clearly. BS (Feb 2012)25 int m[4][2] = {{10,12},{33,14},{25,56},{77,85}};

26 Initializing 2D Arrays (2) 2. Run-time Using nested for statements: Although it is possible to create a multi-dimensional array, arrays above 2-dimensions are rarely used. BS (Feb 2012)26 for (row = 0; row < 4; row++) { for (column = 0; column < 2; column++) m[row][column] = 0; } for (row = 0; row < 4; row++) { for (column = 0; column < 2; column++) m[row][column] = 0; }

27 Passing a 2D Array to Functions When a 2D (or higher dimensional) array is passed to a function, the size of the second (or subsequent) subscript needs to be specified. – For example, if we have: int m[4][5]; – Then the function header which would take m as an argument should be declared like this: void Process2D(int td[ ][5]) An array is stored consecutively in memory regardless of the number of dimensions. Therefore, specifying the subscripts in the function parameter will help the compiler to know the boundary of the different dimensions. BS (Feb 2012)27

28 Summary 1.Introduction to Arrays 2.Operations on Arrays: a)Declaring arrays b)Initializing arrays c)Assigning values to array elements d)Reading values from array elements e)Passing arrays to a function 3.2 Dimensional (2D) Arrays – declaring, initializing and passing 2D array to function BS (Feb 2012)28


Download ppt "CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)"

Similar presentations


Ads by Google