Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental

Similar presentations


Presentation on theme: "Programming Fundamental"— Presentation transcript:

1 Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-14

2 Lecture outline Pre-Processor Directives
#Define Preprocessor : Symbolic Constant #Define Preprocessor : Macros # include Preprocessor Directive: (Header File) Math.h and rand( ) Function

3 Pre-Processor Directives
A preprocessor program executes automatically before the compiler's translation phase begins. The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations include various tasks.

4 Define Preprocessor : Symbolic Constant
#define pi Name can be used inside a program exactly like a variable It cannot be used as a variable CircleArea = pi * radius * radius Circumference = 2 * pi * radius

5 Define Preprocessor : Symbolic Constant
#include<iostream.h> #include<conio.h> # define pi main() { Float radius=5; double area; area = pi * radius * radius; cout<<area; getch(); }

6 #Define Preprocessor : Macros
#include<iostream.h> #include<conio.h> # define pi #define CIRCLE_AREA( x ) ( pi * (x) * (x) ) main() { double area; area=CIRCLE_AREA(4); cout<<area; getch(); }

7 Header Files #include <iostream.h>

8 Using Header Files double pi = 3.1415926;
It is better to define this value in a header file Then simply by including the header file in the program this value is defined and it has a meaningful name

9 Prototype int functionName ( int , int );
Assignment List with data type Return value int functionName ( int , int );

10 Include Preprocessor Directive: (Header File)
How to Make Header file? Write desired function Call it in main program Remove all syntax and logical errors. Write those function in to an other file of Dev-C++ editor Save it with .h extension Place this header file in include directory Include this file <> brackets in ac++ program where required. You may use Include with file “ ”brackets in ac++ program if header file is placed in other folder of Dev-C++.

11 Include Preprocessor Directive: (Header File)
int sum(int x,int y) { return(x+y); }

12 Include Preprocessor Directive: (Header File)
#include<iostream.h> #include<conio.h> #include<sum.h> main() { double area; cout<<sum(3,4); getch(); }

13 Math.h log10 , pow ( xy ) , sin , cos , tan …
#include < math.h > double sqrt ( double ); log10 , pow ( xy ) , sin , cos , tan …

14

15 rand ( ) # include < stdlib.h >

16 Calling rand ( ) x = rand ( ) ;
A call goes to ” rand ( ) “ , it generates a number and returns to x

17 Modulus “ % ” It returns the remainder rand ( ) % 6 = ?
Result has to be between 0 and 5 inclusive 1 + rand ( ) % 6 It will randomly generate number between 1 and 6

18 Rand() function produce same output every time you run the program
If you desire different random number each time, you need to use srand() function: Sets a random starting point. srand(10); srand( time( NULL ) );

19 Example: Tossing a Coin
It has only two possibilities 0 / 1 rand ( ) % 2 ;

20 Importance of rand ( ) It is shipped in every standard library with compiler Most major programming languages give some kind of random number generator as a function as part of library

21 Fair Die If a dice is rolled 100 times, program should display how many times 1’s, 2’s, 3’s, 4’s, 5’s and 6’s came out.

22 main() { int i1=0; int i2=0; int i3=0; int i4=0; int i5=0; int i6=0; int x; for ( int roll = 1; roll <= 100; roll++ ) srand( time(NULL) ); x=1+rand()%6; if(x==1) i1+=1; else if(x==2) i2+=1; else if(x==3) i3+=1; else if(x==4) i4+=1; else if(x==5) i5+=1; else i6+=1; } cout<<i1<<endl; cout<<i2<<endl; cout<<i3<<endl; cout<<i4<<endl; cout<<i5<<endl; cout<<i6<<endl; getch();


Download ppt "Programming Fundamental"

Similar presentations


Ads by Google