Lecture6

# Lecture6 - ECE15 Introduction to Computer Programming Using...

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

ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 6: Arrays

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

View Full Document
Introduction to Arrays Array Initialization and Referencing Computing Average Value using Arrays Example: Computing the Median Example: Sieve of Eratosthenes Lecture Unit 6 ECE15: Introduction to Computer Programming Using the C Language 2 Outline of This Lecture
But these variables cannot be processed in a loop! Observation: You cannot possibly compute all this while reading the salaries from the input. The 800 salaries must be stored somewhere. Why Do We Need Arrays? Lecture Unit 6 ECE15: Introduction to Computer Programming Using the C Language 3 Your Task: There are 800 salaried employees in a company. You need to read the 800 salaries from the input, compute the average salary, and determine how many employees are paid above average. . Maybe you can do this? double salary1, salary2, . .., salary800; Solution: Arrays!

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

View Full Document
What's an array? An array is an ordered list of variables of the same type , that can be referenced by subscripting a single ident- ifier with a variable index , for example like this: . Declaring and Referencing Arrays Lecture Unit 6 ECE15: Introduction to Computer Programming Using the C Language 4 The array elements are always stored in consecutive memory cells. The index of the first element in an array is always 0 . The number of elements in an array must be a constant . For example, you cannot do this: salaries[i] double salaries[800]; Declaring an array: int array_size; scanf( "%d" ,&array_size); int my_array[array_size]; salaries[0], salaries[i], salaries[2*i+j], salaries[ exp ] Referencing an array: Notes on Arrays in C:
Initialization of Arrays Lecture Unit 6 ECE15: Introduction to Computer Programming Using the C Language 5 Arrays can be initialized in a loop , like this: for (i = 0 ; i < 800 ; i++) scanf( "%lf" , &salaries[i]); for (i = 0 ; i < 800 ; i++) salaries[i] = 3.5 * i; double prices[ 3 ] = { 7.49 , 9.99 , 1.49 }; int grades[ 5 ] = { 100 , 97 , 79 , 0, 0 }; int grades[ 5 ] = { 100 , 97 , 79 }; equivalent int

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 16

Lecture6 - ECE15 Introduction to Computer Programming Using...

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

View Full Document
Ask a homework question - tutors are online