Lecture8_handout

Lecture8_handout - Overview Define arrays Initialize arrays...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 EE140: Introduction to 0t o d u c t o t o Programming Concepts for Engineers Lecture 8 1 Arrays Overview Define arrays Initialize arrays Array sorting bubble sort algorithm Array searching linear search binary search 2 y Arrays An array variable is a set of variables share a common name and type yp all linked together int age[30]; /* 30 integers */ name of array is age array size: total 30 elements in array ‘age’ it has to be an integer with known value (to determine the memory needed to store the array) 3 elements: age[0], age[1], . .., age[29] yg each element is int type starts from 0 Initializing arrays type name[n] = {value0, value1, . .. , value(n-1)}; double height[3] = {5.5, 6.3, 5.8}; int x[] = {1, 2, 3, 4, 5}; If all the initial values are given, the number of elements can be omitted. ompiler will count the number of elements and set size of array ‘x’ to 5 Three elements of array ‘height’ are initialized to 5.5, 6.3, and 5.8. Each element is double type. 4 Co p e cou t t e u be o e e e ts a d set s e o a ay to 5 int y[5] = {1, 2}; First two elements y[0] and y[1] are initialized to 1 and 2. Remaining elements [2]-[4] are initialized to zero implicitly.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Example 1 #include <stdio.h> Defining an array and use a loop to set and display the array's elements values. int main( void ) { int i, n[10]; /* n is an array of 10 integers */ for( i = 0; i < 10; i ++ ) { /*loop 10 times*/ n[i]= 0; /* set all elements to 0 */ printf( "n[%d]=%d\n", i, n[i]); /*display value of n[i] */ } printf("\n"); 5 for( i = 0; i < 10; i ++ ) { /*loop 10 times*/ n[i]= 2*i+1; /* set all the elements to odd number from 1 to 19 */ printf( "n[%d]=%d\n", i, n[i]); /*display value of n[i] */ } return 0; } What is the difference between this program and the previous one?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/04/2011 for the course EE 150 taught by Professor Lanxiang during the Spring '11 term at Montgomery College.

Page1 / 5

Lecture8_handout - Overview Define arrays Initialize arrays...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online