LAB 6 FUNCTIONS I School of Computer and Communication Engineering

Similar documents
LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis

LAB 7 FUNCTION PART 2

The Hyderabad Public School, Begumpet, Hyderabad, A.P

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Question 2. [5 points] Given the following symbolic constant definition

Functions. Arash Rafiey. September 26, 2017

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Exercises C-Programming

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

EC 413 Computer Organization

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Arithmetic Expressions in C

Flow Chart. The diagrammatic representation shows a solution to a given problem.

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

C Functions. 5.2 Program Modules in C

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

Chapter 4 Functions By C.K. Liang

BSM540 Basics of C Language

INTI COLLEGE MALAYSIA

Course Outline Introduction to C-Programming

Chapter 4: Basic C Operators

BİL200 TUTORIAL-EXERCISES Objective:

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Introduction to C Language

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

EK131 E5 Introduction to Engineering

ALGORITHM 2-1 Solution for Exercise 4

Introduction to C Final Review Chapters 1-6 & 13

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Functions. Systems Programming Concepts

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CS16 Exam #1 7/17/ Minutes 100 Points total

Programming Language A

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Subject: Fundamental of Computer Programming 2068

C Programs: Simple Statements and Expressions

CSE 142 Wi01 Midterm 2 page 1 of 6

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05

Control Structure: Loop

Introduction to Java Applications

Computer Programming

Computer System and programming in C

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

How to Do Word Problems. Study of Integers

Lecture 9 - C Functions

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

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 ).

Loops / Repetition Statements

User Defined Functions

Structured programming

Lecture 5: C programming

ISLEWORTH & SYON BOYS SCHOOL

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Expression and Operator

Language comparison. C has pointers. Java has references. C++ has pointers and references

Programming for Electrical and Computer Engineers. Loops

C: How to Program. Week /Mar/05

Fundamental of Programming (C)

LAB 2.1 INTRODUCTION TO C PROGRAMMING

Operators And Expressions

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

CS 261 Data Structures. Introduction to C Programming

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Basic and Practice in Programming Lab7

Chapter 5 C Functions

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Final CSE 131B Spring 2004

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

16.216: ECE Application Programming Fall 2011

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

ECE264 Fall 2013 Exam 1, September 24, 2013

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Chapter 2 - Introduction to C Programming

Chapter 2: Overview of C. Problem Solving & Program Design in C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

SOLUTION FOR FUNCTION. 1) Write a program to display Hello 10 times. Create user-defined function message().

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

C: How to Program. Week /Apr/23

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter 6 - Notes User-Defined Functions I

3. Types of Algorithmic and Program Instructions

Chapter 7 Arithmetic

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Functions and Recursion

Final Intro to C Review

Transcription:

LAB 6 FUNCTIONS I School of Computer and Communication Engineering 1

Universiti Malaysia Perlis 1. OBJECTIVES: 1.1 To apply functions as building blocks of programs. 1.2 To write C programs using functions. 2 INTRODUCTION: A C program is generally formed by a set of functions, which subsequently consist of many programming statements. Using functions, a large computing task can be broken into smaller ones. Functions can be created to execute small, frequently-used tasks. In C, there are predefined functions or sometimes called standard functions, and userdefined functions. Predefined functions are already available functions which can be used, called library, such as stdio.h, math.h, string.h and stdlib.h. The library name must be included at the top of the source code (preprocessor directive). User-defined functions in a program are built using: Function prototype Function definition Function call 2.1 Function Prototype To use function in a program, it has to be declared at the beginning of a program, using function prototype. Function prototype has the following form: <return_type> <function_name> (arg_type arg_name,...); For example: int sum (int num1,int num2); //function named sum with two (2) arguments and returns integer data type. void sum (int num1,int num2); //function named sum with two (2) arguments but does not return any data. 2.2 Function Definition Function definition is the function body. It is used to define what the function does. The coding is written inside the function definition. Function definition has the following form: <return_type> <function_name> (arg_type arg_name,...) statements For example: int sum (int num1,int num2) int add; add = num1 + num2; return(add); 2

2.3 Function Call Function call can be made in the main function or in other functions. Function call has the following form: <function_name> (exp, exp...) exp is an expression can be variable or constant For example: result = sum(x,y); 3. TASKS: 3.1 a. Identify and correct the errors, if there are any, in the following program segments. Briefly explain why. i) int fnfunc (double ia); int fnfunc(int ia)) return 10*iA; ii) int fnfunc (int ia) return 2*; fnfunc (2, 3); iii) void fnfunc1 (int ia); int fnfunc1(int ia) int ib; ib=2*ia; return ib; 3

iv) double sum(double ia, double ib) return ia+ib; void fnfunc(double ia, double ib) double sum; sum = sum(ia,ib); v) int fnfunc3(int in) return in*in; ; int in; in= (int)fnfunc3(in); fnfunc3 (in); in = fnfunc3 (8); b. What is displayed by the program that follows? #include <stdio.h> void fnbizarre(int in); int main(void) double ix; ix = 35.8; fnbizarre(ix); printf("%.2f\n", ix); return (0); void fnbizarre(int in) printf("%4d ", in); 4

c. What is displayed by the program defined below? #include <stdio.h> double fnad1(double dx); double fntrpl(double dx); double fnhlf(double dx); int main(void) printf("%.3f\n", fnhlf(fntrpl(fnad1(8.2)))); return (0); double fnad1(double dx) return (dx + 1); double fntrpl(double dx) return (3 * dx); double fnhlf(double dx) return (0.5 * dx); d. Give the output of the following code: #include <stdio.h> void fnfunc(int ia, int ib, double dc); void fnfunc(int ia, int ib, double dc) printf( ia = %d\n, ia); printf( ib = %d\n, ib); printf( dc = %lf\n, dc); int main() fnfunc(3.3, 3.7, 4); return 0; 5

3.2 The formula for the volume of sphere is πr 3, where r is the radius of the sphere. Write a function to return the volume of a sphere with the input argument for the radius of the sphere. Test your program with a radius of 5 meters. 6

3.3 Write a program that calculates the area and perimeter of a rectangle. The area and perimeter are calculated using two separated functions, which both take two arguments of a double type for the width and length of the rectangle. Calculate the area and perimeter using these two functions in the main program for a rectangle with width and length of 2 cm and 5.5, respectively. 7

3.4 An integer number is a prime number if it is divisible only by 1 and itself. For example 2, 3, 5, 7 and 11 are prime numbers. Write a program that reads an integer and determines if it is a prime number. If it is not a prime number, print out the smallest divisor. Otherwise, print out the prime number. Create a function to return the smallest divisor for an integer. Use this function to determine and print all the prime numbers between 1 and 1000. (Hint: use the modulus operator % to determine if a number is divisible by another number. The smallest divisor of even number is 2. However you only need to test up to n to verify if it is divisible) 8

4. ADDITIONAL TASK: Write a C program that calculates a customer water bill. The water bill includes RM 5.00 basic cost and cost for water usage with rate of RM 1.10 per thousand liter. Water usage is calculated by subtracting current meter reading with previous month meter reading (meter is read in thousand liter unit).your program should check whether there is an unpaid bill. If the balance of unpaid bill is greater than 0, RM 2.00 fine is charged together with the unpaid bill and this will be included in the current monthly bill. The program will also calculate the bill collected for the day. The program will continue if user enters y- yes, else it will stop and display the collection for the day. Your program should use these functions : 1. calc_usage_cost - accepts previous and current meter reading, returns usage cost. 2. calc_unpaid_cost - accepts unpaid bill, returns unpaid cost. 3. calc_total_bill - accepts usage cost and unpaid cost, returns total bill. 4. print_bill - accepts account number and total bill. Sample Output -----------------Perlis Water---------------------- This program generates monthly water bill ------------------------------------------------------- Enter account number : 44444 Enter unpaid bill: 0 Enter previous month and current month meter reading: 1000 2000 Your account number is 4444 Your total bill is RM 6.10. Do you want to continue : y or n? y Enter account number : 22222 Enter unpaid bill: 10 Enter previous month and current month meter reading: 1000 2000 Your account number is 2222 Your total bill is RM 18.10. Do you want to continue : y or n? n Perlis Water collection : RM 24.20. 9