general computer science for engineers cisc 106 lecture 19 dr. john cavazos computer and information...
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/1.jpg)
General Computer General Computer Science Science
for Engineersfor EngineersCISC 106CISC 106
Lecture 19Lecture 19
Dr. John CavazosComputer and Information Sciences
04/06/2009
![Page 2: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/2.jpg)
![Page 3: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/3.jpg)
Lecture OverviewLecture OverviewWhile loopsArraysLinear Search
![Page 4: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/4.jpg)
WHILE Loops
> i = 0;
> while (i < 5) ← condition that gets tested
i
i = i+1; ← increment loop variable
end
![Page 5: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/5.jpg)
In class assignmentCode a while loop so that prints only even numbers up to and including 10
![Page 6: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/6.jpg)
Arrays
5 4 3 2 1
A = [ 5 4 3 2 1]
A
![Page 7: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/7.jpg)
Arrays
5 2 3 2 1
Change even positions to have 2
![Page 8: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/8.jpg)
Arrays
5 2 3 2 1
Easy way A(2) = 2A(4) = 2
What happens if have 100 elements!
![Page 9: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/9.jpg)
Using a While loop for this.
>> x = 1;>> while ( x <= length (a) )
if (mod (x,2) == 0) a(x) = 2; end x = x + 1;end
![Page 10: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/10.jpg)
Finding a number in an array
Write pseudo code first!. What are the data structures needed?
. Do I need a loop? What kind would work best?
. Write algorithm in English.
![Page 11: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/11.jpg)
Pseudo code
- Store numbers in array- Initialize array index to 1- Initialize number found boolean
variable- while number not found or
array length not exceeded- if current array position has
num- set number found - end- If number found report!
![Page 12: General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009](https://reader036.vdocument.in/reader036/viewer/2022062714/56649d3f5503460f94a180bd/html5/thumbnails/12.jpg)
Finding a number in an array
>> x = 1;>> numberFound = false;>> while (numberFound == false) if (a(x) == 3.14) numberFound = true; end end
What else is missing?