Sunteți pe pagina 1din 101

QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH

DEPARTMENT OF CHEMICAL ENGINEERING


Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

TABLE OF CONTENTS

Lab Practical Page No.


No.
1 Parts/Components of Computer and how to identify and Connect
the Computer components.

2 Keyboard as an input device and various keyboard shortcuts.

3 To Understand Various Shortcut Keys.

4 Monitor as an Output device.

5 To perform installation of MS Windows 7 operating system

6 Getting Familiar with Microsoft Word 2007 for Windows

7 Introduction to C++ and its basics.

8 .To write C++ program to implement Nested Looping Constructs.

9 To perform Decision making (if & if-else) structures using C++.

10 To perform decision making (Switch, case) statement using C++.

11 To be familiar with the use of Strings and string operators using C+


+.

12 To perform string functions using C++.

13
To be familiar with the use of pointers and references using C++.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL #01
Objective:
Parts/Components of Computer and how to identify and Connect the Computer components.

Theory/Description:

COMPUTER
A computer is an electronic device that manipulates information, or "data." It has the
ability to store, retrieve, and process data. You can use a computer to type documents,
send email, and browse the internet. You can also use it to handle spreadsheets,
accounting, database management, presentations, games, and more.

PARTS/COMPONENTS OF COMPUTER
Computers are made up of many parts. These different parts perform one or more functions
including input, output, processing, or storage.

Output Devices

Output devices send information from your computer to you. This information is usually in
the form of sound and sight, but some devices can send information as touch.

Some common output devices are monitors, printers, and speakers.

   

Input Devices

Input devices are the parts that let you enter and manipulate information on a computer.
These devices range from the standard keyboard and mouse, to scanners, microphones,
joysticks, and light pens.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

   

There are some devices that can input and output. Some examples would touch screen
monitors (input by touch, output by sight) and force feedback joysticks.

On The Inside

The inside of your computer has many parts that all work together. These parts are
generally found within your computer case , this is usually the big "box" that probably sits
under your desk or below your monitor. If you're using an iMac, many of the computer parts
are built into the monitor case.

 Motherboard

The motherboard or mainboard, is the backbone of the computer. All the individual pieces
connect to the motherboard in some way. The motherboard is home the processor chip, pci
slots, and memory.

 Processor

This is the chip that does the "thinking" of the computer. These are the "Pentium" and
"AMD" chips you hear about. Processor speed is measured in MegaHertz(Mhz) and
GigaHertz(Ghz). 1 Ghz = 1000 Mhz
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

 Memory

This is where information is temporarily stored for the processor to use and manipulate
before storing on the HARD DRIVE. Also known as RAM (Random access memory).
Information is stored in memory only when the computer is turned on. Ram is measured in
Megabytes(Mb), which is storage capacity, not to be confused with MegaHertz, which is
speed.

 PCI Slot

These are outlets in the motherboard that allow you to install extra components like sound
cards, modems, video cards, and other devices.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The images below show different PCI card components.

 Hard Drive

This is the part of your computer where information is stored for later retrieval. All the
information you access on your computer, all your documents, pictures, email messages,
and programs are here. Unlike memory, the hard drive stores information even after the
power is turned off. The image to the right shows the inside of a hard drive.

 Floppy Drive

This is the slot in the front of your computer where you insert a disk to store data and move
it to another computer. If your computer is an iMac, you will not have a Floppy Drive.
Floppy disks are 3¼ inches in size, and hold 1.44Mb of data. The images below show a
floppy drive, and some floppy disks.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

 CD ROM or DVD ROM Drives

This plays your music and data CD's, or if you have a DVD drive, it will also play DVD
movies. Data CD's hold up to 700Mb of information. If you have a CD-R or CD-RW drive,
you can store your own information on CDs.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

TO IDENTIFY AND CONNECT THE COMPUTER

 Front Of CPU
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

 Back Of CPU

 Ports & Connectors Closeup


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

A=Network Connector
B=USB ports 1& 2
C=Monitor Connector
D=PS/2 Keyboard Connector
E=PS/2 Mouse Connector
F=Com Port 1
G=25 Pin Parallel Port(also Known as LPTI for Printer)
H=Com port 2
I=Game Stick Port
J=Sound Card-Speaker Connector
K=Sound Card-Audio Out Connector
L=Sound Card-Microphone Connector
A look at other Components
 CPU Power Cord

 Keyboard Connectors
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The illustration above shows two examples of Keyboard connectors, the left is the larger
connector and the right is the smaller. On the larger connector, there is an arrow that
should face up when you are connecting it to the CPU. On the smaller connector, there is a
flat side that should face up when connecting it to the CPU.
 Mouse Connectors

The illustrations above show two examples of mouse connectors.the left is a PS/2 mouse
connector(circle)and the right is a serial connector that would plug into one of your com
ports.
 Speakers Connectors

The following cords are connected to a set of speakers:


o Power cord-Connecting one speaker to the power source
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

o Left to Right speaker connector-Connecting one speaker to the other


o Speaker to CPU connector-Connecting speakers to the CPU
 Monitor Connector

There are however two cords attached to the back of the monitor.The first is a power cord
that connects the monitor to the power source.The second is a monitor cable as illustrated
below:

The monitor cable is a 9 pin male connector that gets connected to the monitor connector
on the back of the CPU.
 Network Cable& Connectors

Nework Cable
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Network Jack
The Network Cable can be blue ,purple or off-white.one end is connected to the back of
the computer(in location A)and the other end is plugged into the Network jack on the
wall.
 Parallel Printer Connector
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The Parallel Printer cable has one connector on each end.The 25 pin male connector gets
connected to the back of CPU in location G and the other end is connected to the local
printer.
 USB Printer Cable

Newer Computers and Printers will support the use of a USB printer cable.A USB cable will
transfer information more quickly than a parallel cable.the flat end of the CPU (in location
B).The square end is connected to the local printer.
Now that you know all the pieces to your computer how do you put the puzzle
together?

Follow the following steps for putting the pieces together.refer to the illustrations above
to help identify the components.
1.Position the CPU in the desired location.
2.Connect one end of the Network Cable to the back of the CPU(location A)and the
other to the wall jack.
3.Connect the Keyboard to the back of the CPU(location D.)
4.Connect the mouse to the back of the CPU(location E).
5.Connect the monitor cable to the back of the CPU(location C).
6.Connect the monitor power cable to the power source.
7.If you have speakers,connect the speaker power cord to the power source,connect
the left and right speaker and connect the speaker to the sound card on the back of the
CPU(Location J),some speakers are color coded to assist in the set-up,if yours
are,follow the color codes.
Now turn the power on!

Review Questions:

1.What can you do with computers?

Ans:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

2.Enlist various input, output, processing and storage devices?

Ans:

3.Write the various appilcation of computer in different fields of life?

Ans:

4.What is difference between Primary storage and Secondary storage?

Ans:

5.Plotter is either input device or Output device?

Ans:

Conclusion:

Name:____________________________________
Roll#:____________________________________
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Date:_____________________________________

Remarks: Subject Teacher/Lab


Engineer
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL #02

Objective:
Keyboard as an input device and various keyboard shortcuts.

THEORY:
Keyboard Basics

A keyboard's primary function is to act as an input device. Using a keyboard, a person


can type a document, use keystroke shortcuts, access menus, play games and perform a
variety of other tasks. Much like many type writers, the keyboard has all the letters of the
alphabet, numbers 0 - 9 and additional special operational keys. These keys are generally
laid out in the same pattern used for typewriters. This layout is known as QWERTY. The
name "QWERTY" for our computer keyboard comes from the first six letters in the top
alphabet row. It is also referred to as the "Universal" keyboard. It was the work of inventor
C. L. Sholes, who put together the prototypes of the first commercial typewriter in a
Milwaukee machine shop back in the 1860's."

Most keyboards have between 80 and 110 keys, including:

Typing keys
A numeric keypad
Function keys
Control keys

TYPES OF KEYBOARDS

QWERTY Keyboard - Developed by Sholes in 1868 and is now the official standard of
computer keyboards.

Dvorak Keyboard - Developed by Dr. August Dvorak in 1936 as a solution to the outdated
QWERTY keyboard. The layout of the keys on this keyboard makes it easier and faster to
type on when compared with other keyboards.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Chiclet Keyboard - Very small keyboard that was introduced with the PC Jr. with small
keys spread out.

Explanation of the Keys on a Windows QWERTY Keyboard

Escape Key  

The Escape key, which is marked ESC on most keyboards, is basically used to exit or escape
from programs and tasks. In many cases, it will have no effect at all. However, it can
sometimes get you out of trouble by making the computer go back or escape to a previous
screen. The Esc key is Equivalent to clicking the Cancel button.

Function Keys
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Most computer keyboards have a row of Function keys at the top of the keyboard. These
keys are marked F1 through F10 or F12. All function keys have pre-defined meanings which
depend on the application software you are using. While they were widely used with older
DOS programs, they are not as popular today. However many programs, including most of
Microsoft's products, support use of the function keys. The function keys are frequently used
in combination with other keys such as the CTRL key, the ALT key, and the Shift key.

Print Screen

Usually located at the upper right hand corner of your keyboard next to the Scroll Lock and
Pause/Break keys, Often abbreviated PrtScr, the Print Screen key is a useful key
supported on most PCs. In DOS, pressing the Print Screen key causes the computer to
send whatever images and text are currently on the display screen to the printer. Some
graphics programs and Windows, use the Print Screen key to obtain Screen Captures.

Pause/Break

This key was previously used in programming and debugging applications. In most current
programs, it is non-functioning. In programming language like C or BASIC, by pressing Ctrl
+ Break, stops program from running.

Scroll Lock 

Scroll Lock is a toggle key that changes the effect of the cursor movement keys. In most
current programs the Scroll Lock key is disabled and pressing it has no effect. In programs
that support this key, when the Scroll Lock key is on, pressing the arrow keys makes the
display appear to scroll while the cursor stays in its original position. When the Scroll Lock
key is off, the cursor moves as far as it can before the display starts scrolling. 

Tab

This key can be used to move forward through options in a dialog box.
Ctrl + Tab allow movement from one open window to the next in an application with more
than one open window.
Alt + Tab display a list of open application windows. Keeping Alt depressed and selecting
Tab cycles through the list. Releasing selects the highlighted application window.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Caps Lock

The Caps Lock key is a toggle key. Pressing it once turns it on. Pressing it again turns it off.
Keyboards have a light or indicator that shows when the Caps Lock is on and when it is off.
When Caps Lock is on, every letter that is typed will be a capital letter.

Shift Key 

The Shift key in combination with an alphabetical key will type an upper case letter. The
Shift key in combination with one of the number keys on the row above the letter keys or
one of the symbol keys will type the symbol that is pictured on the upper part of the key.
The Shift key can also be used in conjunction with other keys as a shortcut to a task or can
be pressed at a certain time to perform a task. For example, holding down the Shift key
while inserting a CD-ROM will skip the auto-run process. Shift + Delete permanently
deletes a selected item, bypasses the Recycle Bin.

Shift + Arrow (Up, Down, Left or Right). For the understanding of this shortcut position
your cursor at the beginning of the area you wish to highlight for copying. Now use the
up, down, left or right arrow keys to select areas of content to be highlight, copied, pasted,
etc.

Control Key (Ctrl) 

The Ctrl key is used in conjunction with another key. Holding it down while pressing another
key will initiate a certain action. Ctrl key combinations are defined by the application that is
being used. For example in most Windows programs, Ctrl+S will save the current file or
document, and Ctrl+P will print the current file or document.

Alternate Key (Alt)

Alt is located on either side of the space bar. Like the Control Key, the Alt key is used in
combination with other keys. In most Windows programs, each of the menu choices at the
top of the screen has one letter underlined. Holding down the Alt key while pressing the key
corresponding to the underlined letter will open the menu
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

For example, in word processing applications pressing Alt key in combination with F key (Alt
+ F) will open the File Menu as shown below:

Windows Key 

The Windows key is marked with a small Microsoft Windows symbol and is usually found on
the bottom row of the keyboard. Pressing the Windows key will bring up the Start menu.
The Window key can also be used in combination with other keys for some very useful
shortcuts. One of my favorites is to use the Windows key +D to minimize all the open
windows and quickly return to the Windows desktop. Pressing Windows +D again will
restore all windows to their previous location. 

Space Bar

The Spacebar is used to insert a blank space between words. It some programs it can also
be used to choose a highlighted item. Pressing the Space Bar while viewing a web page in
Internet Explorer will scroll the page downwards. Shift + Space Bar will scroll the page
upwards.

Application Key 

The Application key has an image of a mouse pointer on a menu (between the Alt and Ctrl
keys ( ) to the right of your Space Bar).Application key is a shortcut for right clicking. It
will display an item's shortcut menu.

Return or Enter Key 

The Enter key is also called Return key usually labeled with a large arrow. The Enter key is
used to enter commands or to move the cursor to the beginning of the next line. There is a
second Enter key on the numeric keypad. This functions exactly like the larger Enter key
near the alphabet letters.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Backspace Key 

While working with the text, the Backspace key will remove the character to the left of the
cursor. The key is sometimes labeled with only a left-pointing arrow.

Insert Key (Ins)   

The Insert key is found only on PC keyboards. It is a toggle key that determines what
happens when you type new characters within an existing line of text or numbers. When the
Insert key is on, the new text that you type is inserted at the cursor location and the text
already in place is moved to the right. When the Insert key is off, new text overwrites the
text that is on the screen to the right of the cursor. There is usually no visual indication of
whether the Insert key is on or off.

Home

The Home key will move the cursor to the beginning of line or top of the document.
Ctrl + Home key will move the cursor to the top of document.

Page Up

Page Up key will move the screen one page upward. In a browser window use the Page Up
key to move up one full screen on a web page.

Delete Key (Del) 

The Backspace key will remove the character to the left of the cursor, the Delete key will
remove the key to the right of the cursor. The Delete key can also be used in Windows to
remove a highlighted or chosen file or shortcut. If you use the keyboard combination Shift +
Delete the item is permanently deleted, bypassing the Recycle Bin.

End

The End key will move the cursor to the end of line, the bottom of the screen or the end of a
document. Ctrl + End Key will move cursor to the last line of the document.

Page Down

Page Down key is Opposite of Page Up key. Page Down key will move the screen one page
downward. In a browser window use the Page Down key to move down one full screen on a
web page.

Cursor or Arrow Keys


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Cursor keys permit the user to move the cursor on the screen. Cursor keys are usually
found on the right hand side of the user. There are four cursor keys namely: Cursor Up,
Cursor Down, Cursor Left and Cursor Right.

Cursor Up key will move the cursor one line up. Pressing Ctrl + will move the cursor to
the beginning of the next line above.

Cursor Down key will move the cursor one line down. Pressing Ctrl + will move the
cursor to the beginning of next line below.

Cursor Right key will move the cursor one space to the left. Pressing Ctrl + will move
the cursor one word to the right.

Cursor Left key will move the cursor one space to the left. Pressing Ctrl + will move
the cursor one word to the left.

Num Lock & Numeric Keypad 

Many, but not all, computer keyboards have a numeric


keypad usually located on the right side of the keyboard.
This keypad has a group of number keys with additional
markings like arrows, PgDn, End, etc. The numeric pad is
controlled by a toggle key marked Num Lock. When the
Num Lock key is on, this pad can be used to enter
numbers. When the Num Lock key is off, the functions
listed below the number will be activated. These functions
usually include arrow keys that can be used to move the
cursor around the screen. Likewise the keys marked PgUp
and Pg Down will move the cursor a page up or down on
the screen. The Home and End keys will move the cursor
to the beginning or end of a line or document,
respectively. 

INSIDE THE KEYBOARD

A keyboard is a lot like a miniature


computer. It has its own processor and
circuitry that carries information to and
from that processor. A large part of
this circuitry makes up the key
matrix.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The microprocessor and controller circuitry of a


keyboard

The key matrix is a grid of circuits underneath the keys. In all keyboards (except for
capacitive models, which we'll discuss in the next section), each circuit is broken at a point
below each key. When you press a key, it presses a switch, completing the circuit and
allowing a tiny amount of current to flow through. The mechanical action of the switch
causes some vibration, called
bounce, which the processor
filters out. If you press and
hold a key, the processor
recognizes it as the
equivalent of pressing a key
repeatedly.

The key matrix

This keyboard uses rubber dome switches.

FROM KEYBOARD TO COMPUTER

Many keyboards connect to the computer through a cable with a


PS/2 or USB (Universal Serial Bus) connector. Laptops use internal
connectors. Regardless of which type of connector is used, the cable
must carry power to the keyboard, and it must carry signals from
the keyboard back to the computer.

Wireless keyboards, on the other hand, connect to the computer


through infrared (IR), radio frequency (RF) or Bluetooth
connections. IR and RF connections are similar to what you'd find in a remote control.
Regardless of which sort of signal they use, wireless keyboards require a receiver, either
built in or plugged in to the USB port, to communicate with the computer. Since they don't
have a physical connection to the computer, wireless keyboards have an AC power
connection or use batteries for power.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

REVIEW QUESTIONS

(1) Discuss the types of Keyboard, which type of keyboard is considered as a “Universal
Keyboard”?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(2) What is the function of “Print Screen” Key in MS-DOS and Windows?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(3) Differentiate between “Backspace” and “Delete” keys?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(4) How does “NUM LOCK” key affect the behavior of “Numeric Keypad”?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(5) Which type of connector is used to connect keyboard to computers? How Wireless
keyboards are connected to the computers?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Conclusion:

Name:____________________________________
Roll#:____________________________________
Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer

PRACTICAL #03
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

OBJECTIVE: To Understand Various Shortcut Keys.

THEORY:

SHORTCUT KEYS

Shortcut keys provide an easier and usually quicker method of using computer software
programs. Shortcut keys are commonly accessed by using the Alt, Ctrl, and/or Shift in
conjunction with a single letter (on IBM compatible computers). The de facto standard for
listing a shortcut is:

the modifier key (Alt, Ctrl, Shift)


plus
The single character.

Or in other words, "ALT+S" is telling you to press the Alt key and while continuing to hold
the Alt key, press the S key to perform the shortcut. 

BASIC SHORTCUT KEYS

The below basic shortcut keys are a listing of shortcut keys that will work with almost all
IBM compatible computers and software programs.

Shortcut Keys Description


Alt + F File menu options in current program.
Alt + E Edit options in current program
F1 Universal Help in almost every Windows program.
Ctrl + A Select all text.
Ctrl + X Cut selected item.
Shift + Del Cut selected item.
Ctrl + C Copy selected item.
Ctrl + Ins Copy selected item
Ctrl + V Paste
Shift + Ins Paste
Home Goes to beginning of current line.
Ctrl + Home Goes to beginning of document.
End Goes to end of current line.
Ctrl + End Goes to end of document.
Shift + Home Highlights from current position to beginning of line.
Shift + End Highlights from current position to end of line.
Ctrl + Left arrow Moves one word to the left at a time.
Ctrl + Right arrow Moves one word to the right at a time.

MICROSOFT WINDOWS SHORTCUT KEYS


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Shortcut Keys Description


Alt + Tab Switch between open applications.
Alt + Space bar Displays the main window's system menu. This is the same as clicking
on the application icon at the left end of the title bar.
Alt + Print Create a screen shot only for the program you are currently in.
Screen
Ctrl + Alt + Del Reboot the computer and/or bring up the Windows task manager.
Ctrl + Esc Bring Up start menu.
Alt + Esc Switch Between open applications on taskbar.
F2 Renames selected Icon.
F3 Starts find from desktop.
F4 Opens the drive selection when browsing.
F5 Refresh Contents.
Alt + F4 Closes Current open program.
Ctrl + F4 Closes Window in a Program without closing the program.
Shift + Del Delete programs/files without throwing into the recycle bin.
Holding Shift Boot safe mode or by pass system files.
Holding Shift When putting in an audio CD, will prevent CD Player from playing.

WINDOWS KEYBOARD SHORTCUTS

Below is a listing of Windows keys that can be used on computers running a Microsoft
Windows operating system and using a keyboard with a Windows key. In the below list of
shortcuts, the Windows key is represented by "WINKEY" word.

Shortcut Keys Description


WINKEY + D  Brings the desktop to the top of all other windows.
WINKEY + M Minimizes all windows.
Undo the minimize done by WINKEY + M and WINKEY +
WINKEY + SHIFT + M
D.
WINKEY + E Open Microsoft Explorer.
WINKEY + Tab Cycle through open programs through the taskbar.
WINKEY + F Display the Windows Search / Find feature.
WINKEY + CTRL + F  Display the search for computers window.
WINKEY + F1 Display the Microsoft Windows help.
WINKEY + R Open the run window.
WINKEY + Pause / Break
Open the system properties window.
key 
WINKEY + U Open Utility Manager.
WINKEY + L Lock the computer (Windows XP and above only).
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

WORD SHORTCUT KEYS

Shortcut Keys Description


Ctrl + A Select all contents of the page.
Ctrl + B Bold highlighted selection.
Ctrl + C Copy selected text.
Ctrl + X Cut selected text.
Ctrl + P Open the print window.
Ctrl + F Open find box.
Ctrl + I Italic highlighted selection.
Ctrl + K Insert link.
Ctrl + U Underline highlighted selection.
Ctrl + V Paste.
Ctrl + Y Redo the last action performed.
Ctrl + Z Undo last action.
Ctrl + L Aligns the line or selected text to the left of the screen.
Ctrl + E Aligns the line or selected text to the center of the screen.
Ctrl + R Aligns the line or selected text to the right of the screen.
Ctrl + M Indent the paragraph.
Ctrl + Shift + F Change the font.
Ctrl + Shift + > Increase selected font +1pts up to 12pt and then increases font +2pts.
Ctrl + ] Increase selected font +1pts.
Decrease selected font -1pts if 12pt or lower, if above 12 decreases
Ctrl + Shift + <
font by +2pt.
Ctrl + [ Decrease selected font -1pts.
Ctrl + <left arrow>Moves one word to the left.
Ctrl + <right
Moves one word to the right.
arrow>
Ctrl + <up arrow> Moves to the beginning of the line or paragraph.
Ctrl + <down
Moves to the end of the paragraph.
arrow>
Ctrl + Del Deletes word to right of cursor.
Ctrl + Backspace Deletes word to left of cursor.
Ctrl + End Moves the cursor to the end of the document.
Ctrl + Home Moves the cursor to the beginning of the document.
Ctrl + Spacebar Reset highlighted text to the default font.
Ctrl + 1 Single-space lines.
Ctrl + 2 Double-space lines.
Ctrl + 5 1.5-line spacing.
Ctrl + Alt + 1 Changes text to heading 1.
Ctrl + Alt + 2 Changes text to heading 2.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Ctrl + Alt + 3 Changes text to heading 3.


F1 Open Help.
Shift + F3 Change the case of the selected text.
Shift + Insert Paste.
F4 Repeat the last action performed (Word 2000+)
F5 Open goto window. 
F7 Spell check selected text and/or document.
Shift + F7 Activate the thesaurus.
F12 Save as.
Shift + F12 Save.
Alt + Shift + D Insert the current date.
Alt + Shift + T Insert the current time.

 MOUSE SHORTCUTS IN MS-WORD

Mouse Shortcuts Description


Click, hold, and Selects text from where you click and hold to the point you drag
drag and let go.
Double-click If double-click a word, selects the complete word.
Double-clicking on the left, center, or right of a blank line will
Double-click
make the alignment of the text left, center, or right aligned.
Double-click Double-clicking anywhere after text on a line will set a tab stop.
Selects the line or paragraph of the text the mouse triple-
Triple-click
clicked.
Ctrl + Mouse
Zooms in and out of document.
wheel

INTERNET EXPLORER SHORTCUT KEYS

Shortcut Keys Description


Alt + Left Arrow Back a Page
Alt + Right Forward a Page
Arrow
F5 Refresh current page / frame.
F11 Display the current website in full screen mode. Pressing F11
again will exit this mode.
Esc Stop page or download from loading.
Ctrl + Enter Quickly complete an address. For example, type yahoo in the
address bar and press CTRL + ENTER to get
http://www.yahoo.com.
Ctrl + N Open New browser window.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Ctrl + P Print current page / frame.


Spacebar Moves down a page at a time.
Shift + Spacebar Moves up a page at a time.

REVIEW QUESTIONS:

(1) What is significance of using shortcut keys?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(2) What is de facto standard for listing shortcut keys?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(3) What will happen when you press HOME, Ctrl + HOME and Shift + HOME in word
processing task?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(4) Which Shortcut key is used in Internet Explorer to complete a web address?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(5) What is purpose of


Ctrl + I following shortcut keys in
Ctrl + U MS-Word?
Ctrl + V
Ctrl + X
Ctrl + P
Ctrl + Y
(6) Ctrl + Z Write down the description
of Ctrl + A following MICROSOFT
Ctrl + B WINDOWS shortcut keys?
Ctrl + C
Ctrl
Alt++[ Tab
Ctrl + ]
Click, hold & drag
Double-Click in
front of text
Double-Click on
blank line
Triple Click
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Alt + Space bar


Alt + Print
Screen
Ctrl + Alt + Del
Ctrl + Esc
Alt + Esc
F2
F3
F4
F5
Alt + F4
Ctrl + F4
Shift + Del

Conclusion:

Name:____________________________________
Roll#:____________________________________
Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer

PRACTICAL #04

OBJECTIVE: Monitor as an Output device.

THEORY:
A computer monitor is the most important output component in your computer system. It
enables you to view text, graphics and DVD movies. There are two main types of monitors:

Cathode Ray Tube (CRT) Monitors


Flat Panel Monitors

Most desktop displays use liquid crystal display (LCD) or cathode ray tube (CRT) technology,
while nearly all portable computing devices such as laptops incorporate LCD technology.
Because of their slimmer design and lower energy consumption, monitors using LCD
technology (also called flat panel or flat screen displays) are replacing the CRT on most
desktops.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

CRT
Monitor

Flat Panel Monitor

(1) CRT Monitors:

Like most TVs, the computer monitor has a CRT,


or Cathode Ray Tube, which is found inside the
computer monitor as shown in the figure. Within
the CRT
are three electron
guns, Red, Green
and Blue. Each of these
guns streams a
steady flow of
electrons, left
to right, for each line
of your monitor.

The cathode ray tube is comprised of a cathode and two anodes. The cathode contains a
heating element that is rather like the filament in a light bulb. When this is heated, the
cathode releases electrons.

Because electrons are negatively charged, the electrons flow towards the positively
charged anodes. One (the ‘accelerating anode’) speeds the electrons up and fires them
towards the screen at the other end of the vacuum chamber. The other anode (the
‘focusing anode’) forces the electrons into a tight beam so that they are not randomly
scattered about the screen. The screen itself is coated with a phosphor.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

When a beam of electron from electron gun hits the phosphor coating on the screen, it
lights up tiny points of phosphor for a short period of time. The gun constantly hits the
screen (normally 30 times in a second), relighting the tiny points of phosphor. There are
also two sets of deflecting coils: horizontal and vertical, that allows constant adjustment of
the direction of the electron beam.

CRT Features and Attributes

Resolution

A key characteristic of any display device is its Resolution or Sharpness of the screen.
Resolution refers to the number of “pixels” contained on a screen. A pixel (Picture Element)
is the smallest dot / point on the screen. Resolution is typically expressed by identifying the
number of pixels on the horizontal axis (rows) and the number of lines on the vertical axis
(columns), such as 640 by 480 means that screen consist of 640 columns and 480 rows of
pixels i-e 640 x 480 or 307,200 pixels.

Dot pitch

Dot pitch is the spacing or distance between pixels on a CRT, measured in millimeters. A
smaller dot pitch produces the better resolution that is the more crisper and detailed image.

Refresh rate

The refresh rate is the number of times that the image on the display is drawn each second.
If your CRT monitor has a refresh rate of 72 Hertz (Hz), then it cycles through all the pixels
from top to bottom 72 times a second. Refresh rates are very important because they
control flicker, and you want the refresh rate as high as possible. Refresh rates that are too
low could cause a flickering, which can lead to headaches and eye strain.

Color

CRT screens can be classified into two types:

Monochrome

Color

Monochrome mono means one; so that monochrome CRTs has only one electron gun that
can display only a single color usually Amber, Green and White.

Color CRT devices have three electron guns, one for the primary color red (R), one for the
primary color green (G), and one for the primary color blue (B). This is the so-called RGB
color monitor. An RGB monitors forms all colors available on the screen by mixing
combinations of only these three primary colors.

Color monitors have evolved through following stages:

CGA (Color Graphic Adapter) supports 4 colors at resolution 320 x 200.

EGA (Enhanced Graphic Adapter) supports 16 colors at resolution 650 x 300.

VGA (Video Graphic Array) supports 256 colors at resolution 640 x 480.

XGA (Extended Graphic Array) supports 65,536 colors at resolution 1024 x 768.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

SVGA (Super Video Graphic Array) supports 16,777,216 colors at resolution


1024 x 768.

(2) Flat Panel Monitors:

CRT monitors are the standard for use with the desktop computers, however, certain
disadvantages associated with CRT monitors include: large size, high power consumption
and occasional flickering images. These drawbacks have led to the development of flat panel
technology. Flat panel displays take little space, are lightweight and requires less power
than CRT monitors. Flat panel technology is particularly useful for laptop computers. There
are three types of flat panel monitors:

LCD (Liquid Crystal Display)

EL (Electroluminescent Display)

Plasma Displays

Liquid Crystal Displays:

LCD (liquid crystal display) is the technology used for displays in notebook and other
smaller computers. A liquid crystal display (LCD) is a thin, flat display device made up of
any number of color or monochrome pixels arrayed in front of a light source or reflector. It
is prized by engineers because it uses very small amounts of electric power, and is therefore
suitable for use in battery-powered electronic devices.

Liquid crystal display technology works by blocking light. Specifically, an LCD is made of two
pieces of polarized glass (also called substrate) that contain a liquid crystal material
between them. A backlight creates light that passes through the first substrate. At the same
time, electrical currents cause the liquid crystal molecules to align to allow varying levels of
light to pass through to the second substrate and create the colors and images that you
see. Working of LCD is shown in the following figures:

Figure NO 01
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Figure NO 02

Figure NO 03
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Figure NO 04

Figure NO 05

Active and Passive Matrix Displays


High-resolution color displays such as modern LCD computer monitors and televisions use
an Active Matrix structure. A matrix of thin-film transistors (TFTs) is added to the
polarizing and color filters. A thin film transistor (TFT) arranges tiny transistors and
capacitors in a matrix on the glass of the display. Active Matrix has transistors for each
pixel, generating their own light, creating a more brighter and crisp display. To address a
particular pixel, the proper row is switched on, and then a charge is sent down the correct
column. Since all of the other rows that the column intersects are turned off, only the
capacitor at the designated pixel receives a charge. The capacitor is able to hold the charge
until the next refresh cycle.

The other type of LCD technology is passive matrix. The passive matrix display has a row
of transistors running on the top of the screen (x-axes) as well as the left of the screen (y-
axes). The amount of transistors is defined by the LCD manufacturer, for example, the
manufacturer may define 800 transistors along the x-axes and 600 transistors along the y-
axes. Although they are less expensive to produce, passive matrix monitors are rarely used
today due to the technology's slow response time and imprecise voltage control compared
to active matrix technology.

LCD Features and Attributes

Native Resolution:
Because an LCD uses a matrix of cells to display its image, it has a fixed resolution, called
its native resolution, also called optimal resolution, at which the display looks best. LCD
monitors display information at only the resolution they are designed for, which is known as
the native resolution. Native resolutions are typically:
 17 inch = 1024x768
 19 inch = 1280x1024
 20 inch = 1600x1200

Brightness or Luminance
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

LCD brightness is calculated by measuring the greatest amount of light that comes from the
screen when displaying pure white. This is a measurement of the amount of light the LCD
monitor produces. It is given in nits or one candelas per square meter (cd/m2). One nit is
equal to on cd/m2. Typical brightness ratings range from 250 to 350 cd/m2 for monitors
that perform general-purpose tasks. For displaying movies, a brighter luminance rating such
as 500 cd/m2 is desirable.

Contrast ratio
Contrast ratio refers to the difference in light intensity between the brightest white and the
darkest black that an LCD can produce. The figure is usually expressed as a ratio, for
example, 500:1.

Viewing angle

Because of the way light passes through the liquid crystals in the display; LCDs may appear
to lose some brightness and image quality as you move to the side of the screen, or above
or below it. An LCD's viewing angle indicates how far, in degrees, you can move from the
center of the display before the image quality deteriorates or decline to unacceptable levels.
A wider viewing angle indicates more freedom to view the monitor from the side or from
above or below the screen position.

Response Rate

The response rate indicates how fast the monitor's pixels can change colors. Faster is better
because it reduces the ghosting effect when an image moves, leaving a faint trial in such
applications as videos or games. Ghosting - An effect of slower response times that cause
blurring or shadow of images on an LCD monitor, it's also known as latency. The effect is
caused by voltage temporarily leaking from energized elements to neighboring, non-
energized elements on the display.

LCD Advantages:

No flicker:

There is no flicker on an LCD display because, while a CRT must be refreshed, the LCD has
a constant source of light over the whole screen. Once a pixel is on, it stays on until turned
off.

Perfect geometry:

LCD monitors provide geometrically perfect, distortion-free images which is a huge


advantage for advanced users such as artists and designers.

Power consumption:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The power required to run an LCD is about one-third of that required for a CRT with the
same screen area. In addition, the amount of heat generated by an LCD monitor is
considerably less than a CRT monitor, resulting in a lower load on air conditioning.

Low emissions
An LCD is essentially emission-free, while a CRT monitor can generate electric, magnetic
and even X-ray emissions due to the high-voltage power supply necessary to drive the CRT.
An LCD causes no electromagnetic interference.

Drawbacks of LCD:

 CRTs are capable of displaying multiple video resolutions, while LCD displays produce
crisp images only in their "native resolution". Thus resolution of LCD is not as good
as CRT

 LCD display panels had a limited viewing angle, thus reducing the number of people
who could conveniently view the same image, laptop screens being a prime example.

 A small percentage of LCD screens occasionally suffer from image persistence, which
is similar to screen burn on CRT and plasma displays.

Electroluminescent Display

Electroluminescence (EL) is an optical and electrical phenomenon where a material emits


light in response to an electric current passed through it. Electroluminescent Displays
(ELDs) are a type of display created by sandwiching a layer of electroluminescent material
such as GaAs between two layers of conductors. When current flows, the layer of material
emits radiation in the form of visible light. GaAs is a compound of two elements, Gallium
and Arsenic. It is an important semiconductor and is used to make devices such as infrared
light-emitting diodes, laser diodes and solar cells. EL displays provide very high image
resolution end excellent graphics capabilities. The disadvantage of this technology is its
cost.

Plasma Display or Gas-plasma Display

A plasma display is a type of flat panel display now commonly used for large TV displays
(typically above 32"). Many tiny cells located between two panels of glass hold an inert
mixture of noble gases (neon and xenon). The gas in the cells is electrically turned into
plasma which then excites phosphors to emit light. This technology resembles to that of
neon light bulb.

REVIEW QUESTIONS:

(1) What are two common types of Monitors, explain each briefly?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(2) How Flat Panel display is better than CRT display?


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(3) Explain the types of CRT monitors?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(4) How CRT monitors work, write its working principle?

______________________________________________________________________
______________________________________________________________________
___________________________________________________________________

(5) Define the following terms?


 Resolution

 Pixel

 Dot pitch

 Native Resolution

 Viewing Angle

(6) Write down the Abbreviation of following terms?

 CRT

 LCD

 EL

 CGA

 VGA

 EGA

 SVGA

 Pixel

 RGB
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

 TFT

Conclusion:

Name:____________________________________
Roll#:____________________________________
Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL #05
Objective:
To perform installation of MS Windows 7 operating system
Required Tools:
A desktop computer (PC) with CD-DVD ROM, Windows 7 CD or DVD
Procedure:
Warning: Backup any data before you begin!
Minimum Hardware requirements of Windows 7:
This is minimum hardware that you need to have on your computer and should be checked
inadvance of installing.
• 1 GHz CPU with 32 bits or 64 bits.
• 1 GB Ram for 32 bits or 2 GB Ram for 64 bits.
• 16 GB empty space hard disk for 32 bits or 20 GB for 64 bits.
• DVD drives (if installing from a DVD).

Step – 1: Turn your computer on then


press Del or F2 key (depend on your
computer’s mainboard) to enter the
system BIOS.

Step – 2: Go to Boot menu and choose


Boot from CD/DVD.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Step – 3: Press F10 to save the


configuration and exit BIOS then reset your
computer.

Step – 4: Insert Windows 7 DVD into your


DVD drive then start up your computer,
Windows 7will be loading files.

Step – 5: Select these parts: Language to


Install, Time and currency format,
keyboard orinput method. Then click Next.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Step – 6: Choose the Windows 7 version


that you want to install in 'Select the
operatingsystem you want to install'. Here
we choose Windows 7 Ultimate then click
next (dependingon your Windows DVD, this
step is an option).

Step – 7: Click 'I accept the license terms'


in 'Please read the license' then click Next.

Step – 8: Choose 'Upgrade' in 'Which type


of installation do you want' if you want to
upgrade from an older Windows version to
Windows 7, 'Custom (advance)' if you want
to install a new version of Windows.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Step – 9: Choose Partition for installation, if


your computer has only one hard disk, it will
beeasy for selection, but if it has some
Partition, you will have to consider which
Partition tochoose.

Step – 10: Wait for Installing Windows to


progress. Your computer might be restarted
during the process

Step – 11: Type user account and computer


name, and then click Next.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Step – 12: Type a password for your account,


you can also Type a password hint to help in
case of forgetting the password in the future,
and then click Next.

Step – 13: Type in activation code or key for


your license in Product key box if you
haveany. Click Next.

Step – 14: Choose how to protect your


Windows. Here we choose Use recommended
settings.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Step – 15: Set up your Time zone then


click Next.

Step – 16: Choose an option from 3


choices: Public Network, Work Network,
Home Network.Choose Home Network for
using Internet at home.

Review Questions / Exercise:

Q #1:The windows 7 setup asks for the user name during the installation. Suppose
you want to add more users who will use the computer. How you will do
that?

_________________________________________________________________________

_________________________________________________________________________

_______________________________________________________
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Q #2: I want to install Windows 7 but when I inserted the CD and restarted the
PC, setup did not start. What could be the possible problems?

_________________________________________________________________________

_________________________________________________________________________

_______________________________________________________

Q #3: What is meant by case-sensitive password?

_________________________________________________________________________

_________________________________________________________________________

_______________________________________________________

Conclusion:

Name:____________________________________
Roll#:____________________________________
Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 06

Objective: Getting Familiar with Microsoft Word 2007 for Windows


Microsoft Word is a word processing software
package. You can use it to type letters,
reports, and other documents.

This lesson will introduce you to the Word


window. You use this window to interact with
Word. To begin this lesson, open Microsoft
Word 2007. The Microsoft Word window
appears and your screen looks similar to the
one shown here.

The Microsoft Office Button

In the upper-left corner of the Word 2007 window is the Microsoft Office
button. When you click the button, a menu appears. You can use the
menu to create a new file, open an existing file, save a file, and perform many
other tasks.

The Quick Access Toolbar

Next to the Microsoft Office button is the Quick Access toolbar. The
Quick Access toolbar provides you with access to commands you
frequently use. By default Save, Undo, and Redo appear on the Quick Access toolbar. You
can use Save to save your file, Undo to rollback an action you have taken, and Redo to
reapply an action you have rolled back.

The Title Bar

Next to the Quick Access


toolbar is the Title bar. The
Title bar displays the title of the document on which you are currently working. Word names
the first new document you open Document1. As you open additional new documents, Word
names them sequentially. When you save your document, you assign the document a new
name.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The Ribbon

You use commands to tell Microsoft Word what to do. In Microsoft Word 2007, you use the
Ribbon to issue commands. The Ribbon is located near the top of the screen, below the
Quick Access toolbar. At the top of the Ribbon are several tabs; clicking a tab displays
several related command groups. Within each group are related command buttons. You click
buttons to issue commands or to access menus and dialog boxes. You may also find a dialog
box launcher in the bottom-right corner of a group. Clicking the dialog box launcher gives
you access to additional commands via a dialog box.

The Ruler

The ruler is found below the Ribbon.

1. Click
the
View tab to choose it.
2. Click the check box next to Ruler in the Show/Hide group. The ruler appears below
the Ribbon.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The Text Area Just below the ruler is a large area called the text area. You type your
document in the text area.

The Status Bar


The Status bar appears at the very bottom of your window and provides such information as
the current page and the number of words in your document. You can change what displays
on the Status bar by right-clicking on the Status bar and selecting the options you want
from the Customize Status Bar menu. You click a menu item to select it. You click it again to
deselect it. A check mark next to an item means it is selected.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

�Understanding Document Views

In Word 2007, you can display your document in one of five views: Draft, Web Layout, Print
Layout, Full Screen Reading, or Online Layout.

Draft View
Draft view is the most frequently used view. You use Draft view to quickly edit your
document.
Web Layout
Web Layout view enables you to see your document as it would appear in a browser
such as Internet Explorer.
Print Layout
The Print Layout view shows the document as it will look when it is printed.
Reading Layout
Reading Layout view formats your screen to make reading your document more
comfortable.
Outline View
Outline view displays the document in outline form. You can display headings without
the text. If you move a heading, the accompanying text moves with it.

You should use Draft view for these lessons. Before moving ahead, make sure you are in
Draft view:

1. Click the View tab.


2. Click Draft in the Document Views group. When the Draft option is selected it
appears in a contrasting color.

Understanding Nonprinting Characters

Certain characters, called nonprinting characters, do not print and will not appear in your printed
document but do affect your document layout. You can elect to see these characters on the screen
as you type or you can elect to have them remain invisible. For these lessons, opt to see them
onscreen. This table describes most of them:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Character Denotes

A tab 

   . A space

   ¶ The end of a paragraph

Hidden text

To view nonprinting characters:

1. Choose the Home tab.


2. Click the Show/Hide button in the Paragraph group . The Show/Hide button
appears in a contrasting color, when it is selected.
Create Sample Data and Select Text
If you type =rand() in your Word document and then press Enter, Word creates three
paragraphs. You can use these paragraphs to practice what you learn. Throughout these
lessons, you will be asked to select text.
EXERCISE 1
Create Sample Data
1. Type =rand().
2. Press Enter. Three paragraphs appear in your document.
Start a New Paragraph
When you type in Microsoft Word, you do not need to press a key to move to a new line. To
start a new paragraph, press the Enter key. 
Exit Word
You have completed Lesson One. Typically, you save your work before exiting.
EXERCISE

Close and Save—Windows XP


1. Click the Microsoft Office button. A menu appears.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

2. Click Exit Word, which is in the bottom-right corner.


3. You will be prompted: "Do you want to save changes to Document1?" To save your
changes, click Yes. Otherwise, click No. If you click Yes, the Save As dialog box
appears.
4. Specify the correct folder in the Save In box.
5. Name your file by typing Lesson One.doc in the File Name field.
6. Click Save. Word saves your file.

Microsoft Word 2007 Basic Features


Bold, Italicize, and Underline
EXERCISE 3ctr+shft+F
Type the following exactly as shown. Remember, pressing the Enter key starts a new
paragraph. Press the Enter key at the end of each of the following lines to start a new
paragraph.

Alternate Method—
Italicize with the
Ribbon
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

All Three with the Dialog Box Launcher


1. On the line that begins with
"Launcher," select the words "All
three."
2. Choose the Home tab.
3. Click the dialog box launcher in the Font group. The Font dialog box appears.
4. In the Font Style box, click Bold Italic.
Note: You can see the effect of your
selection in the preview window. To turn off
the Bold Italic, click Regular.
5. In the Underline box, click to open the pull-
down menu. Click the type of underline you
want to use.
Note: To remove an underline, select None
from the pull-down menu.
6. Click OK to close the dialog box.
7. Click anywhere in the text area to remove
the highlighting.

Alternate Method—All Three with the Ribbon


1. On the line that begins with "Ribbon," select
the words "All three."
2. Choose the Home tab.
3. Click the Bold button in the Font group.
4. Click the Italic button in the Font group.
5. Click the Underline button in the Font group.
6. Click anywhere in the text area to remove the highlighting.

Alternate Method—All Three with Keys


1. On the line that begins with "Keys," select the words "All three."
2. Press Ctrl+b (bold).
3. Press Ctrl+i (italicize).
4. Press Ctrl+u (underline).
Note: You can remove formatting by highlighting the text and pressing
Ctrl+spacebar.
5. Click anywhere in the text area to remove the highlighting.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

EXERCISE 4

Save a File—Windows Vista:

1. Click the Microsoft Office button. A menu appears.


2. Click Save. The Save As dialog box appears, if you are saving your document for the
first time.

3. Use the Address bar to locate the folder  in which you want to save your file.
4. Name your file by typing Lesson Two.docx in the File Name box.
5. Click Save.
6. Click the Microsoft Office button. A menu appears.
7. Click Exit Word, which is located in the bottom-right corner of the window. Word
closes.

More Basic Features


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

The features in Word 2007 can make your work easier, make your documents more
attractive, and/or enable you to work more efficiently. This Microsoft Word lesson teaches
you how to open a file, cut, copy, paste, use AutoText, use spell check, use Find and
Replace, and change fonts. All of these features either make your work easier or make your
document more attractive.
Open a File
When you do not have time to complete your work or when you finish your work, you can
save and close your file. After saving a file, you can later open it to revise or finish it.  You
learned how to save a file in Lesson 2. In the exercise that follows, you learn how to open
the file you saved.
EXERCIS Cut and Paste

1. Type the following:


I want to move. I am content where I am.
2. Select "I want to move. "
3. Choose the Home tab.
4. Click the Cut button in the Clipboard group. Word cuts the text you selected and
places it on the Clipboard. Your text should now read:
"I am content where I am."
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Paste with the Ribbon

Copy and Paste


EXERCISE 3
Copy with the Ribbon

1. Type the following:


You will want to copy me. One of me is all you need.
2. Select "You will want to copy me."
3. Choose the Home tab.
4. Click the Copy button in the Clipboard group. Word copies the data you selected
to the Clipboard.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Paste with the Ribbon

Use Spell Check


Word checks your spelling and grammar as you type. Spelling errors display with a red
wavy line under the word. Grammar errors display with a green wavy line under the error.
In Word 2007, you can use the Review tab's Spelling & Grammar button to initiate a spell
and grammar check of your document.
EXERCISE 6
Use Spell Check

1. Type the following exactly as shown. Include all errors.


Open thr door for Mayrala. She is a teacher from the town of Ridgemont.
2. Select: "Open thr door for Mayrala. She is a teacher from the town of Ridgemont."
3. Choose the Review tab.
4. Click the Spelling & Grammar button. The Spelling and Grammar dialog box appears.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

6. "The" is misspelled, so it is highlighted on the screen and noted in the Not in


Dictionary box. Word suggests correct spellings. These suggestions are found in the
Suggestions box.
7. Click "the" in the Suggestions box.
8. Click Change.
Note: You can also press F7 to initiate a spelling and grammar check. If you don't have
anything selected, Word checks the entire document.
Find and Replace
After you find the word or phrase you are searching for, you can replace it with new text by
executing the Replace command.
EXERCISE 7
Use Find with the Ribbon

1. Type the following:


Monica is from Easton. She lives on the east side of town. Her daughter
attends Eastern High School.
2. Select: "Monica is from Easton. She lives on the east side of town. Her daughter
attends Eastern High School."
3. Choose the Home tab.
4. Click Find in the Editing group. A menu appears.

5. Click the Find option on the menu. The Find and Replace dialog box appears.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

6. Type east in the Find What field.


7. Click Find Next.
Note that the "East" in Easton is highlighted.
8. Click Find Next again.
Note that "east" is highlighted.
9. Click Find Next again.
Note that the "East" in Eastern is highlighted.
10. Click Find Next. The following message should appear: "Word has finished searching
the selection. Do you want to search the remainder of the document?"
11. Click No.
12. Click Cancel.

Use Replace with the Ribbon

1. Select "Monica is from Easton. She lives on the east side of town. Her daughter
attends Eastern High School."
2. Choose the Home tab.
3. Click Replace in the Editing group. The Find and Replace dialog box appears.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

4. Type east in the Find What box.


5. Type west in the Replace With box.
6. Click Find Next. The East in Easton is highlighted.
7. Click Replace. Word replaces the "East" in "Easton" with "West" and then highlights
the word "east."
8. Click Replace. Word replaces the word "east" with "west" and then highlights the
word "Eastern."
9. Click Close. Do not replace the "East" in "Eastern" with "West."
10. Your text should now read,
"Monica is from Weston. She lives on the west side of town. Her daughter attends
Eastern High School."
Change the Font Size
EXERCISE 8 Change the Font size

1. Type the following:


I can be any size you want me to be.
2. Select "I can be any size you want me to be."
3. Choose the Home tab.
4. In the Font group, click the down arrow next to the Font Size box. A menu of font
sizes appears.
5. Move your cursor over the menu of font sizes. As you do, Word 2007 provides a live
preview of the effect of applying each font size.
6. Click 36 to select it as your font size.
Note: If you know the font size you want, you can type it in the Font Size field.
Alternate Method—Change the Font Size with Grow Font and Shrink Font

You can also change the size of your font by clicking the Grow Font and Shrink Font buttons.
Selecting text and then clicking the Grow Font button makes your font larger. Selecting text
and then clicking the Shrink Font button makes your font smaller.

1. Type the following:


Grow Shrink
2. Select "Grow"
3. Choose the Home tab.
4. Click the Grow Font button several times. You font becomes larger.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

5. Select Shrink.
6. Click the Shrink Font button several times. Your font becomes smaller.
Change the Font
In Microsoft Word, you can change the font (the "family" of type you use for your text). This
feature is illustrated in the following exercise:
EXERCISE 9
Change the Font with the Ribbon

Alternate Method—Change the Font with the Mini Toolbar

Formatting Paragraphs
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Open a Blank Document


To begin a new Word project, you start by opening a new document.To begin this lesson,
open a blank document in Microsoft Word.
EXERCISE Open a Blank Document
1. Open Word 2007.
2. Click the Microsoft Office button. A menu appears.
3. Click New. The New Document dialog box appears.
4. Click Blank Document.
5. Click Create. A new blank document opens.

EXERCISE
Create the Paragraphs
1. Type Sample Paragraph.
2. Press Enter.
3. Type =rand(1) to create a paragraph.
4. Press Enter.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Align Paragraphs
The following exercises demonstrate how to justify text.
Right-align
1. Select the paragraphs you created.
2. Choose the Home tab.
3. Click the Align-right button in the Paragraph group. Word right-aligns your
paragraphs.
Left-align
1. Select the paragraphs you created.
2. Choose the Home tab.
3. Click the Align-left button in the Paragraph group. Word left-aligns your
paragraph.
Center
1. Selected the paragraphs you created.
2. Choose the Home tab.
3. Click the Center button in the Paragraph group. Word centers your paragraph.
Justify
1. Select the paragraphs you created.
2. Choose the Home tab.
3. Click the Justify button in the Paragraph group. Word justifies your paragraph.
Adding Bullets and Numbers, Undoing and Redoing, Setting Page Layouts and
Printing Documents
Add Bullets and Numbers
In Microsoft Word, you can easily create bulleted or numbered lists of items. Several
bulleting and numbering styles are available, as shown in the examples. You can select the
one you wish to use.
Bullets
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Numbers

EXERCISE Set the Page Size


1. size. Choose the Page Layout tab.
2. Click Size in the Page Setup group. A menu appears.
3. Click Letter 8.5 x 11in. Word sets your page
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Set the Margins


Margins define the amount of white space that appears at the top, bottom, left, and right
edges of your document. The Margin option in the Page Setup group of the Page Layout tab
provides several standard margin sizes from which you can choose.

EXERCISE 5
Set the Margins
1. Choose the Page Layout tab.
2. Click Margins in the Page Setup group. A menu appears.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Click Moderate. Word sets your margins to the Moderate settings.

Conclusion:

Name:____________________________________
Roll#:____________________________________
Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 07

Objective: Introduction to C++ and its basics.


1.1 A typical C++ environment
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

1.2 Program No 1
#include <iostream.h>
main ( )
{
cout << “ Welcome to Quaid e Awam University “;
}
1.2.1 Out Put of Program No 1

1.3 Declare Variables


Declare 3 Integer Type & 3 float type Variables.
INT Float

1.4 Program No 2 Example Of Assignment Operator


#include <iostream>
using namespace std;
int main ()
{ int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a :";
cout << a;
cout << " b :";
cout << b;
return 0;
}
1.4.1 Out Put of Program No 2

1.5 Arithmetic Operators

Write Arithmetic Operators


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

1.5.1 Precedence
Write precedence of the arithmetic operators

1.6 Program No 3
Write a program in C++ that display following output
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
##############################
##############################
##############################
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

1.6.1 Code of Program No 3

1.7 Program No 4
Write a programin C++ which prints following output,

$**********************************************$
$**********************************************$
$* Welcome to Quaid e awam University of *$
$* Engineering, Sciences & Technology *$
$* Nawabshah. *$
$**********************************************$
$**********************************************$
1.7.1 Code of Program No 4

1.8 Program No 5

Write a program in C++, which takes radius from the user and calculate the area of sphere
i.e Area=4pr2
(Hint p = 3.1416 Area = 4 * 3.1416 * r * r
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

1.8.1 Code of Program No 5

1.8.2 Out Put of Program No 2

Name: _____
Roll #: _____
Date: _____ Subject Teacher / LAB Engineer
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 08
Nested Looping Constructs in C++

Objective: To write C++ program to implement Nested Looping Constructs.

Hardware: Computer System

Software: Turbo C++/any other.

Operational Objectives:

At the completion of the practical:

The student should be aware the looping constructs and nested loops.

Theory:

Nested for Loop: The placing of one loop inside the body of another loop is called
nesting.  When you "nest" two loops, the outer loop takes control of the number of
complete repetitions of the inner loop.  While all types of loops may be nested, the most
commonly nested loops are for loops.

Syntax:

The syntax for a nested for loop statement in C++ is as follows:

for ( init; condition; increment )

for ( init; condition; increment )

statement(s);

statement(s); // you can put more statetments.

}
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Example Program # 01

#include <iostream.h>

int main()

int rows, columns;

char theChar;

cout << "How many rows? ";

cin >> rows;

cout << "How many columns? ";

cin >> columns;

cout << "What character? ";

cin >> theChar;

for (int i = 0; i<rows; i++)

for (int j = 0; j<columns; j++)

cout << theChar;

cout << "\n";

return 0;

Output:

How many rows? 4

How many columns? 12

What character? x

xxxxxxxxxxxx
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

xxxxxxxxxxxx

xxxxxxxxxxxx

xxxxxxxxxxxx

Example Program 2: Write a program that displays following output?

*********
********
*******
******
*****
****
***
**
*

#include <iostream>

using std::cout;

using std::endl;

int main()

//declare variables

int x = 0;
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

int y = 0;

//execute loop

for (int x = 1; x < 10; x = x + 1)

{   //execute nested loop

for (int y = 9; y > 0; y = y - 1)

cout << "*";

//end for

cout << endl;

    } //end for

return 0;

}   //end of main function

EXERCISE:

Q1: Write the syntax of Nested While Loop and Nested Do While Loop?

Ans:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Q2: Understand and Execute the following Programs and attach snapshots of
there output at the end of the practical.

Lab Task # 01:

Lab Task # 02:


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Lab Task # 03: Write the source code for printing the following Pattern?

**

***

****

*****

******

*******

********
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Name:____________________________________

Roll#:____________________________________

Date:_____________________________________

Remarks: Subject Teacher/Lab Engineer


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 09

Decision Making Structures in C++

Object: To perform Decision making (if & if-else) structures using C++.

Hardware: Computer System

Software: Turbo C++

Operational Objectives:
At the completion of the practical:
1. The student should be aware with use of if statement.
2. The Student should be aware to use If-else Statement.
.

Theory:

There are three types of Decision Making statements.


1. if statement
2. if- else statement
3. Switch-case statement.

If Statement: The ability to control the flow of your program, letting it make decisions on
what code to execute, is valuable to the programmer. The if statement allows you to control
if a program enters a section of code or not based on whether a given condition is true or
false. One of the important functions of the if statement is that it allows the program to
select an action based upon the user's input. For example, by using an if statement to check
a user entered password, your program can decide whether a user is allowed access to the
program.

Syntax:

if(Condition)

Statement;
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Example Program 01:

#include <iostream>
using namespace std;
  
int main()
{
    
 cout << "Enter a number: ";
 int nX;
 cin >> nX;
  
  if (nX > 10)
  cout << nX << "is greater than 10" << endl;
  else
  cout << nX << "is not greater than 10" << endl;
  
  return 0;
}

OUTPUT:

Else If Statement:
Another use of else is when there are multiple conditional statements that may all evaluate
to true, yet you want only one if statement's body to execute. You can use an "else if"
statement following an if statement and its body; that way, if the first statement is true, the
"else if" will be ignored, but if the if statement is false, it will then check the condition for
the else if statement. If the if statement was true the else statement will not be checked. It
is possible to use numerous else if statements to ensure that only one block of code is
executed.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

if ( <condition> )
{
// Execute these statements if <condition> is TRUE
}

else if ( <another condition> )

{
// Execute these statements if <another condition> is TRUE and
// <condition> is FALSE

EXAMPLE PROGRAM 02:

#include <iostream>
using namespace std;
int main()
{
    cout << "Enter a number: ";
    int nX;
    cin >> nX;
  
    if (nX > 10)
        {
       
        cout << "You entered " << nX << endl;
        cout << nX << "is greater than 10" << endl;
        }
    else
        {
        // both statements will be executed if nX <= 10
        cout << "You entered " << nX << endl;
        cout << nX << "is not greater than 10" << endl;
        }
  
    return 0;
}

OUTPUT:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Example Program 03:


Using nested if else structure.

int main()
{
    cout << "Enter a number: ";
    int nX;
    cin >> nX;
  
    if (nX > 10)
        cout << nX << "is greater than 10" << endl;
    else if (nX < 5)
        cout << nX << "is less than 5" << endl;
    else
        cout << nX << "is between 5 and 10" << endl;
  
    return 0; }

Output:

EXERCISE:

Q:1-> Write a C++ Program to check number provided by the user and display
either number is even or odd.

Q:2-> Write a program which ask user to enter three numbers and determine
which number is greater.

Q:3-> execute the source codes given above correct errors and write outputs on
manual.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 10
Decision Making Structures in C++

Object: To perform decision making (Switch, case) statement using C++.

Hardware: Computer System

Software: Turbo C++

Operational Objectives:
At the completion of the practical:
1. The student should be aware with use of switch statement.
2. The Student should be aware to use case default keywords.

Theory:
Switch Statement:
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:

Syntax:

switch (expression)
{
case valueOne:
statement;
break;

case valueTwo:
statement;
break;
....
case valueN:
statement;
break;

default:

statement;
}
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

// Demonstrates switch statement

#include <iostream.h>

int main()
{
unsigned short int number;
cout << "Enter a number between 1 and 5: ";
cin >> number;
switch (number)
{
case 0: cout << "Too small, sorry!";
break;
case 5: cout << "Good job!\n"; // fall through
case 4: cout << "Nice Pick!\n"; // fall through
case 3: cout << "Excellent!\n"; // fall through
case 2: cout << "Masterful!\n"; // fall through
case 1: cout << "Incredible!\n";
break;
default: cout << "Too large!\n";
break;
}
cout << "\n\n";
return 0;
}

Output:

Enter a number between 1 and 5: 3


Excellent!
Masterful!
Incredible!

Enter a number between 1 and 5: 8


Too large!
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

GAME PROGRAM:

#include <iostream.h>

void playgame()
{
cout << "Play game called";
}

void loadgame()
{
cout << "Load game called";
}

void playmultiplayer()
{
cout << "Play multiplayer game called";
}

int main()
{
int input;

cout<<"1. Play game\n";


cout<<"2. Load game\n";
cout<<"3. Play multiplayer\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
switch ( input )
{
case 1: // Note the colon, not a semicolon
playgame();
break;

case 2: // Note the colon, not a semicolon


loadgame();
break;

case 3: // Note the colon, not a semicolon


playmultiplayer();
break;

case 4: // Note the colon, not a semicolon


cout<<"Thank you for playing!\n";
break;

default: // Note the colon, not a semicolon


cout<<"Error, bad input, quitting\n";
break;
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

}
cin.get();
}

EXERCISE:

Q:1-> Write a C++ Program to check number provided by the user and display
either number is even or odd.

Q:2-> Write a program which ask user to enter three numbers and determine
which number is greater.

Q:3-> execute the source codes given above correct errors and write outputs on
manual.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL# 11

Strings & String Operators in C++

Object: To be familiar with the use of Strings and string operators using C++.

Hardware: Computer System

Software: Turbo C++

Operational Objectives:
At the completion of the practical:
1. The student should be aware to implement operators in strings.

Theory:

String: The C style character string originated within the C language and continues to be
supported within C++. This string is actually a one-dimensional array of characters which is
terminated by a null character '\0'. Thus a null-terminated string contains the characters
that comprise the string followed by a null.

The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the string
is one more than the number of characters in the word "Hello."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as follows:

char greeting[] = "Hello";

Following is the memory presentation of above defined string in C/C++:

Actually, you do not place the null character at the end of a string constant. The C++ compiler
automatically places the '\0' at the end of the string when it initializes the array. Let us try to print
above mentioned string:

#include <iostream>
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

using namespace std;

int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

cout << "Greeting message: ";


cout << greeting << endl;

return 0;
}

When the above code is compiled and executed, it produces result something as follows:

Greeting message: Hello

  String Operators

 String operators are overloaded operators.


 The comparison between string objects is based on what is called a pair wise
lexicographical comparison of their characters. Two strings are equal if they have the
same number of characters and their respective character values are the
same. Otherwise, they are unequal.
 A lexicographical comparison between strings compares them character by character
until:

1. It finds two corresponding characters unequal, and the result of their comparison is
taken as the result of the comparison between the strings.
2. It finds no inequalities, but one string has more characters than the other, and the
shorter string is considered less than the longer string.
3. It finds no inequalities and finds that the strings have the same number of
characters, and so the strings are equal.
 The following table is a list of string operators.

Operator Brief Description


operator!= Tests if the string object on the left side of the operator is not
str1 != str2 equal to the string object on the right side.
operator== Tests if the string object on the left side of the operator is equal to
str1 == str2 the string object on the right side.
operator< Tests if the string object on the left side of the operator is less
str1 < str2 than to the string object on the right side.
operator<<
same as in
A template function that inserts a string into the output stream.
iostream, e.g.
cout<<
operator<= Tests if the string object on the left side of the operator is less
str1 <= str2 than or equal to the string object on the right side.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

operator> Tests if the string object on the left side of the operator is greater
str1 > str2 than to the string object on the right side.
operator>= Tests if the string object on the left side of the operator is greater
str1 >= str2 than or equal to the string object on the right side.
 
Example Program: By using == and != operators
#include <string>
#include <iostream>
using namespace std;
 
int main( )
{
   
    string str1("testingone");
    string str2("testingtwo");
    cout<<"str1 string is = "<<str1<<endl;
    cout<<"str2 string is = "<<str2<<endl;
   
    char *str3 = "testingone";
    cout<<"C-style str3 string is = "<<str3<<endl;
   
    cout<<"\nOperation: (str1 != str2)"<<endl;
    if(str1 != str2)
        cout<<"str1 & str2 are not equal."<<endl;
    else
        cout<<"str1 & str2 are equal."<<endl;
   
    cout<<"\nOperation: (str3 != str2)"<<endl;
    if(str3 != str2)
        cout<<"str3 & str2 are not equal."<<endl;
    else
        cout<<"str3 & str2 are equal."<<endl;
   
    cout<<"\nOperation: (str1 != str3)"<<endl;
    if(str1 != str3)
        cout<<"str1 & str3 are not equal."<<endl;
    else
        cout<<"str1 & str3 are equal."<<endl;
   
    cout<<"\nOperation: (str1 == str2)"<<endl;
    if(str1 == str2)
        cout<<"str1 & str2 are equal."<<endl;
    else
        cout<<"str1 & str2 are not equal."<<endl;
   
    cout<<"\nOperation: (str3 == str2)"<<endl;
    if(str3 == str2)
        cout<<"str3 & str2 are equal."<<endl;
    else
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

        cout<<"str3 & str2 are not equal."<<endl;


   
    cout<<"\nOperation: (str1 == str3)"<<endl;
    if(str1 == str3)
        cout<<"str1 & str3 are equal."<<endl;
    else
        cout<<"str1 & str3 are not equal."<<endl;
    return 0;
}

Output:

Exercise
Q:1 Construct C++ code by using less than or equal to (<=) and greater than or equal to
(>=) operators in strings?

Q:2 Write C++ code by using greater than (>) and less than (<) operators in strings?

Q:3 Write C++ code that declares four string variables. Use concatenation (+) operator and
add these string in the given format:
String1 + string2
String1 + string3
String2+ string3
String3 + string4
String4 + string1
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 12

String Functions in C++

Object: To perform string functions using C++.

Hardware: Computer System

Software: Turbo C++

Operational Objectives:
At the completion of the practical:
1. The student should be aware with string functions.
2. Student should know why we use string functions.

Theory:

C++ supports a wide range of functions that manipulate null-terminated strings:

S.N. Function & Purpose

strcpy(s1, s2);
1
Copies string s2 into string s1.

strcat(s1, s2);
2
Concatenates string s2 onto the end of string s1.

strlen(s1);
3
Returns the length of string s1.

strcmp(s1, s2);
4
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

strchr(s1, ch);
5
Returns a pointer to the first occurrence of character ch in string s1.

strstr(s1, s2);
6
Returns a pointer to the first occurrence of string s2 in string s1.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

Example 1:
#include <iostream.h>
#include <conio.h>
#include <string.h>

const int MAXLENGTH = 500; // define maximum string length

void main()
{
char FirstName[20] = "Donald";
char LastName[20] = "Duck";
char FullName[MAXLENGTH+1];

// function strlen gives the length of a string


if (strlen(FirstName) + strlen(LastName) > MAXLENGTH)
{
cout << "\nName too long";
return 1;
}
strcpy (FullName, FirstName); // copy string
strcat (FullName, " "); // add space to end of string
strcat (FullName, LastName); // add last name
cout << "\nThe full name is " << FullName;

// compare strings. The function stricmp returns 0 if the strings are equal
if (stricmp(FirstName,LastName) == 0)
{
cout << "\nFirst and last name are identical";
}
getch();
}

Exercise:
Q1: Attach output of the following program?

#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;

// copy str1 into str3


strcpy( str3, str1);
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

cout << "strcpy( str3, str1) : " << str3 << endl;

// concatenates str1 and str2


strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;

// total lenghth of str1 after concatenation


len = strlen(str1);
cout << "strlen(str1) : " << len << endl;

return 0;
}

Q:2 Write the function of the following statements?


(i) strcpy( str3, str1);
(ii) len = strlen(str3);
(iii) strcat( str3, str2);

Q:3 Write C++ code that declares five string variables and performs following string
functions
(i) strcpy
(ii) strcat
(iii) strlen
(iv) strchr

Q:3
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

PRACTICAL # 13

Pointers and References in C++

Object: To be familiar with the use of pointers and references using C++.

Hardware: Computer System

Software: Turbo C++

Operational Objectives:
At the completion of the practical:

1. The student should be aware with the use of Pointers and References.
2. The student should be aware with the use of Arrays of Pointers.
3. The student should be aware with the use of Pointer to Pointer.

Theory:
Pointer : A pointer is a special kind of variable in C and C++ that holds the address of
another variable.

Explain pointers in c++


Pointer is a user defined data type which creates special types of variables which can hold
the address of primitive data type like char, int, float, double or user defined data type like
function, pointer etc. or derived data type like array, structure, union, enum.
Examples:

int *ptr;
int (*ptr)();
int (*ptr)[2];

C++ Null Pointer: The NULL pointer is a constant with a value of zero defined in several
standard libraries, including iostream. Consider the following program:

#include <iostream.h>
int main ()
{
int *ptr = NULL;

cout << "The value of ptr is " << ptr ;

return 0;
}
Pointer Arithmetic: There are four arithmetic operators that can be used on pointers: +
+, --, +, and –.

Incrementing a Pointer:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

We prefer using a pointer in our program instead of an array because the variable pointer can be
incremented, unlike the array name which cannot be incremented because it is a constant pointer. The
following program increments the variable pointer to access each succeeding element of the array:
#include <iostream.h>
const int MAX = 3;

int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;

// let us have array address in


pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i <<
"] =";
cout << ptr << endl;

cout << "Value of var[" << i


<< "] = ";
cout << *ptr << endl;

// point to the next location


ptr++;
}
return 0;
}

When the above code is compiled and executed, it produces result something as follows:
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200

Decrementing a Pointer:

The same considerations apply to decrementing a pointer, which decreases its value by the
number of bytes of its data type as shown below:

#include <iostream.h>

const int MAX = 3;


QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;

// let us have address of the last element in pointer.


ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;

cout << "Value of var[" << i << "] = ";


cout << *ptr << endl;

// point to the previous location


ptr--;
}
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
Address of var[3] = 0xbfdb70f8
Value of var[3] = 200
Address of var[2] = 0xbfdb70f4
Value of var[2] = 100
Address of var[1] = 0xbfdb70f0
Value of var[1] = 10

Arrays of pointers:
Before we understand the concept of arrays of pointers, let us consider the following
example which makes use of an array of 3 integers:

#include <iostream>

using namespace std;


const int MAX = 3;

int main ()
{
int var[MAX] = {10, 100, 200};

for (int i = 0; i < MAX; i++)


{
cout << "Value of var[" << i << "] = ";
cout << var[i] << endl;
}
return 0;
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

}
When the above code is compiled and executed, it produces following result:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
There may be a situation when we want to maintain an array which can store pointers to an
int or char or any other data type available. Following is the declaration of an array of
pointers to an integer:
int *ptr[MAX];
You can also use an array of pointers to character to store a list of strings as follows:
#include <iostream>

using namespace std;


const int MAX = 4;

int main ()
{
char *names[MAX] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};

for (int i = 0; i < MAX; i++)


{
cout << "Value of names[" << i << "] = ";
cout << names[i] << endl;
}
return 0;
}
When the above code is compiled and executed, it produces following result:
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

Pointer to Pointer:

is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the


address of a variable. When we define a pointer to a pointer, the first pointer contains the
address of the second pointer, which points to the location that contains the actual value as
shown below.
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name. For example, following is the declaration to declare a
pointer to a pointer of type int:

int **var;

When a target value is indirectly pointed to by a pointer to a pointer, accessing that value
requires that the asterisk operator be applied twice, as is shown below in the example:

#include <iostream>

using namespace std;

int main ()
{
int var;
int *ptr;
int **pptr;

var = 3000;

// take the address of var


ptr = &var;

// take the address of ptr using address of operator &


pptr = &ptr;

// take the value using pptr


cout << "Value of var :" << var << endl;
cout << "Value available at *ptr :" << *ptr << endl;
cout << "Value available at **pptr :" << **pptr << endl;

return 0;
}

When the above code is compiled and executed, it produces following result:

Value of var :3000


Value available at *ptr :3000
Value available at **pptr :3000
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

A reference variable is an alias, that is, another name for an already existing variable. Once
a reference is initialized with a variable, either the variable name or the reference name
may be used to refer to the variable.

C++ References vs Pointers:

References are often confused with pointers but three major differences between references
and pointers are:

1. You cannot have NULL references. You must always be able to assume that a
reference is connected to a legitimate piece of storage.
2. Once a reference is initialized to an object, it cannot be changed to refer to another
object. Pointers can be pointed to another object at any time.
3. A reference must be initialized when it is created. Pointers can be initialized at any
time.

Creating References in C++:

#include <iostream>

using namespace std;

int main ()
{
// declare simple variables
int i;
double d;

// declare reference variables


int& r = i;
double& s = d;

i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;

d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;

return 0;
}
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)

When the above code is compiled together and executed, it produces following result:

Value of i:5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7

Exercise

Q1: Write C++ code by using Pointer Comparision Operators which creates array of three
values stored in variables and show the address of those variables?

Q2: Write C++ code that creates four variables of different data types and also give
reference of those variable to access them by there reference?

Q3:
(i) Explain the error?
char c = 'A';
double *p = &c;

(ii) Consider the following statements:


int *p;
int i;
int k;
i = 42;
k = i;
p = &i;

After these statements, which of the following statements will change the value of i to 75?

A. k = 75;
B. *k = 75;
C. p = 75;
D. *p = 75;
E. Two or more of the answers will change i to 75.

S-ar putea să vă placă și