You are on page 1of 28

1 Function I

Asn"i7pd

Idsaansuniui c++Zuiisiu%ua"num:nis~un%44sn"$u c++aciuisoa~is4sn"$u16


Imvni~i
ndlu1drunrun6s7o:ZBsn"4u main() 1&t~usBsn"$ua~uaBsn"$uariuisnaa6soon~6
.uinn<inu"s~qn"$u
a$u 2 d.i:anw%
1. i~sn"$uuins~iu
(Standard Libraries Function)
r L 9 nr
2. flsn~u~%~fPlinum
(User-Define Function)

1. ds6%duin-b9iw
(Standard Libraries Function)
c++i 1 6 Q n f i ~ n u m l i ~ l # 3 ~ ~ $l a. ~ui ~~ ~a n?" $ ~ i 1 4iiulnnlnu
a8unirl~siu~sn"$u~0sniui ~

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
ex: +
isalnum('aJ) value > 0
~aCnusFmuo:aisii~ua3u~aaavBinina$ugaaavn%
o ~~a:w1n~ducn'atus:6uo:1661
ii'~Gnpr.ioG~Al~1~jl$
6dI.J0
int isalpha ( int character); r8u~sn"$unisnsaoarou6aGnvs:iia8u~aCn~~s~muo:
ex: +
isalpha('aJ) value > 0
dsiiZua3~63aav~ I M I ~ A ~ u ~ ~ G ~ Mo~ o : z M ~ I ~
ala:ninIBj~$u~aGnuso:1Xi1~8uo
int isdigit ( int character); adu~sn"$unisnsaoa~u6;1'aGnvs:.di16u~a6vFmoo:
ex: isdigit('9') +
value > 0
o$
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
int islower ( int character); a~~dYsn"~pdnisms~o~oucm"a~nvs:iia~u~aCnus~dn
ex: +
islower('ar) value > 0
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); idu~s6$unirmsaono9~$aGn6usziii8~6aGnp1s1~$
ex: isupper('D') 3 value > 0
F~uozai~d1dwi8~6aam
biwinigu6adnpIs%w$~zdu

int tolower (int Character); TRU


iguRsn"~urnsidas6a5n~lslw$i8u6a~np1sidn
ex: toIower('a') 3 'A'
ozds6iZui~u6aGnpIsidn
int toupper (int Character);
ex: toupper('A1) + 'a'

dw w w w
nnnnaod 5-1 m~~$~iufianaununaonvsz~au
Library ctype
#include <iostream>
#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 \ '
'I;

cout << (char)tolower (Ch) << " \ ' ";


1
cout << endl;
1
return (0);
1 ..

662304 - 05 Page 2 of 28
Stdlib Library (std1ib.h or cstdlib) do~3J"od?n"~auzu
namespace std
I double atof ( const char *string) ; ~utudsdiI
I 18uJs6$unisudaadi$onaiu1~u6;1"a~avwa~~
1 ex: atof("45.6") + 45.6
~ua8~6;1"a~avq/la6u~~~b3~'~1
double I
1 int I ~8uJsn"~unis~~nlasdi$ona1ua8u6;1"a~avBiuau1G~~
Fmer I
1 ex:
atoi ( const char *string) ;
atof("30) + 30
dsdifiu~8~6;1"~~avBiuauai~~
int I
TRU 1
I

long int atol( const char *string) ; I l8u~la6~umrlbdasd1$8nai~~b8u6;1"a1av41uaub~u


ex: ato1("100000) + 100000
risdi$u~8usi?mv~iuaub~u1~~1~1 long int I
int rand ( void) ; 18u~sn"$unirrja~16;1"a~av%u~aa
0 to RAND-MAX,
ex: rand () 3 ?
Fmuf~diseed %uni~niis6;1"aaavrju$o
1 dmerriadifiub8u
6a~avBiuau~~u~bp1~1
int
void srand (unsigned int Seed ) ; ~8u~sn"biunisfiinumdi
Seed ~so~18udi~o:%~%uni3
ex: srand (100 ) value > 0
i=-jn4i?mvvosJ.m6$u
rand
int abs (int x) ; 18uJs6$umsmdi~u~sdvo~di
x inerdsd,n"uflu
ex: abs (55) or abs (-55) 3 55

nismnaes$5-2 nis~~siod~s&'pd"bpd
Library cstdlib
#include <iostream>
#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 << ll**************************\n~l.

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);
1

662304 - 05 Page 4 of 28
Math Library (math-hor cmath)
double ceil (double x) ~3uYs6~unisniii6a~av~wmiu~1uu~sii
x %mu
ex: ceil (54.3) +
55.0
dsi15uaduKa~avwaGuu
double floor (double x) ~ ~ u ~ s 6 ~ u n i s ~ i i i 6 a 1 a u t w m i u ~xi s%mu
vosii
ex: floor (54.3) +
54.0
dsdi~ut~u6alavwaGuu
double sqrt (double x) t 3 u ~ s 6 ~ u n i s w i ~ i s 1 n ~ f l o a vxo Fmodsii
s~i
ex: sqrt ( 4 ) 2.0+ ~ul8uKalauwaGuuFW$I x >= o
double exp (double x) exponential v o s h x &?(I
~3uYa6iumrnii-1
ex: exp (2) +
7.389056
giu e, Fnud e 8 i i 2.718282 Fnudsiin"ul$u
6a~avwaGuu
double fabs (double x) ~~uYs6~unisrniidu~sdvo~ii
x Imudsii8u
ex: fabs (78.5) or fabs (-78.5) -3 78.5
lau63tavwffGuu
double log (double x) 1guls6iunismnatural logarithm uosdi x
ex: log (10) -3 2.302585
FmudsiiGulduKalauwaGuu
double log10 (double x) i?!u~s6~urnmiiilogarithm giuzuuosdi x
ex: log10 (10) -3 1.0
FmudsiiZul3uKalauwaGuu
double pow (double x, double y) 4 s x i?Juiigiuaa::
1?Ju4.r6~umswiiiun~ia's
ex: pow ( 5 , 3 ) 125.0+ y i?Ju6a$fiia'sFmisidutauhlaunaGuu
double sin (double x) sin uosii x , ?RUG x d u
13u~t6iumsmdi
ex: sin (0) 1+ 41 radians FmuaisiiGu~~laKa~auwaGu~~
double cos (double x) ~3uls6~unirniii
cosine uosn'i x , TRU$ x
ex: cos (0) -3 1
L ~ U x~ I 131.4 radians '6mu~s6i~udu~alasl
waGuu'
double tan (double x) i$u4s6$unis~iditangent uotdi x ,1muwA x
ex: tan (0) 0+ 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 <iostream>
#include <cmath>

I,
using namespace std;

662304 - 05 Page 5 of 28
double Value;

cout << "Enter floating number : ";


' I
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 <iostream>
#include <string>
#include <cstring>
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 d 9-
Cmwaa'wsIn~m4iu%4iaun's~4sn"&u
setw
right Cmwaaa'wti%n~muai
d 9- I~iauEu4sn"~ u
setw

fixed 6 wgn dmu%ha6 ibu 123.4 o r


iaamsdiuauo~~i$uiaujmw~sd"uu

I.
scientific

1 showpoint
1 noshowpoint I uniFinniriam show~oint I

1 skipws I unt~n6aGnurr$i!hWhiteSpace ~uuorr?u$oya


1 noskipws 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 I uni~nnirianshowbase I
1 boolalpha I tinsn'iyalu Kiaf.n$u true n?on'iii;oillu false
I no boolalpha I uniiinmrian boolalpha

662304 - 05 Page 9 of 28
#include <iostream>
#include <iomanip>
#include <cmath>
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 <iostream>
#include <iomanip>
using namespace std;
int main ( )
{
tout << !' Decimal : Octal : Hexa " << endl;
cout << setfill << setw(30) << "-" << endl;
( I - ' )

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

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

V
$¶JLI'LIPI
Function header + data-type Function Name (type var1,type var2,. ..)
I
Function body + 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() /* g~PiinSui8uinteger */
int Feet2MeterO * 4an"kud~diniiua~n integer */
void main() /* os
1lilnird.tri1ln~n ~ u i u l ~ d */
char findchar ( str, ch) /* dsdintYuigu6a6nur 1$3 */

1* User-define Function for calculate value of power by two */ ]


int square( int n)
{
return( n * n );
A I
2.1 nim.kernflddnd$~(Function Prototype)

int FunctionName ( data-type ); / * function prototype */


void main()
C
int v;

I V = FunctionName( variable ); / * calling function * / I


I int FunctionName( data-type variable
{
statements;
)/* Definition Function*/

#include <iostream>
int square( int ) ; /* function prototype */
int main ( )
t
int x;
while ( x <= 10) {
printf("%d\n" , square(x)); / * calling function * /
x = x + l ;
I
return ( 0) ;
I
int square (int n) /* Definition Function */
I
return ( n * n) ;
I

662304 - 05 Page 12 of 28
#include <iostream>
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
iiwod n4od1n.r~lfln'u~sn"%u~~ndunI4siu
19umroian'iuoaain~a11d~ I~uiiuoso~nGa

input
U U
output e int 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 Ufi
output t int swap(int&a,int&b)
{
return; '

#include <iostream>
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 : " << Max (Valuel,Value2) << endl;
cout << "Min value : " << Min(Valuel,Value2) << endl;
return (0);
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 <iostream>
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 <iostream>
#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 2 ?rGa so
~~Eisoonlgu
1. Global Variable ~~uhud~daiuisn~ild l~~lu~n~
tl~6Guuo.r~s~mrutu
~aomsdszniao:fii~ua~a"mda4d6Gu
main()
Y Y

2. Local Variable i ~ u ~ ~ ~ i ~ l s d n i u i s Asn"%u~ds:nia~ad~pi~~u


n~~~~~wiz~u
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 <iostream>
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 total=l;
while ( n > 0 ) {
total *= x; n--;
1
return (total);
1
oin y = 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 <iostream>
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)


{
b = a + 3; / / legal assignment
a = c + 5; //illegal assignment
I

#include <iostream>
#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~<uwi3i~~mo~~:~"iIwwi:aiauni~~szniai~'iku
I ~ U

void f(int x = 7, int y = 3); / / prototype for f

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


f (20);
f0;

#include <iostream>
#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 ; N < EndNumber ; N++)
Sum += N;
return Sum;
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++
9 9 9, 9, A 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 Swap(f1oat &x, float &y) float Area (float R)


t I
int tmp = x; return (R*R*3.14);
x = y; 1
y = tmp;
1
void Swap(int &x, int & y ) float Area (float L, float W )
I I
float tmp = x; return (L*W) ;
x = y; 1
y = tmp;
1

Page 21 of 28
#include <iostream>
#include <string>
#include <iomanip>
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 << endl;
cout << " P r o g r a m C a l c u l a t e Area " << e n d l ;
cout << " 1. C l r c l e " << e n d l ;
cout << " 2 . R e c t a n g l e " << e n d l ;
cout << " 3 . E x i t " << e n d l ;
cout << " 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
1mebsdo~riuil~idJi!pll4Aao
d&srnsJiwarini~lA~ouw~i~uilGsiu
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 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
long 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++ ) f a c *= x;
return (fac);
1

662304 - 05 Page 23 o f 28
%$Recursion Function om n! = 1x 2 x 3 x 4 x 5 x ... x n
e:l& n! = n x (n-l)! dou'lu (1)

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) = 5!
Call Variables (n)

#include <iostream>
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 <iostream>
#include <cstring>

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 : ~ a 1 2 );
1

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 <iostream>
#include <time.h>
#include <cmath>
using namespace std;
int NumCmp (int vl, int v2) ;
int main ( )
I
int val,num,count = 0;
boo1 flag = true;

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 ~ ~ E U ~ % $ ~ ~ ~ ~ ~ ~ B ~ ~ B ~ $$ohh?u
Boolean ~ ~ ~ S ~ V ~ ~ J L ~ I X ~ L ~ W

662304 -05 Page 28 of 28

You might also like