Formatted and Unformatted Input/Output Functions

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 6: User-Defined Functions I
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CS1313: Standard Library Functions Lesson CS1313 Spring Standard Library Functions Outline 1.Standard Library Functions Outline 2.Functions in Mathematics.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6 - Functions modular programming general function format
Topic 6 Recursion.
Chapter 6: User-Defined Functions I
5.13 Recursion Recursive functions Functions that call themselves
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Function.
Introduction to Problem Solving and Programming
Recursive Algorithms 1 Building a Ruler: drawRuler()
Function.
CPS125.
Presentation transcript:

Formatted and Unformatted Input/Output Functions www.lpuians.com

Outline Unformatted Input/Output functions getchar() putchar() getch() gets() puts()

Unformatted Functions C has three types of I/O functions: Character I/O String I/O File I/O

getchar() This function reads a character-type data from standard input. It reads one character at a time till the user presses the enter key. Example: char c; c = getchar(); Syntax Variable-name = getchar();

#include<stdio.h> void main() { char c; printf(“enter a character”); c=getchar(); printf(“c = %c ”,c); } Enter a character k c = k

putchar() This function prints one character on the screen at a time which is read by standard input. Example: char c= ‘c’; putchar (c); Syntax putchar( variable name);

#include<stdio.h> void main() { char ch; printf(“enter a character: ”); scanf(“%c”, ch); putchar(ch); } enter a character: r r

getch() & getche() These functions read any alphanumeric character from the standard input device The character entered is not displayed by the getch() function until enter is pressed The getche() accepts and displays the character. The getch() accepts but does not display the character. Syntax getche();

#include<stdio.h> void main() { printf(“Enter two alphabets:”); getche(); getch(); } Enter two alphabets a

putch() This function prints any alphanumeric character taken by the standard input device Example: #include<stdio.h> void main() { char ch; printf(“Press any key to continue”); ch = getch(); printf(“ you pressed:”); putch(ch); } Press any key to continue You pressed : e

gets() String I/O This function is used for accepting any string until enter key is pressed (string will be covered later) Syntax char str[length of string in number]; gets(str);

#include<stdio.h> void main() { char ch[30]; printf(“Enter the string:”); gets(ch); printf(“Entered string: %s”, ch); } Enter the string: Use of data! Entered string: Use of data!

puts() This function prints the string or character array. It is opposite to gets() Syntax char str[length of string in number]; gets(str); puts(str);

#include<stdio.h> void main() { char ch[30]; printf(“Enter the string:”); gets(ch); puts(“Entered string:”); puts(ch); } Enter the string: puts is in use Entered string: puts is in use

Math Library functions

Outline Math library functions

Functions in Mathematics “A relationship between two variables, typically x and y, is called a function, if there is a rule that assigns to each value of x one and only one value of y.” So, for example, if we have a function f(x) = x + 1 then we know that f(-2.5) = -2.5 + 1 -1.5 f(-2) -2 -1 f(-1) f(0) +1 f(+1) +2 f(+2) +3 f(+2.5) +2.5 +3.5 …

Functions in Mathematics Likewise, if we have a function a(y) = | y | then we know that … a(-2.5) = | -2.5 +2.5 a(-2) -2 +2 a(-1) -1 +1 a(0) a(+1) a(+2) a(+2.5)

Function Argument f(x) = x + 1 a(y) = | y | We refer to the thing inside the parentheses immediately after the name of the function as the argument (also known as the parameter) of the function. In the examples above: the argument of the function named f is x; the argument of the function named a is y.

Absolute Value Function in C The abs function calculates the absolute value of its argument. It’s the C analogue of the mathematical function a(y) = | y | (the absolute value function) a= abs(y);

Absolute Value Function in C … fabs(-2.5) returns 2.5 abs(-2) 2 abs(-1) 1 abs(0) abs(1) abs(2) fabs(2.5)

Absolute Value Function in C #3 We say “abs of -2 evaluates to 2” or “abs of -2 returns 2.” Note that the function named abs calculates the absolute value of an int argument, and fabs calculates the absolute value of a float argument.

Function Call in Programming In programming, the use of a function in an expression is referred to as a call. We say that the statement printf("%d\n", abs(-2)); invokes or calls the function abs; the statement passes an argument of -2 to the function; the function abs returns a value of 2.

Math Function vs Programming Function An important distinction between a function in mathematics and a function in programming: a function in mathematics is simply a definition (“this name means that expression”), while a function in programming is an action (“this name means execute that sequence of statements”).

C Standard Library Every implementation of C comes with a standard library of predefined functions. Note that, in programming, a library is a collection of functions. The functions that are common to all versions of C are known as the C Standard Library.

Math Library Functions perform common mathematical calculations #include <math.h> Format for calling maths functions functionName( argument ); If multiple arguments, use comma-separated list Example: printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions

Math Library Functions Explain functions(SQRT, FABS,CEIL, FLOOR,POW) in detail

Math Library Functions: pow() The power function, pow(x,y), calculates xy; that is, the value of pow(x,y) = xy. pow(2,3)= 2³ = 8.0 and pow(2.5,3) = 15.625. The function pow is of the type double or that the function pow returns a value of the type double. x and y are called the parameters (or arguments) of the function pow. Function pow has two parameters.

sqrt() The square root function, sqrt(x), calculates the non-negative square root of x for x >= 0.0 sqrt(2.25) is 1.5 sqrt(25) is 5.0 The function sqrt is of the type double and has only one parameter.

fabs() fabs calculates the absolute value of a float argument. fabs(2.25) is 2.25 fabs(-25.0) is 25.0 The function fabs is of the type double and has only one parameter.

floor() The floor function, floor, calculates the largest whole number that is not greater than x. floor(48.79) is 48.0 floor(48.03) is 48.0 floor(47.79) is 47.0 The function floor is of the type double and has only one parameter.

ceil() The ceil function, ceil, calculates the smallest whole number that is not less than x. ceil(48.79) is 49 ceil(48.03) is 49 ceil(47.79) is 48 The function ceil is of the type double and has only one parameter.

Programming: Argument Type Programming has concepts that are analogous to the mathematical domain and range: argument type and return type. For a given function in C, the argument type – which corresponds to the domain in mathematics – is the data type that C expects for an argument of that function. For example: the argument type of abs is int; the argument type of fabs is float.

Argument Type Mismatch An argument type mismatch is when you pass an argument of a particular data type to a function that expects a different data type. Some implementations of C WON’T check for you whether the data type of the argument you pass is correct. If you pass the wrong data type, you can get a bogus answer. This problem is more likely to come up when you pass a float where the function expects an int. In the reverse case, typically C simply promotes the int to a float.

#include <stdio.h> #include <math.h> int main () { /* main */ const float pi = 3.1415926; printf("2.0 = %f\n", 2.0); printf("pi = %f\n", pi); printf("cos(pi) = %f\n", cos(pi)); printf("sin(pi) = %f\n", sin(pi)); printf("sqrt(2.0) = %f\n", sqrt(2.0)); printf("sqrt(2.0) / 2 = %f\n", sqrt(2.0) / 2); } /* main */

Function Use Example 2.0 = 2.000000 pi = 3.141593 cos(pi) = -1.000000 sin(pi) = 0.000000 sqrt(2.0) = 1.414214 sqrt(2.0) / 2 = 0.707107

Recursive function

Recursion Vs Iteration Examples of recursion Finding factorial of a number Finding Fibonacci series up to nth term Recursion Vs Iteration

Recursion Recursive functions Functions that call themselves Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

Recursion example (factorial) Factorial of a number in mathematics 5! = 5 * 4 * 3 * 2 * 1 Another method we have studied is For 5!, we write 5! = 5 * 4! Then for 4!, 4! = 4 * 3! Then for 3!, 3! = 3 * 2! Then for 2!, 2! = 2 * 1! Then for 1!, 1! = 1 * 0! And if its comes to 0, 0!=1 Solve base case (1! = 0! = 1) 1 2 6 24 120 Values returned

Recursion example (factorial)

Recursion example (factorial code) This function calculates factorial of first 10 numbers

Recursion example (factorial code) This function calculates factorial of first 10 numbers 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 output

Recursion example (fibonacci) What is Fibonacci series: …?? 0, 1, 1, 2, 3, 5, 8... Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1)+fibonacci( n – 2 ); }

Recursion example (fibonacci) Set of recursive calls to fibonacci() function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +  

Recursion example (fibonacci code) This function calculates fibonacci number of any given position

Recursion example (fibonacci code) This function calculates fibonacci number of any given position Enter an integer: 0 Fibonacci( 0 ) = 0 or Enter an integer: 1 Fibonacci( 1 ) = 1 Enter an integer: 20 Fibonacci( 20 ) = 6765 output

Recursion vs. Iteration Repetition Iteration: explicit loop(for,while) Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case reached Both can have infinite loops Balance Choice between performance (iteration) and good software engineering (recursion)

Rules for recursive function In recursion, it is essential to call a function itself Only the user defined function can be involved in the recursion. Library function cannot be involved in recursion because their source code cannot be viewed A recursive function can be invoked by itself or by other function. To stop recursive function, it is necessary to base recursion on some condition, and proper termination statement such as exit() or return The user defined function main() can be invoked recursively.