L13 writing program with c

Page 1

emeronTI13 ³ WRITING

PROGRAM WITH C++

I. Introduction to C++ Language:

CaPasakmμviFITUeTAmYyEdlsßitenAkñúg ( High Level Programming Language) . eKbegáIt C PasakmμviFIkMritx<s; edIm,Isresr System program eKfa C CaPasa edIm kMeNItrbs; UNIX Operating System. C/C++ RtUv)aneKcat;TukCaPasamYymanRbCaRbiykñúgkarsresrPa¢b; Application CamYynig System edayPasa C/C++ CaPasakMritx<s; (High Level Language) nig man Compiler bkERbBakü bBa¢aeGayeTACaPasa ma:sIun rYc mkCaPasaEdlGacGan)anedayPasarbs;mnusS. C minGaRs½yEt enAelI O.S(Operating System) rW ma:sIunNamYyedayELkeLIy manny½fa C RtUv)aneKykeTAeRbIR)as; CamYy O.S nig ma:sIunepSg²tam Object rbs;va. qñaM 1970 elak Ken Thompson )anerobcMPasa B Language Edlman\T§iBlenAelI Basic Computer Programming Language EdlbegáIteday elak Martin Richard. qñaM 1972 elak Dennis Ritchie )aneFVIkarEkERb B Programming Language eGayeTACa C Programming Language enAÉ Bell Telephone Laboratories. C/C++

Programming

Language

257


qñaM 1978 elak Brain W Kernighen nig Dennis Ritchie )ane)aHBum<esovePA The C Programming Language (First edition) . qñaM 1982 eTIbeKbegáIt ANSI(American National Standard Institute) edayepþIm sresr enA GnuKmn_Edl Ca Standard Library bBa©ÚleTAkñúg Compiler edIm,IeRbIR)as;CaTUeTArhUtmkdl;sBVéf¶enH. enAqñaM 1988 eKcab;epþImbegáIt OOP( Object Oreinted Programming) edayelak Brjare Stron Stup enAÉ Bell Laboratories EdlkñúgeKalbMNgedIm,IbegáItnUv Application Wizard nig System Programming. II. TMrg;TUeTAén C++: #include<iostream.h>//Header file/Preprocessor/Library int main(){//

cMnuccab;epþIménkmμviFI

statement1; statement2; ………….;

Block of statement

bBa¢ak;fadMeNIrkardl;TIbBa©b; }// cMnucbBa©b;énkmμviFI return 0;//

#include<iostream.h>//Header file/Preprocessor/Library

cMnuccab;epþIménkmμviFI

void main(){// statement1; statement2; ………….; }// -

Block of statement

cMnucbBa©b;énkmμviFI

ebIkkmμviFI C/C++:

- C:\> TC\Bin\Tc.exe - Compiler: Alt +F9 - Run (Make EXE): Ctrl +F9 - Copy: Ctrl+Insert 258


- Past: Shift+Insert - Cut: Shift+Delete Source Code COMPILER

TEMPORARY

OBJECT + LINK

.EXE

III. Feature of iostream.h: iostream.h (Input/ Output Stream library) Ca

sMrab; Input/ Output stream EdlmanmuxgarCaGñkbkRsaykarbeBa©j nig karbBa©Úl Tinñn½y. vapÞúk function cin sMrab;bBa©ÚlTinñn½ytamryH Keyboard nig function cout sMrab;karbeBa©jTinñn½yenAelI Screen . Standard header file/ Standard library

Ex: cout<< “Hello world!”<<endl; cout>>x;

IV. GnuKmn_ getch( ):

eRbIsMrab;Tb; Screen output edIm,IeGayeyIgGacBinitülT§pl (Result) énkmμviFIenAeBlEdleyIg dMeNIrkarkmμviFI . GnuKmn_ getch( ) sßit enA library conio.h (Console Input/Output library) . V. Input and Output function: - cout<< “String”/Variables; Ex:

eRbIsMrab;bgðaj string b¤ tMélmkelI Screen .

int x=10; cout<< “x=”<<x;

- cin>>Var1,var2,var3, ...;

eRbIsMrab;bBa©ÚlTinñny½tamryH Keyboard

eTAeGay var1,var2,... Ex:

int x; cin>>x;

259


VI. Data Type: VI.1. Definition of Data-Type:

CakarkMNt;lkçNHeTAelIGefr (Variables)

nimYy² Edl)an Rbkas. VI.2. Type of Data-Type: Data type -

maneRcInRbePTdUcCa³

Scalars type Structure type Pointer type Data Type

Scalar Type

Structure Type

- int

- array

- float

- file

- double

- struct

Pointer Type

- pointer

- Boolean - ...

Keyword

Byte

Char

1

Int

2

Long

4

Float

4

Double

8

Unsigned int

2

Limited

dl; 128 -32768 dl; 32768 -2147483648 dl; 2147483648 3,4 .10 dl; 3,4 .10 1,7.10 dl; 1,7.10 0 dl; 65535 -128

260

-38

38

-308

308


GnuKmn_ Sizeof()eRbIsMrab;pþl;TMhMénRbePTrbs;Tinñny½ (Data type )pþl;tMélCacMnYn integer.

Note:

Syntax: int sizeof(Data-Type/var); Ex: cout<<“Size of Integer=”<< sizeof(int); Output: Size of Integer=2

VII. Variables:

CaGefr b¤ Address eRbIsMrab;rkSanUvtMélCabeNþaHGasnñkñúg Memory enAeBlkmμviFIdMeNIrkar (Run time) . tMélenHRtUv)an)at;bg;eTAvijenAeBleKQb;dMeNIrkar kmμviFI (Stop Run) . 7.1. Definition of Variable:

7.2. Syntax: Data-type VarName1, VarName2, ..., VarName n; Ex: int x,y; float a,b,z; 7.3. Assignment Statement:

CakarepÞrtMéleTAeGayGefr (Variable) .

Ex1: - int x; x=20; - int y=30; Ex1:

sresr Program edIm,IKNna Y=4x

5

+ 2x2 + 3x + 1

#include<iostream.h> #include<math.h> #include<conio.h> void main(){ clrscr(); float x, y; cout<< “Please Input x=”; cin>>x; y=4*pow(x,5)+2*pow(x,2)+3*x+1; 261


cout<< “y=”<<y<<endl; getch( ); }

VIII. Constants: VIII.1. Definition of Constants:

CatMélEdlminPøas;bþÚrkñúgeBlkmμviFIdMeNIrkar (Process) . eKGacpøas; bþÚrtMélva)anEttamryH Source codeEtb:ueNÑaH. VIII.2. Memory Constants: RtUv)aneKRbkaseday Keyword const . Ex:

const int num=100; const float y=20.00;

CakarbegáIt Constant tamryHkareRbI # (Signal number) nig Keyword define sMrab;kMNt;BI Pre-Processor kñúgkmμviFI. VIII.3. Define Constants:

Ex1: #define PI Ex2:

3.14

kmμviFIsMrab; KNnaRklaépÞrgVg;³

#include<iostream.h> #include<conio.h> #define PI 3.14 void main(){ clrscr(); float radius, circle_area; cout<< “Please Input radius of Circle=”; cin>>radius; circle_area=PI * (radius * radius); cout<< “Circle Area =”<<circle_area<<endl; getch(); }

IX. Comments and Escape Code:

CarkareFVIkMNt;sMKal;enAkñúgkmμviFI. vaminman dMeNIrkareTenAeBl Run Time. enAkñúg C ++ man Comments BIrKW³ - Comments:

-

// One Line 262


- /* Multiple Line */ - Escape Code: - \n : New line - \t : Move one tab - \a : Beeb - \\ : \ - \r : Move cursor to the first of line - \” : ” Ex: // This program used to change dollars to riels #include<iostream.h> #include<conio.h> void main(){ clrscr(); const riel=4000; float dollar, result; cout<< “\n Enter Dollar you have=”; cin>>dollar; result =dollar * riel; cout<< “After dollar to riel=”<<result<<endl; getch(); }

X. Predefined Manipulate function:

CabNþaGnuKmn_mYycMnYnRtUv)aneKeRbIsMrab;eFVIkarEkERbkar bBa©j nig bBa©ÚlTinñn½y. bNþaGnuKmn_TaMgenaHman³ X.1. endl: Ca output manipulate function eRbIsMrab;cuHedImbnÞat;fμI. Ex: cout<< “Area=”<<area<<end<< “Population=”<<pop<<endl;

eRbIsMrab;bMElgRbBn§½eKal³ - hex : CaRbBn§½eKal 16 - dec : CaRbBn§½eKal 10 - oct : CaRbBn§½eKal 8

X.2. hex, dec and oct:

Ex: //Convert to hex, dec, oct; #include<iostream.h> 263


#include<conio.h> void main(){ clrscr(); int num; cout<< “Enter num=”; cin>>num; cout<< “Decimal=”<<dec<<num<<endl; cout<< “Octal=”<<oct<<num<<endl; cout<< “Hexadecimal=”<<hex <<num<<endl; getch(); }

eRbIsMrab;bMElgRbBn§½eKaltamryH base (8, 10, 16); EdlvasßitenAkñúg header file <iomanip.h> . X.3. Setbase(base):

Ex: //Convert to hex, dec, oct; #include<iostream.h> #include<conio.h> void main(){ clrscr(); int num; cout<< “Enter num=”; cin>>num; cout<< “Decimal=”<<setbase(10)<<num<<endl; cout<< “Octal=”<<setbase(8)<<num<<endl; cout<< “Hexadecimal=”<<setbase(16)<<num<<endl; getch(); } 10.4. setw():

eRbIsMrab;kMNt;TMhMbgðajmkelI Screen .

Ex: cout<< setw(4)<< “Web”<<setw(5)<< “Site”; Output: 9Web9Site

eRbIsMrab;bMeBjkEnøg Space enAeBl Output edaynimitþsBaØaNamYy. 10.5. setfill():

264


Ex: cout<<setfill(‘*’); cout<< setw(4)<< “Web”<<setw(5)<< “Site”; Output: *Web*Site 10.6. setprecision(): double

eRbIsMrab;kMNt;eRkayek,óscMeBaHTinñny½RbePT float or

. Ex: float a; a=7/3; cout<< “a=”setprecision(2)<<a<<endl; Output: a=2.33

XI. Operators: enAkñúg C ++ manRbmaNviFIepSg²mYycMnYnEdleKeRbIjwkjab;dUcCa³ XI.1. Arithmetic Operators: Operators + * / %

Meaning Addition Subtraction Multiplication Division Modulo

Example c = a + b; c = a – b; c = a * b; c=a/b; c = a % b;

Ex: int a=20, b=40, c; c= a+ b; cout<< “c=”<<c<<endl; XI.2. Assignment Operators: Operators = += -= /= *= %=

Meaning/Example a = 10; a + = 10; a = a + 10; a - = 10; a = a – 10; a / = 10; a = a / 10; a * = 10; a = a * 10; a % = 10; a = a % 10; 265


>> = <<=

n >> = 1; n = n >> 1; n << = 1; n = n<<1;

XI.3. Comparison and Logical Operators: Meaning

Operators < > <= >= == != && || !

Less than Greater than Less than or equal to Greater than or equal to Equal to Not equal to Logical AND Logical OR Not

Ex: - Logical AND (&&): X 1 1 0 0

Y 1 0 1 0

X && Y 1 0 0 0

X 1 1 0 0

Y 1 0 1 0

X || Y 1 1 1 0

- Logical OR (||):

266


XI.4. Bit-wise Operators: Meaning

Operators & | ^ >> << ~

Bit wise AND Bit wise OR Bit wise XOR Bit wise Right Shift Bit wise Left Shift Bit wise Complement

Ex: - Bit wise AND (&): X 0101 0110 1111 0001

Y 0110 0010 0001 0000

X&Y 0100 0010 0001 0000

X 0101 0110 1111 0001

Y 0010 0010 0001 0000

X|Y 0111 0110 1111 0001

X 0101 0110 1111 0001

Y 0010 0011 0001 0000

X^Y 0111 0101 1110 0001

- Bit wise OR (|):

- Bit wise XOR (^):

267


- Bit wise Complement (~): X 00001011 XI. 5. Special Operators: - Unary Operator:

Y 11111111

~X 11110100

~Y 00000000

Ca Operator EdlRtUvkar Operant EtmYyKt;.

Ex: ++ i; -- i; i ++; i--; - Ternary Operator (?):

Ca Operator lkçxNн EdlmanbI Operants.

Syntax: Operant1 ? Operant2 : Operant3; Ex: MAX= (a>b) ? a : b; MIN=(a>b) ? a : b; cout<< “Max=”<<MAX<<endl; cout<< “Min=”<<MIN; XII. Type Conversion:

dMbUgGefrxøHRtUv)anRbkasCa Integer b:uEnþCYnkalvaGacmanlT§PaB TTYlyknUvcMnYnTsPaK (float) )an EdrtamTMrgxageRkam³ Var1= (Data-type Var1) Var2; Ex1: int x; x=(int)y; float y; Ex2: int x; y=x; float y; 13.

GnuKmn_ Math Ed;lmanenAkñúg Preprocessor directive <math.h>: 268


Functions abs(i) sin(d) cos(d) tang(d) exp(d) log(d) pow(x1,x2) flour(d) ceil(d) fmod(d1,d2) sqrt(d) random(d) 14.

Description |i| Sin(d) Cos(d) Tang(d) Exp(d) Log(d) Pow(d) Flour(d) Ceil(d) Fmod(d1,d2) Sqrt(d) Random(d)

GnuKmn_EdlmanenAkñúg <ctype.h>: Functions

Ex:

Result Data Type int double double double double double double double double double double void

toascii (ch) tolower (ch)

Result Data Type int char

toupper(ch)

char

#include<stdio.h> #include<conio.h> #include<math.h> main( ){ clrscr( ); float a; int result; printf("Please, Input a="); scanf(" %f",&a); result =abs(a); textcolor(6); cprintf("Result are =%d",result); getch( ); return 0; 269

Description Convert ch to Ascii code Convert ch to lower character Convert ch to upper character


}

Exercises:

1. Write program to calculate Sum of Three number? 2. They have output on the screen as follow: W*e*l*C*o*m*e***to***Learn***C++**Programming**Language! Write program that output above? 3. Write program to calculate area of Rectangle? 4. Write program to calculate f(x)= axn + bxn-2 + c 5. Using Ternary operator, write program to find Min and Max of two numbers? 6. Using setbase() function, write program to convert to hexadecimal and octal number? 7. Write program to change Riels to Dollars?

270


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.