Documente Academic
Documente Profesional
Documente Cultură
TABLE OF CONTENTS
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.
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)
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)
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)
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
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
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:
Ans:
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)
Ans:
Ans:
Ans:
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:_____________________________________
PRACTICAL #02
Objective:
Keyboard as an input device and various keyboard shortcuts.
THEORY:
Keyboard Basics
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.
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.
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.
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.
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.
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.
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 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.
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.
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?
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
(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:_____________________________________
PRACTICAL #03
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)
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:
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.
The below basic shortcut keys are a listing of shortcut keys that will work with almost all
IBM compatible computers and software programs.
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.
REVIEW QUESTIONS:
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
(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?
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
Conclusion:
Name:____________________________________
Roll#:____________________________________
Date:_____________________________________
PRACTICAL #04
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:
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
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.
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
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.
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)
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:
EL (Electroluminescent Display)
Plasma 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
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.
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:
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
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?
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
______________________________________________________________________
______________________________________________________________________
___________________________________________________________________
Pixel
Dot pitch
Native Resolution
Viewing Angle
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:_____________________________________
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).
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?
_________________________________________________________________________
_________________________________________________________________________
_______________________________________________________
_________________________________________________________________________
_________________________________________________________________________
_______________________________________________________
Conclusion:
Name:____________________________________
Roll#:____________________________________
Date:_____________________________________
PRACTICAL # 06
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.
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 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
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.
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:
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
Hidden text
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)
EXERCISE 4
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.
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
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)
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)
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.
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
Formatting Paragraphs
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF CHEMICAL ENGINEERING
Introduction to Computers & C++ Programming (2nd Year, 2nd Term)
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 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)
Conclusion:
Name:____________________________________
Roll#:____________________________________
Date:_____________________________________
PRACTICAL # 07
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.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.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)
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++
Operational Objectives:
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:
statement(s);
}
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()
char theChar;
return 0;
Output:
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
*********
********
*******
******
*****
****
***
**
*
#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
//end for
return 0;
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 # 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:_____________________________________
PRACTICAL # 09
Object: To perform Decision making (if & if-else) structures using 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:
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)
#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
}
{
// Execute these statements if <another condition> is TRUE and
// <condition> is FALSE
#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)
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++
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)
#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:
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;
}
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
Object: To be familiar with the use of Strings and string operators using 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."
If you follow the rule of array initialization then you can write the above statement as follows:
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)
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
String Operators
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> 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)
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
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:
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>
void main()
{
char FirstName[20] = "Donald";
char LastName[20] = "Duck";
char FullName[MAXLENGTH+1];
// 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>
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
cout << "strcpy( str3, str1) : " << str3 << endl;
return 0;
}
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
Object: To be familiar with the use of pointers and references using 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.
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;
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;
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>
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
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>
int main ()
{
int var[MAX] = {10, 100, 200};
}
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>
int main ()
{
char *names[MAX] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
Pointer to Pointer:
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>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
return 0;
}
When the above code is compiled and executed, it produces following result:
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.
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.
#include <iostream>
int main ()
{
// declare simple variables
int i;
double 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;
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.