general computer science for engineers cisc 106 lecture 32 dr. john cavazos computer and information...
Post on 19-Dec-2015
213 views
TRANSCRIPT
![Page 1: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/1.jpg)
General Computer General Computer Science Science
for Engineersfor EngineersCISC 106CISC 106
Lecture 32Lecture 32
Dr. John CavazosComputer and Information Sciences
05/08/2009
![Page 2: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/2.jpg)
Lecture OverviewLecture OverviewMore Project 2 hintsC++ while loops
![Page 3: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/3.jpg)
Project 2 HintsProject 2 Hints
Several ways to read in a fileHigh Level: textscan, tdfread, textread
Low Level: fscanfAn example using textread
![Page 4: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/4.jpg)
Songs.txt fileSongs.txt fileColumns separated by tabs (tab-
delimited)
songs.txt
Title Artist Year
2Wicky Hooverphonic 1997
98.6 Keith 1967… …
![Page 5: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/5.jpg)
Using textreadUsing textreadNeed to give format specifierNote: All on same line
Can use ellipsis (…) to continue line on next line
[title artist year] = textread('songs.txt', '%s %s %d',
'headerlines', 1,
'delimiter','\t')
![Page 6: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/6.jpg)
Using textreadUsing textread
[title artist year] = textread('songs.txt', '%s %s %d',
'headerlines', 1,
'delimiter','\t')Calling textread with name of songs file.
![Page 7: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/7.jpg)
Using textreadUsing textread
[title artist year] = textread('songs.txt', '%s %s %d',
'headerlines', 1,
'delimiter','\t')Format specifier. Expects:1st field → string2nd field → string3rd field → integer
![Page 8: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/8.jpg)
Using textreadUsing textread
[title artist year] = textread('songs.txt', '%s %s %d',
'headerlines', 1,
'delimiter','\t')1st line is a header line andtext fields separated by tabs (delimited)
![Page 9: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/9.jpg)
Using textreadUsing textread
[title artist year] = textread('songs.txt', '%s %s %d',
'headerlines', 1,
'delimiter','\t')textread returns three arrays. Types of arrays based on format specifier.
![Page 10: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/10.jpg)
Textread returns three Textread returns three arraysarrays
[title artist year] = …title = '2Wicky’ '98.6’ '#9 Dream’ 'ABC’
artist = 'Hooverphonic’ 'Keith’ 'John Lennon’ 'Jackson 5’…
![Page 11: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/11.jpg)
Looping through Title Looping through Title arrayarray
for j = 1:length(title) title(j)end
ans = '2Wicky’
ans = '98.6’…
![Page 12: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/12.jpg)
Put data in structure arrayPut data in structure arrayS=struct('title',{}, 'artist',{}, 'year',
{});
S(1).title = char(title(1));S(1).artist = char(artist(1));S(1).year = year(1);
S(1).artistans = 'Hooverphonic'
![Page 13: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/13.jpg)
Put data in structure arrayPut data in structure arrayS=struct('title',{}, 'artist',{}, 'year',
{});
S(1).title = char(title(1));S(1).artist = char(artist(1));S(1).year = year(1);
S(1).artistans = 'Hooverphonic'
Create an emptystructure
![Page 14: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/14.jpg)
Put data in structure arrayPut data in structure arrayS=struct('title',{}, 'artist',{}, 'year',
{});
S(1).title = char(title(1));S(1).artist = char(artist(1));S(1).year = year(1);
S(1).artistans = 'Hooverphonic'
Put first element of each array into structure; char(…) converts the cell array of strings to a character array to work with strlexcmp.
![Page 15: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/15.jpg)
C++ while loopC++ while loop
… // main functionint j = 1;
while (j < 3) { cout << “value of j is : “ << j <<
endl; j=j+1; // can also use j++;}
![Page 16: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/16.jpg)
C++ while loopC++ while loop
… // main functionint j = 1;
while (j < 3) { cout << “value of j is : “ << j <<
endl; j=j+1; // can also use j++;}
Loops while j less then 3
![Page 17: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/17.jpg)
C++ while loopC++ while loop
… // main functionint j = 1;
while (j < 3) { cout << “value of j is : “ << j <<
endl; j=j+1; // can also use j++;}
Body of loop; executed 2 times
![Page 18: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/18.jpg)
C++ Functions: Putting all C++ Functions: Putting all togethertogether#include <iostream>
using namespace std;
int main() int height = 0, maxheight = 0; cout << "Enter heights: (enter end of file
to end): ";
// rest on next slide
![Page 19: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009](https://reader030.vdocuments.us/reader030/viewer/2022032800/56649d3a5503460f94a14c8e/html5/thumbnails/19.jpg)
C++ Functions: Putting all C++ Functions: Putting all togethertogether// cont’d from last slide while(cin >> height) { if( height > maxheight) maxheight = height; } cout << "Tallest person's height = " <<
maxheight << endl; return 0;}