Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.

Similar presentations


Presentation on theme: "Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press."— Presentation transcript:

1 Computer Programming Lecture 8 Arrays

2 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press down arrow

3 3 #include void main (void) { char keyPress; printf (“Press any key from the keyboard: \n”); keyPress = getch (); int row = 12; col = 40; gotoxy (col, row); printf (“_”); switch (keyPress) { switch-statement Example (3)

4 4 case 75: // left arrow while (col > 0) { gotoxy (col, row); printf(“ “); col--; gotoxy (col,row); printf(“_”); delay (100); } break; case 77: // right arrow while (col < 80) { gotoxy (col, row); printf(“ “); col++; gotoxy (col,row); printf(“_”); delay (100); } break; switch-statement Example (3)

5 5 case 72: // up arrow while (row > 0) { gotoxy (col, row); printf(“ “); row--; gotoxy (col,row); printf(“_”); delay (100); } break; case 80: // down arrow while (row < 24) { gotoxy (col, row); printf(“ “); row++; gotoxy (col,row); printf(“_”); delay (100); } break; } // end of switch statement } // end of main switch-statement Example (3)

6 6 Duplication Input Problem Consider the problem: 5 input numbers, find the average and the input numbers which are above the average. EXAMPLE #include #include main() {int i, number, sum, average; float average; sum = 0; sum = 0; for (i=0; i< 5; i++) { printf(“input number: “); printf(“input number: “); scanf(“%d”,&number); scanf(“%d”,&number); sum += number; sum += number; }}

7 7 Arrays Array can help with that kind of problems. What is ARRAY? A collection of memory locations (same data type, single variable name) Data structure which can access more than one item using a single variable. (one-to-many correspondence between name and current value.)

8 8 Arrays An array is a collection of two or more consecutive memory cells, call array element, that are associated with a particular symbolic name. To set up an array in memory, we must declare both the name of the array and the number of cells associated with it.

9 9 Arrays Declaration 1 SYNTAX 1 element-type name[size]; EXAMPLE double a[5]; int x[10]; float y[20];

10 10 Arraysmain(){ int x[10]; int x[10]; …} X[0]X[1]X[2]X[3]X[4]X[5]X[6]X[7]X[8]X[9] 10-element array called x 10 boxes for keeping 10 integer numbers

11 11 Arrays x[3] refers to the element in x whose index is 3.x[3] refers to the element in x whose index is 3. Array declaration in C:Array declaration in C: –int x[10] (an array with storage space for 10 integers) (fixed (nonvariable) size: declared before execution) –first element: x[0], last element: x[n-1] (I.e., x[9]) (homogeneous data structure / uniform data type)

12 12 Array subscript is used to differentiate between the individual array elements and to allow us to specify which array element is to be manipulated. Array subscript is used to differentiate between the individual array elements and to allow us to specify which array element is to be manipulated. Arrays Subscription Example: int x[5]; int x[5]; x[0] = 10 x[0] = 10 x[1] = 10 + 5; x[1] = 10 + 5; x[2] = x[1]; x[2] = x[1]; x[3] = 2*x[1]; x[3] = 2*x[1]; x[4] = x[1] + 3*x[3]; x[4] = x[1] + 3*x[3];

13 13 The subscript value is in range 0 to N-1 for an array with size N We call it zero-based indexing because it starts from 0. An array subscript value outside the range often causes a run- time error. Arrays Subscription Example: int a[5]; a[10] = 10; a[-1] =0; !!! Run-time error !!! !!! Run-time error !!!

14 14 Entering Data into the Array Example: for (int day = 0; day < 7; day ++ ) { printf( “ Enter temperature for day “ ); scanf ( “ %d ”, &week [day] ); }

15 15 Reading Data from the Array To calculate the average of the week’s temperature using array (here is the code). float sum = 0; for (int day = 0; day < 7; day++ ) sum += week[day]; printf ( “ Average is %f ”,sum/7);

16 16 A Simple Example of Using Arrays Create an array (Ex. : a 12 floating point array)Create an array (Ex. : a 12 floating point array) Set a value to an element in an arraySet a value to an element in an array (Ex.: set the third element of rainfall to 2.5) (Ex.: set the third element of rainfall to 2.5) Compare two elements of an arrayCompare two elements of an array (Ex.: compare the 4rd and the 12th elements) float rainfall [12]; if (rainfall[3] > rainfall[11]) printf (“more rain in April than in December”); printf (“more rain in April than in December”); rainfall [2] = 2.5;

17 17 Arrays Declaration 2 EXAMPLE int arr[5] = {0, 0, 3, 2, 1}; char v [ ] = {‘A’, ’E’, ’I’, ’O’, ’U’}; int arr[5] = {0, 0, 3, 2, 1}; float x[2] = {0.112, 3.2} SYNTAX 2 element-type aname[size] = {initialization list}

18 18 Multidimensional arrays are defined in much the same manner as one dimensional arrays, Multidimensional arrays are defined in much the same manner as one dimensional arrays, Except that a separate pair of square brackets is required for each dimension. Except that a separate pair of square brackets is required for each dimension. Two-dimensional array ---->[ ][ ] Two-dimensional array ---->[ ][ ] Three-dimensional array ----->[ ][ ][ ] Three-dimensional array ----->[ ][ ][ ] In general term, the multidimensional array definition In general term, the multidimensional array definition can be written as: Multi-Dimensional Arrays

19 19 MultiDimensional Arrays Declaration SYNTAX 1 element-type aname[size 1 ][size 2 ]…[size n ]; element-type aname[size 1 ][size 2 ]…[size n ]; /*decalration*/ element-type aname[][size2]…[sizen] element-type aname[][size2]…[sizen] /* prototype */ element-type aname[][]…[] element-type aname[][]…[] /* prototype */ EXAMPLE double table[NROWS][NCOLS]; /* declaration */ int out[ ][4], /* output parameter */ int nrows[ ][ ][ ] /* number of rows */ Ex. in a[5]; Ex. int mat[3][4]; Ex. double vert[x][y][z];

20 20 Example char tictac[3][3];// declaration Initialization of 2D Arrays char tictac[3][3] = {‘x’, ’o’, ’x’, ’o’, ’x’, ’o’, ’o’, ’x’, ’x’}; char tictac[3][3] = {{‘x’,’o’,’x’},{‘o’,’x’,’o’},{‘o’,’x’,’x’}}; Multi-Dimensional Arrays

21 21 The array The array enroll declared enroll declared here here int enroll [100][5][4]; Multi-Dimensional Arrays


Download ppt "Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press."

Similar presentations


Ads by Google