Unit5

  • Uploaded by: Phichya Laemluang
  • 0
  • 0
  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Unit5 as PDF for free.

More details

  • Words: 4,949
  • Pages: 28
1

I

Function

Asn"i7pd

Idsaansuniui c++Zuiisiu%ua"num:nis~un%44sn"$u Imvni~i c++aciuisoa~is4sn"$u16 .uinn
2. flsn~u~%~fPlinum (User-Define Function)

1. ds6%duin-b9iw (Standard Libraries Function)

a8unirl~siu~sn"$u~0sniui c++i 1 6 Q n f i ~ n u m l i ~ l # 3 ~ ~ $l a. ~ui ~~ ~a n?" $ ~ i 1 4iiulnnlnu ~

T ~ ~ ~ : ~ ~ ~ ~ ~ $ I P I ~ ~ ~ ~ Y ~ ~$d1;~~.6f " $ ~ C ~ I U ~ ~ ! ~ ~ - ~ I ~ I ~ ~ I ~ ~ ~ ~ ctype.h or cctype J~pGpd?z-baun"nr namespace std int isalnum (int Character); a8uBsn"$unismsaoa~u~aGn~s:.dia$u6aaa"unio isalnum('aJ) value > 0 ex: ~aCnusFmuo:aisii~ua3u~aaavBinina$ugaaavn%

+

ii'~Gnpr.ioG~Al~1~jl$ o ~~a:w1n~ducn'atus:6uo:1661

6dI.J0 int isalpha ( int character); ex: isalpha('aJ) value > 0

+

int isdigit ( int character); isdigit('9') value > 0 ex:

+

r8u~sn"$unisnsaoarou6aGnvs:iia8u~aCn~~s~muo:

I M I ~ A ~ u ~ ~ G ~ Mo~ o : z M ~ I ~ ala:ninIBj~$u~aGnuso:1Xi1~8uo dsiiZua3~63aav~

adu~sn"$unisnsaoa~u6;1'aGnvs:.di16u~a6vFmoo:

aisdii%Gh.G'aaav K i w i n a $ u ~ a ~ a v o : ~ u i i ~ l i % o$ aaa:~inI~a~u~aaavo:I6Aia~u o

int islower ( int character); islower('ar) value > 0 ex:

+

a~~dYsn"~pdnisms~o~oucm"a~nvs:iia~u~aCnus~dn 1mao:dsAi~uadu~aaav $iw1na3uKaGn~sadno:Ru ii$?d$Oaaa:nin?~~8u~a~nusadno:~&~ia~u 0

662304 - 05

Page 1 of 28

int isspace ( int character); ex: isspace(' ') value > 0

+

aiaz~inlai~Suiosiisoz1Xdii~u O int isupper ( int character); ex: isupper('D') 3 value > 0

idu~s6$unirmsaono9~$aGn6usziii8~6aGnp1s1~$

int tolower (int Character); toIower('a') 3 'A' ex:

iguRsn"~urnsidas6a5n~lslw$i8u6a~np1sidn TRU

F~uozai~d1dwi8~6aam biwinigu6adnpIs%w$~zdu

ozds6iZui~u6aGnpIsidn

int toupper (int Character); toupper('A1) 'a' ex:

+

dw

w

w

w

nnnnaod 5-1 m~~$~iufianaununaonvsz~au Library ctype #include #include <string> using namespace std; int main ( )

I char Ch; string Message; cout << "Enter character : "; cin >> Ch; cout << endl; if (isalnum(Ch)) { if (isalpha(Ch)) ( if (islower(Ch)) Message = "lower character."; else Message = "upper character."; 1 else if (isdigit(Ch)) Message = "digit."; 1 else Message = "special character."; cout << "\"' << Ch << "\"' << " is " << Message << endl; if (isalpha(Ch)) { if (islower(Ch)) { cout << "\'" << Ch << "\'" << " convert to upper \ ' " ; cout << (char)toupper (Ch) << " \ "'; 1 else ( cout << " \ ' " << Ch << " \ ' " << " to lower \ ' cout << (char)tolower (Ch) << " \ ' "; 1 cout << endl; 1 return (0); 'I;

1

..

662304 - 05

Page 2 of 28

Stdlib Library (std1ib.h or cstdlib) do~3J"od?n"~auzu namespace std

I double

1

ex:

atof ( const char *string) ; atof("45.6") 45.6

+

1 int

1

atoi ( const char *string) ; ex: atof("30) 30

+

I 18uJs6$unisudaadi$onaiu1~u6;1"a~avwa~~ ~utudsdiI ~ua8~6;1"a~avq/la6u~~~b3~'~1 double

I

I ~8uJsn"~unis~~nlasdi$ona1ua8u6;1"a~avBiuau1G~~ Fmer I

I I l8u~la6~umrlbdasd1$8nai~~b8u6;1"a1av41uaub~u TRU 1 risdi$u~8usi?mv~iuaub~u1~~1~1 I dsdifiu~8~6;1"~~avBiuauai~~ int

I

long int atol( const char *string) ; ex: ato1("100000) 100000

+

int rand ( void) ; ex: rand () 3 ?

long int

18u~sn"$unirrja~16;1"a~av%u~aa 0 to RAND-MAX,

Fmuf~diseed %uni~niis6;1"aaavrju$o 1 dmerriadifiub8u 6a~avBiuau~~u~bp1~1 int

void srand (unsigned int Seed ) ; ex: srand (100 ) value > 0

~8u~sn"biunisfiinumdi Seed ~so~18udi~o:%~%uni3

int abs (int x) ; ex: abs (55) or abs (-55) 3 55

18uJs6$umsmdi~u~sdvo~di x inerdsd,n"uflu

i=-jn4i?mvvosJ.m6$u rand

nismnaes$5-2 nis~~siod~s&'pd"bpd Library cstdlib #include #include <string> using namespace std;

int main ( ) {

int IntValue; long Longvalue; float Floatvalue; string StrValue; cout << "Enter string number :

";

Page 3 of 28

cin >> StrValue; / / convert string to numeric IntValue = atoi(StrVa1ue.c str()); LongValue = at01 (StrVa1ue.c str ( ) ) ; Floatvalue = atof (str~a1ue.c -str ( ) ) ; tout << endl; cout << "Convert String to Numeric.\nW; COUt << cout << "Convert to integer = " << IntValue << endl; cout << "Convert to long = " << LongValue << endl; cout << "Convert to float = " << FloatValue << end1 << endl; / / init seed value to ranom srand(IntVa1ue); cout << "Now random integer number 10 number:" << endl; COUt << . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . << endl; for( int N = 1, Num ; N <= 10 ; N++) { Num = rand() % 10; cout << Num << " "; 1 cout << endl; return (0); ll**************************\n~l.

1

662304 - 05

Page 4 of 28

Math Library (math-hor cmath) double ceil (double x) ex: ceil (54.3) 55.0

~3uYs6~unisniii6a~av~wmiu~1uu~sii x %mu

double floor (double x) ex: floor (54.3) 54.0

~ ~ u ~ s 6 ~ u n i s ~ i i i 6 a 1 a u t w m i u ~xi s%mu vosii

double sqrt (double x) ex: sqrt ( 4 ) 2.0

t 3 u ~ s 6 ~ u n i s w i ~ i s 1 n ~ f l o a vxo Fmodsii s~i

double exp (double x) 7.389056 ex: exp (2)

~3uYa6iumrnii-1 exponential v o s h x &?(I

+

+

+

+

dsi15uaduKa~avwaGuu

dsdi~ut~u6alavwaGuu ~ul8uKalauwaGuuFW$I x >= o giu e, Fnud e 8 i i 2.718282 Fnudsiin"ul$u 6a~avwaGuu

double fabs (double x) ex: fabs (78.5) or fabs (-78.5) -3 78.5

~~uYs6~unisrniidu~sdvo~ii x Imudsii8u

double log (double x) ex: log (10) -3 2.302585

1guls6iunismnatural logarithm uosdi x

double log10 (double x) ex: log10 (10) -3 1.0

i?!u~s6~urnmiiilogarithm giuzuuosdi x

double pow (double x, double y) ex: pow ( 5 , 3 ) 125.0

1?Ju4.r6~umswiiiun~ia's 4 s x i?Juiigiuaa::

+

lau63tavwffGuu

FmudsiiGulduKalauwaGuu

FmudsiiZul3uKalauwaGuu

y i?Ju6a$fiia'sFmisidutauhlaunaGuu

double sin (double x) ex: sin (0) 1

13u~t6iumsmdi sin uosii x , ?RUG x d u

double cos (double x) ex: cos (0) -3 1

~3uls6~unirniii cosine uosn'i x

+

41 radians FmuaisiiGu~~laKa~auwaGu~~

131.4 radians

L ~ U x~ I

, TRU$ x

'6mu~s6i~udu~alasl

waGuu'

double tan (double x) ex: tan (0) 0

+

i$u4s6$unis~iditangent uotdi x ,1muwA x L ~ U x~ L I ~ radians U

*smudsii~ulauKalav

waGuu m?jwneesi5-3 n i ? j ~ ~ s i ~ f i s d ~ Library o d ~ o d cmath #include #include

I,

using namespace std;

662304 - 05

Page 5 of 28

double Value; '

I

cout << "Enter floating number : "; cin >> Value; cout << endl; cout << "\nCeiling of " << Value << " is " << ceil(Va1ue); cout << "\nFloor of " << Value << " is " << floor(Va1ue); cout << "\nSquare root of " << Value << " is " <<sqrt(Value); cout << "\nExponential of " << Value << " is " << exp(Va1ue); cout << "\nFloating absolute of " << Value << " is "; cout << fabs(Va1ue); cout << "\nNatural logarithm of " << Value << " is "; cout << log (Value); cout << "\nLogarithm(lO base) of " << Value << " is "; cout << log10 (Value); cout << "\nPower three of " << Value << " is "; cout << pow (Value,3) ; cout << "\nSin of " << Value << " is " << sin(Va1ue); cout << "\nCosine of " << Value << " is " << cos(Va1ue); cout << "\nTangent of " << Value << " is " << tan(Va1ue); cout << endl; return (0);

662304 - 05

Page 6 o f 28

String Library (string.h or cstring) char *strcat ( char *Stringl, const char "String2); ex: strcat("Funct", "ions") "Functions"

+

char *strchr ( const char *String2 int character); ex: strchr("Funct", 'n') "nct"

+

int strcmp ( const "Stringl, const char *String2); ex: strcmp("TurboC","TurboC") 0

+

char *strcpy ( char *Stringl, const char *String2); ex: strcpy(str,"STRINGS") "STRINGS'

+

size-t strlen ( const char *String ); ex: strlen ("WRITE) 5

+

char *strncat(char*Stringl,const char *String2,sizze-t n); ex: stmcat ("Micro", "software",4) "Microsoft"

+

int strncmp (const *Stringl,const char *String2,size-t n); ex: stmcmp ("TurboC", "Turboc",4) 0

+

char *strncpy(char*Stringl,const char *String2,size-t n); ex: stmcpy ( str, "WRTIER,5) "WRITE

+

662304 - 05

Page 7 of 28

msnnns-ri5-4 rn~l~~ub&ul:ulu Library cshing #include #include <string> #include using namespace std;

I

int main ( )

I char Str1[30], Str2 [30]; cout << "Enter string 1 : "; cin >> Strl; cout << "Enter string 2 : "; cin >> Str2; cout << endl; cout << "strcat(Strl,Str2) = " << strcat( Strl, Str2); cout << endl; cout << "strchr(Strl,'sl) = " << strchr( Strl, Is1) << endl; cout << "strcmp(Strl,Str2) = " << strcmp( Strl, Str2); cout << endl; cout << "strcpy(Strl,Str2) = " << strcpy( Strl, Str2); cout << endl; cout << "strlen(Str1) = " << strlen( Strl) << endl; cout << "strlen(Str2) = " << strlen( Str2) << endl; cout << "String 1 : " << Strl << endl; cout << "String 1 reverse : " ; for(int N = strlen( Strl) - 1 ; N >= 0 ; N--) cout << Strl [N]; cout << end1 << endl; return (0);

Page 8 of 28

IOMANIP Library (iomanip) [setw(int w)

I 1Sunirfi1numnaiun~1s~~s3onai%~~0:ii~msm1%~~1u~s w

left

Cmwaa'wsIn~m4iu%4iaun's~4sn"&u setw

right

Cmwaaa'wti%n~muai d 9I~iauEu4sn"~ setw u

fixed

iaamsdiuauo~~i$uiaujmw~sd"uu 6 wgn dmu%ha6 ibu 123.4 o r

d

9-

I.

scientific

1 showpoint 1 noshowpoint

I uniFinniriam show~oint

I

1 skipws 1 noskipws

I unt~n6aGnurr$i!hWhiteSpace ~uuorr?u$oya I fiinum~a~nurzdtguWhitespace luumr?ubbyn

showpos noshowpos

uniEnnirtan showpos

showbase

IGauiiuiiddn oct n?o hex i ~ o t m ~ t w a a " w & ~ u ~ a uibu ~iudi~~

1 noshowbase

1 boolalpha I no boolalpha

662304 - 05

I uni~nnirianshowbase

I

I tinsn'iyalu Kiaf.n$u true n?on'iii;oillu false I uniiinmrian boolalpha

Page 9 of 28

#include #include #include using namespace std; int main ( )

I float Raduis, Angle; const int WIDTH = 9; cout << "t" << setfill('=') << setw(44) << "t" << endl; cout << " : Angle : Sine : Cosine : Tangent : " << endl; cout << "t" << setfill('=') << setw(44) << "+" << endl; cout << setfill ( ' ' ) ; for (Angle = 0.0 ; Angle <= 360.0 ; Angle += 20) ( Raduis = (Angle > 0.0) ? (180.0f*3.14f)/Angle : 0.0; cout << " : " << setw(W1DTH) << fixed << setprecision(2) cout << Angle; cout << " : " << setw(W1DTH) << setprecision(4) << sin(Raduis); cout << " : " << setw(W1DTH) << cos(Raduis); cout << " : " << setw(W1DTH) << tan(Raduis); cout << " : " << endl; 1 cout << "+" << setfill('=') << setw(44) << "t" << endl; return (0); 1

#include #include using namespace std; int main ( ) {

tout << !' Decimal : Octal : Hexa " << endl; cout << setfill << setw(30) << "-" << endl; cout << setfill(' ' ) << showbase; for(int Dec = 0 ; Dec <= 400; Dec += 50 ) { cout << " " << right << setw (7) << setbase(lO)<< Dec << " : cout << right << setw (7) << setbase (8) << Dec << " : "; cout << left << setw(7) << setbase(l6) << Dec << endl; 1 return (0); ( I - ' )

I Page 10 of 28

";

2. fi~nd%d~%il~foan (User-Define Function) d w 2 d w d lumw C++ mlnsoahs4snauuua1l~s1u~~s~~a~4snaun!6~iana1a6osn1s T~uQy.Juuu Y

$¶JLI'LIPI

V

Function header Function body

+ +

data-type Function Name (type var1,type var2,. ..)

I variable declaration; statements; return(va1ue);

Cofii~umnisl~Ysn"$u dw

'4

-

Ysnclru~arfiso:d3:nou&au 2 daun"oFunction header ua: Function body

-

Tau Function header dsznau&?odata-type, a function name, a parameter declaration 6@110%16( )

-

IRU Function body dr:nouhu~iksmuiC++ odmulu { )

- l u Function body bsjimsl~itf'sreturn duondspnoun'iot~nni~~isiu i~arl4iiks return 1~saisdiw6sna'~ro1nnisdunl44sn"~u -

I

dv

div?u Function header o:~~u6aunisfiinumdsz~n~uosniwPilosnis~sn~ua1~os~sn"~u

n1n~nifiiwumdsz~nw!?~~~o~14sn"~udsd1nGut~u~a~au~1uawt~u(int) la(m L L ~ ' & I ! ~ dw

nY

6 ~ ~ ~ l ~ % ~ ~ ~ n " $ ~ ~ ~void i Sfilnum!? d ~ ~ Ll~ ~o uas n" ~~l ~~~ n~' l 6l u ~u ~~n i%~ ~~ 9~j i nli sk ~ dsdin~u(!niijrns%~iiks return)

662304 - 0 5

Page 11 of 28

61th return OZI~IWGI~2 ~ 6 1 4ZB ~3uni~u0nii0uni~p;i1aiu"u0~4an"~pd iiaz ~ ~ S n i r d a ~ i i

-

nriuoinmrSun'l( Tnu argument aoaii& return muirnl3u expression 1A Kaoeiia

main()

/*

int Feet2MeterO

*

void main()

/* /*

char findchar ( str, ch)

*/ 4an"kud~diniiua~n integer */ 1lilnird.tri1ln~n ~ u i u l ~ os d */ dsdintYuigu6a6nur 1$3 */ g~PiinSui8uinteger

1* A

]

*/

User-define Function for calculate value of power by two int square( int n) { return( n * n );

I

2.1 nim.kernflddnd$~(Function Prototype)

int FunctionName void main() C int v;

I

I

V

=

(

data-type

/ * function

);

FunctionName( variable

);

int FunctionName( data-type variable {

prototype

/ * calling )/*

*/

function * /

Definition Function*/

statements;

I #include int square( int ) ; int main ( ) t int x; while ( x <= 10) { printf("%d\n" x = x + l ;

/*

function prototype

*/

, square(x)); / * calling function * /

I return ( 0) ;

I int

I

square (int

n)

/*

Definition Function

*/

return ( n * n) ;

I 662304 - 05

Page 12 of 28

I

#include u s i n g namespace s t d ; f l o a t CalArea ( ) ; i n t main ( )

I f l o a t Area; c o u t << "Progrsm C a l c u l a t e Area o f C i r c l e . " << e n d l ; Area = C a l A r e a O ; c o u t << "Area o f c i r c l e : " << Area << e n d l ; return ( 0 );

1 f l o a t CalArea ( )

I f l o a t R a d i u s , Area; c o u t << " I n p u t r a d i u s : "; c i n >> R a d i u s ; Area = 3 . 1 4 f * R a d i u s * R a d i u s ; r e t u r n ( A r e a );

,1

2.2 2daauumsaisiiwd~szwi~s~sn"tw

1um~imrl~uu1ds~1ns%16a"1.d~:~~~n~sds~1~~iru"0~a1Xn"u~sn"~u~1Gsiu 2 lluu $8 1 ) Pass by Value 19umroian'iuoaain~a11d~ iiwod n4od1n.r~lfln'u~sn"%u~~ndunI4siu I~uiiuoso~nGa

input

U output

e

int (

U

swap( int a, int b) return;

1

662304 - 05

Page 13 of 28

2) Pass by Reference Y

~ ~ u n i s ~ i ~ n ' ~ u o a h ~ ~ p l ~ l ~ n " ~ ~ ~ a n~mudiuoa~~lanls~aialX~uot "Gu$~nd~n~~~iu

input/ output

u

output

t

int {

Ufi

swap(int&a,int&b)

return;

'

I #include using namespace std; int Min (int V1, int V2) ; int Max (int V1, int V2) ; int main ( ) {

int Valuel, Value2; eout << "Enter first number : "; cin >> Valuel; tout! << "Enter second number : "; cin >> Value2; cout << "Max value cout << "Min value return (0);

: :

" << Max (Valuel,Value2) << endl; " << Min(Valuel,Value2) << endl;

1 int Min (int V1, int V2) {

if (V1 < V2 ) return (Vl); else return (V2);

1 int Max (int V1, int V2) {

if (V1 > V2) return (Vl); else return (V2); ,

I

662304 - 05

Page 14 of 28

#include using namespace std; void Sort3 (int &V1, int &V2, int &V3); int main ( ) {

int Valuel, Value2,Value3; cout << "Enter three integer number : "; cin >> Valuel >> Value2 >> Value3; int Output1,0utput2,0utput3; Outputl = Valuel; Output2 = Value2; Output3 = Value3; Sort3(0utput1,0utput2,0utput3); cout << Valuel << " " << Value2 << l1 l1 << Value3 ; cout << " in sorted order is " ; cout << Outputl << l1 " << Output2 << " " << Output3 << endl; return (0);

I void Sort3(int &V1, int &V2, int &V3)

I int tmp; if (Vl > V2) { trnp = V1; Vl = v2; V2 = tmp;

1

if (Vl > V3) { trnp = V1; Vl = v3; V3 = tmp;

I if (V2 > V3) { trnp = V2; v2 = v3; V3 = tmp;

I I

662304 - 05

Page 15 of 28

#include #include <string> using namespace std; void ChangeString(string &S1, string &S2); int main ( )

I string Strl, Str2; cout << "Enter first string : "; cin >> Strl; cout << "Enter second string : "; cin >> Str2; cout << "Data string before call function.\nV; cout << "Strl = " << Strl << endl; cout << "Str2 = " << Str2 << endl; ChangeString (Strl, Str2); cout << "Data string after call function.\nn; cout << "Strl = " << Strl << endl; cout << "Str2 = " << Str2 << endl; ChangeString (Strl, Str2); cout << "Data string after cal function.\nW; cout << "Strl = " << Strl << endl; cout << "Str2 = " << Str2 << endl; return (0);

I void ChangeString(string &S1, string &S2) {

string Temp; Temp = S1; S1 = S2; S2 = Temp;

1

Page 16 of 28

2.3 "usu!"un$3aads(Scope of Variables)

d1n?u6~~sds%uni~111d~nisp;ii~iu ~~Eisoonlgu 2 ?rGa so 1. Global Variable ~~uhud~daiuisn~ild tl~6Guuo.r~s~mrutu l~~lu~n~ ~aomsdszniao:fii~ua~a"mda4d6Gu main() Y

2. Local Variable

Faunisdszmaozo~niu~uuo~~1~a:A~6Gu ilguoulu~6a~lds 2 "

d s ,

I. h ~ d r o z d r z m n u u f t l friou$oddnnglu i statement

#include int x, y; char ch, name[40]; void main()

I int

a, b;

1 int

funcl( int

a

)

I char c; float f;

I char func2

(

char

b

)

I int c, x; char name;

1

msniiiunKia'9oin y

= xn

floatpower(f1oat

x, int

n)

I float while

total=l; n > 0 ) total *= x; (

{

n--;

1 return (total);

1 oin y

Y

i ~ u ~ ~ ~ i ~ l s d n i u i s Asn"%u~ds:nia~ad~pi~~u n~~~~~wiz~u

=

xn

=

(n In

X)

float powern(f1oat x, int n)

I if (n < 1 ) return (1); else return (exp(nxlog(x)));

1 Page 17 of 28

#include using namespace std; void a (void); void b (void); void c (void); int x = 1; int main ( )

I

int x = 5; / * lscal variable to main */ cout << "local x in outer scope of main is " << x << endl;

I int x = 7; cout << "local x in inner scope of main is " << x; cout << endl;

I cout << "local x in outer scope of main is " << x << end1 a(); / * a h . 3 ~automatic local x * / b() ; / * b has static local x */ c() ; /* c uses global x */ a(); / * a reinitialize automatic local x */ b 0 ; /* static local x retains its previous value * / c(); / * global x also retains its value */ cout << "local x in main " << x << endl; return (0);

;

I void

a( )

I int x = 25; / * initialzed each time is called * / cout << "\nlocal x is " << x << " on entering a()"; ++x ; cout << "\nlocal x is " << x << " before exiting a()\nW;

I void

b()

I static int x = 50; / * static initializations only */ cout << "\nlocal static x is " << x << " on entering b o w ; ++X ; cout << "\nlocal static x is " << x; cout << " before exiting b()\n" << endl;

1 void

c()

I cout << "\nglobal x is " << x << " on entering c o n ; x += 10; cout << "\nglobal x is " << x << " before exiting c()\nW;

I 662304 - 05

Page 18 of 28

2.4 rn=ia~iddd6$'~lsuu Constant Parameters

void Example(const int a, int b, int c) { / / legal assignment b = a + 3; //illegal assignment a = c + 5;

I

#include #include <string> using namespace std; void ParseName(string &FirstName,string CLastName, const string FullName); int main

()

{

string Name = "Stroustrup, Bjarne"; string LastName, FirstName; p~

Page 19 of 28

ParseName(FirstNameILastName); Name = FirstName + " " + LastName; cout << "Name : " << Name << endl; return (0);

1 void ParseName(string &FirstName,string &LastName, const string FullName)

I int I = FullName.find(","); LastName = FullName.substr (0,I); FirstName = Fu11Name.substr(1+2,Fu11Name.size()); 1 3J'uw"neraniswmaos

2.5 n1sarfilafisn~k~an.w Default Parameters d

9-

i ~ u n i ~ ~ i n u m ~ i ~ a s w i ~ i ~ ~ m o ~ l n n u ~ s n " ~ u ~ m i~uu?nj!,"fiiwum o~m~up~"~lun~~~~ni wisiG~mai't=?iw?u%m~fiinum~i default 1~
void

f(int x

=

7, int y

=

3);

/ / prototype for f

f (5, 10); / / legal call; f (20); f0;

#include #include <string> using namespace std; int Sumrnation(int StartNumber=l ,int EndNumber=lO); int main ( )

I int Start, End; cout << "Enter start number cin >> Start; cout << "Enter end number : cin >> End; 662304 - 05

: "; ";

Page 20 of 28

cout << endl; cout << "Summation of Number " << Start << " to " << End; cout << " = " << Summation(Start,End) << endl; cout << "Summation of Number " << 5 << " to " << 10; cout << " = " << Summation(5) << endl; cout << "Summation of Number " << 1 << " to " << 10; cout << " = " << Summation() << endl; return (0);

1 int Sumrnation(int StartNumber, int EndNumber)

I long Sum = 0; for(int N = StartNumber Sum += N; return Sum;

;

N < EndNumber

;

N++)

1

2.6 ona?ldl~b6k~!1uu Overloading d

d

dn~nisar$i94sn"~uo:&os~~o~snvuw!i~in'pd m~ounis$i~i~duo~Ka~~ds i~d%uni~li C++ A

9 9 9, 9,

9

4

P

O

mmsolnel.aarri~4~n"$u~~~o~~n"$u~~uan'upJinninum ~mtlii~oulunonoiuau~iw~si~i~ilo~ni

aPiin"Pd ~io~trW"muos~isiG~m~~m"o~~5unua:vtrW"m L$U void t

Swap(f1oat &x, float &y)

float Area (float R)

I int tmp = x; x = y; y = tmp;

return (R*R*3.14); 1

1 void

Swap(int &x, int & y )

float Area (float L, float W )

I

I float tmp x = y; y = tmp;

=

return (L*W)

x;

;

1

1 Page 21 of 28

#include #include <string> #include using namespace std; / / Define prototype function void DisplayMenu ( ) ; float Area(const float Radius); float Area(const float Length, const float Widht); int main ( ) t char Choice; boo1 Flag = true; 40 t DisplayMenu ( ) ; cin >> Choice; if (Choice == '1') { float Radius; cout << "\nEnter radius : "; cin >> Radius; cout << "Area of Circle = " << fixed; cout << setprecision(2) << Area(Radius) << endl; 1 else if (Choice == '2') { float Length,Widht; cout << "Enter length and width : "; cin >> Length >> Widht; cout << "Area of Rectangle = " << fixed; cout << setprecision(2) << Area(Length,Widht); cout << endl; 1 else if (Choice == '3') Flag = false; else { cout << "\nYou choose out of range is "; cout << "not process.\n"; 1 ) while (Flag); cout << "\n . . . Exit Program . . .\n"; return ( 0 ) ; 1 float Area(const float Radius) t return(3.14159F * Radius * Radius); 1 float Area(const float Length, const float Widht)

i

return(Length * Widht);

1 Page 22 of 28

v o i d DisplayMenu ( )

I cout cout cout cout cout cout

<< << << << << <<

endl; " P r o g r a m C a l c u l a t e Area " << e n d l ; " 1. C l r c l e " << e n d l ; " 2 . R e c t a n g l e " << e n d l ; " 3 . E x i t " << e n d l ; " E n t e r y o u r c h o o s e number: ";

1

d u

e

2.3 fl~n%troda%ungaa@s (Recursion Function)

aYiulsn"~u~auRun6aaod ~ n e i i i w u m ~ f i i i s i u.lrr~luadou!udtilwumaa" h~

awdol6istiiu~m

d&srnsJiwarini~lA~ouw~i~uilGsiu 1mebsdo~riuil~idJi!pll4Aao msl8uu Function Recursive Lwdo13uoiru6il~wi86oulu 2 o61.r $8

1. b~~iln~n~8uuiladrnlfio~l~~d~~irrisndun~aaos

2. ~os~6ou!ud~wfu~p1nis~isiuuos~sn".ir"pd 6aoiis nisw161 Factorial uss6alau n!

14 LOOP long

81fl

~ ! = I x ~ x ~ ... x x ~n x ~ x

a e ~ 6 I u ~ r l u s s w a ~ s ~ a e f i ufac = fac x n f a c t o r i a l ( i n t n)

t long fac=l; f o r ( i n t x = 1; x <= n; x++ ) return (fac);

f a c *= x;

1 662304 - 05

Page 23 o f 28

%$Recursion Function e:l&

om

n! = 1x 2 x 3 x 4 x 5 x ... x n

dou'lu (1)

n! = n x (n-l)!

long int factorial (int n) { if ( n > 1) return(n * factorial(n-1)); else return (1);

1 n i n K i ~ u afac = factorial (5)

Call

= 5!

Variables (n)

#include using namespace std; unsigned long Factoriall(int Number); unsigned long Factorial2 (int Number) ; int main ( )

I int Value; cout << "Enter integer number(0-20) : cin >> Value;

";

unsigned long Fac; Fac = Factoriall (Value); cout << "Factoriall of " << Value << " is cout << Fac << endl; Fac = Factorial2 (Value); cout << "Factoriall of " << Value << " is cout << Fac << endl; return (0);

";

";

I

662304 - 05

Page 24 of 28

/ / This function use loop unsigned long Factoriall(int Number) I unsigned long Fac = 1; for ( int N = 1 ; N <= Number ; N++) Fac *= N; return (Fac); 1 / / This function use recursion function unsigned long FactorialZ(int Number) t unsigned long Fac = 1; if (Number > 1) return(Number * Factorial2(Number - 1)) ; else return (1); }

#include #include using namespace std; void UpperStr (char temp [80]) ; int MaxInt (int Vall, int Va12) ; void Swap(int &a,int &b) ; int main ( ) t char Name[80]; int N1, N2; cout << "Enter two number(n1 122): "; cin >> N1 >> N2; cout << "Enter string : "; cin >> Name; cout << endl; cout << "\n Before call function..."; 662304 - 05

Page 25 of 28

cout << "\n nl = " << N1 << ", n2 = " << N2; cout << ", string = " << Name; C O U ~<< "\n Max value of nl & n2 = " << MaxInt(NlrN2); cout << "\n Press key to call function..."; getchar0 ; swap(N1, N2) ; UpperStr (Name); cout << "\n After call function..."; cout << "\n nl = " << N1 << ", n2 = " << N2; cout << ", string = " << Name << endl; return (0);

I int MaxInt(int Vall, int Va12) t return ( (Vall > Va12)? Vall 1

:

~ a 1 2 );

void Swap (int &a, int &b)

I int tmp; tmp = a;

a

=

b;

b

=

tmp;

I void UpperStr( char Str[80])

I

if (strlen(Str) > 0) for( int index = 0 ; index < strlen(Str) ; index++) Str [index] = toupper (Str[index]) ;

#include #include #include using namespace std; int NumCmp (int vl, int v2) ;

int main ( )

I int val,num,count boo1 flag = true;

=

0;

cout << "This game for guess numberic . . . \nl'; cout << "Now random number from system\nn; srand( (unsigned int) time(0)); val = (rand() % 100) + 1; do I tout << "Enter number : "; cin >> num; count++; switch ( NumCmp(va1,num) )

I case

1 :

cout << "Your number less than\nW; break; case -1 : cout << "Your number more than\nl';break; case 0 : cout << "\nYour win,match random number "; cout << val << endl; cout << "YOU used " << count << " times\nW; flag = false; brea k;

1 while (flag); . return ( 0 ) ; }

1 int NumCmp (int vl, int v2)

I

if (vl > v2) return (1); / * more than retrun 1 * / else if (vl < v2) return(-1); / * less than retrun -1 * / else return (0); / * equal retrun 0 * /

662304 - 05

Page 27 of 28

(sin x)2 x (cosx)2

3. a ~ ~ l ° l u o ~ d ~ d s a ~ n s u w s a o a r ~ u ~ a ~ a % ~ ~ ~ ~ u ~ alnonf1.~3w@s6~u ~ a % d i u a u ~ w wCheckprime iz~~o~~ ~

I

H

~

~

662304 -05

E

U

~

%

$

~

~

~

~

~

~

B

~

Boolean ~ B ~ $$ohh?u ~ ~ ~ S ~ V ~ ~ J L ~ I X ~ L ~ W

Page 28 of 28

Related Documents

Unit5
October 2019 16
Unit5-sp
May 2020 12
Ibm Unit5
May 2020 5
Ssd9-unit5
November 2019 19
Unit5.docx
November 2019 15
Unit5.pdf
April 2020 9

More Documents from ""