Lecture6

Lecture6 - ECE15: Introduction to Computer Programming...

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

View Full Document Right Arrow Icon
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 6: Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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!
Background image of page 3

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

View Full DocumentRight Arrow Icon
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:
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

Lecture6 - ECE15: Introduction to Computer Programming...

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

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