{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture8_handout

# Lecture8_handout - Overview Define arrays Initialize arrays...

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

1 EE140: Introduction to 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 Arrays An array variable is a set of variables share a common name and type 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] 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. Compiler 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 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.

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

View Full Document
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?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online