STANDARD FUNCTIONS 350142 - Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Return values.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
1 Algorithms Basic Control Structures Comparisons and if (…) statement What is a function? Math Library Functions Character Functions Reading Sample Programs.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
Numbers. Number data types store numeric values They are immutable data types, which means that changing the value of a number data type results in a.
DEDiscover Differential Equation Modeling Solution CANGLIN WU June
Functions and methods A method is a function that is a member of a class A method is a function that is a member of a class FCL(Framework Class Library)
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Functions and methods A method is a function that is a member of a class FCL(Framework Class Library) provides a rich collection of classes and methods.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
A function is a subprogram that acts on data and often returns a value. You are already familiar with the one function that every C++ program possesses:
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 Functions ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CSC Programming for Science Lecture 7: Math Functions.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Visual Basic I Programming
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 Ordinal types An ordinal data type is an ordered set in which every element, except the first element, has an immediate predecessor, and every element,
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
Recursion It is a process of calling the same function itself again and again until some condition is satisfied. Syntax: func1() { ……….. func1(); }
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Variables, Operators, and Expressions
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
Week 3 - Wednesday CS 121.
Arithmetic Operations
String, Math and the char Type
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Mathematical Functions
ECE 1304 Introduction to Electrical and Computer Engineering
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
ECE Application Programming
Computer Simulation Lab
FUNCTIONS EXAMPLES.
Fundamental of Java Programming Basics of Java Programming
Math Library and IO formatting
Copyright © 2012 Pearson Education, Inc.
Engineering Problem Solving with C
Lecture 2B Expressions Richard Gesick
Using Free Functions Chapter 3 Computing Fundamentals with C++
Functions October 23, 2017.
class 5 retrieving a previous statement in Thonny or IDLE // %
Output Formatting Bina Ramamurthy 4/16/2019 BR.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Computer Simulation Lab
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Standard Functions  How will you do if you need to compute the cosine value of the angle 60  ? Write your own function to calculate it!  Actually, C have prepared a lot of useful functions that cover almost every programming task and that are ready to use in your program. Thus, the more you know about these predefined functions, the more effective your program will be.

Function Usage  Ex. double sqrt (double x) return value (output from the function as the type double) function’s name parameter(input value(s) to the function as the type double) main(void) { double x = 4.0, result; result = sqrt(x); printf("The square root of %lf is %lf\n", x, result); return 0; } Both are the types of double)

Math Function (1)  C standard math functions (trigonon) #include FunctionDescription sin(x) return sine value of angle x (x in radian) cos(x) return cosine value of angle x (x in radian) tan(x) return tangent value of angle x (x in radian) asin(x) return arcsin value in radian of x acos(x) return arccos value in radian of x atan(x) return arctan value in radian of x Remind: 180 angle in degree = π angle in radian ( π is ) Degree to radian transformation : angle in radian = (angle in degree x π )/180 Radian to degree transformation: angle in degree = (angle in radian x 180)/ π All these functions take double as inputs return double as output

Example #include int main(int argc, char *argv[]) { double result; double x; x = (60 * )/180; result = cos(x); printf("The cosine of %.2f is %.2f\n", x, result); return 0; } Transform an angle (60) in degree into an angle in radian. Use of the function cos to compute the cosine of the angle x

Math Functions (2)  Let’s study math for a bit  What is the value of x (double): x = log10(100); x = log2(16); x = log(exp(5)); FunctionDescription exp(x) return value of e x (e = ) log(x) return value of natural logarithm of x (ln) log10(x) return value of common logarithm (based 10) of x log2(x) return value of logarithm (based 2) of x Be careful

Math Functions (3) FunctionDescription pow(x, y) return value of x y sqrt (x) return value of square root of x fabs(x) return value of absolute of x (for floating point data type) abs(x) return value of absolute of x (for integer data type)  What is the value of x x = pow(5, 2); x = sqrt(16); x = fabs(-5.8); x = sqrt(pow(20, 2)); x = (pow(2, 3) + sqrt(25) + fabs(-1)) / fabs(-2);

Math Functions (4) FunctionDescription floor(x) return an integer which rounds down the value of x the largest integer number which is not > x ceil(x) return an integer which rounds up the value of x the smallest integer number which is not < x round(x) return an integer near the value of x - If the number after decimal point is greater than or equal to.5, then round up the number. - Otherwise round down  What is the value of x x = floor(5.8); x = ceil(6.1); x = round(3.2); x = round(3.6); x = floor(-2.5); x = ceil(-2.5); x = round(-3.2); x = round(-3.6);

Character Classification Functions (1) All the following functions are defined in the library ctype.h #include is needed FunctionDescription tolower(ch)Return a lowercase character of ch toupper(ch)Return a uppercase character of ch char x = ‘A’, y; y = tolower(x); printf(“y = %c”, y); char x = ‘a’, y; y = toupper(x); printf(“y = %c”, y); aA

Character Classification Functions (2) FunctionDescription islower(ch)Return non-zero if ch is an lower-case character, otherwise return zero isupper(ch)Return non-zero if ch is an upper-case character, otherwise return zero isalpha(ch)Return non-zero if ch is a letter (A to Z, a to z), otherwise return zero isdigit(ch)Return non-zero if ch is a number digit (0 to 9), otherwise return zero isalnum(ch)Return non-zero if ch is a letter or a number digit (A to Z, a to z, 0 to 9), otherwise return zero char x = ‘A’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘b’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘5’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘#’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x);

Review: ASCII table FunctionDescription islower(ch)Return non-zero if ch is an lower-case character, otherwise return zero isupper(ch)Return non-zero if ch is an upper-case character, otherwise return zero isalpha(ch)Return non-zero if ch is a letter (A to Z, a to z), otherwise return zero isdigit(ch)Return non-zero if ch is a number digit (0 to 9), otherwise return zero isalnum(ch)Return non-zero if ch is a letter or a number digit (A to Z, a to z, 0 to 9), otherwise return zero char x = ‘A’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘b’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘5’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x); char x = ‘#’;  islower(x);  isupper(x);  isalpha(x);  isdigit(x);  isalnum(x);

Character Classification Functions (3) FunctionDescription isascii(ch)Return non-zero if ch is an ASCII character (0x00 – 0x7F), otherwise return zero iscntrl(ch)Return non-zero if ch is a delete character (0x7F) or ordinary control character (0x00 – 0x1F), otherwise return zero isprint(ch)Return non-zero if ch is a printable character (0x20 – 0x7E), otherwise return zero isspace(ch)Return non-zero if ch is is a space, tab, carriage return, new line, vertical tab, or formfeed (0x09 - 0x0D, 0x20)., otherwise return zero char x = 0x7F;  isascii(x);  iscntrl(x);  isprint(x);  isspace(x); char x = 13;  isascii(x);  iscntrl(x);  isprint(x);  isspace(x); char x = ‘#’;  isascii(x);  iscntrl(x);  isprint(x);  isspace(x);

Character Classification Functions (4) FunctionDescription isgraph(ch)Return non-zero if ch is a characters in class print but no characters in class space shall be included, otherwise return zero ispunct(ch)Return non-zero if ch is neither the nor any characters in classes alpha, digit, or cntrl shall be included, otherwise return zero isxdigit (ch)Return non-zero if ch is a hexadecimal digit (‘0’ – ‘9’, ‘A’ – ‘F’, ‘a’ – ‘f’), otherwise return zero char x = 0x7F;  isgraph(x);  ispunct(x);  isxdigit(x); char x = 65;  isgraph(x);  ispunct(x);  isxdigit(x); char x =  isgraph(x);  ispunct(x);  isxdigit(x);

String Functions  There are some important functions in C used for string  We should add #include to use these functions FunctionDescription strlen(str)Take a string as input and return the length of string strcpy(str1, str2)Copy the string data from str2 to str1 strcat(str1, str2)Concatenate the string data of str2 to str1 strcmp(str1, str2)Compare the string data of str1 and str2