C++ while Loops - Lecture Slides | CISC 106, Study notes of Computer Science

Material Type: Notes; Class: General Computer Science for Engineers; Subject: Computer/Information Sciences; University: University of Delaware; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-7cn
koofers-user-7cn 🇺🇸

9 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dr. John Cavazos
Computer and Information Sciences
05/08/2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download C++ while Loops - Lecture Slides | CISC 106 and more Study notes Computer Science in PDF only on Docsity!

Dr. John Cavazos Computer and Information Sciences 05/08/

 More Project 2 hints  C++ while loops

 Columns separated by tabs (tab-delimited)

songs.txt

Title Artist Year

2Wicky Hooverphonic 1997

98.6 Keith 1967

 Need to give format specifier  Note: 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')

[title artist year] = textread('songs.txt', '%s %s %d', 'headerlines', 1, 'delimiter','\t') Format specifier. Expects: 1 st field → string 2 nd field → string 3 rd field → integer

[title artist year] = textread('songs.txt', '%s %s %d', 'headerlines', 1, 'delimiter','\t') 1 st line is a header line and text fields separated by tabs (delimited)

 [title artist year] = … title = '2Wicky’ '98.6’ '#9 Dream’ 'ABC’ artist = 'Hooverphonic’ 'Keith’ 'John Lennon’ 'Jackson 5’ …

for j = 1:length(title)

title(j)

end

ans = '2Wicky’ ans = '98.6’ …

 S=struct('title',{}, 'artist',{}, 'year',{}); S(1).title = char(title(1)); S(1).artist = char(artist(1)); S(1).year = year(1); S(1).artist ans = 'Hooverphonic'

Create an empty

structure

 S=struct('title',{}, 'artist',{}, 'year',{}); S(1).title = char(title(1)); S(1).artist = char(artist(1)); S(1).year = year(1); S(1).artist ans = 'Hooverphonic' Put first element of each array into structure; char(…) converts the cell array of strings to a character array to work with strlexcmp.

… // main function int 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

… // main function int 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

// cont’d from last slide while(cin >> height) { if( height > maxheight) maxheight = height; } cout << "Tallest person's height = " << maxheight << endl; return 0; }