Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Advertisements

Revision.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Chapter 4 Summation.
Functions:Passing Parameters by Value Programming.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
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.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Previously Repetition Structures While, Do-While, For.
First steps Jordi Cortadella Department of Computer Science.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
A Sample Program #include using namespace std; int main(void) { cout
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS 240 Computer Programming 1
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Recursion Chapter 12.
C++ Programming: CS150 For.
Chapter 2 Assignment and Interactive Input
Introduction to C++ October 2, 2017.
Multiple Files Revisited
Random Number Generation
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Screen output // Definition and use of variables
CS150 Introduction to Computer Science 1
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Lab 1 Introduction to C++.
Pass by Reference.
CS150 Introduction to Computer Science 1
Dynamic Memory A whole heap of fun….
If Statements.
Let’s all Repeat Together
CS1201: Programming Language 2
Engineering Problem Solving with C++ An Object Based Approach
Statements and flow control
CS150 Introduction to Computer Science 1
Fundamental Programming
Recursion.
Using string type variables
Pointers & Functions.
The Stack.
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CS31 Discussion 1D Winter19: week 4
Presentation transcript:

Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.

Recursion (Frames and The Stack) void foo( int x) x = 0 { if ( x < 4 ) { // Recursive step cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; // Base case } void foo( int x) x = 1 { if ( x < 4 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 2 { if ( x < 4 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 3 { if ( x < 4 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 4 { if ( x < 4 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } foo(0); Hello 0 Hello 1 Hello 2 Goodbye 3 Goodbye 2 Goodbye 1 Hello 3 Goodbye 0

Recursion (Frames and The Stack) void foo( int x) x = 0 { if ( x < 2 ) { // Recursive step cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; // Base case } void foo( int x) x = 1 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } foo(0) Hello 0 Hello 1 Goodbye 0 Goodbye 1 Hello 1 void foo( int x) x = 1 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } Goodbye 1 void foo( int x) x = 2 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 2 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 2 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; } void foo( int x) x = 2 { if ( x < 2 ) { cout << “ Hello “ << x <<endl; foo(x+1); cout << “ Goodbye ” << x << endl; } return; }

Recursion Program Trace Fib Fact Visualization of Recursion with Java

Exam 1 Anatomy of a Computer Types and Variables Operators –Mathematical –Boolean Control Structures –Selection (if…else, switch) –Iteration(while, for)

Exam 1 Input and Output –cin, cout, >>, << Functions –Calling Functions –Function Prototypes –Function Definitions –Function overloading

Sample Problem Find the bug in this code: #include using namespace std int main( ) { cout << "hello world!" << endl; return 0; }

Sample Problem Find the bug in this code: #include using namespace std; int main( ) { double width, area; cout<<"Enter the width of the square: "; cin >> width; Area = width * width; cout << "The area of the square with the width = " << width << " is " << Area << endl; return 0; }

Sample Problem Find the bug in this code: #include int main() { int i = 30; while( i != 100 ) { i++ if ( i == 100 ) { i = i – 1; } cout << i << “ “ << endl; }

Sample Problem Find the bug in this code: #include int test( int n ); int main() { int n = 5; cout << test( n ); return 0; } int test( int ) { return (n+1); }

Sample Problem Find the bug in this code: #include (iostream) int main() { int i = 30; while( i < 100 ) { i++ if ( i == 100 ) { i = i + 1; } cout << i << “ “ << endl; }

Sample Problem Calculate the value of n: int a = 3.3, b = 2.7, p = 8.7, q = 5.4; double n = p/b + q/a %;

Sample Problem Calculate the value of n: int a = 3.3, b = 2.7, p = 8.7, q = 5.4; double n = (p/b + q/a) % 3; Ans: 2

Sample Problem Give the value of n after this code: int n = 3, a = 2, b = 7, c = 4; if (b>6 && c<4 || a==0) { n = n + 5; } if (a>3 && b>5 || c<=9) { n = n + 20; } if (c>2 || a<8 && b==7) { n = n - 5; } Ans: 18

Sample Problem Give the value of n after this code: int n = 3, a = 2, b = 7, c = 4; if (b>6 && c<4 || a==0) { n = n + 5; } if (a>3 && b>5 || c<=9) { n = n + 20; } if (c>2 || a<8 && b==7) { n = n - 5; } Ans: 18

Sample Problem What does this program print? for ( int i = 0; i < 5; i++ ) { for ( int j = 0; j < i; j++ ) { cout << j << “ “; } cout << endl; } Ans:

Sample Problem What does this code print? #include using namespace std; int main() { int n = 1; for ( int i = 1; i < 6; i++ ) { n *= i; cout << n << " "; } Ans:

Sample Problem What does this code print? #include using namespace std; int main() { int n = 1; for ( int i = 0; i < 6; i++ ) { n *= i; cout << n << " "; } Ans:

Sample Problem What does this code print? int n = 1; while ( n < 5 ) { for ( int i = 1; i < 5; i++ ) { cout << n++ << " "; cout << ++n << " "; } Ans:

Sample Problem Write a program that sums the even numbers between 1 and 100 inclusive. Ans: #include int main() { int sum = 0; for ( int i = 1; i <= 100; i++ ) { if ( i % 2 == 0) { sum = sum + i; }

Sample Problem What does the following code print: char foo = ‘b’, bar = ‘a’; n = 4; while ( n < 20 ) { if ( n % 4 == 0) { cout << foo; } if ( n % 2 == 0) { cout << bar; } n = n + 2; } cout << ‘c’; Ans: baabaabaabaac What does the following code print: char foo = ‘b’, bar = ‘a’; n = 4; while ( n < 20 ) { if ( n % 4 == 0) { cout << foo; } else if ( n % 2 == 0) { cout << bar; } n = n + 2; } cout << ‘c’; Ans: babababac

Sample Problem What does the following code print: #include using namespace std; int cow( int ); int sheep( int ); int main() { int n = 1; for ( int i = 1; i < 3; i++ ) { cout << "quack " << endl; cow( i + 1); } Ans: quack moo moo baa moo baa baa quack moo moo baa moo baa baa int cow( int x ) { for ( int i = 0; i < 3; i++ ) { cout << "moo "; sheep( i ); } return 0; } int sheep( int x ) { for ( int i = 0; i < x; i++ ) { cout << "baa "; } return x; }

Sample Problem What does the following code print: #include using namespace std; int test( int x, int y ); int main() { int j = 1; while( test( j, j ) < 8*j ) { cout << j << " "; j = j * 2; } return 0; } int test( int x, int y ) { return (x * y); } Ans: 1 2 4