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

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Objectives Understand streamed input and output.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
Chapter 4 Summation.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CS161 Introduction to Computer Science Topic #3.
First steps Jordi Cortadella Department of Computer Science.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Objective: Students will be able to: Declare and use variables Input integers.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
CS 240 Computer Programming 1
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
What Actions Do We Have Part 1
Introduction to C++ October 2, 2017.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Wednesday 09/23/13.
CS150 Introduction to Computer Science 1
Arrays of Two-Dimensions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
What Actions Do We Have Part 1
Reading from and Writing to Files
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

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

Lecture Overview Project 2 hints Return values versus output to screen C++ functions

Project 2 Hints Several ways to read in a file High Level: textscan, tdfread Low Level: fscanf Will show an example using tdfread

Project 2 Hints Assume file songs.txt Columns separated by tabs (tab-delimited) Title Artist 2WickyHooverphonic 98.6Keith …

tdfread Reads 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

Project 2 Hints Sorting by Title Use Selection sort But how do we sort character arrays? Can use a utility function on Project 2 website Download strlexcmp.m from here

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

Using strlexcmp.m strlexcmp(‘Able’, ‘Barry’) ans= strlexcmp(‘Cathy’, ‘Barry’) ans= 1 strlexcmp(‘Barry’, ‘Barry’) ans= 0

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

Function calls graphically

Return versus output to screen #include using namespace std; int square(int x) { return x * x; } int main() { int result = 0; result = square(5); cout << “Square of 5 : ” << result <<endl; }

Putting all together Create a function to sum number from 1 to N What do we need?

C++ Functions: Putting all together #include 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

C++ Functions: Putting all together // 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

C++ Functions: Putting all together // main function above this int sumFrom1ToN(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i; } return sum; }