Download presentation
Presentation is loading. Please wait.
Published byEleanor Cook Modified over 6 years ago
1
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
CPCS202- LAB4 Enum ,Char Functions& Math Library Functions I.Mona Alshehri
2
Character functions toupper function: Header file: ctype.h Syntax:
toupper(ch) Where ch is a character literal or variable Job: translates character into upper case check other function tolower from Help
3
Character functions toascii function: Header file: ctype.h Syntax:
toascii(ch) Where ch is a character literal or variable Job: translates character to ascii format to see ascii code, define integer variable
4
Character functions isdigit function: Header file: ctype.h Syntax:
isdigit(ch) Where ch is a character literal or variable Job: returns nonzero if c is a digit. Check other function isalpha, isupper, islower from Help Example: cout <<isdigit(‘F’)<<endl // print 0 << isdigit(‘8’); // print any nonzero number
5
Enumeration Data Type:
Syntax: enum model_name { value1, value2, value3, . . } ; Example: enum colors {black, blue, green, cyan, red, purple, yellow, white}; colors mycolor; mycolor = blue; In fact our enumerated data type is compiled as an integer and its possible values are any type of integer constant specified. If it is not specified, the integer value equivalent to the first possible value is 0 and the following ones follow a +1 progression. Thus, in our data type colors that we defined before, black would be equivalent to 0, blue would be equivalent to 1, green to 2 and so on.
6
Math Library Functions :
Header file is: # include <math.h> sqrt(n) abs(n) pow(b, n) n b^n
7
Math Library Functions :
Syntax: function_name (argument); Data types of arguments and returned value: Function Arguments Return value sqrt(n) number number Ex: sqrt(16) 4 abs(n) ± integer number integer number Ex: abs(-6) 6 pow(b, n) b: ± number ± number n: ± integer number Ex: pow(3,2) 9
8
Math Library Functions :
Syntax: function_name (argument); Arguments can be variables or numbers. Example1: int x= 4; cout<<sqrt(x); 2 Example2: int y; y = pow(2,3); cout<<y; 8 Example3: int x,y; x = -3; y = 6; cout<<abs(x); 3 cout<<abs(y); 6
9
Math Library Functions :
We can use nested functions for example: sqrt ( pow ( abs (-4), 3) ) sqrt ( pow ( 4 , 3) ) sqrt ( 64) = 8
10
Math Library Functions :
#include <iostream.h> #include<conio.h> #include <math.h> // needed for the pow function void main() { int x; cout<<"Enter a number : "; cin>>x; cout<<endl<<x<<"\t"<<pow(x,2)<<"\t"<<pow(x,3) getch(); } Or int x,y,z; y= pow(x,2) ; z= pow(x,3) ; cout<<endl<<x<<"\t"<<y<<"\t"<<z;
11
Example: Calculate and display the area of a circle and the circumference. Area of circle = 3.14 * r2 Circumference = 3.14 * 2 * r where r is the radius Area = 3.14 * r2 Circumference = 3.14 * 2 * r Input radius output Area, Circumference Start End
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.