General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information...

19
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 32 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information...

Page 1: General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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;}