LAB 7 FUNCTION PART 2
|
|
- Shon Horton
- 5 years ago
- Views:
Transcription
1 LAB 7 FUNCTION PART 2 School of Computer and Communication Engineering Universiti Malaysia Perlis 1
2 OBJECTIVES 1. To differentiate the file scope and block scope. 2. To write recursive function. 3. To use library functions in a C program. 4. To write user-defined library function. NOTES Part 1 Scope 1. An identifier declared outside any function has file scope. Such an identifier is known (i.e., accessible) in all functions from the point at which the identifier is declared until the end of the file. Global variables, function definitions, and function prototypes placed outside a function all have file scope. 2. Block Identifiers defined inside a block have block scope. Block scope ends at the terminating right brace () of the block. Local variables defined at the beginning of a function have block scope as do function parameters, which are considered local variables by the function. 3. Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. This can be illustrated in Figure 1. #include <stdio.h> int x; /* Global variable, declared outside any function. */ void funct(void); main() x = 0; /* x isn't declared in main()*/ x++; /* increment x */ printf("the value of x is %d\n", x); funct(); /* x will be different after funct() */ printf("the value of x is %d\n", x); void funct(void) x++; /* x isn't declared in funct()*/ x += 3; Figure 1 Global variable 2
3 Sample output: The value of x is 1 The value of x is 5 4. Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. This can be illustrated in Figure 2. #include <stdio.h> void funct(void); main() int x; /* Local variable, declared inside any function. */ x = 0; x++; /* increment x */ printf("the value of x is %d\n", x); funct(); /* x will be the same after funct() */ printf("the value of x is %d\n", x); void funct(void) int x; /* Local variable, declared inside any function. */ x++; x += 3; Sample output: The value of x is 1 The value of x is 1 Figure 2 Local variable 5. If we want a variable to persist throughout the running of the program. This is where the static type comes in. The use of the static variable allows the variable to be incremented and for the value to be remembered throughout the program. This can be illustrated in Figure 3. 3
4 #include <stdio.h> int times_called(void); main() int i; for (i = 0; i < 10; i++) printf("times_called() has been called %d times\n",times_called()); int times_called(void) /* The variable is declared and defined once*/ static int i = 0; ++i; /* Incrementing the variable */ return i; Figure 3 Static variable Sample output: times_called() has been called 1 times times_called() has been called 2 times times_called() has been called 3 times times_called() has been called 4 times times_called() has been called 5 times times_called() has been called 6 times times_called() has been called 7 times times_called() has been called 8 times times_called() has been called 9 times times_called() has been called 10 times Part 2 Recursive function 1. A recursive function is a function that called itself. 2. The recursive functions should be used very carefully because when a function called by itself it enters into infinite loop. And when a function enters into the infinite loop, the function execution never gets completed. We should define the condition to exit from the function call so that the recursive function gets terminated. 4
5 #include<stdio.h> int factorial(int); int main() int fact, n; printf("enter any positive integer:\n"); scanf("%d", &n); fact = factorial(n); printf("factorial of %d! is %d\n", n, fact); return 0; int factorial(int n) if (n == 0) return 1; else return(n * factorial(n-1)); Sample output: Enter any positive integer: 3 Factorial of 3! = 6 Figure 4 An example of a recursive function 3. In the above example program, the factorial() function call is initiated from main() function with the value 3. Inside the factorial() function, the function calls factorial(2), factorial(1) and factorial(0) are called recursively. The complete execution process of the program in Figure 4 is shown in Figure 5. 5
6 Figure 5 Execution process of the program in Figure 4 (source: 6
7 Part 3 Library functions 1. C supports many built-in functions. Table 1 and Table 2 lists mathematical functions available in C. Most of the functions expect one or more type double arguments and return a type double result. Table 1 Some mathematical Library Functions 7
8 Table 2 More mathematical library functions 2. The program in Figure 6 shows how to use some of the mathematical functions in Table 1 and Table 2. 8
9 #include <stdio.h> #include <math.h> int main() double x,y; printf("acos(0.5)\t%f\n", acos(0.5)); printf("asin(0.5)\t%f\n", asin(0.5)); printf("cos(0.3)\t%f\n", cos(0.3)); printf("cosh(0.5)\t%f\n", cosh(0.5)); printf("sin(0.5)\t%f\n", sin(0.5)); printf("sinh(0.5)\t%f\n", sinh(0.5)); printf("tan(0.5)\t%f\n", tan(0.5)); printf("tanh(0.5)\t%f\n", tanh(0.5)); printf("abs(-7)\t\t%f\n", abs(-7)); printf("ceil(-3.3)\t%f\n", ceil(-3.3)); printf("exp(0.5)\t%f\n", exp(0.5)); printf("fabs(-3.3)\t%f\n", fabs(-3.3)); printf("floor(-3.3)\t%f\n", floor(-3.3)); printf("log(0.5)\t%f\n", log(0.5)); printf("log10(0.5)\t%f\n", log10(0.5)); printf("pow(2,3)\t%f\n", pow(2,3)); printf("sqrt(2)\t\t%f\n", sqrt(2)); return 0; Figure 6 Trigonometric and hyperbolic functions Sample output: acos(0.5) asin(0.5) cos(0.3) cosh(0.5) sin(0.5) sinh(0.5) tan(0.5) tanh(0.5) abs(-7) ceil(-3.3) exp(0.5) fabs(-3.3) floor(-3.3) log(0.5) log10(0.5) pow(2,3) sqrt(2)
10 Part 4 User-defined library function 1. We have seen how the availability of C s standard libraries simplifies program development. However, the standard libraries are not extensive enough to handle every programming need. In fact, one can use the C preprocessor directive #include to make userdefined libraries as well. 2. To create a user-defined library, we must first make a header file (.h extension) a text file containing all the information about a library needed by the compiler when compiling a program that uses the facilities defined in the library. Precisely this type of data is found in system header files such as stdio.h, math.h, and string.h. 3. In Code Block, you must create a new project and put them in appropriate folders i.e. header files into header s folder and source files into source s folder as shown in Figure 7. Figure 7 A project with user-defined library function 4. Basically we will have 3 separate files in a project e.g. shapes.h (Figure 8), shapes.c (Figure 9) and main.c (Figure 10). /*shapes.h*/ void horizontalline();//function declaration(prototype) void triangle();//function declaration(prototype) Figure 8 shapes.h 10
11 EKT 120 Introduction to Computer Programming Laboratory Module /*shapes.c*/ void horizontalline() /*function definition*/ printf("\n*********\n"); void triangle()/*function definition*/ int i, space, rows=6, star=0; /* printing one row in every iteration */ for(i = 1; i <= rows; i++) /* Printing spaces */ for(space = 1; space <= rows-i; space++) printf(" "); /* Printing stars */ while(star!= (2*i - 1)) printf("*"); star++;; star=0; /* move to next row */ printf("\n"); Figure 9 shapes.c /*main.c*/ #include "shapes.h" int main() triangle(); /*function call*/ horizontalline(); /*function call*/ return 0; Sample output: Figure 10 main.c * *** ***** ******* ********* *********** ********* 11
12 EKT 120 Introduction to Computer Programming Laboratory Module PART 1 STRUCTURED QUESTIONS i. Predict and write the output for the program shown in Figure 11. #include <stdio.h> void function1(int, int); void function2(int *, int); int main() int a = 5; int b = 10; printf("\nbefore function1: a = %d function1(a, b); printf("\nafter function1: a = %d b = %d", a, b); b = %d", a, b); printf("\n\nbefore function2: a = %d b = %d", a, b); function2(&a, b); printf("\nafter function2: a = %d b = %d\n", a, b); return 0; void function1(int p, int q) p = p + 5; q = q + 5; printf("\nwithin function1: p = %d void function2(int *r, int s) *r = *r - 5; s = s -5; printf("\nwithin function2: *r = %d q = %d", p, q); s = %d", *r, s); Figure 11 12
13 EKT 120 Introduction to Computer Programming Laboratory Module ii. The following questions are based on Figure 12. #include <stdio.h> void fnfun1(int, int*); int main(void) int ia=3,ib=6; printf("before fun 1\n ); printf( ia = %d ib = %d \n,ia,ib); fnfun1(ia, &ib); printf( \n\nafter fun 1\n ); printf( ia = %d ib = %d \n,ia,ib); return 0; void fnfun1(int iaa,int *ibb) ++iaa; --*ibb; printf("\n\ninside fnfun1\n ); printf( iaa = %d ibb = %d \n,iaa,*ibb); Figure 12 a) Identify and state the parameter which is passed by value and by reference to function fnfun1(int, int*). b) State the difference between the passing parameter by value and by reference in functions. c) Predict the output of Figure
14 EKT 120 Introduction to Computer Programming Laboratory Module PART 2 PROGRAMMING QUESTION i. Write and test a recursive function that finds the greatest common divisor of two integers m and n, using Euclid's algorithm: a) Find the integer remainder of min (m mod n). a) If the remainder is 0, then n is the largest integer divisor of both m and n. b) If the remainder is not 0, replace m with n and n with the integer remainder from the original division. Repeat the steps until the remainder is 0. For example, what is the greatest common divisor of 30 and 12? a) The integer remainder of 30/12 is 6. b) The remainder is not 0. c) The integer remainder of 12/6 is 0. Therefore, 6 is the largest integer divisor of both m and n. ii. Write and test a recursive function that returns 1 if an integer is prime and 0 if it is not. You can take advantage of the following facts: a) The integers 1, 2, and 3 are prime. b) Any even integer greater than 2 is not prime. c) If an integer has no divisor less than or equal to its square root, it must be prime. That is, if an integer has a divisor greater than or equal to its square root, it must also have a divisor less than or equal to its square root. Odd integers that are perfect squares, such as 49, have two divisors, each of which is equal to the square root. To implement this algorithm, it is helpful the use two separate functions, one of which has a single parameter associated with the integer to be tested. The prototypes should look like this: int Is_Prime(int n); int Get_Prime(int n,int m); where n is the integer to be tested and m is a trial divisor. Only the function Is_Prime is called directly. Then Is_Prime conducts some preliminary tests on n and calls Get_Prime only if the other tests cannot determine whether n is prime. Of these two functions, only Get_Prime is called recursively. 14
15 EKT 120 Introduction to Computer Programming Laboratory Module iii. Snell's Law describes the refraction (bending) of light as it passes from one medium to another. If the refractive index of the incident medium is n j and that of the refracting medium is n r, and the angle of incidence i and angle of refraction r of a ray of light, measured from the perpendicular to the boundary between the two mediums, are related by n i sin i = n r sin r Figure 13 illustrates the geometry of Snell s Law. Write a program that asks the user to provide two refractive indices and the angle of an incident ray and then calculates the angle of a refracted ray. Use mathematical library functions to construct the formula. Figure 13 Geometry for Snell s Law of refraction The formula to determine the angle of the refracted ray is: Table 3 gives the angles of refraction for some common materials when a light ray is directed from air, which has a refractive index equal to 1, into the material. Table 3 15
16 EKT 120 Introduction to Computer Programming Laboratory Module Sample output: Input indices of refraction for incident and refracting medium (water, glass or diamond) separated by spaces: Input angle of incident: 20 Refracted angle = 13.8 degree iv. Create a project named calculator and write a header file named calculator.h, a function definition file, calculator.c and a main.c into the project. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result (For division, if the denominator is zero, output an appropriate message). Sample output : Please enter 2 integers: 4 2 Please enter an operator (+, -, *,/): * Result : 8 Do you want to continue : y or n?y Please enter 2 integers: 2 0 Please enter an operator (+, -, *,/): / ERROR denominator is zero!cannot divide by zero Do you want to continue : y or n?n Define function prototypes in calculator.h: a) double add(double,double); b) double multiply(double,double); c) double divide(double,double); d) double subtract(double,double); 16
LAB 6 FUNCTIONS I School of Computer and Communication Engineering
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.
More information6-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 informationProgramming 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 informationBIL 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 informationC 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 informationFunctions. 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 informationC Programming Lecture V
C Programming Lecture V Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ Modular Programming A function in C is a small sub-program that performs a particular task, and supports the concept of modular
More informationDr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)
Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly
More informationINTRODUCTION 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 informationECET 264 C Programming Language with Applications
ECET 264 C Programming Language with Applications Lecture 10 C Standard Library Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 10
More informationCSE123. Program Design and Modular Programming Functions 1-1
CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.
More informationFunction 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 informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor:
More informationComputer 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 informationFunctions. 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 informationChapter 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 informationLOOPS. 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 informationLecture 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 informationC 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 informationProcedural Programming
Exercise 6 (SS 2016) 04.07.2015 What will I learn in the 6. exercise Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Home exercise 4 (3 points) Write a program which is able to handle
More informationCpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps
CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps Overview By the end of the lab, you will be able to: use fscanf() to accept inputs from the user and use fprint() for print statements to the
More informationIntroduction 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 informationAssoc. Prof. Dr. Tansu FİLİK
Assoc. Prof. Dr. Tansu FİLİK Computer Programming Previously on Bil 200 Midterm Exam - 1 Midterm Exam - 1 126 students Curve: 49,78 Computer Programming Arrays Arrays List of variables: [ ] Computer Programming
More informationLAB 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 informationCSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions
CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:
More information'C' Programming Language
F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax
More informationC++, How to Program. Spring 2016 CISC1600 Yanjun Li 1
Chapter 6 Function C++, How to Program Deitel & Deitel Spring 2016 CISC1600 Yanjun Li 1 Function A function is a collection of statements that performs a specific task - a single, well-defined task. Divide
More informationChapter 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 informationProgramming & Data Structure Laboratory. Day 2, July 24, 2014
Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body
More informationUNIVERSITY 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 informationFunctions. 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 information1001ICT Introduction To Programming Lecture Notes
1001ICT Introduction To Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 1, 2015 1 M Environment console M.1 Purpose This environment supports programming
More informationUnit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationPROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE
PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationComputer 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 informationLoops / 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 informationECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement
More informationMethods: A Deeper Look
1 2 7 Methods: A Deeper Look OBJECTIVES In this chapter you will learn: How static methods and variables are associated with an entire class rather than specific instances of the class. How to use random-number
More informationFunctions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa
Functions Autumn Semester 2009 Programming and Data Structure 1 Courtsey: University of Pittsburgh-CSD-Khalifa Introduction Function A self-contained program segment that carries out some specific, well-defined
More informationFunctions. 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 informationCS 108 Computing Fundamentals. October/November Array Bootcamp
CS 108 Computing Fundamentals October/November 2017 Array Bootcamp For arrays: passing to a function "by value" means passing a single element's "contents" For arrays: no more than one element's contents
More informationIntroduction to Programming
Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines
More informationChapter 1 Getting Started Structured Programming 1
Chapter 1 Getting Started 204112 Structured Programming 1 Outline Introduction to Programming Algorithm Programming Style The printf( ) Function Common Programming Errors Introduction to Modularity Top-Down
More informationComputer Programming 6th Week Functions (Function definition, function calls),
Computer Programming 6th Week Functions (Function definition, function calls), Hazırlayan Asst. Prof. Dr. Tansu Filik Computer Programming Previously on Bil-200 loops (do-while, for), Arrays, array operations,
More informationProcedural Programming & Fundamentals of Programming
& Fundamentals of Programming Exercise 4 (SS 2018) 12.06.2018 What will I learn in the 5. exercise Files Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Files A file can be seen as
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationSubject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100
Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space
More informationMultiple Choice Questions ( 1 mark)
Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s
More informationRefraction of Light. c = m / s. n = c v. The index of refraction is never less than 1. Some common indices of refraction are listed below.
Refraction of Light The speed of light in a vacuum is c = 3.00 10 8 m / s In air, the speed is only slightly less. In other transparent materials, such as glass and water, the speed is always less than
More informationBuilt-in Types of Data
Built-in Types of Data Types A data type is set of values and a set of operations defined on those values Python supports several built-in data types: int (for integers), float (for floating-point numbers),
More informationArithmetic and Logic Blocks
Arithmetic and Logic Blocks The Addition Block The block performs addition and subtractions on its inputs. This block can add or subtract scalar, vector, or matrix inputs. We can specify the operation
More informationEC 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 informationCOP 4516: Math for Programming Contest Notes
COP 4516: Math for Programming Contest Notes Euclid's Algorithm Euclid's Algorithm is the efficient way to determine the greatest common divisor between two integers. Given two positive integers a and
More informationFunction. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers
Functions 1 Function n A program segment that carries out some specific, well-defined task n Example A function to add two numbers A function to find the largest of n numbers n A function will carry out
More informationChapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.
Chapter 9 Functions 1 Introduction A function is a series of statements that have been grouped together and given a name. Each function is essentially a small program, with its own declarations and statements.
More informationChapter 7 Functions. Now consider a more advanced example:
Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done
More informationMentor Graphics Predefined Packages
Mentor Graphics Predefined Packages Mentor Graphics has created packages that define various types and subprograms that make it possible to write and simulate a VHDL model within the Mentor Graphics environment.
More informationHome Lab 7 Refraction, Ray Tracing, and Snell s Law
Home Lab Week 7 Refraction, Ray Tracing, and Snell s Law Home Lab 7 Refraction, Ray Tracing, and Snell s Law Activity 7-1: Snell s Law Objective: Verify Snell s law Materials Included: Laser pointer Cylindrical
More information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationFunctions. 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 informationspeed of light in vacuum = speed of light in the material
Chapter 5 Let Us Entertain You Snell s law states that as light enters a substance such as acrylic (high index of refraction) from air (low index of refraction), the light bends toward the normal. When
More informationMain Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University
C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types
More informationCT 229 Java Syntax Continued
CT 229 Java Syntax Continued 06/10/2006 CT229 Lab Assignments Due Date for current lab assignment : Oct 8 th Before submission make sure that the name of each.java file matches the name given in the assignment
More informationApplied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011
Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Autumn 2015 Lecture 3, Simple C programming M. Eriksson (with contributions from A. Maki and
More informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More informationOBJECTIVE QUESTIONS: Choose the correct alternative:
OBJECTIVE QUESTIONS: Choose the correct alternative: 1. Function is data type a) Primary b) user defined c) derived d) none 2. The declaration of function is called a) function prototype b) function call
More informationAN 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 informationUNIVERSITY 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 informationFunctions. 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 informationCarleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012
Carleton University Department of Systems and Computer Engineering SYSC 2006 - Foundations of Imperative Programming - Winter 2012 Lab 2 - C Functions Objective The objective of this lab is to write some
More informationFunction Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read
Function Calls Tom Kelliher, CS 240 Feb. 13, 2002 1 Administrivia Announcements Collect homework. Assignment Read 3.7 9. From Last Time SPIM lab. Outline 1. Function calls: stack execution model, memory
More informationInvestigation 21A: Refraction of light
Investigation 21A: Refraction of light Essential question: How does light refract at a boundary? What is the index of refraction of water? Refraction may change the direction of light rays passing from
More informationComputational Physics
Computational Physics Python Programming Basics Prof. Paul Eugenio Department of Physics Florida State University Jan 17, 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ Announcements Exercise 0 due
More informationUEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter
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
More informationfrom Appendix B: Some C Essentials
from Appendix B: Some C Essentials tw rev. 22.9.16 If you use or reference these slides or the associated textbook, please cite the original authors work as follows: Toulson, R. & Wilmshurst, T. (2016).
More informationFixed point algorithmic math package user s guide By David Bishop
Fixed point algorithmic math package user s guide By David Bishop (dbishop@vhdl.org) The fixed point matrix math package was designed to be a synthesizable matrix math package. Because this package allows
More informationI2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub I2204- Imperative Programming Schedule 08h00-09h40
More informationFunctions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay
Functions 60-141 Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Motivation A complex program Approximate Ellipse Demo Ellipse2DDouble.java
More informationMA 511: Computer Programming Lecture 2: Partha Sarathi Mandal
MA 511: Computer Programming Lecture 2: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Largest
More informationC Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.
C Tutorial: Part 1 Dr. Charalampos C. Tsimenidis Newcastle University School of Electrical and Electronic Engineering September 2013 Why C? Small (32 keywords) Stable Existing code base Fast Low-level
More informationProgramming for Engineers in Python. Recitation 2
Programming for Engineers in Python Recitation 2 Plan Range For loop While loop Lists Modules Operations Arithmetic Operations: + plus - minus * multiply / divide (int / float) % modulo (remainder) **
More informationC Functions Pearson Education, Inc. All rights reserved.
1 5 C Functions 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare Call me Ishmael. Herman Melville
More informationUNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:
UNIVERSITY OF WINDSOR 60-106-01 Fall 2007 QUIZ # 2 Solution Examiner : Ritu Chaturvedi Dated :November 27th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed.
More informationCS1100 Introduction to Programming
Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,
More informationUNIT 3 FUNCTIONS AND ARRAYS
UNIT 3 FUNCTIONS AND ARRAYS Functions Function definitions and Prototypes Calling Functions Accessing functions Passing arguments to a function - Storage Classes Scope rules Arrays Defining an array processing
More informationStructured programming
Exercises 8 Version 1.0, 1 December, 2016 Table of Contents 1. Recursion................................................................... 1 1.1. Problem 1...............................................................
More informationComputing and Statistical Data Analysis Lecture 3
Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret
More information#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;
1. Short answer questions: (a) Compare the typical contents of a module s header file to the contents of a module s implementation file. Which of these files defines the interface between a module and
More informationCS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their
More informationThere are three steps involved in converting your idea of what is to be done to a working program
PROGRAMMING Before we start on a course in Numerical methods and programming we should look at some of the basic aspects of programming. We will learn here, how to convert some of our ideas into a working
More informationME 172. C Programming Language Sessional Lecture 8
ME 172 C Programming Language Sessional Lecture 8 Functions Functions are passages of code that have been given a name. C functions can be classified into two categories Library functions User-defined
More informationBSM540 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 informationLearning to Program with Haiku
Learning to Program with Haiku Lesson 3 Written by DarkWyrm All material 2010 DarkWyrm So far we've been learning about programming basics, such as how to write a function and how to start looking for
More informationPurpose: To determine the index of refraction of glass, plastic and water.
LAB 9 REFRACTION-THE BENDING OF LIGHT Purpose: To determine the index of refraction of glass, plastic and water. Materials: Common pins, glass block, plastic block, small semi-circular water container,
More informationPython. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016
Exercises Center of Atmospheric Sciences, UNAM August 24, 2016 NAND Make function that computes the NAND. It should receive two booleans and return one more boolean. logical operators A and B, A or B,
More informationUnit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.
Unit III Functions Functions: Function Definition, Function prototype, types of User Defined Functions, Function calling mechanisms, Built-in string handling and character handling functions, recursion,
More informationRefraction Ch. 29 in your text book
Refraction Ch. 29 in your text book Objectives Students will be able to: 1) Identify incident and refracted angles 2) Explain what the index of refraction tells about a material 3) Calculate the index
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information
More informationFUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value
FUNCTIONS Definition: A is a set of instructions under a name that carries out a specific task, assigned to it. CLASSIFICATION of s: 1. User defined s (UDF) 2. Library s USER DEFINED FUNCTIONS Without
More information