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

21
Computer Programming Lecture 8 • Arrays

Transcript of Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use...

Page 1: 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.

Computer Programming

• Lecture 8• Arrays

Page 2: 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.

2

switch-statement Example (3)

If use press left arrow

If use press right arrow

If use press up arrow

If use press down arrow

Page 3: 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.

3

#include <stdio.h>#include <conio.h>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)

Page 4: 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.

4

case 75: // left arrowwhile (col > 0){

gotoxy (col, row);printf(“ “);col--;gotoxy (col,row);printf(“_”);delay (100);

}break;

case 77: // right arrowwhile (col < 80){

gotoxy (col, row);printf(“ “);col++;gotoxy (col,row);printf(“_”);delay (100);

}break;

switch-statement Example (3)

Page 5: 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.

5

case 72: // up arrowwhile (row > 0){

gotoxy (col, row);printf(“ “);row--;gotoxy (col,row);printf(“_”);delay (100);

}break;

case 80: // down arrowwhile (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)

Page 6: 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.

6

Duplication Input Problem

• Consider the problem: 5 input numbers, find the average and the input numbers which are above the average.

EXAMPLE EXAMPLE #include <stdio.h>#include <stdio.h>main()main(){{ int i, number, sum, average;int i, number, sum, average;

float average;float average; sum = 0;sum = 0;

for (i=0; i< 5; i++)for (i=0; i< 5; i++){{ printf(“input number: “);printf(“input number: “); scanf(“%d”,&number);scanf(“%d”,&number); sum += number;sum += number;

}}}}

Page 7: 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.

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.)

Page 8: 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.

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.

Page 9: 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.

9

Arrays Declaration 1Arrays Declaration 1

SYNTAX 1SYNTAX 1

element-type element-type namename[size];[size];

EXAMPLE EXAMPLE

double a[5];double a[5];

int x[10];int x[10];

float y[20];float y[20];

Page 10: 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.

10

Arrays

main()main()

{ {

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

Page 11: 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.

11

Arrays

• x[3] refers to the element in x[3] refers to the element in xx whose index is 3. whose index is 3.

• Array declaration in C: Array declaration in C: – int x[10] int x[10]

(an array with storage space for 10 integers)(an array with storage space for 10 integers)

(fixed (nonvariable) size: declared before (fixed (nonvariable) size: declared before execution)execution)

– first element: x[0], last element: x[n-1] (I.e., first element: x[0], last element: x[n-1] (I.e., x[9]) x[9])

(homogeneous data structure / uniform data (homogeneous data structure / uniform data type)type)

Page 12: 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.

12

• Array subscriptArray subscript is used to is used to differentiatedifferentiate between the between the individual array elements and to allow us to specify which individual array elements and to allow us to specify which array element is to be manipulated.array element is to be manipulated.

Arrays SubscriptionArrays Subscription

ExampleExample: :

int x[5];int x[5];

x[0] = 10x[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];

Page 13: 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.

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 SubscriptionArrays Subscription

ExampleExample::

int a[5];int a[5];

a[10] = 10; a[-1] =0; a[10] = 10; a[-1] =0;

!!! Run-time error !!!!!! Run-time error !!!

Page 14: 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.

14

Entering Data into the Array

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

Page 15: 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.

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);

Page 16: 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.

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 array Set 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)(Ex.: compare the 4rd and the 12th elements)

float rainfall [12];float rainfall [12];

if (rainfall[3] > rainfall[11])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;rainfall [2] = 2.5;

Page 17: 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.

17

Arrays Declaration 2Arrays Declaration 2

EXAMPLE EXAMPLE

int arr[5] = {0, 0, 3, 2, 1};int arr[5] = {0, 0, 3, 2, 1};

char v [ ] = {‘A’, ’E’, ’I’, ’O’, ’U’};char v [ ] = {‘A’, ’E’, ’I’, ’O’, ’U’};

int arr[5] = {0, 0, 3, 2, 1};int arr[5] = {0, 0, 3, 2, 1};

float x[2] = {0.112, 3.2}float x[2] = {0.112, 3.2}

SYNTAX 2SYNTAX 2

element-type element-type anameaname[size] = {initialization list}[size] = {initialization list}

Page 18: 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.

18

• Multidimensional arrays are defined in much the same Multidimensional arrays are defined in much the same manner as one dimensional arrays, manner as one dimensional arrays,

• Except that a separate pair of square brackets is Except that a separate pair of square brackets is required for each dimension.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:can be written as:

Multi-Dimensional ArraysMulti-Dimensional Arrays

Page 19: 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.

19

MultiDimensional Arrays DeclarationMultiDimensional Arrays Declaration

SYNTAX 1SYNTAX 1

element-type element-type anameaname[size[size11][size][size22]…[size]…[sizenn]; ]; /*decalration*/

element-type element-type anameaname[][size2]…[sizen] [][size2]…[sizen] /* prototype */

element-type element-type anameaname[][]…[] [][]…[] /* prototype */

EXAMPLE EXAMPLE

double table[NROWS][NCOLS]; /* declaration */double table[NROWS][NCOLS]; /* declaration */

int out[ ][4], /* output parameter */int out[ ][4], /* output parameter */

int nrows[ ][ ][ ] /* number of rows */int nrows[ ][ ][ ] /* number of rows */Ex. in a[5];Ex. in a[5];Ex. int mat[3][4];Ex. int mat[3][4];Ex. double vert[x][y][z];Ex. double vert[x][y][z];

Page 20: 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.

20

Examplechar tictac[3][3]; // declaration

Initialization of 2D Arrayschar 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 ArraysMulti-Dimensional Arrays

Page 21: 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.

21

• The array The array enrollenroll declared declared herehere

int int enroll enroll [100][5][4];[100][5][4];

Multi-Dimensional ArraysMulti-Dimensional Arrays