56
COMPUTER SIMULATION LAB Prepared By: Checked By: Approved By Engr. Najeeb Saif Engr. M.Nasim Kha Dr.Noman Jafri Lecturer Electrical Senior Lab Engineer Dean Electrical, FUUAST-Islamabad FUUAST-Islamabad FUUAST-Islamabad FIRST SEMESTER INTRODUCTION TO PROGRAMMING DEPARTMENT OF ELECTRICAL ENGINEERING

INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

COMPUTER SIMULATION LAB

Prepared By: Checked By: Approved By

Engr. Najeeb Saif Engr. M.Nasim Kha Dr.Noman Jafri Lecturer Electrical Senior Lab Engineer Dean Electrical,

FUUAST-Islamabad FUUAST-Islamabad FUUAST-Islamabad

FIRST SEMESTER

INTRODUCTION TO PROGRAMMING

DEPARTMENT OF ELECTRICAL ENGINEERING

Page 2: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 2 

Registration No: ________________________________

Roll No: _______________________________________

Semester: ______________________________________

Batch: ________________________________________

Name: ________________________________________

Page 3: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 3 

Contents

1

2 Installation of Windows Operating System 3 Command Prompt Commands4 Introduction into Microsoft Word5 Introduction into Microsoft PowerPoint6 Introduction into Microsoft Excel7 Introduction to C++ Programming8 C++ Programming: Basic Input/output9 C++ Programming: If & If -Else

10 C++ Programming: Switch11 C++ Programming: Iteration Structures (For loop)

C++ Programming: Iteration Structures (while loop and do-while loop) 12 C++ Programming: Array13 C++ Programming: Function14

15 C++ Programming: Pointer

Project List 16

Exp No List of Experiments

Scrutinize Different Computer Components

Page 4: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 4 

Scrutinize Different Computer Components

Experiment No 1

Page 5: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 5 

Experiment No 2

Installation of Windows Operating System

Page 6: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 6 

Experiment 3

Command Prompt Commands

Page 7: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 7 

.

Page 8: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 8 

Experiment 4

Introduction to Microsoft Word

Page 9: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 9 

 

 

Page 10: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 10 

Page 11: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 11 

 

 

Page 12: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 12 

Page 13: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 13 

Page 14: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 14 

Page 15: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 15 

Page 16: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 16 

Page 17: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 17 

Experiment No 6

Page 18: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 18 

Page 19: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 19 

Page 20: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 20 

Page 21: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 21 

Page 22: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 22 

Page 23: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 23 

Experiment No 7

Page 24: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 24 

 

 

 

 

 

PROGRAM # 1     Printing name and star pattern 

#include<iostream.h> 

void main() 

    cout<<"\n      Imran\n"; 

 cout<<"\t\n       *\n"; 

 cout<<"\t\n      ***\n"; 

 cout<<"\t\n     *****\n"; 

 cout<<"\t\n      ***\n"; 

 cout<<"\t\n       *"; 

Page 25: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 25 

Experiment No 8

Page 26: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 26 

PROGRAM     Sum of two numbers 

#include<iostream.h> 

void main() 

int a,b; 

 cout<<"THIS PROGRAM IS FOR ADDITION OF TWO NUMBERS:\n\n"; 

 cout<<"ENTER 1st NUMBER"; 

 cin>>a 

cout<<"ENTER 2nd NUMBER";    

cin>>b; 

 cout<<"\n"<<a<<" + "<<b<<" = "<<a+b; 

}   

Page 27: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 27 

Experiment No 9

C++ Programming: If & If -Else

Objective:

To understand the use of If and Else statement.

Theory:

Before discussing the actual structure of the if statement, let us examine

the meaning of TRUE and FALSE in computer terminology. A true statement is one

that evaluates to a nonzero number. A false statement evaluates to zero. When you

perform comparison with the relational operators, the operator will return 1 if the

comparison is true, or 0 if the comparison is false. For example, the check 0 == 2

evaluates to 0. The check 2 == 2 evaluates to a 1. If this confuses you, try to use a cout

statement to output the result of those various comparisons (for example cout<< ( 2 ==

1 );) 

When programming, the aim of the program will often require the checking of one

value stored by a variable against another value to determine whether one is larger,

smaller, or equal to the other. 

The first type of branching statement we will look at is the if

statement. An if statement has the form:

if (condition)

{

// code to execute if condition is true

Page 28: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 28 

}

else

{

// code to execute if condition is false

}

In an if statement, condition is a value or an expression that is used to determine which

code block is executed, and the curly braces act as "begin" and "end" markers.

Program: Simple calculator by using if else statements

#include<iostream.h>

void main()

{

int a,b,c;

cout<<"SIMPLE CALCULATOR\n\n";

cout<<"\n1.ADDITION\n2.MULTIPLICATION\n3.SUBTRACTION\n4

.DIVITION\n";

cout<<"ENTER ANY TWO NUMBERS AND THE OPERATION

SIGN & PRESS ENTER: \n";

cin>>a>>b>>c;

if(c==1)

Page 29: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 29 

cout<<"\n"<<a<<" + "<<b<<" = "<<a+b;

else if(c==2)

cout<<"\n"<<a<<" * "<<b<<" = "<<a*b;

else if(c==3)

cout<<"\n"<<a<<" - "<<b<<" = "<<a-b;

else if(c==4)

cout<<"\n"<<a<<" / "<<b<<" = "<<a/b;

}

Page 30: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 30 

Experiment No 10

C++ Programming: Switch Objectives: To be able to deal with multiple conditions.

Theory: if and if/else statements can become quite confusing when nested too deeply, and C++ offers an alternative. Unlike if, which evaluates one value, switch statements allow you to branch on any of a number of different values. The general form of the switch statement is:

switch (expression)

{ case valueOne: statement; break; case valueTwo: statement; break; .... case valueN: statement; break; default: statement; } expression is any legal C++ expression, and the statements are any legal C++ statements or block of statements. switch evaluates expression and compares the result to each of the case values. Note, however, that the evaluation is only for equality; relational operators may not be used here, nor can Boolean operations. If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. If there is no default and there is no matching value, execution falls through the switch statement and the statement ends. It is important to note that if there is no break statement at the end of a case statement, execution will fall through to the next case statement. This is sometimes necessary, but usually is an error. If you decide to let execution fall through, be sure to put a comment, indicating that you didn't just forget the break. This C++ program illustrates use of the switch statement.

Page 31: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 31 

Program: 1: // 2: // Demonstrates switch statement 3: 4: #include <iostream.h> 5: 6: int main() 7: { 8: unsigned short int number; 9: cout << "Enter a number between 1 and 5: "; 10: cin >> number; 11: switch (number) 12: { 13: case 0: cout << "Too small, sorry!"; 14: break; 15: case 5: cout << "Good job!\n"; // fall through 16: case 4: cout << "Nice Pick!\n"; // fall through 17: case 3: cout << "Excellent!\n"; // fall through 18: case 2: cout << "Masterful!\n"; // fall through 19: case 1: cout << "Incredible!\n"; 20: break; 21: default: cout << "Too large!\n"; 22: break; 23: } 24: cout << "\n\n"; 25: return 0; 26: } Analysis: The user is prompted for a number. That number is given to

the switch statement. If the number is 0, the case statement on line 13 matches, the

message Too small, sorry! is printed, and the break statement ends the switch. If the

value is 5, execution switches to line 15 where a message is printed, and then falls

through to line 16, another message is printed, and so forth until hitting the break on

line 20.

Page 32: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 32 

The net effect of these statements is that for a number between 1 and 5, that many

messages are printed. If the value of number is not 0-5, it is assumed to be too large,

and the default statement is invoked on line 21.

The syntax for the switch statement is as follows:

switch (expression) { case valueOne: statement; case valueTwo: statement; .... case valueN: statement default: statement; } The switch statement allows for branching on multiple values of expression. The

expression is evaluated, and if it matches any of the case values, execution jumps to

that line. Execution continues until either the end of the switch statement or

a break statement is encountered. If expression does not match any of

the case statements, and if there is a default statement, execution switches to

thedefault statement, otherwise the switch statement ends.

Example 1: switch (choice) { case 0: cout << "Zero!" << endl; break case 1: cout << "One!" << endl; break; case 2: cout << "Two!" << endl; default: cout << "Default!" << endl;

Page 33: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 33 

} Example 2: switch (choice)

{

choice 0:

choice 1:

choice 2:

cout << "Less than 3!";

break;

choice 3:

cout << "Equals 3!";

break;

default:

cout << "greater than 3!";

}

Page 34: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 34 

Experiment No 11

Page 35: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 35 

Example 1:

Program Print name by using for loop

#include<iostream.h>

void main()

{

for(int a=1;a<=5;a++)

cout<<"\Yasir";

}

Page 36: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 36 

Example 2:

Page 37: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 37 

Example 3:

Example 4: #include <iostream> using namespace std; int main () { int n; for (n=10; n>0; n--)

Page 38: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 38 

{ cout << n << ", "; if (n==3) { cout << "countdown aborted!"; break; } } return 0;

Page 39: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 39 

Experiment No 12

Page 40: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 40 

PROGRAM     Printing name using while loop

#include<iostream.h>

void main()

{

int a=5;

while(a>=1)

{

cout<<"\nArsalan";

a--;

}

}

PROGRAM   Printing name using do while loop

#include<iostream.h>

void main()

{

int a=0;

do

{

cout<<"\nArsalan";

a++;

}

while(a<=4);

}

Page 41: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 41 

Program: #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

Page 42: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 42 

Experiment No 13

Objective:

To understand the working of array.

Theory:

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. For example, an array to contain 5 integer values of type int called billy could be represented like this:

where each blank panel represents an element of the array, that in this case are integer values of type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array has to contain. Therefore, in order to declare an array called billy as the one shown in the above

C++ Programming: Array

Page 43: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 43 

diagram it is as simple as:

Initializing arrays When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros. In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces { }. For example:

This declaration would have created an array like this:

The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ]. For example, in the example of array billy we have declared that it has 5 elements and in the list of initial values within braces { } we have specified 5 values, one for each element. When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty [ ]. In this case, the compiler will assume a size for the array that matches the number of values included between braces { }:

int billy [] = { 16, 2, 77, 40, 12071 };

int billy [5];

int billy [5] = { 16, 2, 77, 40, 12071 };

Page 44: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 44 

After this declaration, array billy would be 5 ints long, since we have provided 5 initialization values.

Accessing the values of an array:

In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. The format is as simple as: name[index] Following the previous examples in which billy had 5 elements and each of those elements was of type int, the name which we can use to refer to each element is the following:

For example, to store the value 75 in the third element of billy, we could write the following statement:

billy[2] = 75;

and, for example, to pass the value of the third element of billy to a variable called a, we could write:

a = billy[2];

Therefore, the expression billy[2] is for all purposes like a variable of type int. Notice that the third element of billy is specified billy[2], since the first one is billy[0], the second one is billy[1], and therefore, the third one isbilly[2]. By this same reason, its last element is billy[4]. Therefore, if we write billy[5], we would be accessing the sixth element of billy and therefore exceeding the size of the array. In C++ it is syntactically correct to exceed the valid range of indices for an array. This can create problems, since accessing out-of-range elements do not cause compilation errors but can cause runtime errors. The reason why this is allowed will be seen further ahead when we begin to use pointers. At this point it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays. They perform two different tasks: one is to specify

Page 45: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 45 

the size of arrays when they are declared; and the second one is to specify indices for concrete array elements. Do not confuse these two possible uses of brackets [ ] with arrays.

1 2

int billy[5]; // declaration of a new array billy[2] = 75; // access to an element of the array.

If you read carefully, you will see that a type specifier always precedes a variable or array declaration, while it never precedes an access. Some other valid operations with arrays:

1 2 3 4

billy[0] = a; billy[a] = 75; b = billy [a+2]; billy[billy[a]] = billy[2] + 5;

Program: // arrays example #include <iostream> using namespace std; int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; }

Multidimensional arrays Multidimensional arrays can be described as "arrays of arrays". For example, a bi dimensional array can be imagined as a bi dimensional table made of elements, all of them of a same uniform data type.

Page 46: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 46 

jimmy represents a bi dimensional array of 3 per 5 elements of type int. The way to declare this array in C++ would be: and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:

int jimmy [3][5];

jimmy[1][3]

Page 47: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 47 

Experiment No 14

C++ Programming: Function

Objective:

To know the operation of function.

Theory:

Functions are building blocks of the programs. They make the programs more modular and easy to read and manage. All C++ programs must contain the function main( ). The execution of the program starts from the function main( ). A C++ program can contain any number of functions according to the needs. The general form of the function is: -

return_type function_name(parameter list)

{

body of the function

}

The function of consists of two parts function header and function body. The function header is:-

return_type function_name(parameter list)

The return type specifies the type of the data the function returns. The return type can be void which means function does not return any data type. The function name is the name of the function. The name of the function should begin with the alphabet or underscore. The parameter list consists of variables separated with comma along with their data types. The parameter list could be empty which means the function do not contain any parameters. The parameter list should contain both data type and name of the variable. For example,

int factorial(int n, float j)

Page 48: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 48 

is the function header of the function factorial. The return type is of integer which means function should return data of type integer. The parameter list contains two variables n and j of type integer and float respectively. The body of the function performs the computations.

Function Declaration:

Function declaration is made by declaring the return type of the function, name of the function and the data types of the parameters of the function. A function declaration is same as the declaration of the variable. The function declaration is always terminated by the semicolon. A call to the function cannot be made unless it is declared. The general form of the declaration is:-

return_type function_name(parameter list);

For example function declaration can be

int factorial(int n1,float j1);

The variables name need not be same as the variables of parameter list of the function. Another method can be

int factorial(int , float);

The variables in the function declaration can be optional but data types are necessary.

Function Arguments:

The information is transferred to the function by the means of arguments when a call to a function is made. Arguments contain the actual value which is to be passed to the function when it is called. The sequence of the arguments in the call of the function should be same as the sequence of the parameters in the parameter list of the declaration of the function. The data types of the arguments should correspond with the data types of the parameters. When a function call is made arguments replace the parameters of the function.

The Return Statement and Return values:

A return statement is used to exit from the function where it is. It returns the execution of the program to the point where the function call was made. It returns a value to the calling code. The general form of the return statement is:-

return expression;

Page 49: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 49 

The expression evaluates to a value which has type same as the return type specified in the function declaration. For example the statement,

return(n);

is the return statement of the factorial function. The type of variable n should be integer as specified in the declaration of the factorial function. If a function has return type as void then return statement does not contain any expression. It is written as:-

return;

The function with return type as void can ignore the return statement. The closing braces at the end indicate the exit of the function. Here is a program which illustrates the working of functions.

Program: #include<iostream>

using namespace std;

int factorial(int n);

int main ()

{

int n1,fact;

cout <<"Enter the number whose factorial has to be calculated" << endl;

cin >> n1;

fact=factorial(n1);

cout << "The factorial of " << n1 << " is : " << fact << endl;

return(0);

}

int factorial(int n)

{

int i=0,fact=1;

if(n<=1)

Page 50: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 50 

{

return(1);

}

else

{

for(i=1;i<=n;i++)

{

fact=fact*i;

}

return(fact);

}

}

Program : Use of pointer with functions

#include <iostream.h>

int addition (int a, int b)

{

return (a+b);

}

int subtraction (int a, int b)

{

return (a-b);

}

int operation (int x, int y, int (*functocall)(int,int))

Page 51: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 51 

{

int g;

g = (*functocall)(x,y);

return (g);

}

int main ()

{

int m,n;

int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);

n = operation (20, m, minus);

cout <<"addition "<<n<<"\nminus "<<m;

return 0;

}

 

 

 

 

 

 

 

 

 

 

 

Page 52: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 52 

Experiment No 15

C++ Programming: Pointer Theory:

A pointer is a variable that is used to store a memory address. The address is the location of the variable in the memory. Pointers help in allocating memory dynamically. Pointers improve execution time and saves space. Pointer points to a particular data type. The general form of declaring pointer is:-

type *variable_name;

type is the base type of the pointer and variable_name is the name of the variable of the pointer. For example,

int *x;

x is the variable name and it is the pointer of type integer.

 Pointer Operators           There are two important pointer operators such as ‘*’ and ‘&’. The ‘&’ is a unary operator. The unary operator returns the address of the memory where a variable is located. For example,

int x*;

int c;

x=&c;

variable x is the pointer of the type integer and it points to location of the variable c. When the statement

x=&c;

is executed, ‘&’ operator returns the memory address of the variable c and as a result x will point to the memory location of variable c.

The ‘*’ operator is called the indirection operator. It returns the contents of the memory location pointed to. The indirection operator is also called deference operator. For example,

int x*;

Page 53: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 53 

int c=100;

int p;

x=&c;

p=*x;

variable x is the pointer of integer type. It points to the address of the location of the variable c. The pointer x will contain the contents of the memory location of variable c. It will contain value 100. When statement

p=*x;

is executed, ‘*’ operator returns the content of the pointer x and variable p will contain value 100 as the pointer x contain value 100 at its memory location. Here is a program which illustrates the working of pointers.

Program: #include<iostream>

using namespace std;

int main ()

{

int *x;

int c=200;

int p;

x=&c;

p=*x;

cout << " The address of the memory location of x : " << x << endl;

cout << " The contents of the pointer x : " << *x << endl;

cout << " The contents of the variable p : " << p << endl;

return(0);

}

Page 54: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 54 

Program # 1  

#include <iostream.h>

int main ()

{

int numbers[5];

int * p;

p = numbers;

*p = 10;

p++;

*p = 20;

p = &numbers[2];

*p = 30;

p = numbers + 3;

*p = 40;

p = numbers;

*(p+4) = 50;

for (int n=0; n<5; n++)

cout << numbers[n] << ", ";

return 0;

}

 

 

 

Page 55: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 55 

Exercise 16

Project List

1. ATM Machine in C++

2. Banking System in C++

3. Scientific Calculator in C++

4. Library system in C++

5. Lab Inventory Record in C++

6. Telephone Directory in C++

7. Calculating Waited GPA

8. Electric Bill

9. Salary Calculation

10. Unit Converter

11. Hospital Management System

12. Departmental Store Management System

Page 56: INTRODUCTION TO PROGRAMMING - FuuasTeefuuastee.weebly.com/uploads/1/0/5/6/10561538/...5 Introduction into Microsoft PowerPoint 6 Introduction into Microsoft Excel 7 Introduction to

     

 F e d e r a l   U r d u   U n i v e r s i t y   A r t s ,   S c i e n c e   &   T e c h n o l o g y   Page 56