cs 1001 programming in c lecture 5
TRANSCRIPT
-
8/14/2019 CS 1001 Programming in C Lecture 5
1/19
s
s
CS 1001 Programming in CLecture 5
Arrays
-
8/14/2019 CS 1001 Programming in C Lecture 5
2/19
Arrays in Action
are 2D arrays (matrices) of numbers:
-
8/14/2019 CS 1001 Programming in C Lecture 5
3/19
Array
An array is a set of variables,represented by a single name. The individual variables are called
elements and are identified by indexnumbers. The following example declares an
array with ten elements.int buf[10];
-
8/14/2019 CS 1001 Programming in C Lecture 5
4/19
Indexing
The first element in the array has anindex number of zero. Therefore, the array buf has tenelements, indexed from zero to nine.
buf[0]buf[1]
.
.buf[9]
-
8/14/2019 CS 1001 Programming in C Lecture 5
5/19
Accessing the Elements
To access an individual element in thearray, the index number follows thevariable name in square brackets.
The variable can then be treated likeany other variable in C. The following example assigns a value
6 to the first element in the array buf .buf[0] = 16;
-
8/14/2019 CS 1001 Programming in C Lecture 5
6/19
wArray
The example prints the value of thethird element in array buf .
printf("%d\n", buf[2]);
The example uses the scanf functionto read a value from the keyboard intothe last element of array buf .
scanf("%d", &x[9]);
-
8/14/2019 CS 1001 Programming in C Lecture 5
7/19
Initializing Array Elements
Arrays can be initialized like any othervariables by assignment.As an array contains more than one
value, the individual values are placedin curly braces, and separated withcommas.
The example initializes a tendimensional array with the first tenvalues of the three times table.int x[10] = {3, 6, 9, 12, 15, 18, 21, 24, 27, 30};
-
8/14/2019 CS 1001 Programming in C Lecture 5
8/19
Looping through an Array
As the array is indexed sequentially,we can use the for loop to display allthe values of an array.
The example displays all the values of an array.
-
8/14/2019 CS 1001 Programming in C Lecture 5
9/19
Example#include
void main (void)
{
int x[10]={3,6,9,12,15,18,21,24,27,30};
int i;
for(i=0; i
-
8/14/2019 CS 1001 Programming in C Lecture 5
10/19
Assignment
This saves assigning the values individuallyint x[10];x[0] = 3;x[1] = 6;
x[2] = 9;x[3] = 12;x[4] = 15;x[5] = 18;x[6] = 21;x[7] = 24;x[8] = 27;
x[9] = 30;
-
8/14/2019 CS 1001 Programming in C Lecture 5
11/19
Example
The program reads a line, stores it in abuffer, and prints its length (excludingthe newline at the end).
-
8/14/2019 CS 1001 Programming in C Lecture 5
12/19
void main(void )
{int n, c;
char line[100];
n = 0;
while( (c=getchar( )) != '\n' ) {
if( n < 100 )
line[n] = c;
n++;}
printf("length = %d\n", n);
}
-
8/14/2019 CS 1001 Programming in C Lecture 5
13/19
Working with Arrays
Example: adds up the elements of anarray:
int array[5]={2, 3, 4, 5,2}int sum = 0;for( i=0; i
-
8/14/2019 CS 1001 Programming in C Lecture 5
14/19
Arrays of Different Types
int ID[30];/* Could be used to store the IDnumbers of students in a class */
float temperatures[31];/* Could be used to store thedaily temperatures in a month */
char name[20];/* Could be used to store a
character string*/
-
8/14/2019 CS 1001 Programming in C Lecture 5
15/19
Multidimensional Arrays
An array can have more than onedimension.By allowing the array to have more
than one dimension provides greaterflexibility.Spreadsheets are built on a two
dimensional array; an array for therows, and an array for the columns.
-
8/14/2019 CS 1001 Programming in C Lecture 5
16/19
Example
The example uses a two dimensionalarray with two rows, each containingfive columns.
-
8/14/2019 CS 1001 Programming in C Lecture 5
17/19
#include
void main(){
/* Declare a 2 x 5 multidimensional array */
int x[2][5] = { {1, 2, 3, 4, 5},
{2, 4, 6, 8, 10} };int row, column;
for (row=0; row
-
8/14/2019 CS 1001 Programming in C Lecture 5
18/19
Storage in Memory
Since computer memory is essentially one-dimensional, with memory locations runningstraight from 0 up through the highestlocation in memory.
A multidimensional array cannot be stored inmemory as a grid. Instead, the array isdissected and stored in rows.Consider the two-dimensional array.
-
8/14/2019 CS 1001 Programming in C Lecture 5
19/19
Storage
---------row 0 | 1 | 2 | 3 |---------
row 1 | 4 | 5 | 6 |---------
row 2 | 7 | 8 | 9 |---------
To the computer, the array above actually "looks" like this:
--------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |--------------------------| row 0 | row 1 | row 2 |