Programming Fundamental

Slides:



Advertisements
Similar presentations
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Computer Science 210 Computer Organization Introduction to C.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
CMSC 1041 Functions II Functions that return a value.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
C++ Programming Lecture 10 Functions – Part II
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Chapter 9: Value-Returning Functions
Arithmetic Expressions
CSCE 206 Structured Programming in C
EKT120: Computer Programming
User-Written Functions
Computer Science 210 Computer Organization
Introduction to Programming
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
CSE 220 – C Programming C Fundamentals.
Programming Fundamental
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 13 - The Preprocessor
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Objectives Identify the built-in data types in C++
Beginning C++ Programming
Introduction to C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
Deitel- C:How to Program (5ed)
C Basics.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Introduction to Programming
Preprocessor C program → Modified C program → Object Code
User Defined Functions
Introduction to Programming
Chapter 6 - Functions Outline 5.1 Introduction
Lexical Elements, Operators, and the C Cystem
Preprocessor.
Assignment Operators Topics Increment and Decrement Operators
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
FUNCTION CSC128.
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
C++ Compilation Model C++ is a compiled language
Introduction to Programming - 1
Programming Fundamental
Basic Elements of Computer Program
Assignment Operators Topics Increment and Decrement Operators
Programming Fundamental
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
C++ PROGRAMMING SKILLS Part 3 User-Defined Functions
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Programming Fundamental Instructor Name: Muhammad Safyan Lecture-14

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

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.

Define Preprocessor : Symbolic Constant #define pi 3.1415926 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

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

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

Header Files #include <iostream.h>

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

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

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++.

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

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

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

rand ( ) # include < stdlib.h > 0 - 32767

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

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

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

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

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

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.

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