LAB 6 FUNCTIONS I School of Computer and Communication Engineering

Size: px
Start display at page:

Download "LAB 6 FUNCTIONS I School of Computer and Communication Engineering"

Transcription

1 LAB 6 FUNCTIONS I School of Computer and Communication Engineering 1

2 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

3 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

4 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

5 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

6 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

7 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

8 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 (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

9 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 : Enter unpaid bill: 0 Enter previous month and current month meter reading: Your account number is 4444 Your total bill is RM Do you want to continue : y or n? y Enter account number : Enter unpaid bill: 10 Enter previous month and current month meter reading: Your account number is 2222 Your total bill is RM Do you want to continue : y or n? n Perlis Water collection : RM

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

LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis Laboratory Module [Updated October 2017] LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis 1 OBJECTIVES 1. To differentiate between predefined function and

More information

LAB 7 FUNCTION PART 2

LAB 7 FUNCTION PART 2 LAB 7 FUNCTION PART 2 School of Computer and Communication Engineering Universiti Malaysia Perlis 1 OBJECTIVES 1. To differentiate the file scope and block scope. 2. To write recursive function. 3. To

More information

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

The Hyderabad Public School, Begumpet, Hyderabad, A.P The Hyderabad Public School, Begumpet, Hyderabad, A.P. 500 016 2012-13 Department of Computer Science Class 8 Worksheet 3 1) How many times will the following statement execute? ( ) int a=5; while(a>6)

More information

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

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

Question 2. [5 points] Given the following symbolic constant definition CS 101, Spring 2012 Mar 20th Exam 2 Name: Question 1. [5 points] Determine which of the following function calls are valid for a function with the prototype: void drawrect(int width, int height); Assume

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

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

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10

More information

Exercises C-Programming

Exercises C-Programming Exercises C-Programming Claude Fuhrer (claude.fuhrer@bfh.ch) 0 November 016 Contents 1 Serie 1 1 Min function.................................. Triangle surface 1............................... 3 Triangle

More information

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

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions) SECTION A TRUE / FALSE QUESTIONS ( MARKS) (INSTRUCTION: Please answer all questions) 1) In pre-test loop, the condition is tested first, before executing the body of the loop. ) The while loop can be used

More information

EC 413 Computer Organization

EC 413 Computer Organization EC 413 Computer Organization C/C++ Language Review Prof. Michel A. Kinsy Programming Languages There are many programming languages available: Pascal, C, C++, Java, Ada, Perl and Python All of these languages

More information

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

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer. Functions A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting

More information

Arithmetic Expressions in C

Arithmetic Expressions in C Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic

More information

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

Flow Chart. The diagrammatic representation shows a solution to a given problem. low Charts low Chart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as various symbols, and their order by connecting them with arrows. he diagrammatic representation

More information

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

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ FUNCTIONS Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Functions: Program modules in C Function Definitions Function

More information

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

More information

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4 BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions

More information

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

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lesson 5: Functions and Libraries 1 Functions 2 Overview Function is a block of statements which performs a specific task, and can be called by others Each function has a name (not identical to any other),

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

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

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable. CS 101, Spring 2016 Feb 23rd Exam 1 Name: Question 1. [2 points] Write a statement(s) to declare a variable named num students that will be used to store the number of students and set the value of the

More information

Chapter 4 Functions By C.K. Liang

Chapter 4 Functions By C.K. Liang 1 Chapter 4 Functions By C.K. Liang What you should learn? 2 To construct programs modularly from small pieces called functions Math functions in C standard library Create new functions Pass information

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 9: Functions I Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture Introduce the switch and goto statements Introduce the arrays in C

More information

INTI COLLEGE MALAYSIA

INTI COLLEGE MALAYSIA CSC 107 (F) / Page 1 of 6 INTI COLLEGE MALAYSIA COMPUTING AND INFORMATION TECHNOLOGY CERTIFICATE CSC 107 : PROGRAMMING IN C (BASED ON AB) FINAL EXAMINATION : AUGUST 2000 SESSION This paper consists of

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

Chapter 4: Basic C Operators

Chapter 4: Basic C Operators Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

More information

BİL200 TUTORIAL-EXERCISES Objective:

BİL200 TUTORIAL-EXERCISES Objective: Objective: The purpose of this tutorial is learning the usage of -preprocessors -header files -printf(), scanf(), gets() functions -logic operators and conditional cases A preprocessor is a program that

More information

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

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf(%f\n, *j); You try /* EXAMPLE 3 */ #include int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; /* EXAMPLE

More information

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Introduction to C Language

Introduction to C Language Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,

More information

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

6-1 (Function). (Function) !*+!#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x (Function) -1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)

More information

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

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING MODULE CODE: MODULE TITLE: ET4131 Introduction to Computer Programming SEMESTER:

More information

EK131 E5 Introduction to Engineering

EK131 E5 Introduction to Engineering EK131 E5 Introduction to Engineering Lecture 5: Conditional, Functions, Recursions Prof. Michel A. Kinsy Conditional execution Conditional constructs provide the ability to control whether a statement

More information

ALGORITHM 2-1 Solution for Exercise 4

ALGORITHM 2-1 Solution for Exercise 4 Chapter 2 Recursion Exercises 1. a. 3 * 4 = 12 b. (2 * (2 * fun1(0) + 7) + 7) = (2 * (2 * (3 * 0) + 7) + 7) = 21 c. (2 * (2 * fun1(2) + 7) + 7) = (2 * (2 * (3 * 2) + 7) + 7) = 45 2. a. 3 b. (fun2(2, 6)

More information

Introduction to C Final Review Chapters 1-6 & 13

Introduction to C Final Review Chapters 1-6 & 13 Introduction to C Final Review Chapters 1-6 & 13 Variables (Lecture Notes 2) Identifiers You must always define an identifier for a variable Declare and define variables before they are called in an expression

More information

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

More information

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013 DEPARTMENT OF MATERIAL AND ENGINEERING DESIGN FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING UNIVERSITI TUN HUSSEIN ONN MALAYSIA (UTHM), JOHOR TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

More information

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is

More information

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?

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? Lecture 16 Functions II they re back and they re not happy Daily Puzzle If it is raining at midnight - will we have sunny weather in 72 hours? function prototypes For the sake of logical clarity, the main()

More information

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

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

CS16 Exam #1 7/17/ Minutes 100 Points total CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,

More information

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

C Programs: Simple Statements and Expressions

C Programs: Simple Statements and Expressions .. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. C Programs: Simple Statements and Expressions C Program Structure A C program that consists of only one function has the following

More information

CSE 142 Wi01 Midterm 2 page 1 of 6

CSE 142 Wi01 Midterm 2 page 1 of 6 CSE 142 Wi01 Midterm 2 page 1 of 6 Answer all of the following questions. READ EACH QUESTION CAREFULLY. Answer each question in the space provided on these pages. Keep your answers short and to the point.

More information

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Unit 1: Introduction to C Language Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories

More information

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

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming. Programming Fundamentals for Engineers - 0702113 7. Functions Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 Modular programming Your program main() function Calls AnotherFunction1() Returns the results

More information

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

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil 200 Low-Level I/O getchar, putchar,

More information

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

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Functions in C Practice Sheet #05 Date: 30-01-2017 Instructions: For the questions consisting code segments,

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Computer Programming

Computer Programming Computer Programming Introduction Marius Minea marius@cs.upt.ro http://cs.upt.ro/ marius/curs/cp/ 26 September 2017 Course goals Learn programming fundamentals no prior knowledge needed for those who know,

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

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

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar Examples of Software Programming and Data Structure Lecture 2 Sudeshna Sarkar Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix

More information

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

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

How to Do Word Problems. Study of Integers

How to Do Word Problems. Study of Integers Study of Integers In this chapter, we are are going to closely look at the number line system and study integers. -3-2 -1 0 1 2 3 4 5 6 An integer is simply a number like 0, 1, 2, 3, and 4, but unlike

More information

Lecture 9 - C Functions

Lecture 9 - C Functions ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin

More information

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

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!)

More information

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. 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 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 ). 2-Give the result of the following program: #include

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

User Defined Functions

User Defined Functions User Defined Functions CS 141 Lecture 4 Chapter 5 By Ziad Kobti 27/01/2003 (c) 2003 by Ziad Kobti 1 Outline Functions in C: Definition Function Prototype (signature) Function Definition (body/implementation)

More information

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information

Lecture 5: C programming

Lecture 5: C programming CSCI-GA.1144-001 PAC II Lecture 5: C programming Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Brian Kernighan Dennis Ritchie In 1972 Dennis Ritchie at Bell Labs writes C and in 1978

More information

ISLEWORTH & SYON BOYS SCHOOL

ISLEWORTH & SYON BOYS SCHOOL ISLEWORTH & SYON BOYS SCHOOL YEAR 7 - LEVEL 1 NUMBER & MEASURE PERSONAL LEARNING CHECKLIST Skill Number size and rounding Example question Can I do it? I CAN do it now! Read, write, order and compare

More information

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

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries

More information

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

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved. Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

More information

Expression and Operator

Expression and Operator Expression and Operator Examples: Two types: Expressions and Operators 3 + 5; x; x=0; x=x+1; printf("%d",x); Function calls The expressions formed by data and operators An expression in C usually has a

More information

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

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

LAB 2.1 INTRODUCTION TO C PROGRAMMING

LAB 2.1 INTRODUCTION TO C PROGRAMMING LAB 2.1 INTRODUCTION TO C PROGRAMMING School of Computer and Communication Engineering Universiti Malaysia Perlis 1 1. OBJECTIVES: 1.1 To be able to apply basic rules and structures of C in writing a simple

More information

Operators And Expressions

Operators And Expressions Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

More information

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

COP 3223 Introduction to Programming with C - Study Union - Spring 2018 COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

CS 261 Data Structures. Introduction to C Programming

CS 261 Data Structures. Introduction to C Programming CS 261 Data Structures Introduction to C Programming Why C? C is a lower level, imperative language C makes it easier to focus on important concepts for this class, including memory allocation execution

More information

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

4. C++ functions. 1. Library Function 2. User-defined Function 4. C++ functions In programming, function refers to a segment that group s code to perform a specific task. Depending on whether a function is predefined or created by programmer; there are two types of

More information

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Basic and Practice in Programming Lab7

Basic and Practice in Programming Lab7 Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;

More information

Chapter 5 C Functions

Chapter 5 C Functions Chapter 5 C Functions Objectives of this chapter: To construct programs from small pieces called functions. Common math functions in math.h the C Standard Library. sin( ), cos( ), tan( ), atan( ), sqrt(

More information

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

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design What is a Function? C Programming Lecture 8-1 : Function (Basic) A small program(subroutine) that performs a particular task Input : parameter / argument Perform what? : function body Output t : return

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

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

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5

More information

16.216: ECE Application Programming Fall 2011

16.216: ECE Application Programming Fall 2011 16.216: ECE Application Programming Fall 2011 Exam 2 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

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

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable

More information

ECE264 Fall 2013 Exam 1, September 24, 2013

ECE264 Fall 2013 Exam 1, September 24, 2013 ECE264 Fall 2013 Exam 1, September 24, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.

More information

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19 What

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

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

Chapter 2: Overview of C. Problem Solving & Program Design in C Chapter 2: Overview of C Problem Solving & Program Design in C Addison Wesley is an imprint of Why Learn C? Compact, fast, and powerful High-level Language Standard for program development (wide acceptance)

More information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Final Examination December 14, 2012 2:00 p.m. 4:30 p.m. (150 minutes) Examiners: J. Anderson, B. Li, M. Sadoghi,

More information

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

SOLUTION FOR FUNCTION. 1) Write a program to display Hello 10 times. Create user-defined function message(). SOLUTION FOR FUNCTION 1) Write a program to display Hello 10 times. Create user-defined function message(). #include #include void message(int); int i; for (i=1;i

More information

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

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that

More information

C: How to Program. Week /Apr/23

C: How to Program. Week /Apr/23 C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )

More information

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

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Chapter 6 - Notes User-Defined Functions I

Chapter 6 - Notes User-Defined Functions I Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B.

More information

3. Types of Algorithmic and Program Instructions

3. Types of Algorithmic and Program Instructions 3. Types of Algorithmic and Program Instructions Objectives 1. Introduce programming language concepts of variables, constants and their data types 2. Introduce types of algorithmic and program instructions

More information

Chapter 7 Arithmetic

Chapter 7 Arithmetic Chapter 7 Arithmetic 7-1 Arithmetic in C++ Arithmetic expressions are made up of constants, variables, operators and parentheses. The arithmetic operators in C++ are as follows + (addition) - (subtraction)

More information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 20, 2011 6:15 p.m. 8:00 p.m. (105 minutes) Examiners: J. Anderson, T. Fairgrieve,

More information

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

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C CSE 130: Introduction to Programming in C Stony Brook University WHY C? C is a programming lingua franca Millions of lines of C code exist Many other languages use C-like syntax C is portable

More information

Functions and Recursion

Functions and Recursion Functions and Recursion CSE 130: Introduction to Programming in C Stony Brook University Software Reuse Laziness is a virtue among programmers Often, a given task must be performed multiple times Instead

More information

Final Intro to C Review

Final Intro to C Review Final Exam Content: Final Intro to C Review - Pass by reference Functions - General Syntax - Structures - Recursion(maybe?) - Programming by nature is cumulative so any past material is up for grabs as

More information