UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Similar documents
Lab Instructor : Jean Lai

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Subprograms. Introduction to Programming (in C++) Subprograms: procedures and functions. Subprograms. Subprograms. Functions are defined as follows:

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

LAB 4.1 Relational Operators and the if Statement

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Chapter 4 Functions By C.K. Liang

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Lab ACN : C++ Programming Exercises

Add Subtract Multiply Divide

Local and Global Variables

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Programming & Data Structure Laboratory. Day 2, July 24, 2014

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

Arrays in C++ Instructor: Andy Abreu

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

#include <iostream> using namespace std;

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Computer Programming

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Describing and Implementing Algorithms

Part II Answer to all the questions (2 Marks):

Loop Invariant Examples

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

UEE1302(1066) F12: Introduction to Computers and Programming. Lab 12: Pointer & Dynamic Array (II)

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Func%ons. CS449 Fall 2017

Lab Manual. Program Design and File Structures (P): IT-219

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

TWO DIMENSIONAL ARRAY OR MATRIX common name scores Defined as scores[10][5] IE 10 rows x 5 columns showing name of each position; some values below

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Introduction to C++ Introduction to C++ 1

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

CS 115 Exam 2, Fall 2009

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors


BITG 1113: Function (Part 2) LECTURE 5

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

EE 109 Lab 8a Conversion Experience

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Errata for C++ and Algorithmic Thinking for the Complete Beginner

What Is a Function? Illustration of Program Flow

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

Exam I Review Questions Fall 2010

UNIT- 3 Introduction to C++

Declaring a 2D Array

Chapter-13 USER DEFINED FUNCTIONS

Help File on C++ and Programming at CSUSM by Rika Yoshii

Lecture 7: General Loops (Chapter 7)

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

Arrays. Week 4. Assylbek Jumagaliyev

Score score < score < score < 65 Score < 50

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Assertions & Verification & Example Loop Invariants Example Exam Questions

CHAPTER 3 Expressions, Functions, Output

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings


o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Conditionals !

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

CS103L SPRING 2017 UNIT 8: RECURSION

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

Transcription:

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter What you will learn from Lab 7 In this laboratory, you will understand how to use typical function prototype with call-by-value concept and the concept of local and global variables. TASK 7-1 : PARAMETER PASSING Call-by-value example: Please predict the result of program lab7-1, and then execute this //File: lab7-1.cpp void swap(int, int); int x = 5, y = 6; swap(x, y); void swap(int x, int y) int temp; temp = x; x = y; y = temp; It is noted that the swap function is utilized to exchange the two values with each other. However, the values of x and y in the main function do not swap. What observation can be concluded from the usage of call-by-value? Below program lab7-2 and program lab7-3 are two kinds of solutions to solve the problem. 1/6

Globle Variable example: Please predict the result of program lab7-2, and then execute this //File: lab7-2.cpp void swap(); int x = 5, y = 6; swap(); void swap() int temp; temp = x; x = y; y = temp; It is noted that no parameter passing is performed in the program. The swap function is simply utilized to exchange the two values with each other. Since the variables of x and y are global variables in the program, the values are swapped. What observation can be concluded from the usage of global variables? Call-by-Reference example: Please predict the result of program lab7-3, and then execute this //File: lab7-3.cpp void swap(int&, int&); int x = 5, y = 6; 2/6

swap(x, y); void swap(int& x, int & y) int temp; temp = x; x = y; y = temp; What observation can be concluded from the usage of call-by-reference? TASK 7-2 : FUNCTION INPUT & OUTPUT Return-Type output: Please predict the result of program lab7-4, and then execute this //File: lab7-4.cpp int fun(int, int); int a = 1, b = 1, c = 1; c = fun(a, b); cout << "The value returned is " << c << endl; int fun(int x, int y) return (x+y); 3/6

Call-by-Reference Type output: Please predict the result of program lab7-5, and then execute this //File: lab7-5.cpp void sumavg(double, double, double, double, double); double n1, n2, n3, sum, avg; sum = 0.0; avg = 0.0; cout << "Please enter score of three students" << endl; cin >> n1; cin >> n2; cin >> n3; sumavg(n1, n2, n3, sum, avg); cout << "The total score of three students is " << sum << endl; cout << "The average score of three students is " << avg << endl; void sumavg(double n1, double n2, double n3, double sum, double avg) sum = n1 + n2 + n3; avg = (n1 + n2 + n3) / 3; Modify the prototype of function sumavg as void sumavg(double, double, double, double &, double &) 4/6

TASK 7-3 : RECURSION Lab7-6 is an example code for recursive. Please write a C++ program to ask the user to enter an integer i and calculate the answer for the following equation: i f() i = 2 f( f(3i+ 1)) if i is even elsewhere //File: lab7-6.cpp int ff(int); cout << "Enter i: "; int i; cin >> i; cout << "f(" << i << ") = " << ff(i) << endl; int ff(int x) if (x%2==0) return x/2; else return ff(ff(3*x+1)); The required format is shown as follows. (Ex) >./lab7-6 Enter i: 4 f(4)= 2 5/6

TASK 7-4 : EXERCISES Write a C++ program to convert the decimal number to the binary number. You should write a function to do the conversion. Please meet the format requirements as follows. >./ex7-1 Enter a decimal value: 17 The binary value of 17 is 10001 Enter a decimal value: 1024 The binary value of 1024 is 10000000000 Enter a decimal value: -1 Exit!! > Note: Please use data type long to declare the variable(s) of the input variable and output sequence. Note: If input is less than 0, exit. Design a system which can calculate the greatest common divisor (g.c.d.) and least common multiple (l.c.m.) of 5 integers. Please meet the requirements as follows. >./ex 7-2 Input five integer number: 200 500 800 300 700 g.c.d. : 100 l.c.m. : 84000 > Your function prototype should follow the structure: n1 n2 gcd() gcd And please use recursive function to do this: n2 if n2 divided with no remainder For n1>=n2>0, gcd (n1,n2)= gcd (n2,n1%n2) otherwise Try to put the code of calculating the g.c.d. part and l.c.m. into a function. Then you can call the function for four times instead of writing the same codes again and again. 6/6