Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Programming Language. Functions. Eng. Anis Nazer First Semester

Lab Instructor : Jean Lai

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

Chapter 6 - Notes User-Defined Functions I

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Using Free Functions

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Lab # 02. Basic Elements of C++ _ Part1

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

LAB: INTRODUCTION TO FUNCTIONS IN C++

I/O Streams and Standard I/O Devices (cont d.)

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Fundamentals of Programming CS-110. Lecture 2

INTRODUCTION TO PROGRAMMING

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

(created by professor Marina Tanasyuk) FUNCTIONS

Introduction. What is function? Multiple functions form a larger program Modular programming


A SHORT COURSE ON C++

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

C++ Programming: Functions

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Summary of basic C++-commands

C++ Support Classes (Data and Variables)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Engineering Problem Solving with C++, Etter/Ingber

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

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

VARIABLES & ASSIGNMENTS

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Objectives. In this chapter, you will:

C++ basics Getting started with, and Data Types.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

2. Functions I: Passing by Value

1. In C++, reserved words are the same as predefined identifiers. a. True

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

REVIEW. The C++ Programming Language. CS 151 Review #2

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Ch 6. Functions. Example: function calls function

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

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Computer Programming : C++

Functions and Recursion

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Chapter 4. Procedural Abstraction and Functions That Return a Value

Chapter 3 - Functions

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

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply blocks for the functions.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Why Is Repetition Needed?

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

Scientific Computing

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.

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Understanding main() function Input/Output Streams

Study Guide for Test 2

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

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

Object Oriented Design

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

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

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

WT I (SET-I) Date: Class XI Sec. Time: 1 Hr. 10 min. Computer Science M.M.: 30

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Pre- Defined Func-ons in C++ Review for Midterm #1

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Chapter 1 INTRODUCTION

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

Binomial pricer (1.1)

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

Project Identity Shampoo-Conditioner Identification Device/ Medicine Reminder Week 2: 1/28/07-2/3/07 Karla Sittnick

In this chapter you will learn:

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

6. User Defined Functions I

Problem Solving: Storyboards for User Interaction

do { statements } while (condition);

C Functions. 5.2 Program Modules in C

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

4. C++ functions. 1. Library Function 2. User-defined Function

Transcription:

Superior University Department of Electrical Engineering CS-115 Computing Fundamentals Experiment No.6 Pre-Defined Functions, User-Defined Function: Value Returning Functions Prepared for By: Name: ID: Section: Semester: Total Marks: Obtained Marks: Signature: Date:

Contents of Lab 1 6.1. Objectives:... 3 6.2. Predefined Functions... 3 Example 1:... 4 6.3. User-Defined Functions... 5 6.4. Value-Returning Functions... 5 6.5. Syntax: Value-Returning function... 6 Example 2:... 6 Example 3:... 7 6.6. Lab Tasks:... 8 Task 1:... 8 Task 2:... 8 Task 3:... 8 6.7. Home Assignment:... 8 Task 1:... 8 Task 2:... 8 Task 3:... 9 2 Engr. Syed Intazar Hyder

Pre-Defined Functions, User-Defined Function: Value Returning Functions 6.1. Objectives: To learn about predefined functions. To learn how to use them in programs. To learn about User-defined, Value Returning Functions To learn how to use them 6.2. Predefined Functions In C++, the concept of a function, either predefined or user-defined, is similar to that of a function in algebra. For example, every function has a name and, depending on the values specified by the user, it does some computation. Some of the predefined mathematical functions are pow (x, y), sqrt (x), and floor (x). Table 1: Pre-Defined Functions 3 Engr. Syed Intazar Hyder

To use predefined functions in a program, you must include the header file that contains the function s specification via the include statement. For example, to use the function pow, the program must include: #include <cmath> Example 1: // Lab_6_Example_1_Predefined functions Result //How to use predefined functions. #include <iostream> #include <cmath> #include <cctype> using namespace std; int main() int x; double u, v; cout << "Uppercase a is " << static_cast<char>(toupper('a')) << endl; u = 4.2; v = 3.0; cout << u << " to the power of " << v << " = " << pow(u, v) << endl; cout << "5.0 to the power of 4 = " << pow(5.0, 4) << endl; u = u + pow(3.0, 3); cout << "u = " << u << endl; x = -15; cout << "Absolute value of " << x 4 Engr. Syed Intazar Hyder

<< " = " << abs(x) << endl; return 0; 6.3. User-Defined Functions As Example-1 illustrates, using functions in a program greatly enhances the program s readability because it reduces the complexity of the function main. Also, once you write and properly debug a function, you can use it in the program (or different programs) again and again without having to rewrite the same code repeatedly. Because C++ does not provide every function that you will ever need and designers cannot possibly know a user s specific needs, you must learn to write your own functions. User-defined functions in C++ are classified into two categories: Value-returning functions-functions that have a return type. These functions return a value of a specific data type using the return statement, which we will explain shortly. Void functions-functions that do not have a return type. These functions do not use a return statement to return a value. 6.4. Value-Returning Functions The previous section introduced some predefined C++ functions such as pow, abs, islower, and toupper. These are examples of value-returning functions. To use these functions in your programs, you must know the name of the header file that contains the functions specification. You need to include this header file in your program using the include statement and know the following items: 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. The data type of the value computed (that is, the value returned) by the function, called the type of the function Because the value returned by a value-returning function is unique, the natural thing for you to do is to use the value in one of three ways: Save the value for further calculation. Use the value in some calculation. Print the value. This suggests that a value-returning function is used: In an assignment statement. As a parameter in a function call. In an output statement. That is, a value-returning function is used (called) in an expression. 5. The code required to accomplish the task. 5 Engr. Syed Intazar Hyder

6.5. Syntax: Value-Returning function The syntax of a value-returning function is: Computing Fundamentals Lab 6 functiontype functionname(formal parameter list) statements in which statements are usually declaration statements and/or executable statements. In this syntax, functiontype is the type of the value that the function returns. The functiontype is also called the data type or the return type of the value-returning function. Moreover, statements enclosed between curly braces form the body of the function. Example 2: // Lab_6_Example_2_user-defined functions #include<iostream> using namespace std; double country(); double city(); double institute(); int main() country(); institute(); return 0; Result double country() cout<<"my country is Pakistan\n"; city(); double city() 6 Engr. Syed Intazar Hyder

cout<<"my city is Lahore\n"; double institute() cout<<"my istitute is Superior University"; Example 3: // Lab_6_Example_3_user-defined functions //Program: Largest of three numbers #include <iostream> using namespace std; double larger(double x, double y); double comparethree(double x, double y, double z); int main() double x, y; cout << "Enter two numbers: \n"; cin >> x >> y; cout << endl; cout << "The larger of " << x << " and " << y << " is: " << larger(x, y) << endl; Result double a, b, c; cout << "Enter three numbers: \n"; cin>>a>>b>>c; cout<<endl; cout<<"the Larger of "<< a <<", " << b <<" and "<<c <<" is: " <<comparethree(a, b, c) << endl; return 0; double larger(double x, double y) double max; if (x >= y) max = x; else max = y; return max; double comparethree (double x, double y, double z) return larger(x, larger(y, z)); 7 Engr. Syed Intazar Hyder

6.6. Lab Tasks: Task 1: Write a function to calculate the factorial of a number that is being generated by random from random generator and call it main function. Task 2: Write a function that rolls a pair of dice until the sum of the numbers rolled is a specific number. We also want to know the number of times the dice are rolled to get the desired sum. The smallest number on each die is 1, and the largest number is 6. So the smallest sum of the numbers rolled is 2, and the largest sum of the numbers rolled is 12. Task 3: Write a program that uses the function isnumpalindrome given in Example 6-5 (Palindrome Number). Test your program on the following numbers: 10, 34, 22, 333, 678, 67876, 44444, and 123454321. 6.7. Home Assignment: Task 1: Write a function to with function larger is used to determine the largest number from a set of 10 numbers. Task 2: Write a function that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per minute. Premium service: $25.00 plus: a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute. b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute. Your program should prompt the user to enter an account number, a service code (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service. For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night. 8 Engr. Syed Intazar Hyder

Task 3: Write a function that rolls a pair of dice until the sum of the numbers rolled is a specific number. We also want to know the number of times the dice are rolled to get the desired sum. The smallest number on each die is 1, and the largest number is 6. So the smallest sum of the numbers rolled is 2, and the largest sum of the numbers rolled is 12. No. of attempts must limit to 6 times. 9 Engr. Syed Intazar Hyder