Ch8 - CSEB114:PrincipleofProgramming...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style 9/16/2008 prepared by NI, edited by CSEB114: Principle of Programming Chapter 8: Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
9/16/2008 prepared by NI, edited by Objectives In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Relationship with pointers Passing array to function 2 Dimensional arrays Simple Searching Simple Sorting
Background image of page 2
9/16/2008 prepared by NI, edited by Introduction to Array 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
9/16/2008 prepared by NI, edited by Let say we have an array called a. -10 99 -8 100 27 10 1976 -2020 1 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] Name of the array The position number within the square brackets is formally called a subscript . A subscript can be an integer or an integer expression. For example if x = 1 and y = 2, then a[x+y] is equal to a[3]. Notice that the position starts from 0.
Background image of page 4
9/16/2008 prepared by NI, edited by Array Declaration 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: data_type array_name[size]; Examples: int my_array[100]; char name[20]; double bigval[5*200];
Background image of page 5

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

View Full DocumentRight Arrow Icon
9/16/2008 prepared by NI, edited by Array Initialization There are 2 ways to initialize an array: during compilation and during execution. During compilation: int arr[ ] = {1, 2, 3, 4, 5}; unsized array We can define how many elements that we want since the array size is not given. int arr[3] = {90, 21, 22}; We can define only 3 elements since the array size is already given. int arr[5] = {2,4} Initialize the first two elements to the value of 2 and 4 respectively, while the other elements are initialized to zero. int arr[5] = {0}
Background image of page 6
9/16/2008 prepared by NI, edited by Array Initialization During execution: Using loop to initialize all elements to zero int arr[3], index; for (index = 0; index < 3; index++) arr[index] = 0; Using loop and asking the user to specify the value for each element. int arr[3], index; for (index = 0; index < 3; index++) { printf (“arr[%d]:”,index);
Background image of page 7

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

View Full DocumentRight Arrow Icon
9/16/2008 prepared by NI, edited by Assigning value to array element We can assign a value to a specific array element by using its index number.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Ch8 - CSEB114:PrincipleofProgramming...

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

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