general computer science for engineers cisc 106 lecture 31 dr. john cavazos computer and information...

15
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 31 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

General Computer General Computer Science Science

for Engineersfor EngineersCISC 106CISC 106

Lecture 31Lecture 31

Dr. John CavazosComputer and Information Sciences

05/06/2009

Page 2: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Lecture OverviewLecture OverviewProject 2 hintsReturn values versus output to

screenC++ functions

Page 3: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Project 2 HintsProject 2 HintsSeveral ways to read in a fileHigh Level: textscan, tdfreadLow Level: fscanfWill show an example using tdfread

Page 4: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Project 2 HintsProject 2 HintsAssume file songs.txtColumns separated by tabs (tab-

delimited)

Title Artist2Wicky Hooverphonic98.6 Keith…

Page 5: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

tdfreadtdfreadReads a tab-delimited file

Expects file with variable names in first row

Returns a structure

S=tdfread(“songs.txt”);

S.Title(1,:) S.Artist(1,:) ans = ans=2Wicky Hooverphonic

Page 6: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Project 2 HintsProject 2 HintsSorting by TitleUse Selection sort

But how do we sort character arrays?

Can use a utility function on Project 2 websiteDownload strlexcmp.m from herehttp://www.udel.edu/CIS/106/cavazos/

09S/project2/

Page 7: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

strlexcmp.mstrlexcmp.mstrlexcmp(string1,string2) If string1 comes before string2 returns -1 if string1 and string2 are equal returns 0 if string1 comes after string2 return 1

Page 8: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Using strlexcmp.mUsing strlexcmp.mstrlexcmp(‘Able’, ‘Barry’)ans= -1

strlexcmp(‘Cathy’, ‘Barry’)ans= 1

strlexcmp(‘Barry’, ‘Barry’)ans= 0

Page 9: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Remember Remember S=tdfread(‘songs.txt’) S=tdfread(‘songs.txt’) S.Artist(1,:)ans= Hooverphonic

S.Artist(2,:)ans= Keith

strlexcmp(S.Artist(1,:), S.Artist(2,:))ans= -1

Page 10: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Function calls graphicallyFunction calls graphically

Page 11: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Return versus output to Return versus output to screenscreen#include <iostream>using namespace std;

int square(int x) { return x * x;}

int main() { int result = 0; result = square(5); cout << “Square of 5 : ” << result

<<endl;}

Page 12: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

Putting all togetherPutting all togetherCreate a function to sum number

from 1 to NWhat do we need?

Page 13: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

C++ Functions: Putting all C++ Functions: Putting all togethertogether#include <iostream>using namespace std;

int sumFrom1ToN(int); // function prototype

int main() { int n = 0, sum = 0; // declare some

variables cout << “Enter a number \n”; // ask

for input cin >> n; // put user input in

variable n

// the rest of main is on next slide

Page 14: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

C++ Functions: Putting all C++ Functions: Putting all togethertogether// main function continued

sum = sumFrom1ToN(n); // call function

cout << “The sum of numbers to ” << n;

cout << “ is ” << sum << endl; return 0;}

// the rest of program is on next slide

Page 15: General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009

C++ Functions: Putting all C++ Functions: Putting all togethertogether// main function above this

int sumFrom1ToN(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i; } return sum;}