array - s3.amazonaws.com

Download Report

Transcript array - s3.amazonaws.com

Computer
Programming
• Lecture 8
• Arrays
switch-statement Example (3)
If use press up arrow
If use press left arrow
If use press right arrow
If use press down arrow
2
switch-statement Example (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)
{
3
switch-statement Example (3)
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;
4
switch-statement Example (3)
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
5
Duplication Input Problem
• Consider the problem: 5 input numbers, find the average
and the input numbers which are above the average.
EXAMPLE
#include <stdio.h>
main()
{
int i, number, sum, average;
float average;
sum = 0;
for (i=0; i< 5; i++)
{
printf(“input number: “);
scanf(“%d”,&number);
sum += number;
}
}
6
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.)
7
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.
8
Arrays Declaration 1
SYNTAX 1
element-type name[size];
EXAMPLE
double a[5];
int x[10];
float y[20];
9
Arrays
main()
{
int x[10];
…
}
10-element array called x
10 boxes for keeping 10 integer numbers
X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8] X[9]
10
Arrays
• x[3] refers to the element in x whose index is 3.
• 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)
11
Arrays Subscription
• Array subscript is used to differentiate between the individual
array elements and to allow us to specify which array element is to
be manipulated.
Example:
int x[5];
x[0] = 10
x[1] = 10 + 5;
x[2] = x[1];
x[3] = 2*x[1];
x[4] = x[1] + 3*x[3];
12
Arrays Subscription
•
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 runtime error.
Example:
int a[5];
a[10] = 10; a[-1] =0;
!!! Run-time error !!!
13
Entering Data into the Array
• Example:
for (int day = 0; day < 7; day ++ )
{
printf( “Enter temperature for day“);
scanf (“%d”, &week [day] );
}
14
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);
15
A Simple Example of Using Arrays
•
Create an array (Ex. : a 12 floating point array)
float rainfall [12];
•
Set a value to an element in an array
(Ex.: set the third element of rainfall to 2.5)
rainfall [2] = 2.5;
•
Compare two elements of an array
(Ex.: compare the 4rd and the 12th elements)
if (rainfall[3] > rainfall[11])
printf (“more rain in April than in December”);
16
Arrays Declaration 2
SYNTAX 2
element-type aname[size] = {initialization list}
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}
17
Multi-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.
•
•
•
Two-dimensional array ---->
[ ][ ]
Three-dimensional array ----->
[ ][ ][ ]
In general term, the multidimensional array definition
can be written as:
18
MultiDimensional Arrays Declaration
SYNTAX 1
element-type aname[size1][size2]…[sizen]; /*decalration*/
element-type aname[][size2]…[sizen] /* prototype */
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];
19
Multi-Dimensional Arrays
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’}};
20
Multi-Dimensional Arrays
• The array
enroll declared
here
int enroll [100][5][4];
21