EEE 121 Structured Programming Language

Instructor: Tishna Sabrina Spring 2014 Lecture 11: Arrays

Relational. Bitwise operators Expressions Selection Statement Iteration Statement Jump Statement Structured Programming Language: Lecture 10 2 3/19/2014 . Logical.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.

  Lowest address corresponds to the first element. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations.  Arrays may have from 1 to multiple dimensions. Highest address corresponds to the last element. .What is an Array    An array is a collection of variables of the same type that are referenced by a common name.

14 8.14 8.g. float. Name N q r type integer float float address 28C4 28C8 28CC value 35 3. char) address value N q r 35 3.Review of scalar variables Variables (up to now) have: name type (int.9 28C4 (int) 28C8 (float) 28CC (float) e.9 3/19/2014 Structured Programming Language: Lecture 11 4 . double.

x[7]+x[1]).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]). // prints 115 3/19/2014 Structured Programming Language: Lecture 11 5 . printf("%d".

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

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 7 .Declaring Arrays Define an 8 element array: int x[8].

You can also define the values to be held in the array and instruct the compiler to figure out how many elements are needed.14159.718 4440 0.23 4430 3.7071 4448 3/19/2014 Structured Programming Language: Lecture 11 8 . A[0] A[1] A[2] A[3] 1. Not putting a value within the [] tells the compiler to determine how many locations are needed.7071 }. 2. 3.718.14159 4438 2.23.Declaring/defining Arrays double A[]={ 1. 0.

i+1). int i. } } 3/19/2014 Structured Programming Language: Lecture 11 9 . i<8. // get 8 values into x[] for (i=0. i++) { printf("Enter test %d:".&x[i]). scanf("%d".h> void main(void) { int x[8].Working with Arrays (input) #include <stdio.

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 .

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

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 .)    Although x has 8 elements.Pitfalls (cont.

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 variable’s data or even into the program’s code. It is programmer’s 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.

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