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.
e.g.
Name N q r
3/19/2014
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
3/19/2014
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
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.
3/19/2014
3/19/2014
3/19/2014
10
Pitfalls
#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
12
Single-Dimension 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;