ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
Advertisements

Programming Recursion.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 4 Summation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
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.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
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.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
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.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Program Development and Design Using C++, Third Edition
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Working with Batches of Data
Basic concepts of C++ Presented by Prof. Satyajit De
ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2
Topic 6 Recursion.
Dr. Shady Yehia Elmashad
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Dr. Shady Yehia Elmashad
توابع در C++ قسمت اول اصول كامپيوتر 1.
One-Dimensional Array Introduction Lesson xx
Loops (iterations, repetitions)
Pass by Reference.
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
Fundamental Programming
CS 101 First Exam Review.
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
Recursive Algorithms 1 Building a Ruler: drawRuler()
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.
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Presentation transcript:

ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2

Exercise 1 HomeWork1 Condition Result (x>y) && !y Given the following assignment of variables to values: Fill in the result values of the conditions in the table below Condition Result (x>y) && !y (item>MIN)||(DAY!=’M’) 1 ((num*128)<power)&&y (!(power!=MAX))&&(Sens==num) ((y+x)<num)||(DAY==’M’) (Sens*(!y))!=0 (!x||y)&&(!y||x) 10/2013

HomeWork1 Exercise 2 Write a program in C++ that performs the following tasks: Read three integer values using cin. Determine the maximum of the three values entered by the user. Print the maximum of this three values using cout. int main() { int a, b, c; cout << "Enter the first integer : " ; cin >> a; cout << "Enter the second integer : " ; cin >> b; cout << "Enter the third integer : " ; cin >> c ; cout << endl; if((a > b) && (a > c)) cout << "Maximum integer is " << a <<endl; } else if((b > c ) && (b > a)) cout << "Maximum integer is " << b <<endl; else cout << "Maximum integer is " << c << endl; return 0; 10/2013

Exercise 3 HomeWork1 int main() { int a, sum = 0; do Write a program that asks the user to type numbers. After each entry, the program should report the cumulative sum of the entries. The program should terminate when the user enters 0. int main() { int a, sum = 0; do cout << "Enter the number : " ; cin >> a; sum = sum + a; }while( a!=0); cout << "Sum is " << sum << endl; return 0; } 10/2013

HomeWork1 Exercise 4 Create a program to determine the GCD (Greatest Common Divisor) of two integers x and y using a ‘while loop’. Formal description of the Euclidean algorithm Input Two positive integers, a and b. Output The greatest common divisor, g, of a and b. Internal computation If a<b, exchange a and b. Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b. Replace a by b and replace b by r. Return to the previous step. 10/2013

Exercise 4 HomeWork1 int main() { int a, b, r; cout << "Enter the two integers to determine the GCD : "; cin >> a >> b; while((a < 0) || (b < 0)) { cout << "Don't enter the negative number! Please.. enter again : "; } if(a < b) { int temp; temp = a; a = b; b = temp; do { r = a % b; if( r == 0) { cout << "The GCD of a and b is "<< b << endl; break; else b = r; } while(1); return 0; Exercise 4 10/2013

HomeWork2 Exercise 1 1) Write a function that computes the value of the binomial coefficient 2) Embed your function into a little program that reads two integers n and r from std::cin and writes the value of the binomial coefficient to std::cout int fac(int x) { if(x <= 0) return 1; else return x * fac(x-1); } int bc(int n, int r) return fac(n) / (fac(n-r)*fac(r)); void main() int n, r; std:: cout << "Enter two natural numbers: "; std:: cin >> n >> r; if(n < 0 && r < 0) do std::cout << "Please again enter two natural numbers !! " <<std::endl; std:: cout << "Enter two natrural number "; std::cin >> n >> r; } while(n < 0 || r < 0); std::cout << "C(" << n << "," << r << ") = " << bc(n, r) << std::endl;

Exercise 2 HomeWork2 void permutNumbers(int data[], int x, int n) { Write a function permutNumbers that prints all n! many permutations of the numbers 1 to n on std::out. Example: the output for permutNumbers (3) shall be: 123, 132, 213, 231, 312, 321 void permutNumbers(int data[], int x, int n) { int k, temp; if(x==n-1) for(k=0; k<n; k++) cout << data[k]; if(k==n) cout << " "; } else for(k=x; k<n; k++) temp=data[k]; data[k]=data[x]; data[x]=temp; permutNumbers(data, x+1, n); 10/2013

cout << "Enter one natural number : "; cin >> a; HomeWork2 Exercise 2 Write a function permutNumbers that prints all n! many permutations of the numbers 1 to n on std::out. Example: the output for permutNumbers (3) shall be: 123, 132, 213, 231, 312, 321 int main() { int a, i; int data[MAX]; cout << "Enter one natural number : "; cin >> a; for(i=0; i<a; i++) data[i] = i+1; permutNumbers(data, 0, a); return 0; } 10/2013

Exercise 1 HomeWork3 19 Given the following function definition: int sum_down(int x) { if (x >= 0) x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else return 1; a) What is this smallest integer value of the parameter x, so that the returned value is greater than 1.000.000 ? 19 10/2013

while (x <= x_final) sum = (x - 1) + 2 * sum; x = x + 1; } HomeWork3 Exercise 1 b) Rewrite the function, so that it is free of recursion. I.e. give an iterative definition on the foundation of a loop-construct. TIP: First transform the recursive definition, so that you have just a single recursive call. int sum_down_while(int x_final) { int sum = 1; int x = 0; while (x <= x_final) sum = (x - 1) + 2 * sum; x = x + 1; } return sum; 10/2013

Exercise 1 Error!! x is would be -1 HomeWork3 c) Is it ok to switch the type of the parameter x to unsigned int? Discuss your decision / give an argumentation. int sum_down(unsigned int x) { if (x >= 0) x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else return 1; Error!! x is would be -1 10/2013

Exercise 1 Possible!! HomeWork3 d) Is it ok to switch the type of the parameter x to double? Discuss your decision / give an argumentation. int sum_down(double x) { if (x >= 0) x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else return 1; Possible!! 10/2013

Exercise 1 Error!! HomeWork3 e) Is it ok to switch the function head to int sum_down(const int x)? Discuss your decision / give an argumentation. int sum_down(const int x) { if (x >= 0) x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else return 1; Error!! 10/2013