You are on page 1of 14

EEE 121 Structured Programming Language

Instructor: Tishna Sabrina Spring 2014 Lecture 11: Arrays

Key points covered so far


Introduction & overview of C Basic console I/O, Formatted I/O Use of escape sequences Use of comment in programming Data types Constants Variables Arithmetic, Logical, Relational, Bitwise operators Expressions Selection Statement Iteration Statement Jump Statement
Structured Programming Language: Lecture 10 2

3/19/2014

What is an Array

An array is a collection of variables of the same type that are referenced by a common name. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations.

Lowest address corresponds to the first element. Highest address corresponds to the last element.

Arrays may have from 1 to multiple dimensions.

Review of scalar variables


Variables (up to now) have: name type (int, float, double, char) address value N q r 35 3.14 8.9 28C4 (int) 28C8 (float) 28CC (float)

e.g.

Name N q r

type integer float float

address 28C4 28C8 28CC

value 35 3.14 8.9

3/19/2014

Structured Programming Language: Lecture 11

Intro to Arrays
Any single element of x may be used like any other scalar x[0] variable x[1] x[2] x[3] x[4] x[5] x[6] x[7] 45 3044 55 3048 25 304C 85 3050 75 3054 65 3058 100 305C 60 3060

// prints 85 printf("%d",x[3]); printf("%d",x[7]+x[1]); // prints 115

3/19/2014

Structured Programming Language: Lecture 11

Single-Dimension Arrays

Syntax:
type var_name[size];

type declares the base type of the array, which is the type of each element of array. size defines how many elements the array will hold. zero is the index of the first element. size-1 is the index of the last element. Memory area needed for an array is:
total area in bytes = sizeof( type ) * size of array

Declaring Arrays
Define an 8 element array: int x[8]; Elements numbered 0 to 7 Arrays in C always start with location 0 (zero based) The initial value of each array element is unknown (just like scalar variables) x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] ? 3044 ? 3048 ? 304C ? 3050 ? 3054 ? 3058 ? 305C ? 3060

3/19/2014

Structured Programming Language: Lecture 11

Declaring/defining Arrays
double A[]={ 1.23, 3.14159, 2.718, 0.7071 };

You can also define the values to be held in the array and instruct the compiler to figure out how many elements are needed. Not putting a value within the [] tells the compiler to determine how many locations are needed.

A[0] A[1] A[2] A[3]

1.23 4430 3.14159 4438 2.718 4440 0.7071 4448

3/19/2014

Structured Programming Language: Lecture 11

Working with Arrays (input)


#include <stdio.h> void main(void) { int x[8]; int i; // get 8 values into x[] for (i=0; i<8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); } }

3/19/2014

Structured Programming Language: Lecture 11

Working with Arrays (input)


Sample run (user input underlined): Enter Enter Enter Enter Enter Enter Enter Enter test test test test test test test test 1:80 2:75 3:90 4:100 5:65 6:88 7:40 8:90 x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 80 75 90 100 65 88 40 90

3/19/2014

Structured Programming Language: Lecture 11

10

Pitfalls

What happens if we change previous code to:

#include <stdio.h> void main(void) { int x[8]; int i; float sum, avg; // used later // get 8 values into x[] for (i=0; i<=8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); } }
3/19/2014 Structured Programming Language: Lecture 11 11

Pitfalls (cont.)

Although x has 8 elements, x[8] is not one of those elements! Compiler will not stop you from accessing elements outside the array Must make sure you know the size of the array

3/19/2014

Structured Programming Language: Lecture 11

12

Single-Dimension Arrays

C does not do any bound checking on arrays.

Programmer could overwrite either end of an array and write into some other variables data or even into the programs code. It is programmers job to provide bounds checking where needed.

Single-dimension arrays are essentially lists of information of the same type that are stored in contiguous memory locations in index order.

Examples
// a 100-element array of type double // name of the array is balance double balance[100]; char s[10]; // s is an array that can hold 10 char // s[0] thru s[9] void main( void ) { int t, x[100]; // 100 integer array for( t=0; t<100; t++ ) x[t] = t; } // bound check error int count[10], t; // this causes count to be overrun for( t=0; t<100; t++ ) count[t] = 10;

You might also like