- sharada vikas trust

advertisement

MODULE 1

UNIT 1

COMPUTER FUNDAMENTALS

Structure:

1.1

1.2

1.3

1.4

1.5

Introduction

History of Computers

Types of computers

Conclusion

Exercise

1.1 INTRODUCTION

We are living in the era of knowledge. The knowledge era is sensitized by the information and communication technology evolution. The knowledge era is arrived surpassing many developments and still people are in the process of developing this technology.

The history of computer goes back to 1000 BC when

‘ Antikythera mechanism’,

‘Astrolabe’, and ‘Abacus’ was invented and used for mathematical computation. Today we have computers that are portable with varied features and facilities for the user. In this context, it is necessary to know how the sophisticated system has pervaded the human life and has become part and parcel of our personal, professional and work life. The following paragraphs describe the development of computer technology from the view point of Stone Age to the present internet era.

1.2 HISTORY OF COMPUTERS

The history of computers starts out about 2000 years ago, at the birth of the abacus. Abacus is a wooden rack holding two horizontal wires with beads strung on them. When these beads are moved around, according to programming rules memorized by the user, all regular arithmetic

problems can be solved. Another important invention around the same time was the Astrolabe, used for navigation.

The abacus was in use centuries before the adoption of the written modern numeral system and is still widely used by merchants and clerks in China, Japan, Africa and elsewhere. The user of an abacus is called an abacist; he or she slides the beads of the abacus by hand. Sources, such as the ‘ Abhidharmakosa’

describe the knowledge and use of abacus in India. Around the 5th century, Indian clerks were already finding new ways of recording the contents of the Abacus. Hindu texts used the term shunya (means Zero) to indicate the empty column on the abacus.

FIGURE: ABACUS

Blaise Pascal is usually credited for building the first digital computer in 1642. It added numbers entered with dials and was made to help his father, a tax collector. In 1671,

Gottfried Wilhelm von around, multiply. Leibniz invented a special stepped gear mechanism for introducing the addend digits, and this is still being used. When Thomas of Colmar (A.K.A. Charles Xavier Thomas) created the first successful mechanical calculator that could add, subtract, multiply, and divide the prototypes made by Pascal and Leibniz become unpopular.

Series of very interesting developments in computers was started in Cambridge, England, by

Charles Babbage a mathematics professor. He assumed that long mathematical calculations, are really a series of predictable actions that were constantly repeated and hence can be automated. Having this in mind Babbage designed a machine called ‘difference engine’.

By 1822, the working model of Babbage machine was ready and demonstrated the working modality. While working with the difference engine, he developed new ideas and around 1833 he developed a general purpose, fully program-controlled, automatic mechanical digital computer named as Analytical Engine.

Movement in the development of automated computing led to the development of ‘punched cards’, which were first successfully used with computers in 1890 by Hermen Holreith and James

Powers, who worked for the US. Census Bureau. The device developed by them could read the information that had been punched into the cards automatically, without human intervention and eliminated the errors that may happen with the human intervention. This led to increased work flow, and, most importantly, stacks of punched cards were used as easily accessible memory of almost unlimited size. This device also facilitated to store data on different stacks of cards and accessed when needed

The advantages of the punched card system were grasped by the commercial companies and especially led to the development of improved punch-card using computers. The companies like

International Business Machines (IBM), Remington, Burroughs, and other corporations become the pioneer in this area. These computers used electromechanical devices in which electrical power provided mechanical motion like turning the wheels of an adding machine. The devices enabled the user to feed in a specified number of cards automatically, add, multiply, and sort, feed out cards with punched results etc. The device then used could process 50 - 220 cards per minute, each card holding about 80 decimal numbers (characters). They provided a means of

I/O, and memory storage on a huge scale. For more than 50 years after their first use, punched card machines did most of the world’s first business computing, and a considerable amount of the computing work in science.

The World War II made the people to realize the need and importance of computer especially for defense/military purpose. The need initiated the development of MARK I computer by IBM and Harvard Aiken. MARK I used the relays and electromagnetic components by replacing the mechanical components. The later developments in computers were categorized as computer generation viz 1 st generation, 2 nd Generation, 3 rd generation, 4 th

generation, and 5 th

generation computers.

An analog computer (spelled analogue in British English) is a form of computer that uses continuous physical quantities such as electrical, mechanical or hydraulic phenomena to model the problem being solved. The Norden bombsight was a highly sophisticated optical/mechanical analog computer used by the United States Army Air Force during World War II, the Korean War, and the Vietnam War to aid the pilot of a bomber aircraft in dropping bombs accurately.

Analog computers were used for the calculation of mathematical, physical and technical problems. They were especially capable of solving equations containing variables which are dependent on time or differential equations.

Among others, computer systems by Telefunken (1961-64) are installed. They draw awesome curves on a storage oscilloscope and ‘X’ ‘Y” plotter. They solve difficult problems quite quickly, but handing, programming (plug connections) and analysis is not easy.

In 1876, the differential analyser, a mechanical analog computer designed to solve differential equations by integration, using wheel-and-disc mechanisms to perform the integration was invented and were first built in the 1920s and 1930s. These mechanical analog computers were used during World War II in the gun directors and bomb sights.

Computer Engineering Associates was spun out of Caltech in 1950 to provide commercial services using the "Direct Analogy Electric Analog Computer" ("the largest and most impressive general-purpose analyzer facility for the solution of field problems") developed there by Gilbert

D. McCann, Charles H. Wilts, and Bart Locanthi. The MONIAC Computer, a hydraulic model was

built in the early 1950s and during 1960’s educational analog computer was made by Heath

Company, USA.

1.3 Types of Computers

Electronic analog computers

The similarity between linear mechanical components, such as springs, and electrical components, such as capacitors, inductors, and resistors is striking in terms of mathematics as they can be modeled using equations that are of essentially the same form. The following figure

Polish analog computer AKAT-1 is an example of an early electronic analog computer.

However, the difference between these systems is what makes analog computing useful.

If one considers a simple mass-spring system, constructing the physical system would require buying the springs and masses. This would be proceeded by attaching them to each other and an appropriate anchor, collecting test equipment with the appropriate input range, and finally, taking measurements. The electrical equivalent can be constructed with a few operational amplifiers (Op amps) and some passive linear components. All measurements can be taken directly with an oscilloscope. In the circuit, the (simulated)

'mass of the spring' can be changed by adjusting a potentiometer.

The electrical system is an analogy to the physical system, hence the name, but it is less expensive to construct, safer, and easier to modify. Also, an electronic circuit can typically operate at higher frequencies than the system being simulated. This allows the simulation to run faster than real time, for quicker results. The drawback of the mechanical-electrical analogy is that electronics are limited by the range over which the variables may vary. This is called dynamic range. They are also limited by noise levels.

Analog digital computer is a hybrid computer, in which a digital computer is combined with an analog computer. Hybrid computers are used to obtain a very accurate but imprecise 'seed' value, using an analog computer front-end, which is then fed into a

digital computer iterative process to achieve the final desired degree of precision. With a three or four digit, highly accurate numerical seed, the total digital computation time necessary to reach the desired precision is dramatically reduced, since many fewer iterations are required. Or, for example, the analog computer might be used to solve a non-analytic differential equation problem for use at some stage of an overall computation (where precision is not very important). In any case, the hybrid computer is usually substantially faster than a digital computer, but can supply a far more precise computation than an analog computer. It is useful for real-time applications requiring such a combination (e.g., high frequency phased-array radar or a weather system computation).

While digital computation is extremely popular, research in analog computation is being done by a handful of people worldwide. Computer theorists often refer to idealized analog computers as real computers (because they operate on the set of real numbers. In the United States, Jonathan Mills from Indiana University, Bloomington, Indiana has been working on research using Extended Analog Computers. At the Harvard Robotics

Laboratory, analog computation is a research topic. Analog synthesizers can also be viewed as a form of analog computer, and their technology was originally based on electronic analog computer technology.

EAI 180 is a hybrid computer manufactured in 1971 from "Electronic Associates

Incorporated", New Jersey. It contains the parts of an analogue computer and these from a digital computer. The device is equipped with IC's from the first generation (DTLtechnology). The calculation circuit is plugged with cables on the front panel. The cycle time from the analogue part can be set to less than 10ms. With that parameter, an equation will be solved at least 100 times per second. So you can watch the output with a simple oscilloscope.

Digital Computer

The development of digital computer is explained as Computer generations.

UNIT 2

GENERATION OF COMPUTERS

Structure:

2.1

2.2

2.3

2.4

2.5

2.6

2.7

2.8

Computer Generation

Basic Input Output System (BIOS)

Functions of BIOS

Firmware On Adapter Cards

Microprocessor

Personal Computer

Conclusion

Exercise

2.1 Computer Generation

2.1.1 First Generation Computer (1940-1956): Vacuum Tubes

The first digital computer developed was Electrical Numerical Integrator and Calculator (ENIAC) by John P. Eckert, John W. Mauchly, and their associates at the Moore school of Electrical

Engineering of University of Pennsylvania. The size of ENIAC’s numerical "word" was 10 decimal digits, and it could multiply two of these numbers at a rate of 300 per second, by finding the value of each product from a multiplication table stored in its memory. ENIAC was therefore about 1,000 times faster than the relay computers.

ENIAC used 18,000 vacuum tubes; about 1,800 square feet of floor space, and consumed about

180,000 watts of electrical power. It had punched card I/O, one multiplier, one divider/square rooter, and 20 adders using decimal ring counters , which served as adders and also as quickaccess (.0002 seconds) read-write register storage. The executable instructions making up a program were embodied in the separate "units" of ENIAC, which were plugged together to form a "route" for the flow of information.

ENIAC is commonly accepted as the first successful high – speed electronic digital computer

(EDC) and was used from 1946 to 1955. However, before the ENIAC period, another scientist at

Iowa State College developed a computer using simple vacuum, tube device during 1930 but not been made known.

Fascinated by the success of ENIAC, the mathematician John Von Neumann undertook, in 1945, an abstract study of computation emphasizing that a computer should have a very simple, fixed physical structure, and yet be able to execute any kind of computation by means of a proper programmed control without the need for any change in the unit itself. Von Neumann contributed a new awareness of how practical, yet fast computers should be organized and built. These ideas, usually referred to as the stored - program technique, became essential for future generations of high - speed digital computers and were universally adopted. As soon as the advantage of these techniques became clear, they became a standard practice.

The first generation of modern programmed electronic computers to take advantage of these improvements was built in 1947. This group included computers using Random - Access -

Memory (RAM), which is a memory designed to give almost constant access to any particular piece of information. . These machines had punched - card or punched tape I/O devices and

RAM’s of 1,000 - word capacity and access times of .5 Greek MU seconds (.5*10-6 seconds).

Some of them could perform multiplications in 2 to 4 MU seconds. Physically, they were much smaller than ENIAC. Some were about the size of a grand piano and used only 2,500 electron tubes, a lot less then required by the earlier ENIAC. The first - generation stored - program computers needed a lot of maintenance, reached probably about 70 to 80% reliability of operation (ROO) and were used for 8 to 12 years. They were usually programmed in Machine

Language, although by the mid 1950’s progress had been made in several aspects of advanced programming.

This group of computers included EDVAC and UNIVAC and dominated the field through the mid-

1970s. In June 1951, the UNIVAC I (Universal Automatic Computer) was delivered to the U.S.

Census Bureau. Although manufactured by Remington Rand, the machine often was mistakenly referred to as the "IBM UNIVAC". It used 5,200 vacuum tubes and consumed 125 kW of power.

It used a mercury delay line capable of storing 1,000 words of 11 decimal digits plus sign (72-bit words) for memory. Unlike IBM machines it was not equipped with a punch card reader but metal magnetic tape input, making it incompatible with some existing commercial data stores.

High speed punched paper tape and modern-style magnetic tapes were used for input/output by other computers of the era.

The first universal programmable computer in the Soviet Union was created by a team of scientists under direction of Sergei Alekseyevich Lebedev from Kiev Institute of Electrotechnology, Soviet Union now Ukraine.

The computer, Small Electronic Calculating Machine built at University of Manchester in 1948 became operational in 1950. It had about 6,000 vacuum tubes and consumed 25 kW of power. It could perform approximately 3,000 operations per second. Another early machine was CSIRAC, an Australian design that ran its first test program in 1949.

SIRAC is the oldest computer still in existence and the first to have been used to play digital music.

In October 1947, the directors of J. Lyons & Company, a British catering company having strong interests in new office management techniques, decided to take an active role in promoting the commercial development of computers. By 1951 the Lyons

Electronic Office (LEO) I computer was operational and ran the world's first regular routine office computer job.

Manchester University's machine became the prototype for the Ferranti Mark I. The first

Ferranti Mark I machine was delivered to the University in February, 1951 and at least nine others were sold between 1951 and 1957.

In 1952, IBM publicly announced the IBM 701 Electronic Data Processing Machine, the first in its successful 700/7000 series and its first IBM mainframe computer. The IBM

704, introduced in 1954, used magnetic core memory, which became the standard for large machines. The first implemented high-level general purpose programming language, FORTRAN, was also being developed at IBM for the 704 during 1955 and

1956 and released in early 1957.

IBM introduced a smaller, more affordable computer in 1954 that proved very popular.

The IBM 650 weighed over 900 kg, the attached power supply weighed around 1350 kg and both were held in separate cabinets of roughly 1.5 meters by 0.9 meters by 1.8 meters costing $500,000. Its drum memory was originally only 2000 ten-digit words, and required arcane programming for efficient computing.

In 1955, Maurice Wilkes invented microprogramming, which was later widely used in the CPUs and floating-point units of mainframe and other computers, such as the IBM

360 series. Microprogramming allows the base instruction set to be defined or extended by built-in programs now referred as firmware, microcode , or millicode .

In 1956, IBM sold its first magnetic disk system, RAMAC (Random Access Method of

Accounting and Control). It used 50 24-inch metal disks, with 100 tracks per side. It could store 5 megabytes of data and cost $10,000 per megabyte. (As of 2006, magnetic storage, in the form of hard disks, costs less than one tenth of a cent per megabyte).

The two discoveries during 1950’s ‘magnetic core memory’ and ‘transistor circuit element’ paved a new path for the development of digital computer technology. The major advantages realized were increased RAM capacities from 8,000 to 64,000 words in commercially available machines, increased access times of 2 to 3 MS (Milliseconds), and facilitated the development of time sharing computation.

2.1.2 Second generation: transistors 1956-1963: Transistors

The first generation computers left an image that due to their size, cost, and the lack of foresight; computers would be ever be produced or used. But with the introduction of new techniques and programming methods the ‘calculation machines' become very popular all over the world. United States have about 2500 models in all. At the end of the fifties, the electronic calculation machines have won their users' trust.

The invention of transistors during late 1950’s again created the wave. The transistor was realized in 1948 by the Americans John Bardeen, Walter Brattain and William Shockley who shared the 1956 Nobel Prize in Physics for their invention. The vacuum tubes were replaced by the transistors and the computers that used transistors named as second generation calculation machines or computers. Using the transistors and improving the machines and the programs, the calculation machine gets quicker and more economical and it diffuses in a tenth of thousand models all over the world.

For some values of the electric tension to which the transistor is exposed, it has the capacity to transmit or not the current, so to represent the 1 or the 0 which are recognized by the machine. Compared to the valves, the transistor have a lot of advantages: they have a smaller making price and a ten bigger times speed, passing from the position 1 to

0 in a few millionth of second. The sizes of a transistor are of some millimetre compared to the quite a lot centimetre of empty tube. The safety-operating directions is increased because the transistors, working "at cold", avoid the breaks because of the heating which were frequent in the valves. Machines are so built with ten thousand complex circuits which are included in a small space.

Among the systems of the second generation we mark the IBM 1401, which was installed from 1960 to 1964 in more than one hundred thousand models, monopolizing about a third of the world market. In this period there was also the only Italian attempt: the ELEA by Olivetti, produced in 110 models. The remarkable development of the calculation machines and their applications in this period is not due only to the feature of the CPU

(Central Processing Unit), but also to the continuous improvements done to the auxiliary memories and to the unities for the intake and emission of data.

The disks memories are able to set tens million of letters or digits. More unities can be connected at the same time to the calculation machine, taking so the total memory capacity to some hundreds million characters.

The second generation calculation machines, through a particular device made to sort the inside data, are able to superpose different operations, that means to read and punch the cards at the same time, to execute calculus and to take logical decisions, to write and read information on magnetic ribbons.

2.1.3 Third Generation - 1964-1971: Integrated Circuits

The third generation computers are attributed with integrated circuits. The figure THREE

GENERATION OF COMPUTER COMPONENTS presents the transition in the technology.

In the 1960’s, efforts to design and develop the fastest possible computer with the greatest capacity reached a turning point with the LARC machine, built for the Livermore Radiation

Laboratories of the University of California by the Sperry - Rand Corporation, and the Stretch computer by IBM. The LARC had a base memory of 98,000 words and multiplied in 10 Greek MU seconds. Stretch was made with several degrees of memory having slower access for the ranks of greater capacity, the fastest access time being less then 1 Greek MU Second and the total capacity in the vicinity of 100,000,000 words. During this period, the major computer manufacturers began to offer a range of capabilities and prices, as well as accessories such as

Consoles, Card Feeders, Page Printers, cathode - ray - tube displays, and graphic devices etc. The business application domain included areas like Accounting, Payroll, Inventory control, Ordering

Supplies, and Billing etc.

The element base of the Third Generation COMPUTER was small scale integrated circuits (SSIC) or integrated circuits. Due to the integrated circuits it was possible essentially to improve technical-operating performances of the COMPUTER. For

example, the machines of the third generation in comparison with the machines of the second generation have greater size of the main memory, the speed has increased, the reliability has raised, but power consumption and occupied the are square and weight have decreased.

The universal COMPUTERS of the third generation of EU compatible as both among themselves and with other makes of COMPUTERS of the third generation (IBM-360 etc.

- USA) are developed. The experts of the USSR, National Republic of Bulgaria (NRB),

Hungarian National Republic (HNR), Polish National Republic (PNR), Czechoslovak

Soviet Socialist Republic (CSSR) and German Democratic Republic (GDR) take part in development of machines of EU of COMPUTERS. At the same time in the USSR created and released multiprocessor and quasi analogue COMPUTERS, mini computers

"World - 31", "World - 32", "NAIRI-34" etc. For control of technological processes,

COMPUTERS of the series ASVT М-6000 and М-7000 are created. Further desktop mini computers on integrated chips М-180, “Electronics -79, -100, -125, -200",

“Electronics DZ-28 ", " Electronics NC-60 " etc. are released to the market.

The third generation era was a commercial break in the field of microelectronic technology. The characteristic feature of the third generation computer was the sharp decrease of the prices on the hardware. It was achieved mainly due to the use of the integrated circuits. The usual electrical connections with the help of wires were built in a chip. It allowed receiving access time up to 2х10

-9 second. At this period in the market the user-friendly workstations have appeared with networking interface. The further progress in the development of computer facilities was connected while the development of semiconductor memory, liquid crystal screens and electronic memory. Another interesting development is in the area ‘computers of real time’ and ‘decentralised computing’.

2.1.4 Fourth Generation - 1971-Present: Vlsi

The technology which revolutionised the computer technology is the invention of Very large integrated circuits (VLSI). The high degree of integration of subroutine and linkage

of the electronic equipment brought high reliability that leads to increase of the speed of the COMPUTER and lowering of its cost.

It is during 1980’s Very Large Scale Integration (VLSI), in which hundreds of thousands of transistors were placed on a single chip, became more and more common. Many companies, some new to the computer field, introduced in the 1970s programmable minicomputers supplied with software packages. The "shrinking" trend continued with the introduction of personal computers (PC’s), which are programmable machines small enough and inexpensive enough to be purchased and used by individuals.

Many companies, such as Apple Computer and Radio Shack, introduced very successful PC’s in the 1970s, encouraged in part by a fad in computer (video) games. In the 1980s some friction occurred in the crowded PC field, with Apple and IBM keeping strong. In the manufacturing of semiconductor chips, the Intel and Motorola Corporations were very competitive into the

1980s, although Japanese firms were making strong economic advances, especially in the area of memory chips. By the late 1980s, some personal computers were run by microprocessors that, handling 32 bits of data at a time, could process about 4,000,000 instructions per second.

All this renders essential effect on logical structure/ architecture of the COMPUTER and on its software. Closer becomes the connection between the structure of the machine and its software, especial by its operating system (or screen monitor) — of the of program, which organize continuous operation of the machine without interference of the man

Many companies, such as Apple Computer and Radio Shack, introduced very successful PC’s in the 1970s, encouraged in part by a fad in computer (video) games. In the 1980s some friction occurred in the crowded PC field, with Apple and IBM keeping strong. In the manufacturing of semiconductor chips, the Intel and Motorola Corporations were very competitive into the

1980s, although Japanese firms were making strong economic advances, especially in the area of memory chips. By the late 1980s, some personal computers were run by microprocessors that, handling 32 bits of data at a time, could process about 4,000,000 instructions per second.

2.1.5 Fifth Generation: Present & Beyond: Artificial Intelligence

Fifth generation computing devices, based on artificial intelligence, are still in development, though there are some applications, such as voice recognition, that are being used today. The use of parallel processing and superconductors is helping to make artificial intelligence a reality. Quantum computation and molecular and nanotechnology will radically change the face of computers in years to come. The goal of fifth-generation computing is to develop devices that respond to natural language input and are capable of learning and self-organization. Some of the new developments in the computer domain are:

Chemical Computing:

A new concept where computations are performed by naturally occurring chemical reactions is being explored by the scientists and technocrats. Computer developed this concept is termed to be Chemical computer, also called reaction-diffusion computer is an unconventional computer based on a semi-solid chemical "soup" where data is represented by varying concentrations of chemicals.

The simplicity of this technology is one of the main reasons why it in the future could turn into a serious competitor to machines based on conventional hardware. A modern microprocessor is an incredibly complicated device that can be destroyed during production by no more than a single airborne microscopic particle. In contrast a cup of chemicals is a simple and stable component that is cheap to produce.

In a conventional microprocessor the bits behave much like cars in city traffic; they can only use certain roads, they have to slow down and wait for each other in crossing traffic, and only one driving field at once can be used. But in this technology, the waves are moving in all thinkable directions in all dimensions, across, away and against each other.

These properties might make a chemical computer able to handle billions of times more data than a traditional computer. An analogy would be the brain; even if a microprocessor

can transfer information much faster than a neuron, the brain is still much more effective for some tasks because it can work with a much higher amount of data at the same time.

The basic principle adopted is wave and wave properties. In 1989 it was demonstrated how light-sensitive chemical reactions could perform image processing. This led to an upsurge in the field of chemical computing. Andrew Adamatzky at the University of the

West of England has demonstrated simple logic gates using reaction-diffusion processes.

An increasing number of individuals in the computer industry are starting to realize the potential of this technology. IBM is at the moment testing out new ideas in the field of microprocessing with many similarities to the basic principles of a chemical computer.

DNA Computing/ Molecular computers

These are the computers that are in the process of development taking advantage of the computational power of molecules (specifically biological). DNA computing is a form of computing which uses DNA, biochemistry and molecular biology, instead of the traditional silicon-based computer technologies. DNA computing, or, more generally, molecular computing, is a fast developing interdisciplinary area. R&D in this area concerns theory, experiments and applications of DNA computing.

It is during 1994; Leonard Adleman of the University of Southern California demonstrated a proof-of-concept use of DNA as a form of computation which solved the seven-point Hamiltonian path problem. Since then, advances have been made and various

Turing machines have been proven to be constructible using DNA as a form of computation.

During 2002, researchers from the Weizmann Institute of Science (WIS) in Rehovot,

Israel, discovered a programmable molecular computing machine composed of enzymes and DNA molecules instead of silicon microchips which could process 330 trillion operations per second, more than 100,000 times the speed of the fastest PC. Again in

2004 at WIS, Rehovot, Ehud Shapiro, Yaakov Benenson, Binyamin Gil, Uri Ben-Dor, and Rivka Adar announced the possibility of developing DNA computer. The prototype

model is coupled with an input and output module and is capable of diagnosing cancerous activity within a cell, and then releasing an anti-cancer drug upon diagnosis.

DNA computing is fundamentally similar to parallel computing in that it takes advantage of the many different molecules of DNA to try many different possibilities at once.

2.2 Basic Input Output System (Bios)

In computing, BIOS stands for Basic Input/Output System . BIOS refer to the firmware code run by a personal computer when first powered on. The primary function of the

BIOS is to identify and initiate component hardware such as hard drives, floppies, and

CDs. This is to prepare the machine so other software programs stored on various media can load, execute, and assume control of the PC. This process is known as booting, or booting up, which is short for bootstrapping.

BIOS can also be said to be a coded program embedded on a chip that recognizes and controls various devices that make up X 86 personal computers. Among other classes of computers, the generic terms boot monitor , boot loader or boot ROM were commonly used. Some Sun and Macintosh PowerPC computers used Open Firmware for this purpose. The term first appeared in the CP/M operating system, describing the part of

CP/M loaded during boot time that interfaced directly with the hardware (CP/M machines usually had a simple boot loader in ROM, and nothing else). Most versions of DOS have a file called "IBMBIO.COM" or "IO.SYS" that is analogous to the CP/M disk BIOS.

2.3 Functioning of BIOS

The BIOS runs from the PROM, EPROM or, most commonly, flash memory when the computer is powered on. It initializes several motherboard components and peripherals, including clock generator, processors and caches, chipset (memory controller and I/O controller), system memory, PCI devices (by assigning bus numbers and resources), primary graphics controller, Mass storage controllers, and Various I/O controllers such keyboard/mouse and USB. Finally, it loads the boot loader for the operating system, and transfers control to it. The entire process is known as power-on self-test (POST). Once

system memory is initialized, the BIOS typically copies/decompresses itself into that memory and keep executing from it.

Nearly all BIOS implementations can optionally execute a setup program interfacing the nonvolatile BIOS memory (CMOS). This memory holds user-customizable configuration data (passwords, time, date, hard drive details, etc.) accessed by BIOS code. In most modern BIOS implementations, users select where the BIOS obtains its boot image like compact disc, hard disk, floppy disk, USB device or a networked connection. This is particularly useful for installing operating systems or booting to a Live CD or flash key drive, and for selecting the order of testing for the presence of bootable media. Some

BIOSes allow the user to select the operating system to load.

2.3.1 ROM With BIOS

Before 1990 or so BIOSes were held on ROM chips that could not be altered. As its complexity and need for updates grew, BIOS firmware was subsequently stored on

EEPROM or flash memory devices. The first flash chips attached to the ISA bus. Starting in 1998, the BIOS flash moved to the LPC bus, a functional replacement for ISA, following a new standard implementation known as "firmware hub" (FWH). In 2006, the first systems supporting a Serial Peripheral Interface (SPI) appeared, and the BIOS flash moved again.

EEPROM chips are advantageous because they can easily be updated by the user; hardware manufacturers frequently issue BIOS updates to upgrade their products, improve compatibility and remove bugs. However, the risk is that an improperly executed or aborted BIOS update can render the computer or device unusable. To recover from

BIOS corruption, some new motherboards have a backup BIOS (i.e. they are referred to as "Dual BIOS" boards, Gigabyte even offers a motherboard with quad BIOS). Also, most BIOSes have a "boot block" which is a portion of the ROM that runs first and is not updateable. This code will verify that the rest of the BIOS is/are intact (via checksum, hash, etc.) before transferring control to it. If the boot block detects that the main BIOS is

corrupted, then it will typically initiate a recovery process, by booting to a removable media (floppy, CD or USB memory) so that the user can try flashing again.

Due to the limitation on the number of times that flash memory can be flashed, flashbased BIOS is vulnerable to "flash-burn" viruses that repeatedly write to the flash, permanently corrupting chip content. Such attacks can be prevented by some form of write-protection, the ultimate protection being the replacement of the flash memory with a true ROM. Chip varies in size, with models up to 4 megabytes.

2.4 Firmware on adapter cards

A computer system can contain several BIOS firmware chips. The motherboard BIOS typically contains code to access fundamental hardware components such as the keyboard, floppy drives, ATA (IDE) hard disk controllers, USB human interface devices, and storage devices. In addition, plug-in adapter cards such as SCSI, RAID, Network interface cards, and video boards often include their own BIOS, complementing or replacing the system BIOS code for the given component. In some devices that can be used by add-in adapters and actually directly integrated on the motherboard, the add-in

ROM may also be stored as separate code on the main BIOS flash chip. It may then be possible to upgrade these "add-in" BIOS (sometimes called an option ROM) separately from the main BIOS code. Add-in cards usually only require such an add-in BIOS if they:

Need to be used prior to the time that the operating system loads (e.g. they may be used as part of the process which loads (bootstraps) the operating system), and:

Are not sufficiently simple, or generic in operation to be handled by the main BIOS directly

Older operating systems such as DOS, as well as boot loaders, may continue to make use of the BIOS to handle input and output. However, most modern operating systems will interact with hardware devices directly by using their own device drivers to directly access the hardware.

2.5 Microprocessor

Microprocessor is a semiconductor device consisting of electronic logic circuits either

LSI or VLSI technique. Microprocessor is capable of performing computing functions and making decisions to change the sequence of program execution. In large computers, the Central processing Unit (CPU) performs these functions. The microprocessor is similar to CPU in many ways however, the microprocessor includes a;; the logic circuit including the control unit (CU) on one chip. The microprocessor hence includes ALU, control unit, and register unit. We will discuss the features and functions of these components in detail in the later part of this chapter.

Though there are many manufacturers of microprocessor, the widely used microprocessors include INTEL 8080A/8085, and Z80 series. The second level manufacturers are Motorola 6800 and 6809, MOS technology 6500 series. The others include Radio Shack, TRS -80, and Tele Video 802. The following paragraphs details in brief about Zilog and INTEL microprocessors.

ZILOG:

Zilog was incorporated in California in 1974 by Federico Faggin, who left Intel after working on the 8080. The company became a subsidiary of Exxon in 1980, but the management and employees bought it back in 1989. Zilog often seen as ZiLOG (the official company denotation since 1998), is a manufacturer of 8-bit, 16-bit, 24-bit, and

32-bit CPUs, and is most famous for its Intel 8080-compatible Z80 series.

The Z80 was an improved 8080 which was faster, more capable, and much cheaper; alongside the 6502 it was one of the most popular 8-bit processors for general purpose microcomputers and other applications. It was used in the Sinclair ZX80, ZX81 and ZX

Spectrum home microcomputers as well as the MSX architecture and the Tandy TRS-80 series - among many others. Also, many Texas Instruments graphing calculators used the

Z80 as the main processor and found continued use in some newer game consoles such as the Sega Mega Drive (Genesis in the United States) and Saturn. The CP/M operating system (and its huge software library featuring hits like Wordstar and dBase) was known to be " the Z80 disk operating system", and its success is partly due to the popularity of

the Z80. It also manufactures microcontrollers for various purposes. The evolutions of

Zilog products are as below.

 Zilog Z80 (1976)

Zilog Z8000 (ca 1978)

 Zilog Z8 (1979)

 Zilog Z800 (1985)

 Zilog Z80000 (late 1985)

Zilog Z280 (early 1986)

 Zilog Z180 (late 1986)

 Zilog Z380 (1994)

 Zilog eZ80 (2001)

 Zilog eZ8 (2005)

 Zilog Z16 (2006)

The Intel 8085 was an 8-bit microprocessor made by Intel in the mid-1970s. It was binary compatible with the more-famous Intel 8080 but required less supporting hardware, thus allowing simpler and less expensive microcomputer systems to be built.

The "5" in the model number came from the fact that the 8085 required only a +5-volt

(V) power supply rather than the +5V, -5V and +12V supplies the 8080 needed. Both processors were sometimes used in computers running the CP/M operating system, and the 8085 later saw use as a microcontroller (much by virtue of its component count reducing feature). Both designs were eclipsed for desktop computers by the compatible but more capable Zilog Z80, which took over most of the CP/M computer market as well as taking a large share of the booming home computer market in the early-to-mid-1980s.

The 8085 had a very long life as a controller.

Intel 8086 microprocessor is a first member of x86 family of processors. Advertised as a

"source-code compatible" with Intel 8080 and Intel 8085 processors, the 8086 was not object code compatible with them. The 8086 has complete 16-bit architecture - 16-bit internal registers, 16-bit data bus, and 20-bit address bus (1 MB of physical memory). Because the processor has 16-bit index registers and memory pointers, it can effectively address only 64 KB of memory. To address memory beyond 64 KB the CPU uses segment registers - these registers

specify memory locations for code, stack, data and extra data 64 KB segments. 64 KB direct addressing limitation was eliminated with the introduction of the 32-bit protected mode in Intel

80386 processor.

Intel 8086 instruction set includes a few very powerful string instructions. When these instructions are prefixed by REP (repeat) instruction, the CPU will perform block operations - move block of data, compare data blocks, set data block to certain value, etc, that is one 8086 string instruction with a REP prefix could do as much as a 4-5 instruction loop on some other processors. However, the Zilog Z80 included move and search block instructions, and Motorola

68000 could execute block operations using just two instructions. The Intel microprocessors are grouped under different series as mentioned below.

Intel® Core™ 2 Processor

Intel® Core™ Processor

Intel® Pentium® D Processor

Intel® Pentium® M Processor

Intel® Itanium® Processor Family

Intel® Xeon™ Processor

Intel ® Pentium® 4 Processor

Intel® Pentium® III Processor

Intel® Celeron® Processor

Intel® Pentium® III Xeon™ Processor

Intel® Pentium® II Xeon™ Processor

Intel® Pentium® II Processor

Intel® Pentium® Pro Processor

Intel® Pentium Processor

Intel486™ Processors and Earlier

The developments in microprocessor are in line with the developments in computer technology and today Intel has 13 micron processors. With an understanding that there is always room for improvement, the overall goal is to make the processor smaller, faster, and more robust to deliver a more powerful processor at a price the end users are willing to accept.

The 2000s have come along and it's too early yet to say what will happen by decade's end. As the day nears when process will be measured in Angstroms instead of nanometers, researchers are furiously experimenting with layout, materials, concepts, and process. As for 2005 and beyond, the second half of the decade is sure to bring as many surprises.

2.6 Personal Computer

The evolution of personal computer is associated with the developments in technologies like microprocessor. The first prototype of personal computer was announced during the year 1974.

The first personal computer prototype was developed by MITS Inc of Mexico named Altair 8800 microcomputer. The original name for the computer is "PE-8", in honour of the Popular

Electronics magazine. Ed Roberts decides that the programming language of the new microcomputer should be BASIC. MITS technical writers David Bunnell suggest the name "Little

Brother" for the new MITS computer.

At the same time the young programmer Bill Gate, today the world’s richest person and Paul

Allen developed the instructions using BASIC. Microsoft Corporation was created to market the

Gates-Allen BASIC Program and Microsoft BASIC was licensed to MITS Inc in late 1975. Today

Microsoft is a leading supplier of personal computer software. Bill Gates signed a document formalizing the existence of the Traf-O-Data company owners in order to use Traf-O-Data's 8008 simulator to develop BASIC for the Altair. Later Micro-Soft gave the licenses of 6800 BASIC to

MITS.

Another pioneer which grew from the garage as a big giant in the computer industry is APPLE

Computer during 1976. It is Steve Woznaik the technician from Hewlett Packard bought a microprocessor from MOS technology and set out to build a computer around it which was named as APPLE computer. Later APPLE II was developed and become a pioneer in the industry.

Former Engineer of INTEL helped Steve and suddenly APLLE also become one among the top listed Personal computer manufacturer.

Another entrant during 1977 was Radio Shack division of Tandy Corporation. The computer was known as TRS-80 model. Later to this part many people joined in the industry. By the end of

1970’s, PC becomes popular and were being used every where like home, schools, and work places. The low cost PCs were produced from a company named Atari and Commodore. IBM entered into the market and produced powerful new generation computers that are being used in schools and workplaces.

Hewlett-Packard introduces the HP-85 microcomputer. It features 16 kB RAM, 32-character wide 5-inch diameter CRT display, small built-in printer, cassette tape recorder, and keyboard.

Price is US$3250. Code-name during development was Project Capricorn.

The next development was made by Pal Alto Research centre of XEROX which developed a system ‘Star Information System (SIS) in 1981, to represent data and programs together on the screen by small pictures called ‘icons’. For example a picture of a trash took the place of typed command DEELTE. Using this technology APPLE Computer designed a low cost Personal computer incorporating the SIS features. The hardware and software system named LISA was unveiled in 1983. Later part of the years was all the innovations on the products available by various manufacturers.

2.7 Conclusion

2.8 Exercise

1.

2.

Explain Different types Computer Generations

What are Functions of BIOS?

UNIT 3

COMPONENTS OF COMPUTERS

Structure:

3.1 Introduction

3.2 Components of Computers

3.3 Input Devices

3.4 Output Devices

3.5 CPU

3.6 Types of Computers

3.1 Introduction

In the previous paragraphs we have discussed about the historical development of computers both analogue, digital computers, personal computers, microprocessors, and the importance of BIOS in a computer system.

3.2 Components of Computers

The following paragraphs discuss about the various components of the computer like input devices, output devices, Central processing units (CPU) etc. Computer systems ranging from a controller to a large supercomputer contain components providing five functions. A typical personal computer has hard disk(s), floppy disk and CD-ROM disk for storage, memory and CPU chips inside the system unit, a keyboard and mouse for input, and a display, printer and speakers for output. The arrows represent the direction information flows between the functional units. The following figure is a simplified version representing the five components of a computer.

(A)

(B)

(FIGURE A: the above picture shows a TFT monitor, Keyboard,

Mouse, CPU, Printer and Speaker of a Computer System)

FIGURE B: SIMPLIFIED REPRESENTATION OF

COMPUTER COMPONENTS

(C) FIGURE C shows the connection between different devices of a computer. Modem is an interface used for connectivity especially Internet connectivity.

3.3 Input Devices

INPUT device is a hardware device that sends information into the CPU. Without any input devices a computer would simply be a display device and not allow users to interact with it, much like a TV. The various input device include Keyboard, Mouse, Joystick, Microphone,

Scanner, Digital camera, Web Cam, and Sensory devices etc.

DIFFERENT TYPES OF KEY BOARDS 1964 mouse invented by

Douglas Engelbart and William & New Generation Mouse

FIGURE: MICROPHONE

TOUCH PAD,

GRAPHIC INPUT DEVICE

DIFFERENT TYPES OF

SCANNERS & WEB CAMS

Following are the input device cards that are used to connect the various input devices.

FIGURE: INPUT CARDS

Some of the other input devices are Magnetic Ink Character recognition (MICR) system, Bar code readers, Optical mark reader, and Optical scanners etc.

3.4 Output Devices

Out Put devices are the devices which enable a user to see the processed information. Out put can be obtained in both hard and soft forms. The devices that provide soft forms of out put are monitors, speakers in the audio form, projectors, head mounted displays, and volumetric displays etc. Hard form of output can be obtained from printers, plotters, microforms etc.

FIGURE: CRT, TFT and EXTENDED MONITORS AND PRINTERS

Today, multipurpose printers are available in the market. This type of printer can be used to send and receive FAX, Scan the document and as well as telephone.

MULTI FUNCTION PRINTERS

(PHONE/FAX/SCANNER/COPIER)

SPEAKERS

3.5 CENTRAL

UNIT (CPU)

PROCESSING

CPU is known as brain of the computer. A typical CPU consists of the following interconnected functional units:

Registers

Arithmetic/Logic Unit (ALU)

Control Circuitry

Registers are temporary storage units within the CPU. Some registers, such as the program counter and instruction register, have dedicated uses. Other registers, such as the accumulator, are for more general purpose use

Arithmetic/Logic Unit (ALU): All processors contain an arithmetic/logic unit, which is often referred to simply as the ALU The ALU, as its name implies, is that portion of the

CPU hardware which performs the arithmetic and logical operations on the binary data. It has accumulator and General purpose registers

Control Unit:

The control circuitry is the primary functional unit within a CPU. Using clock inputs, the control circuitry maintains the proper sequence of events required for any processing task After an instruction is fetched and

decoded, the control Unit issues the appropriate signals (to units both internal and external to the CPU) for initiating the proper processing action.

The CPU is centrally located on the motherboard. The name CPU is because it is the centre of data processing, it is processor because it processed (moves and calculated) the data and it is unit because it is a chip, which contains millions of transistors.

Since the CPU carries out a large share of the work in the computer, data pass continually through it. P ersonal computers can execute instructions in less than one-millionth of a second while supercomputers can execute instructions in less than onebillionth of a second. The data come from the RAM and from the various input devices. After processing, the data is sent back to the RAM and to the output devices as per the instruction or to Read Only Memory for storage purpose and later use. The CPU continually receives instructions to be executed. Each instruction is a data processing order.

The Instruction-Execution Cycle

The CPU performs four steps in executing an instruction:

1.

The control unit gets the instruction from memory.

2.

The control unit decides what the instruction means and directs the necessary data to be moved from the memory to the arithmetic logic unit.

3.

The arithmetic logic unit performs the actual operation on the data.

4.

The result of the operation is stored in memory or a register.

The first two instructions make up what is called the instruction time. The last two instructions make up what is called the execution time. The combination of these two is called a machine cycle.

Each central processing unit has an internal clock (or system clock), which produces pulses at a fixed rate to synchronise all computer operations. A single machine cycle instruction is made up of a number of sub-instructions, each of which must take at least one clock cycle. Each type of CPU is designed to

understand a specific group of instruction called the instruction set.

The location in memory for each instruction and each piece of data is identified by an address, or a number that stands for a location in the computer memory. An address can hold only one item - a fixed amount of data, a number or a word - at any one time.

The following is an example of a simple case of adding two numbers together and placing the result in a location X. The command executed is - Let X = N1 + N2. The following figure illustrates the functioning of CPU.

3.6 Types of Computers

Computers can be classified based on many factors. Computers can be analog and digital computers. Computers can be classified based on size. Computers can also be classified based on processing abilities, application areas etc. Computers can also be classified depending upon the users who use it. For example individual user and multiple user.Generally computers are classified as:

Micro computers or personal computers (PCs) o Desk top computer o Work stations o Portable computer o Notebook computers o Handheld computers o Smart phones

Network computers

Mini Computers

Mainframe computers

Supercomputers

Desktop Computers

Desktop computer is the most common type of computer that is designed to sit on or under a desk or table. The PCs today are very powerful. They can be used for communication, produce music, video, play games etc apart from data processing. Today the design orientation is vertical in nature than the horizontal design. Similarly, the monitors also have become slim and the TFT monitors have replaced the CRT monitors.

Work stations are like PCs but differ from PC with their processing abilities, and high resolution display devices, graphic handling abilities, and are suitable for engineering and architecture designs, animations and video editing.

Notebook computers as the name suggest are small in size usually8.5 by 11 inches and can be easily fit inside the brief case. They are also called laptops and weigh less than five kilograms. Today a laptop weighs just 2.5 kilograms.

These computers can operate on alternate current or special batteries.

Tablet PCs are smaller in size and accept input from stylus or digital pen.

These computers offer all the functionality of desktop or notebook computers.

Handheld computers are the computers which are small enough to fit into hand. Often referred as personal digital assistants (PDA).These are used for specialized tasks like taking notes, displaying address and telephone numbers and keep track of routine meetings and activities.

Smart phones are the devices miniature version of mobile phones offering advanced features like email, personal organizers, camera mentioned features. and music players etc. The new NOKIA model has all the above

Network Computers

The network computer is also called Network Server. Network server is usually a powerful personal computer with special software and equipments. Many personal computers and workstations are connected to the network server. The server controls and monitors the functions of the computers that are connected to the server and also handles tasks like printing, internet connectivity, data and information storage etc. When many such servers are used in the organization, the network server is called ‘clusters’ or cluster farms’.

Minicomputers:

Mini computers are called as mid range computers. It is designed to serve the needs of several people simultaneously. The first mini computer was brought out during the year 1960 by digital equipment Corporation (DEC) which is named as PDP series. It is smaller than the mainframe computer but much faster than the personal computers. These are also used for real time controls and engineering devices.

Minicomputers pretty much died out when the microprocessor took over. They went upscale to machines like the VAX, which eventually grew to mainframe size. Some minicomputer architectures lived on as microprocessors, the PDP-8 in the Intersil 6100 series chips and the PDP-11 in the LSI-11 processors.

Mainframe computers

Mainframe computers are large general purpose computers. Mainframe computers generally require special attention and are kept in a controlled atmosphere. They are multi-tasking and generally used in areas where large databases are maintained e.g. government departments and the airline industry.

SUPERCOMPUTER

A supercomputer is generally known as fastest computers currently available and are often used for number crunching including scientific simulations, animated graphics, analysis of geological data (e.g. in petrochemical prospecting), structural analysis, computational fluid dynamics, physics, chemistry, electronic design, nuclear energy research and meteorology. The father of supercomputer is Seymour Cray (1925-96) who formed the Cray Research Company which was the U.S. leader in building the fastest supercomputers for many years.

Since supercomputers have always been number-crunchers, their speed is measured in floating point operations per second, or FLOPS, in units of megaflops (MFLOPS), gigaflops (GFLOPS), and teraflops (TFLOPS) which refer to millions, billions, and trillions of FLOPS, respectively. Centre for Development of Advanced Computing (C-

DAC), India has manufactured PARAM Supercomputer for the first time.

UNIT 4

DATA PROCESSING

Structure:

4.1 Introduction

4.2 Batch Processing and Online Analytical processing

4.3 Summary

4.1 Introduction

Some of the common features of computers irrespective of size and type are speed, reliability,

storage capacity, and productivity. Computers provide the processing speed required by all facets of society. The quick service we expect at any situation, may be at bank, shop, while engaged in stock business depends on the speed of the computers. The speed of the computer is measures in terms of Hertz per second. Computers are extremely reliable as they are programmed and hence do not commit errors. Computers are capable of storing enormous amounts of data that must be located and retrieved very quickly. The capability to store and retrieve volumes of data is at the core of the Information Age. Computer is also known for accuracy. In most cases errors are due to human factor rather than the computer. For example, if the program is wrongly coded, the result will be wrong. Error is data entry is also a human error than the computer error. The computer hence become versatile in nature as it performs simple to complex functions. They are capable of performing any task. Further, the versatility increases when the computer is connected. The diligence features of computer make it more reliable and effective. As it does not get tired and loose concentration like human being, it can

perform the same task any number of time and continuously work. With all the above characteristics the computer becomes more productive than people in the organization.

The other unique characteristics are multiprogramming, multiprocessing, and multitasking.

Multiprogramming is a technique used to utilize maximum CPU time by running multiple programs simultaneously. The execution begins with the first program and continues till an instruction waiting for a peripheral is reached, the context of this program is stored, and the second program in memory is given a chance to run. The process continued until all programs finished running. However, multiprogramming has no guarantee that a program will run in a timely manner.

Since there is only one processor, there can be no true simultaneous execution of different programs. Instead, the operating system executes part of one program, then part of another, and so on. To the user it appears that all programs are executing at the same time.

Multiprogramming is the rapid switching of the CPU between multiple processes in memory. It was commonly used to keep the CPU busy while one or more processes are doing I/O.

Multiprogramming makes efficient use of the CPU by overlapping the demands for the CPU and its I/O devices from various users. It attempts to increase CPU utilization by always having something for the CPU to execute.

Multiprocessing is running a system with more than one processor. In order to employ multiprocessing effectively, the computer system must have a motherboard must have additional sockets or slots for the extra chips, and a chipset capable of handling the multiprocessing arrangement, processors that are capable of being used in a multiprocessing system, and an operating system that supports multiprocessing, such as Windows NT or any version of UNIX operating system.

Multitasking is a feature with which the computer allows a user to use more than one application at a time.

4.2 Batch Processing and Online Analytical processing

Batch processing is execution of a series of programs or " jobs " on a computer without human interaction. Batches of data are prepared in advance to be processed during regular ‘runs’ (for example, each night). This allows efficient use of the computer and is well suited to applications of a repetitive nature. The major advantage of the batch processing is the computer is used to the optimum level with out keeping it idle. For example company payroll, or the production of utility bills etc. The benefits of batch processing are:

It allows sharing of computer resources among many users

It shifts the time of job processing to when the computing resources are less busy

It avoids idling the computing resources with minute-by-minute human interaction and supervision.

Online Analytical Processing: (OLAP): It is defined as F ast A nalysis of S hared

M ultidimensional I nformation (FASMI). This definition was first used by us in early

1995. FAST means that the system is targeted to deliver most responses to users within about five seconds, with the simplest analyses taking no more than one second and very few taking more than 20 seconds.

ANALYSIS means that the system can cope with any business logic and statistical analysis that is relevant for the application and the user, and keep it easy enough for the target user.

SHARED means that the system implements all the security requirements for confidentiality (possibly down to cell level) and, if multiple write access is needed, concurrent update locking at an appropriate level. Not all applications need users to write data back, but for the growing numbers that do, the system should be able to handle multiple updates in a timely, secure manner.

MULTIDIMENSIONAL is the key term in the definition of OLAP. The system must provide a multidimensional conceptual view of the data, including full support for hierarchies and multiple hierarchies, as this is certainly the most logical way to analyze businesses and organizations.

INFORMATION is all of the data and derived information needed, wherever it is and however much is relevant for the application.

4.3 Summary

The chapter details about the evolution of computing technology from the period of 1000

BC to till date. A detailed discussion of analog and digital computer is explained. The evolution of digital computer is explained in terms of computer generation. The five generations of computer is explained with illustrations. The innovations in the technology may lead to have new devices adopting DNA technology, molecular technology, artificial intelligence etc. However, one has to wait and see the reality of the technology that is still in the process of development.

Part of the chapter also focus on evolution of personal computers and microprocessors.

Microprocessor is the one technology that has made the personal computing possible.

Further, a detailed discussion is made on Basic Input Output System.

Continuing to the concept of BIOS, the chapter details the components of computer, types of computers, and features/characteristics of computers. It is the user requirement that is constantly changing is making all the innovations to be made and make it a reality.

MODULE 2

UNIT 1

RECENT DEVELOPMENTS IN INFORMATION TECHNOLOGY

Structure :

1.1

1.2

1.3

1.4

Introduction

Components of Computers

Computer Hardware

Data Access Methods and Data Processing Methods in Computer

1.5

1.6

1.7

Computer Software

Web Based System And Network Computing

Extranet

1.1 Introduction

Information technology encompasses wide range of tools and applications. Information technology alone is not beneficial to any organization. Its benefits are reaped only when communication technology is also used along with IT. Hence today it is not just information technology it is called information and communication technology (ICT). The historical development of both information and communication technology is interesting. The developments are seen in the from of user friendly use, reduced size, fast processing, huge memory capacity, reduced cost, high reliability, better performance, large amount of data transfer with high speed and secured transfer, multimedia data transfer etc. Today we have hand held devices to process billions of data in a second. Revolution has enabled the room size computer to reduce to a hand held device to perform all the complicated functions. A sales man today can update the information system located else where using personal digital assistants (PDA). The figure illustrates how a portable computer can be used to issue parking slips in a parking area.

Computer forms the major component of IT. The convergence of computer and communication technology is presented in the following figure.

The figure illustrates that computer forms one of the components of the new converged system.

The other components that enable the new converged system available to the users are: a.

communication technology including telecommunication(telephone, telegraphs), b.

broadcasting technology (radio and television) c.

Networking technology ranging from LAN to satellite networks d.

Modern digital communication systems like fax, email, mobile technology, voice mail, video conferencing.

To understand various aspects of IT, it is essential to understand the functional characteristic and components of the computer. The same is presented in the following figure.

1.2 Components Of Computer

Computer is a combination of hardware and software and these components become more effective when integrated with communication system/devices. The following paragraphs provide a brief description of computer hardware and computer software.

1.3 Computer Hardware

A computer is always represented with the components like input devices, output devices, memory, central processing unit and devices that link all the devices. The device that links the four devices is called ‘BUS’. The following figure illustrates the components of a computer system. The block also provides types of such devices that are used. The devices some time is also refereed as peripherals.

Components of computer

The traditional input devices include key board and mouse. Today the various input devices that are in use are pointing devices; pen based computing, speech recognition, optical Scanning,

Magnetic Ink Character Recognition, Touch screen, Smart Cards, and digital cameras.

The traditional out put devices in use are computer monitors, printers. However, today we have advanced devices like audio output devices, video output devices, microforms etc.

The CPU consists of sub components like Control Unit, Arithmetic Logic Unit, Special purpose processor, cache memory, and primary storage systems. The control unit interprets instructions and directs processing, ALU performs arithmetic operations, logic functions and makes comparisons, the special purpose processor enable to process different types of data, and primary memory stores data and program instructions during processing. Cache memory enables fast accessibility to the instructions and processing. CPU also includes circuitry for

devices such as registers. The following figure illustrates the relationship between Central

Processing Unit and Memory devices.

CPU and

Memory

The storage device includes Primary storage device called Random Access Memory (RAM), and secondary memory called Read Only Memory (ROM). Generally it is also known as hard disk. The other secondary storage or memory devices are CD-ROMs, Magnetic tapes, magnetic discs,

DVDs etc.

The ‘bus’ that links all the devices to process an instruction is of three types. The data bus which enables to transfer the data across the system, address bus which provides the source and destination of the data, and control bus enables to control the transfer and any instruction.

The trend in hardware technology with reference to processing system is seen from vacuum tubes used in first generation computers to small micro chips used in fifth generation computers. The second generation computers used solid state circuits and third generation computers used integrated circuits. The fourth generation computers adopted Large Scale and

Very large scale integrated circuits and microprocessors. The fifth generations are in the process

of adopting artificial intelligence and devices with greater power to process the data. The trend is towards small, reliable and less cost to make the computer.

The developments in input and out devices are presented in the following figures.

First Second

Generation

Punched

Generatio n

Punched

Cards Cards

Third

Generatio n

Key to Tape/

Disk

Paper Tape

Fourth Fifth

Generation

Keyboard Data

Entry

Pointing Devices

Optical Scanning

Generation

Voice

Recognition

Speech and

Touch Devices

Handwriting

More Natural and Easy to Use

First Second Third Fourth Fifth

Generation

Punched

Generation

Punched

Generation

Printed Reports

Generation

Video Displays

Generation

Video Displays

Cards Cards and Documents

Printed Reports Printed Reports Video Displays and Documents and Documents

Audio

Responses

Printed Reports and Documents

Voice

Responses

Hyperlinked

Multimedia

Communicate Naturally, Quickly, and Clearly

Similarly the change in the storage/memory systems also is from huge system to micro devices.

Today we use pen drives which enables a user to carry all office information in his/her pocket.

The same is presented in the following figure.

Primary

First Second Third Fourth Fifth

Generation

Magnetic

Generation

Magnetic

Generation

Magnetic

Generation

LSI

Generation

VLSI

Drum Core Core Semiconduct or

Semiconduct or

Trend : Towards Large Capacities Using Smaller Microelectronic Circuits

Chips Chips

Secondary

Storage

Magnetic

Tape

Magnetic Disk Magnetic Disk Optical Disk

Magnetic

Drum Magnetic Disk

Magnetic

Tape

Optical Disk Magnetic Disk

Magnetic

Tape

Media

1.4 Data Access methods and Data Processing Method in

Computer

Computers allow two types of data access methods viz ‘direct access’ and ‘sequential access’ method. RAM allows direct access or random access to data. Devices like magnetic storage systems which allow direct access to data are called Direct Access Storage Devices. Magnetic

Tape cartridges are known as Sequential Access Devices as they allow the user to access the data sequentially.

Similarly, data can be processed in two forms viz sequential processing and parallel processing.

The other types that are generally referred are batch processing and online or real time processing system.

In sequential processing each instruction or task is processed sequentially one after another by the CPU while in Parallel processing all the tasks or instructions given to the CPU will be processed parallelly. However, the user is never made to realize how the data/instruction or task is processed by the CPU as the time taken to process each instruction is in micro seconds.

In parallel processing the CPU capacity is shared while in sequential processing all the CPU processing capability is used for one task.

Sequential and parallel processing

In batch processing mechanism, all the tasks that are to be processed are accumulated and will be processed at one time while in real time processing each task is processed immediately. With batch processing, user will not find the system updated where as in real time processing mechanism the system will be updated immediately. Earlier banks adopted batch processing system and now the systems are integrated and perform real time processing mechanism. The concept of ‘any time’, ‘any where’ banking is possible due to real time processing. Today a customer can operate single bank account from any place which was quite impossible few years back. For example Mr. Ramesh has opened a bank account in Corporation Bank at Chennai, the transactions are possible only at Chennai branch where the account is opened and all updates where not available immediately after the transaction. But today, one can open an account any where and operate from any where. The first instance is an example for batch processing and the later is an example for online processing.

1.5 Computer Software

Computer software enables the hardware to function. Software in simple form can be understood as instructions to the computer. Generally computer software is classified into two major categories.

They are System software and application software. Further, another group of software is known as compilers or programming languages. The language computer understands is known as ‘assembly language’ or Machine Language. Complier and Interpreters are software that compiles the user defined instructions into computer language. Following figure illustrates the types of evolution of computer software over a period of time from first generation to fifth generation systems.

Computer Software

Application

Software

System

Software

General- Application- System System

Purpose Specific Management Development

Programs Programs Programs Programs

Following table provides the type of programming languages used by the various generations of computers.

Computer Generation Language

First Generation Computes

Second generation computers

Third Generation computers

Fourth generation Computers systems

Fifth Generation computers

Machine Language

Symbolic Language or Packaged Programs

High Level Languages

Fourth Generation languages, Data Base management

Natural and object oriented languages, Multipurpose

Graphic interfaces and expert assisted language,

The trend is seen in terms of easy to use multipurpose network enabled application packages for productivity and easy collaboration.

Following figure illustrates the major types of software used in general by a user. It also explains the integration of hardware and software and functions of system and application software.

1.6 Web Based System and Network Computing

Web-based systems refer to those applications or services that are resident on a server that is accessible using a Web browser. The only client-side software needed to access and execute these applications is a Web browser environment. Apart from the computer and communication devices, a web based system has other major components like internet, intranet, and extranet.

Often a web based system is also referred as inter organizational system (IOS).

IOS are systems that connect two or more organizations. These systems are common among business partners and play a major role in e-commerce, as well as in supply chain management support.

The first type of IT system that was developed in the 1980s to improve communications with business partners was electronic data interchange (EDI), which involved computer-to-computer direct communication of standard business documents (such as purchase orders and order confirmations) between business partners. These systems became the basis for electronic commerce. IOS is facilitated by internet, intranet and extranet.

One of the major elements of network computing is Internet. Generally Internet is referred as

“the Net,” the Internet is a worldwide system of computer networks—a network of networks hence Internet, in which users at any one computer can get information from any other computer. The Internet uses a portion of the total resources of the currently existing public telecommunication networks. Technically, what distinguishes the Internet is its use of a set of protocols called TCP/IP (Transmission Control Protocol/Internet Protocol). The following figure illustrates the evolution of Internet and how does internet has enabled to provide complex services in user friendly approach.

In addition to the Internet and the Web there are two other major infrastructures of network computing: the intranet and the extranet.

An intranet is the use of Web technologies to create a private network, usually within one enterprise. It is typically a complete LAN, or several intra-connected LANs. Intranets are used for work-group activities, the distributed sharing of projects within the enterprise, Controlled access to company financial documents, use of knowledge management, research materials, online training, and other information that requires distribution within the enterprise. The main objective of Intranet is to serve the internal informational needs of a company, using Internet concepts and tools. Intranet supports browsing, search capabilities, communication and collaboration.

1.7 Extranet

Extranet connect several intranets via the Internet, by adding a security mechanism and some additional functionalities. They form a larger virtual network that allows remote users (such as

business partners or mobile employees) to securely connect over the Internet to the enterprise’s main intranet. Extranets are also employed by two or more enterprises (suppliers & buyers) to share information in a controlled fashion, and therefore they play a major role in the development of business-to-business electronic commerce and Supply Chain systems. Extranet allows secure communications (connects the intranets of different organizations) among business partners over the Internet. It enables business-to-business (B2B) transactions and provides an interface to exchange of business forms.

Network computing has enabled Telecommuting, or Tele-working. With this organizations provide an arrangement whereby employees can work at home, at the customer’s premises, in special work places, or while traveling, usually using a computer linked to their place of employment. Web-based systems enable business transactions to be conducted seamlessly twenty-four hours a day, seven days a week. Some classifications of E-commerce systems are:

B2C (Business to Consumer)

B2B (Business to Business)

B2E (Business to Employee)

C2C (Customer to Customer)

G2B (Government to Business)

UNIT 2

WEB BASED TECHNOLOGICAL DEVELOPMENTS

Structure:

2.1

2.2

2.3

2.4

Major Components Of Web-Based EC

Limitations of File Servers

Database Server Architectures/Two Tier Architecture

Three tier architecture

2.1.1 Electronic storefronts

These are Web-equivalents of a physical store. Through the electronic storefront, an e-business can display and/or sell its products. The storefront may include electronic catalogs that contain descriptions, graphics, and possibly product reviews. They have following common features of electronic storefront is they have an e-catalogue, shopping cart, and check out mechanism and functions like payment processing and back office order fulfillment system

2.1.2 Electronic Markets

An electronic market is a web-based network of interactions and relationships over which information, products, services, and payments are exchanged. It is equivalent to a physical marketplace except is Web-based. The principal participants in marketplaces are: transaction handlers, buyers, brokers, and sellers. The means of interconnection vary among parties and can change from event to event, even between the same parties. Electronic markets can reside in one company, where there is either one seller and many buyers, or one buyer and many sellers.

These are referred to as private marketplaces.

2.1.3 Electronic Payments

Electronic Payments are an integral part of doing business, whether in the traditional way or online. Unfortunately, in most cases traditional payment systems are not effective for EC, especially for B2B. The different forms of electronic payments are

Electronic checks (e-checks) are similar to regular checks. They are used mostly in

B2B

Electronic credit cards make it possible to charge online payments to one's credit card account.

Purchasing cards, the B2B equivalent of electronic credit cards.

Electronic cash (e-cash) appears in three major forms: stored-value cards, smart cards, and person-to-person payments.

Electronic Bill Presentment and Payments allow customers to pay their recurring monthly bills, such as telephone, utilities, credit cards, etc. online.

E-wallets are mechanisms that provide security measures to EC purchasing. The wallet stores the financial information of the buyer, including credit card number, shipping information, and more.

Virtual credit cards are services that allow you to shop with an ID number and a password instead of with a credit card number.

The trend in the context of electronic commerce and web based system are as below:

2.1.4 Electronic Exchanges

Electronic exchange is a special form of electronic markets. Electronic exchanges are Web-based public marketplaces where many buyers and many sellers interact dynamically. This was originally set as trading places for commodities. Today, electronic exchanges have emerged for all kinds of products and services

2.1.5 Corporate Portals

Portals are the Web sites that provide the gateway to corporate information from a single point of access. They aggregate information and content from many files and present it to the user.

Corporate portals also are used to personalize information for individual customers and for employees. Intranets and Extranets are usually combined with and accessed via a corporate portal. Yahoo is an example for corporate portal.

2.1.6 Enterprise Web and Web Services

Enterprise Web is an open environment for managing and delivering Web applications. It combines services from different vendors in a technology layer that spans rival platforms and business systems, creating a foundation for building applications at a lower cost. Here one can avail the applications, including business integration, collaboration, content management, identity management, and search, which work together via integrating technologies. The result is an environment that spans the entire enterprise.

Web services are self-contained, self-describing business and consumer modular applications, delivered via the Internet, that users can select and combine through almost any device, ranging from PC to mobile phones.

The web based system has enabled the organizations to cut cost, people resources, improve quality, and provide better services in reduced time. This has further enhanced by optical communication networks, object technology, mobile computing, wireless networks, Storage area networks, inter organizational systems and new emerging computing technologies.

2.1.7 The virtual corporation (VC)

Virtual Corporation is an organization composed of several business partners sharing costs and resources for the purpose of producing a product or service. In a virtual organization the resources of the business partners remain in their original locations but are integrated. The major attributes of virtual organizations are:

 Excellence

 Utilization

 Opportunism

 Lack of border

 Trust

 Adaptability to change

Technology

Some of the developments in the computing environment include the following.

Utility Computing is computing that is as available, reliable, and secure as electricity, water services, and telephony. The vision behind utility computing is to have computing resources flow like electricity on demand from virtual utilities around the globe—always on and highly available, secure, efficiently metered, priced on a pay-as-you-use basis, dynamically scaled, selfhealing, and easy to manage.

Subscription Computing is a form of utility computing that puts the pieces of a computing platform together as services, rather than as a collection of separately purchased components.

Grid Computing employs networked systems to harness the unused processing cycles of all computers in that given network thus creating powerful computing capabilities. Grid computing is already in limited use, for example the well-known grid-computing project SETI (Search for

Extraterrestrial Intelligence) @Home project. In this project, PC users worldwide donate unused processor cycles to help the search for signs of extraterrestrial life by analyzing signals coming from outer space.

Pervasive Computing, a future in which computation becomes part of the environment.

Computation will be embedded in things, not in computers.

Some other developments that are in progress and which constantly are improving costperformance ratio and productivity of the implementing organizations are:

Increasing storage and memory: The storage capacity of the laptop, and secondary storage devices has immensely increased. One can carry the office with himself/herself. With the growth of web enabled system, a new sector of service providers i.e. Internet Service Providers (ISP) has emerged. Initially the scope of ISP were limited but today the scope is much wider and they are involved in extending the services of hosting the company web site, securing the data/information, provide networking facilities etc.

User friendly interfaces: The development in user interface devices is from command based system to icon based system. This has enabled any body to implement ICT who has little interest to adopt ICT in any of the activities. The computer accepts the data even as voice input through interface like microphone, speech tools etc. Similarly the output can be obtained in any form.

The Technology has enabled to process, store and get the information in multimedia, a converged media of text, image, voice, video, graphics, pictures, animation etc. The ability of the system is increased requiring less skill from human to operate and use the system. The user friendly interface is not just limited to input and output devices, the user friendly interface is extended in all the functionalities. If a user want to install the hardware or software the whole process is completed by pressing the buttons for ‘yes’, ‘accept’, ‘agree’, disagree’ or no. The application today can be implemented on any hardware platform. The user only has to know which brand of the computer he/she is using like IBM, or Macintosh etc.

Web has become a toy to all. Days are not far where ICT becomes part of every ones life irrespective of rich and poor. However, still we talk of ‘digital divide’. The term Digital Divide was coined in mid 1990s in the adversarial atmosphere underlying the issue of whether regulation should be built into the telecommunication act of 1996 to offset market forces arising with the new information infrastructure. Simply this can also be defined as the Gap between the haves and have-nots in relation to Access to Information and Technology. The Digital Divide is a term encapsulating the dramatic worldwide variation in access to information technologies available to individuals and communities

2.1.8 Client/server architecture:

Client/server systems operate in networked environments, splitting the processing of an application between a front-end client and a back-end processor. Generally, the client process requires some resource, which the server provides to the client. Clients and servers can reside in the same computer, or they can be on different computers that are networked together. Both clients and servers are intelligent and programmable, so the computing power of both can be used to devise effective and efficient applications.

The client server architecture has evolved from file server architecture and database server or two tier architecture. The client server architecture is called as three tier architecture.

2.1.9 File Server Architectures

In a basic file server environment all data manipulation occurs at the workstations where data are requested. The client handles the presentation logic, processing logic, and much of the storage logic (that part associated with a DBMS). One or more file servers are attached to the

LAN. A file server is a device that manages file operations and is shared by each of the client PCs attached to the LAN. Here each file servers act as an additional hard disk for each of the client

PC. For example, your PC might recognize a logical F: drive, which is actually a disk volume stored on a file server on the LAN. Programs on your PC refer to files on this drive by the typical path specification involving this drive and any directories, as well as the file name. With a file server, each client PC may be called a fat client, one where most processing occurs on the client rather than on a server. This enables the user to access all the data. The following figure illustrates the file server architecture.

2.2 Limitations of File Servers

There are three limitations when using file servers on LANs. Large amount of data required for processing and generated across the network is transferred to the client PC and the server does very little work. The processing burden will be on the client PC and creates a high network traffic load.

Second, the client being involved with server functions each client workstation must devote memory to a full version of the DBMS. This means that there is less room in memory for application programs on the client Pc. Hence RAM memory of client need to be increased.

Third, and possibly most important, the systems in each workstation must manage the integrity of the data shared between different clients.

2.3 Database Server Architectures/Two Tier Architecture

In this system, the client workstation is responsible for managing the user interface, including presentation logic, data processing logic, and business rules logic, and the database server is responsible for database storage, access, and processing. The following figure represents the two tieer architecture. With the application systems and information systems placed on the database server, LAN traffic is reduced, because only those records that match the requested criteria are transmitted to the client station, rather than entire data files. Some people refer to the central DBMS functions as the back-end functions, whereas they call the application programs on the client PCs the front-end programs.

Most of the limitations of file server is eliminated in two tier architecture. With this architecture, only the server requires processing power adequate to handle the database, and the systems is stored on the server, not on the clients. Therefore, the database server can be tuned to optimize

database-processing performance. Because less data are sent across the LAN, the communication load is reduced. User authorization, integrity checking, data dictionary maintenance, and query and update processing are all performed at one location, on the database server. The advantages of two tier architecture are:

Performance improves for compiled query

Network traffic decreases as processing moves from the client to the server.

Security improves if the stored procedure is accessed rather than the data and code is moved to the server, away from direct end-user access.

Data integrity improves as multiple applications access the same stored procedure.

Stored procedures result in a thinner client and a fatter database server.

However, the disadvantages of this architecture are:

Writing stored procedures takes more time

The proprietary nature of stored procedures reduces their portability and may make it difficult to change

These drawbacks to database server architectures have led to the popularity of three-tier architectures.

2.4 Three tier architecture

Client/server systems: A networked computing model that distributes processes between clients and servers, which supply the requested services. In a database system, the database generally resides on a server that processes the application packages and information systems.

The clients may process the application systems or request services from another server that holds the application programs.

The several client/server architectures that have evolved can be distinguished by the distribution of application logic components across clients and servers. There are three components of application logic. The following figure represents the client server architecture.

The first is the input/output (I/O), or presentation logic, component. This component is responsible for formatting and presenting data on the user's screen or other output device, and for managing user input from a keyboard or other input device. The second component is the processing component. It handles data processing logic, business rules logic, and data management logic. Data processing logic includes such activities as data validation and identification of processing errors. Business rules that have not been coded at the DBMS level may be coded in the processing component. Data management logic identifies the data necessary for processing the transaction or query. The third component is storage, the component responsible for data storage and retrieval from the physical storage devices associated with the application.

Advantages of the three-tier compared with the two-tier architecture, such as increased scalability, flexibility, performance, and reusability, have made three-layer architectures a popular choice for Internet applications and net-centric information systems.

Three-tier architectures can provide several benefits (Thompson, 1997):

Scalability Three-tier architectures are more scalable than two-tier architectures. For example, the middle tier can be used to reduce the load on a database server by using a transaction processing (TP) monitor to reduce the number of connections to a server, and additional application servers can be added to distribute application processing. A TP monitor is a program that controls data transfer between clients and servers in order to provide a consistent environment for on-line transaction processing (OLTP).

Technological flexibility It is easier to change the systems, though triggers and stored procedures will need to be rewritten, with a three-tier architecture. The middle tier can even be moved to a different platform. Simplified presentation services make it easier to

 implement various desired interfaces such as Web browsers or kiosks.

Lower long-term costs Use of off-the-shelf components or services in the middle tier can

reduce costs, as can substitution of modules within an application rather than an entire application.

Better match of systems to business needs New modules can be built to support specific business needs rather than building more general, complete applications.

Improved customer service Multiple interfaces on different clients can access the same business processes.

Competitive advantage The ability to react to business changes quickly by changing small modules of code rather than entire applications can be used to gain a competitive advantage.

Reduced risk Again, the ability to implement small modules of code quickly and combine them with code purchased from vendors limits the risk assumed with a large-scale development project.

Electronic commerce requires that client browsers be able to access dynamic Web pages attached to databases that provide real-time information. Personal computers linked through networks that support workgroup computing are the norm. Mainframe applications have been rewritten to run in client/server environments and take advantage of the greater costeffectiveness of networks of personal computers and workstations. The need for strategies that fit specific business environments is being filled by client/server solutions because they offer flexibility, scalability (the ability to upgrade a system without having to redesign it), and extensibility (the ability to define new data types and operations).

UNIT 3

DATA WAREHOUSING AND DATA MINING

Structure:

3.1

3.2

3.3

Data warehousing and Data Mining

Conclusion

Exercise

3.1 Data warehousing and Data Mining

Data warehousing and Data Mining (Explained in the chapter ‘database, data warehouse an data mining)

Object-oriented environment:

The object oriented environment represents a system where object oriented systems are developed and used. These systems are usually made available on client server system/architecture. The object oriented systems are developed using object oriented programming language, object oriented modeling and Unified Modeling Language. Object oriented approach is developed using the concept like object, class.

A class is an entity that has a well-defined role in the application domain about which the organization wishes to maintain state, behavior, and identity. A class is a concept, abstraction, or thing that makes sense in an application context. A class could be a tangible or visible entity

(e.g., a person, place, or thing); it could be a concept or event (e.g., Department, Performance,

Marriage, Registration, etc.); or it could be an artifact of the design process (e.g., User Interface,

Controller, Scheduler, etc.).

In simple terms an object is any thing that we can feel and touch. Technically, an object is an instance of a class (e.g., a particular person, place, or thing) that encapsulates the data and behavior we need to maintain about that object. A class of objects shares a common set of attributes and behaviors.

The state of an object encompasses its properties (attributes and relationships) and the values those properties have, and its behavior represents how an object acts and reacts. An object's state is determined by its attribute values and links to other objects. An object's behavior depends on its state and the operation being performed. An operation is simply an action that one object performs upon another in order to get a response. An object is a self contained unit which contains both operations and as well as data. Following example represents a simple example of an object in object oriented language. ‘student’ which has relationship with the

‘course’ being offered.

class Student {

attribute string name;

attribute Date dateOfBirth;

attribute Address address;

attribute Phone phone;

// relationship between Student and CourseOffering relationship set <CourseOffering> takes inverse CourseOffering::taken_by;

// operations

short age();

float gpa();

boolean register_for(string crse, short see, string term);

};

Here it is not possible to explain in detail about object oriented approach as Object Oriented

Programming in itself is a subject. Student interested in the may refer books on C and C++ programming language. An easy to understand book is authored by Balaguruswamy.

Middleware:

A set of services required for providing connectivity and management services in a distributed computing environment. These services include database connectivity, messaging, remote procedure calls, object request brokers, transaction services, timing services, and naming services.

Middleware is often referred to as the glue that holds together client/server applications. It is a term that is commonly used to describe any software component between the pc-client and the relational database in n-tier architectures. Simply put, middleware is any of several classes of software that allow an application to interoperate with other software without requiring the user to understand and code the low-level operations required to achieve interoperability.

Middleware has existed for decades-examples are IBM's transaction-processing middleware,

CICS etc, it is only after the advent of client/server technologies, and now Web-oriented development, has stimulated new development of commercially available middleware.

Universal middleware, one magical software package that could integrate and connect every type of system, would be ideal, of course. At this time, however, such a middleware package is not available. Most organizations use several different middleware packages, sometimes even within one application. The development of e-business requires that computers be able to communicate with each other over the Web.

Another consideration is whether the communication involved is synchronous or asynchronous.

With synchronous systems, the requesting system waits for a response to the request in real time. An online banking system where the teller checks an account balance before cashing a check is an example of a synchronous system. Asynchronous systems send a request but do not wait for a response in real time. Rather, the response is accepted whenever it is received.

Electronic mail is an example of an asynchronous system with which you are probably familiar.

Following are the categories based on scalability and recoverability of middleware.

1. Asynchronous remote procedure call (RPC) The client requests services but does not wait for a response. It will typically establish a point-to-point connection with the server and perform other processing while it waits for the response. If the connection is lost, the client must reestablish the connection and send the request again. This type of middleware has high scalability but low recoverability, and has been largely replaced by synchronous RPC since 1998.

2. Synchronous RPC A distributed program using synchronous RPC may call services available on different computers. This middleware makes it possible to establish this facility without undertaking the detailed coding usually necessary to write an RPC.

Examples include Microsoft's Transaction Server and IBM's CICS. The Java equivalent of an RPC is a Remote Method Invocation (RMI).

3. Publish/subscribe This type of middleware monitors activity and pushes information to subscribers. It is asynchronous-the clients, or subscribers, perform other activities between notifications from the server. The subscribers notify the publisher of information that they wish to receive, and when an event occurs that contains such information, it is sent to the subscriber, who can then elect to receive the information or not. For example, you can supply electronic bookstores with keywords of topics that interest you. Whenever the bookstore adds a book title that is keyword coded with one of your keywords, information about that title will be automatically forwarded to you for consideration. This type of middleware is very useful for monitoring situations

where actions need to be taken when particular events occur.

4. Message-oriented middleware (MOM) MOM is also asynchronous software, sending messages that are collected and stored until they are acted upon, while the client continues with other processing. Workflow applications such as insurance policy applications, which often involve several processing steps, can benefit from MOM. The queue where the requests are stored can be journalized, thus providing some recoverability.

5. Object request broker (ORB) This type of middleware makes it possible for applications to send objects and request services in an object-oriented system. The ORB tracks the location of each object and routes requests to each object. Current ORBs are synchronous, but asynchronous ORBs are being developed.

6. SQL-oriented data access Connecting applications to databases over networks is achieved by using SQL-oriented data access middleware. This middleware also has the capability to translate generic SQL into the SQL specific to the database. Database vendors and companies that have developed multidatabase access middleware dominate this middleware segment.

In client/server systems, database-oriented middleware provides some sort of application program interface (API) access to a database. APIs are sets of routines that an application program uses to direct the performance of procedures by the computer's operating system. For example, in achieving access to a database, an API calls library routines that transparently route

SQL commands from the front-end client application to the database server. An API might work with existing front-end software, such as a third-generation language or custom report generator, and it might include its own facilities for building applications. When APIs exist for several program development tools, then you have considerable independence to develop client applications in the most convenient front-end programming environment yet still draw data from a common server database. Such middleware makes it possible for developers to link an application easily to popular databases.

Open database connectivity (ODBC) is similar to API, but is for Windows-based client/server applications. It is most useful for accessing relational data, and not well suited for accessing other types of data, such as ISAM files (LaRue, 1997). Even though ODBC is difficult to program and implement, it has been well accepted because it allows programmers to make connections to almost any vendor's database without learning proprietary code specific to that database. For a more detailed discussion of ODBC and establishing Internet database connectivity, see Chapter

10. Microsoft's OLE-DB adds value to the ODBC standard by providing a single point of access to multiple databases (Linthicum, 1997). Microsoft is planning to make OLE-DB a universal data access standard and has added OLE-DB for data mining applications and OLE-DB for OLAP.

Access to legacy data while moving to client/server systems can be achieved by products such as

EDA/SQL, which attempt to support many different operating systems, networks, and databases.

Java Database Connectivity (JDBC) classes can be used to help an applet access any number of databases without understanding the native features of each database. JDBC defines a call-level interface (CLI) for Java development and borrows from ODBC conventions. Establishing a common language to define interfaces between components and a mechanism to mediate will facilitate the development of universal middleware (Keuffel, 1997). The Object Management

Group (OM G), established in 1989, is an industry coalition that has produced the Common

Object Request Broker Architecture (CORBA), which sets the specification of object-oriented universal middleware. Microsoft has developed a competing model, Distributed Component

Object Model (DCOM), but CORBA is a more robust specification because it has been developed to handle many different platforms. Interoperability between the two standards is slowly emerging. Such standards are particularly important on the World Wide Web because of the diversity of the platforms that are connecting.

UNIT 4

MOBILE AND WAP

Structure:

4.1

4.2

4.3

4.4

4.5

M-Commerce and Mobile Database

Data in Mobile Applications

Criteria for selecting a database solution

Wireless Application Protocol

Conclusion

4.1 M-Commerce And Mobile Database

M-commerce or Mobile commerce is commerce (buying and selling of goods and services) in a wireless environment, such as through wireless devices like cellular telephones and PDAs. Mcommerce enables users to access the Internet without needing to find a place to “plug” in their device. As this wireless environment expands, a pervasive computing environment will develop, employed by mobile employees and others, will change the way business is transacted. Mobile commerce is achieved by having mobile data base. The mobile computing environment is some time referred as portable computing. Personal Digital Assistants, Slim laptops, Point of Sales terminals etc are some common examples of portable devices.

4.1.1 Mobile Database

Mobile databases facilitate a user to access the data from any where at any time. However, security will be a major challenge. The client device is portable where the data is stored and is accessible with mobile data base application, Operating system and DBMS. While the client is accessing the data from the server which is basically will be a mainframe computer in which data is managed with conventional DBMS and one would face the problem of connectivity and rely on wireless networking. This requires a continuous connectivity with the base.

Figure: Mobile database environment

Figure: General architecture of Mobile Platform

While developing mobile applications, one needs to store data (sometimes lots of data) on client devices. This is obvious with batch applications where users will infrequently synchronize with the server. Sometimes, you may be able to rely on wireless networking, but sporadic loss of connectivity caused by Radio Frequency interference and spotty coverage can disrupt the use of the application. As a practical matter, then, maintaining data on the client device is a necessity.

Current database tools make it fairly easy to replicate data on the mobile device. The client devices and server databases remain synchronized, making it possible for users to continually

use the mobile application even when disconnected. Unfortunately, many mobile software developers store data in flat files on the client device. This makes applications run very slowly.

By adopting "wireless-ready" relational databases the access speed increases 20-30 times faster than the flat files. With careful database design, relational databases can also significantly improve data integrity.

4.2 Data in Mobile Applications

Data in mobile applications are classified as horizontal and vertical applications. In horizontal applications, the user cooperates on performing a task and they can handle data distributed through the network. Mail enabled applications and information services are examples of horizontal applications. IN vertical applications user accesses data within a specific cell but not outside the cell. User checking the availability of drug or doctor at the hospital premise with in a cell is an example for vertical application.

4.2.1 Data Classification in Mobile Database

Similarly the data is classified as public, private and shared data on the mobile databases.

Data owned and managed by a single user is private data, data that is accessed by any one but managed by one user is called private, and shared data is one which is read, updated by a group of users. Public data is usually managed by vertical applications and shared data by horizontal applications.

4.2.2 On the Road to Mobile Databases

The flourishing of mobile devices is driving businesses to deliver data to employees and customers wherever they may be. The potential of mobile gear with mobile data is enormous. A salesperson equipped with a PDA running corporate databases can check order status, sales history, and inventory instantly from the client's site. And drivers can use handheld computers to log deliveries and report order changes for a more efficient supply chain.

Sybase's SQL Anywhere currently dominates the mobile database market. The company has deployed SQL Anywhere to 6 million users at over 10,000 sites and serves 68 percent of the

mobile database market, according to a recent Gartner Dataquest study. Other mobile databases include IBM's DB2 Everyplace 7, Microsoft SQL Server 2000 Windows CE Edition, and

Oracle9i Lite. Smaller player Gupta Technologies' SQLBase also targets handheld devices.

Mobile databases are often stripped-down versions of their server-based counterparts. They contain only basic SQL operations because of limited resources on the devices. In addition to storage requirements for data tables, the database engines require from 125K to 1MB, depending on how well the vendor was able to streamline its code.

Mobile databases replicate data among themselves and with a central database. Replication involves examining a database source for changes due to recent transactions and propagating the changes asynchronously to other database targets. Replication must be asynchronous, because users don't have constant connections to the central database.

Transaction-based replication, in which only complete transactions are replicated, is crucial for integrity across the databases: Replicating partial transactions would lead to chaos. Serial transaction replication is important to maintain the same order in each database. This process prevents inconsistencies among the databases. Another consideration in mobile database deployment is how conflicts over multiple updates to the same record will be resolved.

Though the market is still evolving, there's already a sizeable choice of sturdy products that will extend your business's data to mobile workers.

4.3 Criteria for selecting a database solution for mobile applications

4.3.1 OS Platform a Likely Requirement

In many mobile systems, the platform of the client device will be chosen early in the life of the project, because, the client may require the application to be run on specific platform. For example, a client wanting the application solution being developed for a city government that wanted housing inspectors to use Compaq iPaqs when collecting data at building construction sites to run on Windows CE. As with this and other

applications, the platform is usually a requirement that drives the initial selection of the database.

Platform support is a key issue in choosing a mobile database: No organization wants to devote development and training resources to a platform that may become obsolete. Microsoft's mobile database supports Win32 and Windows CE. The IBM, Oracle, and Sybase products support Linux, Palm OS, QNX Neutrino, Symbian EPOC, Windows CE, and Win32.

If application software as a product is available apart from the client device, then the database must work on a wide variety of platforms. This reduces the need to support multiple versions of the software, which can be extremely costly. One can develop an application PalmOS devices using Satellite Forms and at the same time by using other tools like PointBase and Java, the same application can be made available to work on

Windows CE and Linux platforms. Satellite Forms is a really good tool for rapid mobile application development, but it only operates on PalmOS. Thus with a single databasedevelopment environment a single application can be used on different environments.

Other mobile databases (such as IBM DB2 Everyplace and Oracle9i Lite) also feature broad platform support.

4.3.2 Session Persistence Increases Usability

Wired systems generally provide continuous connectivity between client devices and the server. For example, the synchronization process between a PDA and a PC over a USB connection seldom fails. It's a different story with wireless systems, which demand smarter client software and is helpful if the mobile database has built-in data synchronization. This works automatically and compensates for bumps that occur with wireless connectivity. For example, Oracle9i Lite incorporates client-side representation of server data and applications. As a user unplugs a batch device from the cradle, or a wireless phone or PDA loses connection with the server database, the application continues to run with the most up-to-date information in the database. After the client device regains the connection, Oracle9i Lite automatically updates the central server database.

If session-persistence features are not available in the database of choice, then we need to consider incorporating wireless middleware. Middleware acts as a intermediary between the client device and the server. If wireless connections are lost, middleware maintains appropriate communications with the server. This keeps sessions open, and minimizes headaches for users. It also provides necessary synchronization of data.

4.3.3 Security Provisions Protect Data

Security is very important for mobile applications, especially wireless and public solutions. For example, a financial institution sending credit card numbers over a wireless network must pay close attention to security. Freely available sniffing tools make it easy to intercept unencrypted data.

Wireless standards (such as 802.11 and Wi-Fi) offer encryption, but it only applies to the air interface. Client devices encrypt the data before transmission, and the access point performs decryption before forwarding the data to the server. As a result, data sent over the distribution system (whether Ethernet or the public Internet) is sent ensures that any transfer of data is not left open to hackers.

4.3.4 Memory Footprint Matters

Because client devices have limited memory, the mobile database software should fit within a relatively small footprint. Look for databases requiring less than 500 KB of memory. For example, the Sybase iAnywhere UltraLite database only needs 150 KB of space. PointBase does even better at only 45 KB. This maximizes storage area for such important things as application software and data. In fact, memory footprint is one of the biggest differences between a mobile database and one that runs on a server. You no longer need to allow 1.5 MB for your mobile database applications.

4.3.5 Connection Protocols Make a Difference

In both batch and wireless applications synchronization of data between the client device and a server is to be ensured so that the chosen database works with specialized synchronization services or server products. Standard APIs (such as ODBC and JDBC)

are crucial for most applications, especially if the interface required for one particular vendor's database on the client device to another one on the server. For example, Oracle9i

Lite offers support for ODBC and JDBC, which lets companies integrate and migrate to different applications as needs change. However, the standard connectivity interfaces can run slower than proprietary connections.

4.3.6 Development Tools Make Life Easier

Generally, enterprise databases have better development tool support than mobile database. However, there are exceptions. For example, iAnywhere supports all leading development tool environments (such as Java, Sybase PowerBuilder, Microsoft Visual

Basic, Visual C++, Visual Studio .NET, AppForge MobileVB, Symantec Visual Cafe,

Borland Delphi, and Metrowerks CodeWarrior).Oracle9i Lite offers a Mobile

Development Kit, which includes many APIs and sample code that accelerates the development of mobile applications. The Development Kit also offers an effective packaging wizard that ties together all mobile application components (such as executables, DLLs, and images) into a self-executable file that is easily deployed to client devices. Toolkits such as this are essential for getting started on the right foot.

4.3.7 Open Source Tools Offer Flexibility

To improve flexibility, one can consider using an Open Source mobile database (such as

Sleepycat's Berkeley DB). With this approach, the dependency on an outside vendor that may make changes that impact your software would be eliminated and can have total control over all changes. Open Source solutions provide plenty of flexibility, but the total responsibility of coding and supporting the application rests on the application developer.

Also, the relatively wide distribution often means that Open Source solutions have been subjected to more technical scrutiny than comparable proprietary databases.

4.3.8 Management Utilities Ease Support

When selecting a mobile database, we need to keep management features in mind. For example, Oracle9i Lite includes a mobile server control center, which enables fairly good server-side management of the mobile applications and users. After publishing the

application, an administrator uses the control center to assign application access privileges to users and views status of client devices. To maximize interoperability with management tools, consider choosing a mobile database supplied by the server-side database vendor.

The Open Source solutions (such as Berkeley DB) don't require management utilities separate from what the existing operating system provides. Berkeley DB uses standard operating system services to perform maintenance tasks such as backups, recovery, performance-tuning, and routine maintenance. This results in a much more integrated solution, which reduces total cost of ownership.

4.3.9 Previous Experience a Deciding Factor

To break a tie between equivalent databases, it is feasible to consider the experience with developing database applications. If one is been implementing Microsoft database applications for the past ten years, then it is advisable to strongly consider using

Microsoft tools. For example, Microsoft offers SQL Server CE 2.0 that supports the familiar Structured SQL syntax and APIs consistent with Microsoft SQL Server. This would be familiar grounds with a minimal learning curve. The extra time needed to learn a new development environment would always impact the schedule and budget of the project. At least today, the major database vendors (such as Oracle, Microsoft, Sybase, and IBM) have mobile database solutions. It's likely that one of those comply with your previous experience.

4.4 Wireless Application Protocol

Wireless Application Protocol (WAP) is a protocol like Internet Protocol (TCP/IP HTTP and others) supports wireless network like CDPD, CDMA, GSM, PDC, PHS, TDMA, FLEX, ReFLEX, IDEN,

TETRA, DECT, DataTAC, Mobitex ETC., and allows user to access information instantly using wireless devices such as mobile phones, pagers, smart phones. WAP was made necessary to

change the internet protocols and document types. The need for a new standard to meet the requirements Ericsson, Motorola, Nokia and Unwired Planet founded the WAP forum a non profit organization to work on WAP standards. Later, many these giants joined hand with them to work on WAP.

WAP use ‘micro browsers’ to display and access the Internet. The microbrowsers are browsers are small screens and used with out keyboard. These are useful because they can accommodate the low memory constraints of hand help devices and low bandwidth constraints of a wireless handheld network. The advantage with this is that it also supports HTML, XML but WML is designed specifically for these browsers.

WAP is created using different technologies like Global System for Mobile Communication

(GSM), Short Messaging Services (SMS), Unstructured Supplementary Services Data (USSD) etc., enabling to work in full duplex mode and transmit data and documents from one place to another. The major challenge WAP needed to focus on the security, session and transaction management. The WAP protocol provide features that are necessary for web browsing similar to HTTP and TCP/IP protocols, define the format of the document using WML. WML scripts a simplified version of Java Scripts facilitate insertion of objects similar to HTML.

WAP has two components. One is an end to end application protocol and an application environment based on the browser. (Figure WAP network architecture and programming model). The application protocol is a communication protocol that is embedded in the WAP enabled client device. The server implements the other side of the protocol enabling the gateway to communicate with any client. The server side is known as WAP gateway and WAP gateway can be implemented either with telecom or ISP network.

The major function of WAP gateway is to translate the client requests to WWW request and vice versa. The programming model of WAP is also similar to web/HTTP programming and uses WML

instead of HTML. The advantage of WML is that by adopting java script as WML script it can enable a programmer to develop the program enabling the system to provide the functions of validity of user input and send message or dialog box. The features of WML are illustrated in the above section.

The Wireless Markup Language (WML)

WML is a markup language that is based on XML (eXtensible Markup Language). The official

WML specification is developed and maintained by the WAP Forum, an industry-wide consortium founded by Nokia, Phone.com, Motorola, and Ericsson. This specification defines the syntax, variables, and elements used in a valid WML file. A valid WML document must correspond to this DTD or it cannot be processed.

WML has garnered enormous support due its ability to describe data (HTML, meanwhile, is used to describe the display of data...a big difference). WML has gained importance due to the fact that the applications that are developed must enable the data access with mobile devices. It gains importance for the simple reason that the devices have specific characteristics like small display, limited input capability, narrowband network connection, limited memory, and limited CPU power etc.,

We also need to understand that WML functions different from a normal web delivery of pages to HTML browsers. A gateway is used to do a protocol conversion when a document is delivered from an origin server to a phone. A protocol conversion also occurs.

While HTML predefines a "canned" set of tags guaranteed to be understood and displayed in a uniform fashion by a Web browser, XML allows the document creator to define any set of tags he or she wishes to. This set of tags is then grouped into a set of grammar "rules" known as the

Document Type Definition, or DTD.

If a phone or other communications device is said to be WAP-capable, this means that it has a piece of software loaded onto it (known as a microbrowser) that fully understands how to handle all entities in the WML 1.1 DTD.

The first statement within an XML document is known as a prolog. While the prolog is optional, it consists of two lines of code: the XML declaration (used to define the XML version) and the document type declaration (a pointer to a file that contains this document’s DTD). A sample prolog is as follows:

<xml version='1.0'>

<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"

"http://www.wapforum.org/DTD/wml_1.1.xml">

Following the prolog, every XML document contains a single element that contains all other subelements and entities. Like HTML all elements are bracketed by the < > and </> characters. As an example: <code><element>datadatadata</element></code>. There can only be one document element per document. With WML, the document element is

<code><wml></code>; all other elements are contained within it.

The two most common ways to store data within an XML document are elements and attributes. Elements are structured items within the document that are denoted by opening and closing element tags. Elements can also contain sub-elements as well. Attributes, meanwhile, are generally used to describe an element. As an example, consider the following code snippet:

<!-- This is the Login Card -->

<card id="LoginCard" title="Login">

Please select your user name.

</card>

In the code above, the card element contains the id and title attributes. (On a side note, a comment in WML must appear between the tags.) We will make use of the WMLdefined elements and their attributes later as we build our examples.

Valid WML Elements

WML predefines a set of elements that can be combined together to create a WML document.

These elements include can be broken down into two groups: the Deck/Card elements and the

Event elements.

Deck/Card Elements wml Card template head access meta

Event Elements do ontimer onenterforward onenterbackward onpick onevent postfield

Tasks go prev refresh noop

Variables

Setvar

User input input select option optgroup Fieldset

Anchors, Images, and Timers a anchor img timer

Text Formatting

Following are few formatting elements.

Br: the tag enables to break the line. ‘P’ creates a paragraph. ‘Table; represents table and

‘tr’ and ‘td’ represents table row and table data/values

Each of these elements is entered into the document using the following syntax:

<element> element value </element>

If an element has no data between it (as is often the case with formatting elements such as

<br> ), you can save space by entering one tag appended with a \ character (for instance,

<br/> ).

Building Applications With WML

WML was designed for low-bandwidth, small-display devices. As part of this design, the concept of a deck of cards was utilized. A single WML document (i.e. the elements contained within the

<wml> document element) is known as a deck. A single interaction between a user agent and a user is known as a card. The beauty of this design is that multiple screens can be downloaded to the client in a single retrieval. Using WMLScript, user selections or entries can be handled and routed to already loaded cards, thereby eliminating excessive transactions with remote servers.

Of course, with limited client capabilities comes another tradeoff. Depending on your client’s memory capabilities, it may be necessary to split multiple cards up into multiple decks to prevent a single deck from becoming too large.

Using Variables

Because multiple cards can be contained within one deck, some mechanism needs to be in place to hold data as the user traverses from card to card. This mechanism is provided via WML variables. Variables can be created and set using several different methods. For instance:

 Using the <setvar> element as a result of the user executing some task. The

<setvar> element can be used to set a variable’s state within the following elements:

 go, prev, and refresh. The following element would create a variable named x with a value of 123:

<setvar name="x" value="123"/>

Variables are also set through any input element (input, select, option, etc.). A variable is automatically created that corresponds with the name attribute of an input element.

For instance, the following element would create a variable named x:

<select name="x" title="X Value:">

Although we haven’t discussed WMLScript yet, it is important to note that WML and

WMLScript within a document share the same variables.

Creating A WML Deck

In this example, we’ll start by creating a WML deck that allows us to first select a username from a list, enter in a password, then have our selections repeated back to us. This will illustrate the basic handling of user input, events, and variables all within one deck using multiple cards.

Listing 1 - WMLExample.wml

<?xml version='1.0'?>

<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"

"http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>

<card id="Login" title="Login">

<do type="accept" label="Password">

</do>

<go href="#Password"/>

<p>

UserName:

<select name="name" title="Name:">

<option value="John Doe">John Doe</option>

<option value="Paul Smith">Paul Smith</option>

<option value="Joe Dean">Joe Dean</option>

</select>

<option value="Bill Todd">Bill Todd</option>

</card>

</p>

<card id="Password" title="Password:">

<do type="accept" label="Results">

<go href="#Results"/>

</do>

<p>

Password: <input type="text" name="password"/>

</card>

</p>

<card id="Results" title="Results:">

<p>

You entered:<br/>

Name: $(name)<br/>

Password: $(password)<br/>

</p>

</wml>

</card>

As you can see, the prolog of this document contains the XML version number to be used as well as the Document Type Definition to be referenced. Following DTD, WML document element (the deck) contains three cards: Login, Password, and Results. Each of these cards is defined using the element. Because the Login and Password cards also

define events, they use the element to define the event to be triggered. Following Figure shows the initial card loaded in a test browser.

Figure 16-3

When the "accept" type of the do element is encountered, it is displayed as an option on the WAP device display as shown in the following figure.

Selecting this option causes the element to be analyzed.

If you are familiar with the anchor tag () in HTML, you know that it specifies an href attribute that tells the browser where to link to if this anchor is selected (href element in

HTML enables the user to link the text with in a document and external document). The

WML element’s "href" attribute works in the same manner. As with HTML, to link to another card in the document, you simply prepend a # symbol before it. For example, to link to the Results card, we define the following element:

<go href="#Results"/>

This Results card makes use of variables by retrieving and displaying the contents of the name and password variables. Recall that variables are substituted into a card or deck by using the following syntax:

$(variable_name)

WML offers software developers an entirely new, exciting platform on which to deploy their applications. With this new platform, however, comes a host of tradeoffs and challenges. A new wrinkle will be added to the design process as things like server round-trips, bandwidth, and display sizes become issues to contend with. While it may take several iterations for developers and vendors to get their product offerings right, there is no doubt that WAP opens the door to a new era in application development and deployment.

We have seen the tremendous opportunities the mobile computing and database has today and people are eager to tap the potential and convert the same to their benefits. Though the standards and standard languages are developed still, the technology behind this booming concept is to be given importance. It needs attention with reference to the hardware devices used in using the data which range from a laptop to mobile handset, its storage capacity speed with which the device can access and transfer the data, information and objects etc. One has to give importance to database requirements and graphic user interfaces to enhance the online accessibility to the data and information from any location to facilitate the transaction to be complete and update immediately on the host server or database. The database should be such that it should facilitate the data to be stored with a back up in a limited disk space. To be precise, the database should provide secure and with improved performance capability. The mobile device and database need communication requirements that are different from a traditional database environment. The mobile database should be web enabled to facilitate the user to work from any point where the challenge is to provide the interfaces with out compromising the functionality and ease of use. The bottom line is to make the user to access, process and use the data and information from any where any time.

4.5 Conclusion

Aligning business process and information and communication technology in the organization, enables the management to achieve success. Analyzing the business forces that influence a company’s competitive position will assist management in developing an appropriate strategy aimed at establishing a sustained competitive advantage. To establish such a position, it is

Information and communication technology that helps the company to develop a strategy of performing activities differently than a competitor. This success could be seen in the following factors.

 Cost leadership: Appropriate utilization of ICT enables the organization to produce and provide products and/or services at the lowest cost in the industry.

 Differentiation strategy: Continuous improvement according to the market need would enable the organization to offer different products, services, or product features.

 Niche strategy: Depending upon the situation, organization may choose/select a narrow-scope segment (niche market) and be the best in quality, speed, or cost in that market.

 Growth strategy: Effective adoption and utilization enables the company to increase market share, acquire more customers, or sell more products.

 Alliance strategy: ICT promotes and encourages working with business partners in partnerships, alliances, joint ventures, or virtual companies.

 Innovation strategy: Continuous improvements enables the company to introduce new products and services, put new features in existing products and services, or develop new ways to produce them.

 Operational effectiveness strategy: Improve the manner in which internal business processes are executed so that a firm performs similar activities better than other players in the industry.

 Customer-orientation strategy: Concentrate on making customers happy

 Time strategy: Effective and appropriate ICT tools and its implementation facilitates effective and efficient use of time as a resource, then manage it and use it to the firm’s advantage.

 Entry-barriers: Create barriers to new entrants in the industry.

 Lock in customers or suppliers: Fast and quality services encourage customers or suppliers to stay with the organization rather than going to the competitors.

 Increase switching costs: Discourage customers or suppliers from going to competitors for economic reasons.

Selection and implementation of appropriate information and communication technology add value to the activities/process/functions and hence enables to achieve the desired result, productivity and advantage. According to the Michel Porter the activities conducted in any organization can be divided into two parts: primary activities and support activities. Primary activities are those activities in which materials are purchased, processed into products, and delivered to customers. Since each activity are related to each other, and add value to each other, the whole process is known as value chain. The functions/tasks/activities identified under primary activities are:

Inbound logistics (inputs)

Operations (manufacturing and testing)

Outbound logistics (storage and distribution)

Marketing and sales

Service

Unlike the primary activities, which directly add value to the product or service, the support activities are operations that support the creation of value (primary activities) are:

The firm’s infrastructure (accounting, finance, management)

Human resources management

Technology development (R&D)

Procurement

While we observe primary and support activities in the organization, one would realise that information and communication technology as tools, information system, Enterprise resource planning, enterprise applications, supply chain management, customer relationship management or web enabled system can play a vital role in enabling the organization to achieve the set vision.

With the development of such magnificent technologies which allow to navigate in the information ocean, Dennis Tsichritzis says that the following features will be in common in near future.

1. Relative time : Since we can go back in time via on demand access to video databases and ahead in time via realistic simulation (virtual systems), the notion of time becomes vary flexible. With such system which allows to go back and move ahead one can move at any speed in any direction. This indicates that people would like to move relatively in past and future direction.

2.

3.

Real virtual systems : The new quality of virtual environment renders the distinction between real and unreal impossible. Here imagination poses the only limits.Man's imagination will emerge as virtual systems which is now making its entry in the real systems.

Open space : Since any environment can be brought to us ie real or virtual and we can insert ourselves in any foreign environment, space becomes plastic. We no longer move and act only in our own proximity but also entre into the realistic virtual environment which is described as 'open space '.

4.

5.

6.

Hyper-interactive : With the realistic time, open space features there is no limit to the speed, the degree and channels of interaction. One can interact from any corner to each other.

Active presence : With much interactive activity going on any subject, passive presence equals nonexistence. It will be difficult to defend any thing like status, position, acceptance without aggressive participation.

Indirect control : The IT will enable the remote control of things and operations.

However the nice feeling of absolute and immediate control is lost.

Dennis has the opinion that in such an environment the tools which will be developed are a. Imagination Description Language(IDL) : A high level language to describe what we imagine.

b. c.

Virtual Character Recognition : A scripting tool to outline the behaviour of the animated character.

Community Forum Moderation: Tool to coordinate and help to participate in the multitude of the existing communities and tools for services that do not necessarily provide any thing useful but create a good feeling for the people using them.

It emphasizes, that in future success will come to the thinkers who act fast and thrive in chaotic environments and necessarily to the highly organized or the hardworking. Whatever the technology developed it is we who decide about using and promoting the use of the technology. Therefore the need of the hour is to adopt open minded attitude towards learning rather than being negative in using these products.

Module 3

Unit1

MICROSOFT WORD

Structure

1.1.

1.2.

1.3.

1.4.

1.5.

1.6.

1.7.

1.8.

Introduction

Getting Start with Word 2007

Work area

Creating, Saving, Closing The Document And Quitting Word

Opening , Editing And Proofing A Document

Formatting Text and Paragraphs

Styles

Tables

1.1. Introduction

Microsoft Word is a word processing software package. You can use it to type letters, reports, and other documents. It helps you to produce professional-looking documents by providing a comprehensive set of tools for creating and formatting your document in a new interface . Word

2007 presents a new user-interface which is quite different than previous versions of Word.

WHAT IS WORD-PROCESSING?

Word Processor is a Software package that enables you to create, edit, print and save documents for future retrieval and reference. Creating a document involves typing by using a keyboard and saving it. Editing a document involves correcting the spelling mistakes, if any, deleting or moving words sentences or paragraphs.

(a) Advantages of Word Processing

One of the main advantages of a word processor over a conventional typewriter is that a word processor enables you to make changes to a document without retyping the entire document.

(b) Features of Word Processing

Most Word Processor available today allows more than just creating and editing documents.

They have wide range of other tools and functions, which are used in formatting the documents.

The following are the main features of a Word Processor

Text is typing into the computer, which allows alterations to be made easily.

Words and sentences can be inserted, amended or deleted.

Paragraphs or text can be copied /moved throughout the document.

Margins and page length can be adjusted as desired.

Spelling can be checked and modified through the spell check facility.

Multiple document/files can be merged.

Multiple copies of letters can be generated with different addresses through the mailmerge facility.

IMPORTANT FEATURES OF MS-WORD

Ms-Word not only supports word processing features but also DTP features. Some of the important features of Ms-Word are listed below:

It can be used to work with a wide range of documents from simple letters, memos to complex documents like newsletters, forms and now with blogs too with Word 2007

Using word you can create the document and edit them later, as and when required, by adding more text, modifying the existing text, deleting/moving some part of it.

Changing the size of the margins can reformat complete document or part of text.

Font size and type of fonts can also be changed. Page numbers and Header and Footer can be included.

Spelling can be checked and correction can be made automatically in the entire document. Word count and other statistics can be generated.

Text can be formatted in columnar style as we see in the newspaper. Text boxes can be made.

Tables can be made and included in the text.

Word also allows the user to mix the graphical pictures with the text. Graphical pictures can either be created in word itself or can be imported from outside like from Clip Art

Gallery.

Word also provides the mail-merge facility.

Word also has the facility of macros. Macros can be either attached to some function/special keys or to a tool bar or to a menu.

It also provides online help of any option.

1.2. Getting Start with MICROSOFT WORD 2007?

Click the Windows START MENU button, and then select ALL PROGRAMS. Locate the

MICROSOFT OFFICE folder. Then, click the MICROSOFT OFFICE WORD 2007 option.

1.3. MICROSOFT WORD 2007 WORK AREA

Description Of Some Important Elements

Quick Access Toolbar : Which contains a customized set of command buttons, like SAVE, UNDO, or REDO.

Office Button: A button in the top left corner of the screen where high-level file controls can be found, such as SAVE, PRINT, and CLOSE. This includes two panels (Figure 2). The left pane contains major file tasks. The right pane changes to show specific options related to the task selected on the left. The image below shows the specific options relating to the Print task selected in the left pane.

Ribbon: Functions as a combination of menu bar and toolbar, offering tabbed "pages" of buttons, lists, and commands.

Title bar : A bar located across the top of the window, which displays the name of the current document and the current application.

Command Tabs : Eight default Command Tabs are located at the top of the Ribbon. They group task-related commands together. Command tab titles include: HOME, INSERT,

PAGE LAYOUT, REFERENCES, MAILINGS, REVIEW, VIEW and ADD-INS.

Minimize Button: A button located to the left of the Maximize/Restore button in the top right corner of the window, which reduces the window to an icon at the bottom of the screen.

Maximize/Restore Button : A button located to the left of the Close button in the top right corner of the window, which controls the size of the application and document windows.

Close Button : It is used to close the current window.

Command Groups : Commands available for the selected tab that relate to what you are trying to do. The name of the Command Groups appears below the commands. To display the dialog box for a particular command set, click the dialog launcher .

Status Bar : A toolbar located in the bottom left corner of the window, which displays page and word counts

Document Layouts: This provides different ways to view your document.

Zoom Controls: A slide scale located in the bottom right corner of the window, which controls the magnification of the document. Slide the marker closer to the plus (+) or minus (-) to increase or decrease the appearance of the document.

Browse Buttons : Used to go to previous and next page and to browse different commands like footnote, endnote, find etc.

View Ruler Button : To hide or display ruler.

1.4. Creating, Saving, Closing The Document And Quitting

Word

Click the Microsoft Office Button

, and then click New.

Under Templates, you see options you can use to create:

A blank document, workbook, or presentation.

A document, workbook, or presentation from a template.

A new document, workbook, or presentation from an existing file.

SAVING A DOCUMENT

Word has two commands in the Office Button menu that are commonly used to save a file on a disk: Save As and Save.

The Save As command enables you to:

Save a document for the first time

Save a document with a new file name

Save a document to a different location and different versions.

The Save Command enables you to:

Record changes made to an existing file

Automatically display the Save As dialog box when saving a new file

Naming a Document

When you save a file for the first time, Word automatically assigns a file name by using the first words of the document. You can accept this default name or name it something different. A file name should be descriptive, so that you can remember the file’s contents.

A file name in Word can contain up to 255 characters, spaces, and other punctuation. A file name cannot contain any of the following characters: /\:,;*?”<>|.

Click the Office Button

Highlight Save As , and then select Word Document for the document type.

CLOSING A DOCUMENT

When you are finished with a document, click the OFFICE BUTTON and select CLOSE to close the document window. You may also close the document window by clicking on the window’s CLOSE button marked with an in the top right corner.

EXIT WORD

Click the Microsoft Office button. A menu appears.

Click Exit Word, which you can find in the bottom-right corner.

1.5.

OPENING , EDITING AND PROOFING A DOCUMENT

OPEN COMMAND

To open a listed file, click the OFFICE BUTTON and select OPEN. Then, either highlight the file name in the File Name list box and click the OPEN button, or simply double-click on the file name. A copy of the file is placed in an active document window.

To make recently used documents more accessible, Word tracks documents that have been opened and places their names as choices in the right column of the Office Button menu. To open one of these listed files, click on the document name.

EDITING A DOCUMENT

By default, Microsoft Word is in the Insert mode; as you type, text to the right of the insertion point is pushed to the right. To insert text, place the insertion point where you want the text to appear and begin typing. To place the insertion point, position the I-beam at the desired location and click the left mouse button .

Microsoft Word gives you the option of turning off Insert mode and turning on Overtype mode. Instead of pushing text to the right as you type, Overtype mode replaces existing text, one character at a time.

You can turn on Overtype mode by pressing the [INSERT] key on the keyboard.

Selecting Text

To select text, position the cursor at the front or back end of the text to be selected. Press and hold the left mouse button. Drag the cursor across the text by sliding the mouse over. The highlighted text will be given a shaded appearance. Release the mouse button when finished selecting the desired text.

Deleting Text

To delete the character immediately to the right of the insertion point, press the Delete key. To delete the character or space immediately to the left of the insertion point, press the Backspace key. To delete selected text, use the mouse to highlight the text and press the Delete key.

Copy/Cut and Paste

In Microsoft Word, you can copy/move information from one area of a document and place the information you copied anywhere in the same or another document. In other words, after you type information into a document, if you want to place the same information somewhere else, you do not have to retype the information. You simple copy it and then paste it in the new location. As with cut data, Word stores copied data on the Clipboard.

Steps : a.

Select the Text b.

Choose the Home tab.

c.

Click the Copy button or Cut button in the Clipboard group. Word copies the data you selected to the Clipboard. d.

Then click on Paste button .

Using the Clipboard

As you cut or copy, Word can store the information you have cut or copied on the Clipboard in a hierarchy. Then each time you cut or copy, the data you just cut or copied moves to the top of the Clipboard hierarchy and the data previously at the top moves down one level. When you choose Paste, the item at the top of the hierarchy is the item Word pastes into your document.

The Clipboard can store up to 24 items. You can paste any item on the Clipboard into your document by placing your cursor at the insertion point, displaying the Clipboard pane, and then clicking the item.

The Clipboard pane includes an Options button. You can click the Options button to set the

Clipboard options described in the following table.

Option Description

Show Office Clipboard Automatically Shows the Clipboard automatically when you copy items.

Show Office Clipboard When Ctrl+c Shows the Clipboard when you press Ctrl+c

Pressed Twice

Collect Without Showing Office

Clipboard

Show Office Clipboard Icon on

Taskbar

Show Status Near Taskbar When

Copying twice.

Copies to the Clipboard without displaying the

Clipboard pane.

Displays the Clipboard icon on your system taskbar.

Displays the number of items copied on the taskbar when copying.

Find and Replace

If you need to find a particular word or phrase in your document, you can use the Find command. This command is especially useful when you are working with large files. If you want to search the entire document, simply execute the Find command. If you want to limit your search to a selected area, select that area and then execute the Find command.

After you find the word or phrase you are searching for, you can replace it with new text by executing the Replace command.

Steps:

Choose the Home tab.

Click Find in the Editing group. A menu appears.

Click the Find option on the menu. The Find and Replace dialog box appears.

Type the word in the Find What box.

Type the word in the Replace With box.

Click Find Next. The word is highlighted.

Click Replace

Symbols and Special Characters

Special characters are punctuation, spacing, or typographical characters that are not generally available on the standard keyboard. To insert symbols and special characters:

Place your cursor in the document where you want the symbol

Click the Insert Tab on the Ribbon

Click the Symbol button on the Symbols Group

Choose the appropriate symbol.

Equations

Word 2007 also allows you to insert mathematical equations. To access the mathematical equations tool:

 Place your cursor in the document where you want the symbol

 Click the Insert Tab on the Ribbon

 Click the Equation Button on the Symbols Group

 Choose the appropriate equation and structure or click Insert New Equation

To edit the equation click the equation and the Design Tab will be available in the

Ribbon

Inserting Date and Time

Place your cursor in the document where you want the symbol

Click the Insert Tab on the Ribbon

Click the Insert Date and Time Group

AutoCorrect

You can use the AutoCorrect feature to correct misspelled text automatically as you type. For example, if you type “teh,” AutoCorrect will replace it with “the” as soon as you press the

[SPACEBAR]. Word supplies a number of default AutoCorrect entries. The AutoCorrect command also has other options available, such as correcting initial capitalization and capitalizing the days of the week. To activate or deactivate the various AutoCorrect options click the OFFICE BUTTON, and then click the WORD OPTIONS button. Click PROOFING and then click the AUTOCORRECT

OPTIONS button to view the default AutoCorrect entries or add or delete an AutoCorrect entry.

Spelling, Grammar and Thesaurus

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.

Click on the Review tab.

Click on Spelling and Grammar from the Proofing group.

Enable or disable Contextual Spell Check

Contextual Spell Check recognizes words which are used incorrectly in context by placing a blue wave line under the text.

Click the Microsoft Office button .

 Click the Word Options button and select Proofing.

Under the When correcting spelling and grammar in Word section, check or uncheck the use contextual spelling box.

Thesaurus

Suggests other words with a similar meaning to the word you have selected.

Select the Review tab.

Click on Thesaurus from the Proofing group.

1.6. Formatting Text and Paragraphs

Font and Font Size

Character formats – such as bold, italics, and underlining – can be used to emphasize text in a document. Use the Font command group on the Home tab dialog box to apply character formats to, and remove them from, selected text.

One way to apply character formats is to select the text that you want to format, and then click on the appropriate button to apply the format. To remove the format, select the text and click the same button again.

You can change how selected text looks by changing its font characteristics. A font consists of a typeface and a font size.

You can use either the Font command group on the Home tab, the Mini Toolbar, or launch the Font dialog box to specify fonts and font sizes.

Formatting using Home tab

Formatting using Mini Toolbar

Select the first line of the document.

Right-click the mouse button. Notice that the Mini Toolbar appears.

Formatting using Mini Toolbar

Choose the Home tab.

Click the dialog box launcher in the Font group. The Font dialog box appears.

Select the Font, Font size, Bold, Italics, Underline etc

Press OK

Formatting using Format Painter

You can quickly copy character formatting from selected text to another section of text by using the Format Painter button, , which can be found in the Clipboard command group on the Home tab.

Font Dialogue Box

Paragraph Spacing, Alignment and Indents

Paragraph formatting, such as alignment, indents, and tabs, can be used to change the appearance of your document. To apply a paragraph format, select the paragraph and apply the desired formatting. You can select the paragraph by just placing the insertion point in the paragraph, or by selecting any amount of text in the paragraph. The formats you apply will affect the entire paragraph.

Select the text that you wish to format. Click the dialog launcher button , in the bottom right corner of the Paragraph command group to display the Paragraph dialog box .

Paragraph Alignment

Align Left:

Lines of text are aligned along the left indent. The text along the right side of the paragraph appears ragged. Left-aligned is the default paragraph-alignment setting.

Center:

Lines of text are aligned between the indents. Both the left and right sides of the paragraph appeared ragged.

Align Right:

Lines of text are aligned along the right indent. The left and right indents are even with the left and right margins by default.

Justify:

Lines of text are aligned along both the left and right indents. Word adjusts the spacing between words so that they stretch from left indent to right indent. When the last line of a justified paragraph is short, however, it will not be stretched out.

Indents

Indents are used to create left and right boundaries for selected paragraphs without changing the margins for the entire document. By default, indents are set equal to the margins. The indent markers are the up and down pointing triangles and small box on the ruler. There are four indent markers:

The First Line Indent marker, , is the downward-pointing triangle on the left end of the ruler. It controls the left boundary for the first line of a paragraph.

The Hanging Indent marker, , is the upward-pointing triangle on the left end of the ruler. It controls the left boundary of every line in a paragraph except the first line.

The Left Indent marker, , is the small rectangle under the Hanging Indent marker. It controls the left boundary for every line in a paragraph except when a First Line or Hanging Indent is applied. When no left-margin indents are applied, moving the Left Indent marker simultaneously moves the First Line and Hanging Indent markers as well.

The Right Indent marker, , is the upward-pointing triangle on the right end of the ruler. It controls the right boundary for every line in a paragraph.

Bulleted or Numbered list

You can also apply bullets to a series of paragraphs in a document by using the same method you used for a numbered list. Select the paragraphs, and then click the Bullets button,

You can change the style of the bullet that is used in your list. To do so, click the drop-down

. arrow to the right of Bullets button, , from the Paragraph command group on the Home tab. Select one of the additional bullet styles from the Bullet Library.

You can choose from additional symbols to be used as a bullet by clicking on Define New Bullet, while viewing the Bullet Library, and then selecting a suitable symbol. Click OK, when finished.

When numbering is added to paragraphs, Word formats the paragraphs with hanging indents.

To create a numbered list while composing a document:

1. Place the insertion point where you want the numbered list to begin.

2. Click the Numbering button, , from the Paragraph command group on the Home tab.

3. Type the text for the list. Press the [ENTER] key to move to the next line and start a new number.

4. Click the Numbering button again to discontinue the number formatting.

To create multilevel list select the text and click on button.

Tabs

By default, left-aligned tab stops are set at 0.5-inch increments between the margins. These default tab stops are visible on the ruler only when a tab character is inserted in the text. When you set custom tab stops, all default tab stops to the left of the custom tab stops are automatically cleared. One tab stop should be set for each tab character that appears in selected paragraphs.

Select the text that you wish to format. Click the dialog launcher button , in the bottom right corner of the Paragraph command group to display the Paragraph dialog box .

Setting tabs using Ruler

Select the appropriate paragraph or paragraphs. Click on the TAB STOP button on the far left corner of the ruler to choose the appropriate type of tab stop (below figure). Point to the desired tab-stop position, just under the tick marks on the ruler, and click the left mouse button.

Left Tab: text flows to the right of the tab stop.

Center Tab: text is centered on the tab stop.

Right Tab: text flows to the left of the tab stop.

Decimal Tab (used for numbers): text aligns on the decimal point.

To delete a custom tab stop, select the appropriate paragraph(s). On the ruler, point to the marker that represents the custom tab stop, and drag it down into the text area.

Borders and Shading

In Microsoft Office Word 2007, borders can add interest and emphasis to various parts of your document. You can add borders to pages, text, tables and table cells, graphic objects, and pictures.

Add a border to a picture, a table, or text

1.

Select the picture, table, or text that you want to apply a border to.

2.

On the Page Layout tab, in the Page Background group, click Page Borders.

3.

Click one of the border options under Settings.

4.

To specify that the border appears on a particular side of a page, such as only at the top, click

5.

Custom under Setting.

Select the style, color, and width of the border.

6.

To specify an artistic border, such as trees, select an option in the Art box.

To apply shading click on Shading tab, then select the color.

Drop Cap

The dropped cap, a large dropped initial capital letter, can be used to begin a document or a chapter, or to add interest to a newsletter or invitation.

Click in the paragraph that you want to begin with a drop cap.

On the Insert tab, in the Text group, click Drop Cap.

Click Dropped or In margin

1.7. Styles

When working with Word, you can use styles to quickly format your documents. A style is a set of formats consisting of such things as fonts, font colors, font sizes, and paragraph formats.

Word 2007 supplies you with predesigned style sets that contain styles for titles, subtitles, quotes, headings, lists and more. The sections that follow all show you how to work with styles.

Choose the Home tab.

Click Change Styles in the Styles group. A menu appears.

Click Style Set. A menu appears. You can choose from any of the styles listed on the menu.

Click Simple. Word 2007 reformats all of the paragraphs into the Simple style by applying the Normal format to each paragraph.

Change Style Sets

Once you have applied styles, changing to another style set is easy. You simply open the Style

Set gallery. As you move your cursor down the menu, Word 2007 provides you with a live preview of the effect of applying the style set. To choose a style set, you click it.

1.8. TABLES

You can create a Table to enhance the presentation of data, to create side-by-side paragraphs, and to organize information used in form letters.

To insert a table into your document, place the insertion point where you want to insert the table. Then, click the INSERT tab on the ribbon and click the drop-down menu on the TABLES command group. Use the mouse to shade the desired number of columns and rows. Word will create a table that fills the area inside the margins with the width of the columns adjusting automatically according to the amount of space available. Click the left mouse button to insert the table to your document according to your specifications.

Moving around a Table

Each block in a table is called a Cell. You can move around a table by using the left, right, up, and down arrow keys. To move to a specific cell, use your mouse pointer to move to the specified cell. Or, you can use the [TAB] key to move from cell to cell, from left to right. You can hold down the [SHIFT] key and press the [TAB] key simultaneously to move from cell to cell, from right to left.

Entering text into a table

To enter text into a table, simply type the text inside the desired cell. Press the [TAB] key to move to the next cell.

Editing Tables

Inserting additional rows or columns

With your cursor inside the table, click the LAYOUT tab from the Table Tools contextual tab area.

From the ROWS & COLUMNS command group, select the appropriate insertion method; in this case, click INSERT BELOW.

Notice a new row has been added beneath the current row.

You can add multiple rows or columns at once by selecting the desired number of rows or columns to add, and then clicking the desired insertion method.

Note: The simplest way to add a new row at the bottom of the table is to move to the last column of the last row and press the [TAB] key.

Deleting a row or column

Use your mouse to select the last column.

Then, from the LAYOUT tab, click the DELETE option, command group.

Select DELETE COLUMNS.

, from the ROWS & COLUMNS

Converting Table to Text

Select the table, on Convert to text option by using Layout Tab.

Formatting Tables

Applying Design Format

Microsoft Word 2007 allows you to easily apply borders, shading, special fonts, and color to your table. All of the pre-bundled formats can be previewed and applied. If none of the options are exactly right, you can customize the format to meet your needs. Notice that you can see a live preview of the design format, while hovering above the style with your mouse.

For calculations, click on Layout tab and click on

.

Sort

In Print Layout view (Print Layout view: A view of a document or other object as it will appear when you print it. For example, items such as headers, footnotes, columns, and text boxes appear in their actual. positions.), move the pointer over the table until the table move handle appears.

Click the table move handle to select the table that you want to sort.

Under Table Tools, on the Layout tab, in the Data group, click Sort.

In the Sort dialog box, select the options that you want.

Repeat a table heading on subsequent pages

When you work with a very long table, it will be divided into several pages. You can make adjustments to the table so that the table headings appear on each page.

Repeated table headings are visible only in Print Layout view (Print Layout view: A view of a document or other object as it will appear when you print it. For example, items such as headers, footnotes, columns, and text boxes appear in their actual positions.) or when you print the document.

Select the heading row or rows. The selection must include the first row of the table.

 Under Table Tools, on the Layout tab, in the Data group, click

.

Unit 2

Graphics Vs Macros

Structure:

2.1.

2.2.

2.3.

2.4.

Graphics and charts

Mail merge

Macros

Page Setup and Printing

2.1. Graphics and charts

Illustrations, Pictures, and SmartArt

Word 2007 allows you to insert illustrations and pictures into a document. To insert

illustrations:

Place your cursor in the document where you want the illustration/picture

Click the Insert Tab on the Ribbon

Click the Clip Art Button

The dialog box will open on the screen and you can search for clip art.

Choose the illustration you wish to include

To insert a picture:

Place your cursor in the document where you want the illustration/picture

Click the Insert Tab on the Ribbon

Click the Picture Button

Browse to the picture you wish to include

Click the Picture

Click Insert

Smart Art is a collection of graphics you can utilize to organize information within your document. It includes timelines, processes, or workflow. To insert SmartArt

Place your cursor in the document where you want the illustration/picture

Click the Insert Tab on the Ribbon

Click the SmartArt button

Click the SmartArt you wish to include in your document

Click the arrow on the left side of the graphic to insert text or type the text in the graphic.

Resize Graphics

All graphics can be resized by clicking the image and clicking one corner of the image and dragging the cursor to the size you want the picture.

Watermarks

A watermark is a translucent image that appears behind the primary text in a document. To insert a watermark:

 Click the Page Layout Tab in the Ribbon

 Click the Watermark Button in the Page Background Group

 Click the Watermark you want for the document or click Custom Watermark and create your own watermark

 To remove a watermark, follow the steps above, but click Remove Watermark

2.2. Mail merge

The mail merge function in Microsoft Word enables you to create a standard letter in Word and then change personal details on multiple copies according to recipients' name and address details held in an Excel spreadsheet. An Access database can also be used to hold the address details, but in this example, we will look at using Excel to hold this information.

So, to do a mail merge in Word you will need two things:

A letter prepared in Word

Address details of all those people who will receive the letter

To help the Word mail merge wizard identify the data correctly, it's best to ensure that the address details in Excel don't have any missing rows - the data should have a continuous range. Here is an example of the kind of data you would use.

Starting The Mail Merge in Word

In Word start the Word mail merge wizard by clicking Mailings > Start Mail Merge > Start Mail

Merge > Step by Step Mail Merge Wizard. In the panel on the right, ensure that Letters is selected and click Next: Starting document. Because we are going to write the letter after the recipients have been selected, leave Use the current document selected and then click Next:

Select recipients. This is where our Excel spreadsheet of address details comes in. Ensure that

Use an existing list is selected and click browse. Navigate to where your recipient Excel spreadsheet is, select it and then in the Select Table dialogue box select the worksheet that contains your address details.

Ensure that the First row of data contains column headings is selected only where appropriate and click OK.

Note that if you have the spreadsheet open in Excel while you are performing the mail merge in

Word, you may get the following cry for help from Word! If you do, don't panic. Simply close the spreadsheet in Excel and try selecting it again in Word.

When you click OK, the Mail Merge Recipients dialogue box is presented.

You can use the Mail Merge Recipients dialogue box to deselect any addresses you don't want to merge. Click OK when you have completed your selections. It's now time to write the letter, so click Next: Write your letter. You have two options: you can either insert items of personal information about your recipients as you go along, or you can write the letter in its entirety, leaving gaps for that information for you to fill in once the letter is complete. We'll go with the latter option. Once you have completed the letter, locate the cursor in one of the gaps you left for recipient information and click More Items on the right. Ensure that Database Fields is selected and select the piece of information you need from the list.

When you click Insert, that piece of data is inserted into the document where the cursor was positioned. When all the recipient data has been added, click Next: Preview your letters. Here, an example letter showing information about your first recipient is shown as a preview. Check the preview and if you need to make any adjustments, click Previous: Write your letter, otherwise click Next: Complete the merge.

At this point the mail merge is complete and all that is left to do is print the letters. When you click Print, the Merge to Printer dialogue box is presented for you to choose which recipients to print.

You can select all recipients, only the current recipient or you can select a range of recipients, in much the same way you would select what pages to print when you perform a normal print.

Same data you can use for Envelopes and Labels. Use Envelopes or Labels options from Mailings

Tab. Click on Create.

2.3. Macros

Macros can be used to automate repetitive tasks to save you time and effort. For example, you may have jumped to the bottom of a document and inserted a picture of your cat before saving the document. If you find that you have several more documents that you want to apply this change to, you may find it a great time saver to create a macro to perform these actions automatically.

Creating a macro involves recording your keystrokes so that they can be played over later again and again. When you're at the point where you can begin recording your actions, go to the View

Tab > Macros > Record Macro.

You should give the macro a meaningful name so that you can easily identify it later. The two buttons you see in the Record Macro window allow you to assign the macro to a button on the

Quick Access Toolbar or a keyboard shortcut. Clicking the button will take you to the Customise category of Word Options. Here, you can add the new macro to the Quick Access Toolbar, see which commands are already there, and also change the order in which they appear.

To add the macro, select it in the box on the left and click Add. Don't worry if you didn't select an informative name earlier; you can change that now by making sure it's selected and then clicking Modify. Go on, click Modify.

At the bottom of the Modify Button window, give the macro a more meaningful display name.

You can also choose an icon to represent the macro, and this icon will appear in the Quick

Access Toolbar.

When you click OK, you can see the new addition in the Quick Access Toolbar, but the macro doesn't yet exist. Notice that the cursor has changed to a cassette tape, indicating that recording has started. You can now perform the actions you want to record. Once you've completed all the steps you want included in the macro, stop recording by clicking the View Tab > Macros >

Stop Recording.

To run it, come up to the Quick Access Toolbar and click the icon you chose to represent it.

2.4. Page setup and Printing

Set the Orientation

Before you print your document, you may want to change the orientation of your pages. There are two orientations you can use: portrait and landscape. Paper, such as paper sized 8 1/2 by 11, is longer on one edge than it is on the other. If you print in Portrait, the shortest edge of the paper becomes the top of the page. Portrait is the default option. If you print Landscape, the longest edge of the paper becomes the top of the page.

Portrait

Landscape

1.

2.

3.

Choose the Page Layout tab.

Click Orientation in the Page Setup group. A menu appears.

Click Portrait. Word sets your page orientation to Portrait.

Set the Page Size

Paper comes in a variety of sizes. Most business correspondence uses 8 1/2 by 11 paper which is the default page size in Word. If you are not using 8 1/2 by 11 paper, you can use the Size option in the Page Setup group of the Page Layout tab to change the Size setting.

1.

2.

3.

Choose the Page Layout tab.

Click Size in the Page Setup group. A menu appears.

Click Letter 8.5 x 11in. Word sets your page size.

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.

1.

2.

3.

Choose the Page Layout tab.

Click Margins in the Page Setup group. A menu appears.

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

Add Page Numbers

Page numbers help you keep your document organized and enable readers to find information quickly. You can add page numbers to the top, bottom, or margins of your pages, and you can choose where the numbers appear. For example, numbers can appear at the top of the page, on the left, right, or center of the page. Word also offers several number styles from which you can choose.

1.

2.

3.

4.

Choose the Insert tab.

Click the Page Number button in the Header & Footer group. A menu appears.

Click Bottom of Page.

Click the right-side option.

Insert Page Breaks

As you learned in Lesson 1, you can display your document in any of five views: Draft, Web

Layout, Print Layout, Full Screen Reading, or Online Layout. In Print Layout view you see your document as it will appear when you print it. You can clearly see where each page ends and a new page begins.

As you review your document, you may find that you want to change the point at which a new page begins. You do this by inserting a page break. For example, if a page heading appears on

one page and the first paragraph under the heading appears on the next page, you may want to inser a page break before the heading to keep the heading and the first paragraph together.

1.

2.

Choose the View tab.

Click Print Layout in the Document Views group. Your document changes to the Print

Layout view.

Insert Page Breaks

1.

Place your cursor before the D in "Displaced Homemakers"

2.

Choose the Insert tab.

3.

Click Page Break. Word places a page break in your document.

To delete a page break, you select the page break and then press the Delete key.

Preview and Print Documents

When you have your margins, tabs, and so on the way you want them, you are ready to print. In

Word, You can preview your document before you print. In the Preview mode, you can review each page, view multiple pages at the same time, zoom in on a page, and access the Size,

Orientation, and Margin options.

If you press the Zoom button while you are in Preview mode, the Zoom dialog box appears. In the Zoom dialog box you can set the sizes of the pages that display as well as the number of pages that display.

When you are ready to print, you use the Print dialog box. In the Print Range area, choose All to print every page of your document, choose Current Page to print the page you are currently on, or choose Pages to enter the specific pages you want to print. Type the pages you want to print in the Pages field. Separate individual pages with commas (1,3, 13); specify a range by using a dash (4-9).

Print Preview

1.

2.

3.

4.

5.

6.

7.

results.

Click the Microsoft Office button. A menu appears.

Highlight the Print option. The Preview and Print the Document menu appears.

Click Print Preview. The Preview window appears, with your document in the window.

Click One Page to view one page at a time. Click Two Pages to view two pages at a time.

To view your document in normal size, click 100%.

Click the Zoom Button. The Zoom dialog box appears.

Select an option and then click OK. Perform this task for each option and note the

Print

1.

2.

Click the Print button. The Print dialog box appears.

Click the down arrow next to the Name field and select the printer to which you want to print.

3.

Choose All as the page range.

4.

Click OK. Word prints your document.

Unit 3

BASICS OF MICROSOFT EXCEL

Structure:

3.1. Introduction

3.2. Introduction to Spreadsheets

3.3. Getting start with Excel

3.4. Editing Data in workbook

3.5. Managing Workbooks

3.1 Introduction

Excel is a computerized spreadsheet, which is an important business tool that helps you report and analyze information.

Excel stores spreadsheets in documents called workbooks.

Each workbook is made up of individual worksheets, or sheets.

Because all sorts of calculations can be made in the Excel spreadsheet, it is much more flexible than a paper spreadsheet.

The Excel window has some basic components, such as an Active cell, Column headings, a Formula bar, a Name box, the mouse pointer, Row headings, Sheet tabs, a Task Pane, Tab scrolling buttons and Toolbars.

When you set up calculations in a worksheet, if an entry is changed in a cell, the spreadsheet will automatically update any calculated values that were based on that entry.

When you open Excel, by default it will open a blank workbook with three blank worksheets.

3.2 Introduction to Spreadsheets

A spreadsheet is an electronic document that stores various types of data. There are vertical columns and horizontal rows. A cell is where the column and row intersect. A cell can contain data and can be used in calculations of data within the spreadsheet. An Excel spreadsheet can contain workbooks and worksheets. The workbook is the holder for related worksheets.

3.3 Getting started with Excel

Getting started with Excel 2007 you will notice that there are many similar features to previous versions. You will also notice that there are many new features that you’ll be able to utilize.

There are three features that you should remember as you work within Excel 2007: the Microsoft

Office Button, the Quick Access Toolbar, and the Ribbon. The function of these features will be more fully explored below figure.

Microsoft Office Button

The Microsoft Office Button performs many of the functions that were located in the File menu of older versions of Excel. This button allows you to create a new workbook, Open an existing workbook, save and save as, print, send, or close.

Ribbon

The ribbon is the panel at the top portion of the document It has seven tabs: Home, Insert, Page

Layouts, Formulas, Data, Review, and View. Each tab is divided into groups.

Quick Access Toolbar

The quick access toolbar is a customizable toolbar that contains commands that you may want to use. You can place the quick access toolbar above or below the ribbon. To change the location of the quick access toolbar, click on the arrow at the end of the toolbar and click Show Below the

Ribbon .

Quick Access Toolbar

You can also add items to the quick access toolbar. Right click on any item in the Office Button or the Ribbon and click Add to Quick Access Toolbar and a shortcut will be added.

The Name Box

Under the Ribbon, there is a white box displaying a name like A1 (it may not display A1...), that small box is called the Name Box:

The Insert Function Button

On the right side of the Name box, there is a gray box with an button. That button is called the Insert Function button.

The Formula Bar

On the right side of the Insert Function button is a long empty white box or section called the Formula Bar:

You can hide or show the Formula Bar anytime. To do this, on the Ribbon, click View. In the

Show/Hide section:

To hide the Formula Bar, remove the check mark on the Formula Bar check box

To show the Formula Bar, check the Formula Bar check box

The Column Headers

Under the Name Box and the Formula bar, you see the column headers. The columns are labeled A, B, C, etc:

There are 255 of columns.

The Row Headers

On the left side of the main window, there are small boxes called row headers. Each row header is labeled with a number, starting at 1 on top, then 2, and so on:

The Cells

The main area of Microsoft Excel is made of cells. A cell is the intersection of a column and a row:

A cell is identified by its name and every cell has a name. By default, Microsoft Excel appends the name of a row to the name of a column to identify a cell. Therefore, the top-left cell is named A1. You can check the name of the cell in the Name Box.

Home : Clipboard, Fonts, Alignment, Number, Styles, Cells, Editing

Insert : Tables, Illustrations, Charts, Links, Text

Page Layouts : Themes, Page Setup, Scale to Fit, Sheet Options, Arrange

Formulas : Function Library, Defined Names, Formula Auditing, Calculation

Data : Get External Data, Connections, Sort & Filter, Data Tools, Outline

Review : Proofing, Comments, Changes

View : Workbook Views, Show/Hide, Zoom, Window, Macros

Mini Toolbar

A new feature in Office 2007 is the Mini Toolbar. This is a floating toolbar that is displayed when you select text or right-click text. It displays common formatting tools, such as Bold,

Italics, Fonts, Font Size and Font Color.

Navigate within worksheets

To navigate within a workbook, you use the arrow keys, PageUp, PageDown, or the Ctrl key in combination with the arrow keys to make larger movements.

The most direct means of navigation is with your mouse.

Scroll bars are provided and work as they do in all Windows applications.

Navigate between worksheets

 o o

To move to other Worksheets, you can:

Click their tab with the mouse

Use the Ctrl key with the Page Up and Page Down keys to move sequentially up or down through the worksheets

If you are familiar with Microsoft Access, you will find the tab scrolling buttons for moving between worksheets to be similar to record browsing on an Access form or datasheet.

Entering Data in a cell

To enter data, first make the cell in which you want to enter the data active by clicking it.

Enter the data (text, formulas, dates, etc.) into the active cell.

Use the Alt+Enter key combination to enter text on multiple lines within the same cell.

Use TAB key, arrow keys, or ENTER key to navigate among the cells.

Workbook

Excel workbook is a file that contains one or more worksheets (worksheet: The primary document that you use in Excel to store and work with data. Also called a spreadsheet. A worksheet consists of cells that are organized into columns and rows; a worksheet is always stored in a workbook.) that you can use to organize various kinds of related information.

Save

This feature allows you personalize how your workbook is saved. You can specify how often you want auto save to run and where you want the workbooks saved.

To save a file:

You can press Ctrl + S

On the Quick Access Toolbar, you can click the Save button

 You can click the Office Button and click Save

Whenever you decide to save a file for the first time, you need to provide a file name and a location. The file name helps the computer identify that particular file and register it.

A file name can consist of up to 255 characters, you can include spaces and dashes in a name.

Although there are many characters you can use in a name (such as exclamation points, etc), try to avoid fancy names. Give your file a name that is easily recognizable, a little explicit. For example such names as salary details, stock report etc. Microsoft Excel file has an extension, which is .xls but you don't have to type it in the name.

The second important piece of information you should pay attention to when saving your file is the location. The location is the drive and/or the folder where the file will be saved. By default,

Microsoft Excel saves its files in the My Documents folder. You can change that in the Save As dialog box. Just click the arrow of the Save In combo box and select the folder you want.

Microsoft Excel allows you to save its files in a type of your choice. To save a file in another format:

 You can click the Office Button and position the mouse on Save As and select the desired option:

 On the Quick Access Toolbar, you can click the Save button . Then, in the Save As dialog box, click the arrow of the Save As Type combo box and select a format of your choice.

Closing the Excel Workbook and Quitting Excel

To close Microsoft Excel, click the Office Button and click Exit Excel

3.4 Editing Data

Select Data

To select a cell or data to be copied or cut:

Click the cell

Click and drag the cursor to select many cells in a range

Select a Row or Column

To select a row or column click on the row or column header.

Copy and Paste

To copy and paste data:

Select the cell(s) that you wish to copy

On the Clipboard group of the Home tab, click Copy

Select the cell(s) where you would like to copy the data

On the Clipboard group of the Home tab, click Paste

Cut and Paste

To cut and paste data:

Select the cell(s) that you wish to copy

On the Clipboard group of the Home tab, click Cut

Select the cell(s) where you would like to copy the data

On the Clipboard group of the Home tab, click Paste

Undo and Redo

To undo or redo your most recent actions:

On the Quick Access Toolbar

Click Undo or Redo

Auto Fill

The Auto Fill feature fills cell data or series of data in a worksheet into a selected range of cells.

If you want the same data copied into the other cells, you only need to complete one cell. If you want to have a series of data (for example, days of the week) fill in the first two cells in the series and then use the auto fill feature. To use the Auto Fill feature:

Click the Fill Handle

Drag the Fill Handle to complete the cells

Insert Cells, Rows, and Columns

Right-click the column header of the column that will be on the right side of the new column you want to create, and click Insert

Click the column header or any box under it. On the Ribbon, click Home. In the Cells section, click the arrow under Insert and click Insert Sheet Columns

To remove a column, row or cell:

Right-click the column header and click Delete

Click the column header or any box under it. On the Ribbon, click Home. In the Cells section, click Delete and click Delete Sheet Columns

Changing the Layout

Increasing or Decreasing the Row/Column Height/width

To manually resize a column/row, position the mouse on the short line that separates a column/row header from its right column/row header.

Click, then drag left or right until the small box displays the width you desire, then release the mouse.

Automatically Resizing the Columns/Rows

 On the Ribbon, click Home. In the Cells section, click Format and click AutoFit

Column/row Width/height

Setting the Width/Height Value of Columns/Rows

You can use a dialog box to set exactly the desired width/height of a column/row or a group of columns/rows. To specify the width/height of a column/row:

 Right-click the column/row header and click Column/row Width/Height...

 Click a column/row header or any box under it. Then, on the Ribbon, click Home. In the

Cells section, click Format and click Column/Row Width/Height...

To specify the same width/height for many columns/rows:

 Select a range of columns/rows. Right-click one of the columns/rows (right-click either one of the column/row headers or inside the selection) and click Column/Row Width/Height...

 Randomly select a group of (non-adjacent) columns/rows. Right-click one of the column/row headers and click Column/Row Width/Height...

 Select the columns/rows, whether in a range or randomly (non-adjacent). On the

Ribbon, click Home. In the Cells section, click Format and click Column/Row Width/Height...

Hiding, Freezing, and Splitting Columns/Rows

When working on a list, you don’t always need all columns/rows displaying all the time. You can hide a column/row whose presence is not required at a particular time. In Microsoft Excel, you can hide one or more columns/rows.

To hide one column:

 Right-click the column/row and click Hide

 Click the column header. On the Ribbon, click Home. In the Cells section, click Format, position the mouse on Hide & Unhide, and click Hide Columns/rows

Freezing One or More Columns

If you have a list wider than the Microsoft Excel area can show, you can scroll to the right to see hidden columns or rows. While you are scrolling to the right, some columns rows would be disappearing from the left section of the Microsoft Excel interface. If you want, you can freeze a column or rows so that, when you scroll to the right, a column or row or some columns or rows would be fixed and would not move. Also, the column(s) or row(s) from the left of the frozen column or row would not move either.

To freeze a column, click the column/row header of the column or row that will lead the moving columns or rows. On the Ribbon, click View. In the Window section, click Freeze Panes, and click

Freeze Panes.

Splitting the Columns

An alternative to freezing is to split the group of columns into two sections. Just as done for the freezing, you can choose a column to use as reference and scroll the columns from its side. To split the group of columns in two, click a column header. On the ribbon, click View. In the

Window section, click Split.

Aligning Data

When reviewing cells, we found out that a cell doesn't have dimensions of its own. Its width is imposed by its parent column and its height is set on its parent row. All of the cells we have used so far were considered individually. Microsoft Excel allows you to combine various cells in a group. This is referred to as merging cells.

To merge cells, select them and:

On the Ribbon, click Home. In Alignment section, click the Merge & Center button

On the Ribbon, click Home. In Alignment section, click the button on the right side of

Merge & Center and select from the list

 Right-click the selected cells and click Format Cells. In the Alignment property page, click the Merge Cells check box and click OK

We have already seen how Microsoft Excel differentiates data you enter into cells. Sometimes its default configurations will not suit your particular scenario, you should be able to control how text is aligned in cells.

Since a cell is really a rectangular box, you can completely control how text is displayed inside of it: left, center, right, top, middle, or bottom. As we move on, we will see various situations of aligning cells content.

The Alignment Property Page

Besides using the alignment buttons on the Ribbon, to be more precise or to perform various actions in one step, you can use the Alignment property page of the Format Cells property sheet.

To access the Alignment property page:

 On the Ribbon, click Home. In the Alignment section, click the more options button

 Right a cell or the selected cells and click Format Cells. In the Format Cells dialog box, click Alignment

To provide the same options as the Ribbon, the Alignment property page is equipped with the

Horizontal combo box. Besides the left, center, and right alignments, this combo box goes further and allows text to be justified. This can be useful especially if the text is significantly long. If you select to indent text, you can use the Indent spin button to specify the number of units to indent by.

The Vertical combo box provides options not available on the Ribbon. It allows you to align the contents of a cell towards the top, the middle or the bottom area of a cell.

The Text Control section provides three options: Wrap Text, Shrink To Fit, and Marge Cells.

The Orientation section allows you to "bend" text by a fix angle. There are two main ways you can set an angle. If you drag the small red diamond, you can use it to specify the desired angle.

You can also click one of the arrows of the Degrees spin button.

Number Formats

Help you to change the appearance of numbers or values in a cell in the Excel spreadsheet. They are not difficult, and can be achieve with a few clicks.

Formatting is done to improve the appearance of the spreadsheet and to make the numbers easier to read and understand. Commonly used number formats include adding commas ( , ), percent symbols ( % ), decimal places, and dollar signs( $ ).

In Excel 2007, the basic number formatting options are located on the Home tab, Number group as shown here.

To change the basic number formatting

Select the cell containing number that you wish to format.

Click on the down arrow next to the Number Format drop-down list and select a suitable command.

To change number formatting using the formatting icons

You can quickly change the formatting of a cell or selected range by using the following icons on the Home tab, Number group.

To format a number as a currency

Select the cell or range of cells you want to format.

Right-click on the cell and choose Format Cells from the pop-up menu.

From the Format Cells dialog box displayed, select the Number tab.

Under the Category: section, select Currency.

Select the number of decimal places you require by using the Decimal places: spin box arrows.

In the Symbol: drop down list, select the type of currency.

Click OK.

To format a number as a percentage

Select the cell or range of cells you wish to format.

Right-click on the cell and choose Format Cells from the pop-up menu.

From the Format Cells dialog box displayed, select the Number tab.

Under the Category: section, select Percentage.

Select the number of decimal places you require by using the Decimal places: spin

 box arrows.

Click OK.

To change the number of decimal places

Select the cell or range of cells, you wish to change the number of decimal places.

To increase a decimal place, click on the Increase Decimal icon on the Home tab,

Number group. You can continue to click to increase the decimals as required.

To reduce a decimal place, click on the Decrease Decimal icon on the Home tab,

Number group. You can continue to click to reduce the decimals as required.

To round numbers using a numeric format

Select the cell or range of cells you wish to format.

Right-click on the cell and choose Format Cells from the pop-up menu.

From the Format Cells dialog box displayed, select the Number tab.

In the Category: section, select Number.

Select the number of decimal places you require by using the Decimal places spin box arrows.

Click OK.

To change colors based on the value in the cells

Select the cells you wish to change, which contain numeric values.

Right-click on the cell and choose Format Cells from the pop-up menu.

From the Format Cells dialog box displayed, select the Number tab.

Choose Custom from the Category: list box.

Use the scroll bars in the Type: section of the dialog box to view what custom number formats are available.

For example, to force all negative numbers to be displayed in red, you would select the option illustrated below.

Click OK.

To display negative numbers that enclosed within brackets

You would edit the above example, as illustrated.

3.5 Managing Workbooks

As you may realize, Microsoft Excel's primary purpose is to help you create lists using readily available cells spread on a sheet that resembles a piece of paper. For this reason, Microsoft

Excel is referred to as a spreadsheet application

In Microsoft Excel, the list of cells of a document is called a worksheet. When Microsoft Excel starts, it creates three worksheets. If you don't need all of them, you can delete those that appear useless. You can also add new worksheets as you see fit. If you want Microsoft Excel to always start with less or more worksheets, you can change its default settings in the Excel

Options dialog box accessible from the Office Button.

Selecting a Worksheet

In some circumstances, you will need to perform a general action on a worksheet. Before doing this, you may need to select the contents of the whole worksheet first.

 Since there are usually many worksheets presented to you, each is represented by a tab on the lower left corner. Therefore, to select a worksheet:

 You can click its tab

 You can press and hold Ctrl. Then press either Page Up or Page Down. Once the desired worksheet has been selected, you can release Ctrl

If you have many worksheets, to select a range of worksheets, click a tab that is considered one end of the range. Press and hold Shift, then click the tab at the end of the range and release

Shift.

To select worksheets at random, click one of the desired worksheets. Press and hold Ctrl. Then click each desired worksheet. When the selection has been made, release Ctrl.

Insert and Delete Worksheets

To insert a worksheet

Open the workbook

Click the Insert button on the Cells group of the Home tab

Click Insert Sheet

To delete a worksheet

Open the workbook

Click the Delete button on the Cells group of the Home tab

Click Delete Sheet

Worksheets Names

The starting worksheets are named Sheet1, Sheet2, and Sheet3. You can change any or all of these worksheet names.

To rename a worksheet:

You can double-click its sheet tab, then type a new name

You can right-click a sheet’s tab, click Rename, and type the new name

While a certain worksheet is selected, on the Ribbon, click Home. In the Cells section, click

Format. In the Organize Sheets section, click Rename Sheet:

Then type the new name, and press Enter.

Copy and Paste Worksheets

To copy and paste a worksheet:

Click the tab of the worksheet to be copied

Right click and choose Move or Copy

Choose the desired position of the sheet

Click the check box next to Create a Copy

Click OK

Hide Worksheets

To hide a worksheet:

Select the tab of the sheet you wish to hide

Right-click on the tab

Click Hide

To unhide a worksheet:

Right-click on any worksheet tab

Click Unhide

Choose the worksheet to unhide

Unit 4

Functions and Advanced Features

Structure:

4.1. Formulas and Functions

4.2. Analysing Data

4.3. Managing Data

4.4. Organizing Data

4.5. Charting Data and Printing

4.1.

Excel Formulas

Formulas and Functions

A formula is a set of mathematical instructions that can be used in Excel to perform calculations. Formals are started in the formula box with an = sign.

This feature allows you to modify calculation options, working with formulas, error checking, and error checking rules.

There are many elements to and excel formula.

References: The cell or range of cells that you want to use in your calculation

Operators: Symbols (+, -, *, /, etc.) that specify the calculation to be performed

Constants: Numbers or text values that do not change

Functions: Predefined formulas in Excel

To create a basic formula in Excel:

Select the cell for the formula

Type = (the equal sign) and the formula

Click Enter

Calculate with Functions

A function is a built in formula in Excel. A function has a name and arguments (the mathematical function) in parentheses. Common functions in Excel:

Sum: Adds all cells in the argument

Average: Calculates the average of the cells in the argument

Min: Finds the minimum value

Max: Finds the maximum value

Count: Finds the number of cells that contain a numerical value within a range of the argument

To calculate a function:

Click the cell where you want the function applied

Click the Insert Function button

Choose the function

Click OK

Complete the Number 1 box with the first cell in the range that you want calculated

Complete the Number 2 box with the last cell in the range that you want calculated

Function Library

The function library is a large group of functions on the Formula Tab of the Ribbon. These functions include:

AutoSum: Easily calculates the sum of a range

Recently Used: All recently used functions

Financial: Accrued interest, cash flow return rates and additional financial functions

Logical: And, If, True, False, etc.

Text: Text based functions

Date & Time: Functions calculated on date and time

Math & Trig: Mathematical Functions

Relative, Absolute and Mixed References

Calling cells by just their column and row labels (such as "A1") is called relative referencing.

When a formula contains relative referencing and it is copied from one cell to another, Excel does not create an exact copy of the formula. It will change cell addresses relative to the row and column they are moved to. For example, if a simple addition formula in cell C1 "=(A1+B1)" is copied to cell C2, the formula would change to "=(A2+B2)" to reflect the new row. To prevent this change, cells must be called by absolute referencing and this is accomplished by placing dollar signs "$" within the cell addresses in the formula. Continuing the previous example, the formula in cell C1 would read "=($A$1+$B$1)" if the value of cell C2 should be the sum of cells

A1 and B1. Both the column and row of both cells are absolute and will not change when copied.

Mixed referencing can also be used where only the row OR column fixed. For example, in the formula "=(A$1+$B2)", the row of cell A1 is fixed and the column of cell B2 is fixed.

Linking Worksheets

You may want to use the value from a cell in another worksheet within the same workbook in a formula. For example, the value of cell A1 in the current worksheet and cell A2 in the second worksheet can be added using the format "sheetname!celladdress". The formula for this example would be "=A1+Sheet2!A2" where the value of cell A1 in the current worksheet is added to the value of cell A2 in the worksheet named "Sheet2".

4.2. Data Analysis

Excel's Goal Seek feature allows you to alter the data used in a formula in order to find out what the results will be.

Goal Seek is used to get a particular result when you're not too sure of the starting value.

Consider this problem:

Your business has a modest profit of 25,000. You've set yourself a new profit Goal of 35,000. At the moment, you're selling 1000 items at 25 each. Assume that you'll still sell 1000 items. The question is, to hit your new profit of 35,000, by how much do you have to raise your prices?

Create the spreadsheet below, and we'll find a solution with Goal Seek.

The spreadsheet is split into two: Current Sales, and Future Sales. We'll be changing the Future

Sales with Goal Seek. But for now, enter the same values for both sections. The formula to enter for B4 is this:

= B2 * B3

And the formula to enter for E4 is this:

= E2 * E3

The current Price Per Item is 25.00. We want to change this with Goal Seek, because our prices will be going up to hit our new profits of 35,000. So try this:

From the Excel menu bar, click on Data

Locate the Data Tools panel and the What if Analysis item. From the What if Analysis menu, select Goal Seek

 The following dialogue box appears:

For "Set cell", enter E4. This is where the formula is. The "To Value" is what we want our new profits to be. So enter 35000. The "By changing cell" is the part we're not sure of. For us, this was the price each item needs to be increased by. This was coming from cell E3 on our spreadsheet. So enter E3 in the "By changing cell" box. Your Goal Seek dialogue box should now look like this:

Click OK to see if Excel can find an answer:

Excel is now telling that it has indeed found a solution. Click OK to see the new version of the spreadsheet:

Our new Price Per Item is 35. Excel has also changed the Profits cell to 35 000.

Try this Exercise

You've had a meeting with your staff, and it has been decide that a price change from 25 to 35 is not a good idea. A better idea is to sell more items. You still want a profit of 35 000. Use Goal

Seek to find out how many items you'll have to sell to meet your new profit figure.

4.3. Managing Data

Sorts

To execute a basic descending or ascending sort based on one column:

Highlight the cells that will be sorted

Click the Sort & Filter button on the Home tab

Click the Sort Ascending (A-Z) button or Sort Descending (Z-A) button

Custom Sorts

To sort on the basis of more than one column:

Click the Sort & Filter button on the Home tab

Choose which column you want to sort by first

Click Add Level

Choose the next column you want to sort

Click OK

Filtering

Filtering allows you to display only data that meets certain criteria. To filter:

Click the column or columns that contain the data you wish to filter

On the Home tab, click on Sort & Filter

Click Filter button

Click the Arrow at the bottom of the first cell

Click the Text Filter

Click the Words you wish to Filter

To clear the filter click the Sort & Filter button

Click Clear

Using Custom AutoFilter

Custom AutoFilter allows you to filter a range of information and/or set multiple criteria.

1.

Activate Table Filtering

2.

In the column you want to filter, click the

» select

Text Filters or Number Filters »

Custom Filter…

The Custom AutoFilter dialog box appears.

NOTES:

If a column contains text, the Table filter pull-down list provides Text Filters ; if the column contains numbers, Number Filters are provided.

3.

In the dialog box below, the column being filtered is called Amount and contains values ranging from 134.78 to 987.32, which are displayed in ascending order in the Custom

AutoFilter pull-down list.

4.

In the Comparison Operator pull-down list, select a type of comparison

EXAMPLE: Select is greater than

5.

In the Corresponding pull-down list, select or type a criteria value

EXAMPLE: Type 300

6.

OPTIONAL: If you want multiple criteria, select either And or Or and repeat steps 3 and

4

EXAMPLE:

In the Comparison Operator pull-down list, select is less than

In the Corresponding pull-down list, type 500

7.

Click OK

Your Table is filtered to display rows in the selected column containing values between

300 and 500

8.

To remove the filter from your Table , in the filtered column, click the

» select

Clear

Filter From...

Turning Off the AutoFilter

1.

Select a cell within the Table

2.

From the Home command tab, in the Editing group, click SORT & FILTER deselect Filter

OR

»

From the Data command tab, in the Sort & Filter group, click FILTER

AutoFilter is disabled; the AutoFilter buttons are removed from the Table .

Using Advanced Filter

Excel's Advanced Filter has advantages not offered by the standard filter, such as its complex "and/or" filtering options. It also lets you move filtered Table data to a different area of the current worksheet.

Creating a criteria range

A criteria range consists of at least two rows. The first row must contain a column label, the other must provide a filtering condition.

For example, if your Table has a column labeled Assignment , the top row of the criteria could be Assignment (i.e., the column label), and the next row could be the name of a particular assignment (i.e., the condition) you want filtered.

Additional filtering conditions can be established in subsequent rows, allowing you to establish a complex filter. At least one blank row must separate your Table from your criteria range. For more information on criteria, refer to Establishing Criteria.

Running an Advanced Filter

1.

Create a criteria range within your worksheet

2.

Select any cell within your Table

3.

From the Data command tab, Sort & Filter group, click ADVANCED FILTER

The Advanced Filter dialog box appears.

4.

If you want the filter to replace the current Table , select Filter the list, in-place

NOTE: If you do not want the filter to replace the current Table , refer to Copying an

Advanced Filter to Another Location.

5.

In the List range text box, type the cell range containing your Table

OR

To minimize the Advanced Filter dialog box so you can manually select your Table range a.

Click COLLAPSE DIALOG b.

Select your Table range c.

Click RESTORE DIALOG

6.

In the Criteria range field, type the cell range (or range name) containing the criteria

OR

To minimize the Advanced Filter dialog box so you can manually select cell range a.

Click COLLAPSE DIALOG b.

Select the criteria range c.

Click RESTORE DIALOG

7.

Click OK

Your Table is filtered.

Table row numbers turn blue.

Turning Off Advanced Filter

To remove an Advanced Filter from your Table :

1.

From the Data command tab, in the Sort & Filter group, click CLEAR

All Table filters are removed.

Copying an Advanced Filter to Another Location

1.

Create a criteria range within your worksheet

2.

Select a cell within your Table

3.

From the Data command tab, click ADVANCED FILTER

The Advanced Filter dialog box opens.

4.

Select Copy to another location

5.

In the List range text box, type the cell range containing your Table

OR

To minimize the Advanced Filter dialog box so you can manually select your Table range a.

Click COLLAPSE DIALOG b.

Select your Table range c.

Click RESTORE DIALOG

6.

In the Criteria range field, type the cell range (or range name) containing the criteria

OR

To minimize the Advanced Filter dialog box so you can manually select the cell range a.

Click COLLAPSE DIALOG b.

Select the criteria range c.

Click RESTORE DIALOG

7.

In the Copy to text box, type a cell or range in the active worksheet where the filter results will appear

OR

To minimize the Advanced Filter dialog box so you can manually select a cell or range a.

Click COLLAPSE DIALOG b.

Select the cell or range c.

Click RESTORE DIALOG

8.

Click OK

4.4. Organizing Data

Analyzing data using What-if analysis

In Excel, a Data Table is a way to see different results by altering an input cell in your formula.

As an example, we're going to alert the interest rate, and see how much a £10,000 loan would cost each month. The interest rate will be our input cell. By asking Excel to alter this input, we can quickly see the different monthly payments. Want to know how much we'd pay back each month if the interest was 24 percent per year. But other banks may be offering better deals. So we'll ask

Excel to calculate how much we'd pay each month if the interest rate was 22 percent a year, 20 percent a year, and 18 percent a year.

The formula we need is the - PMT( ).:

PMT( rate, nper, pv, fv, type )

We only need the first three arguments. So for us, it's just this:

PMT( rate, nper, pv )

Rate means the interest rate. The second argument, nper , is how many months you've got to pay the loan back. The third argument, pv , is how much you want to borrow.

Let's make a start then. On a new spreadsheet, set up the following labels:

So we'll put our starting interest rate in cell B3 ( rate ), our loan length in cell B4 ( nper ), and our loan amount in cell B5 ( pv ).

Enter the following in cells B3 to B5:

So you need to enter 24.00% in cell B3, 60 in cell B4, and £10,000 in cell B5.

We'll enter our formula now. Click inside cell D2 and enter the following:

=PMT( B3 / 12, B4, -B5 )

Cell B3 is the interest rate. But this is for the entire year. In the formula, we're diving whatever is in cell B3 by 12. This will get us a monthly interest rate. B4 in the formula is the number of months, which is 60 for us. B5 has a minus sign before it. It's a minus figure because it's a debt.

When you press the enter key on your keyboard, Excel should give you an answer of £287.68.

Now that we have our function in place, we can create an Excel Data Table. First, though, we need to tell Excel about those other interest rates. It will use these to work out the new monthly payments. Remember, Excel is recalculating the PMT function. So it needs some new values to calculate with.

So enter some new values in cells C3, C4, and C5. Enter the same ones as in the image below:

We have put the PMT function in cell D2 for a reason. This is one Row up, and one Column to the right of our first new interest rate of 22%. The new monthly payments are going to go in cells

D3 to D5. Excel needs the table setting out this way.

So that Excel can work out the new totals, you have to highlight both the new values and the

Function you're using.

So Highlight the cells C2 to D5. Your spreadsheet should look like this:

As you can see, the cells C2 to D5 are now highlighted. This includes our new interest rate values in the C column, and our PMT function in cell D2. We can now create an Excel 2007 Data Table.

This will work out new monthly payemnts for us. So do this:

From the Excel menu bar, click on Data

Locate the Data Tools panel

 Click on the "What if Analysis" item:

When you click on the "What if Analysis" item, you'll see the following menu:

Click on Data Table , and you'll see this small dialogue box:

In the dialogue box, there is only a Row input cell or a Column input cell . We want Excel to fill downwards, down a column. So we need the second text box on the dialogue box "Column input cell". If we were filling across in rows, we would use the "Row input cell" text box.

The Input Cell for us is the one that contains our original interest rate. This is the cell you want

Excel to substitute.

So click inside the Column input cell box and enter B3:

Click OK. When you do, Excel will work out the new monthly payments:

So if we could get an 18 percent interest rate, our monthly payments would be £253.93.

If you click inside any of the cells D3 to D5, then look at the formula bar, you will see this:

{=TABLE(,B3)}

Table has been created, based on the input cell B3.

Two-input data table

You have 250 items that you want to sell on EBay. Your unique selling point is this - All items are only £5 each! Except, you feel £5 may be a bit expensive for the goods you're selling! What you want to know is how much profit you'll make if you reduce your prices to £4.50, how much if you reduce to £4.00, and how much for a reduction to £3.50. Assume that everything gets sold.

To start creating your Table, construct a spreadsheet like the one below. Make sure that you start on a new sheet.

In cell B1 is the number of items we want to sell (250). Cell B2 has the original price (£5.00).

And the Reductions Row has our new values. Cell B3 has a 0 because there's no reduction for

£5.00. Row 4 is where our Profits will go.

The formula to work out the profits is simply the Number of Items multiplied by the Price Per

Item . So click inside cell B4 and enter the following formula:

= B1 * B2

Your spreadsheet will then look like this:

So if we manage to sell all our items at £5, we'll make £1,250. We're a bit dubious, though.

Realistically, all our items won't sell at this price! Let's use an Excel Data Table to work out how much profit we'd make at the other prices.

Again, we put the answer in cell B4 for a reason. This is because when you want Excel to calculate a Data Table in Rows, the formula must be inserted one Column to the Left of your first new value, and then one Row down. Our first new value is going in cell C3. So one column to the left takes us to the B column. One row down is Row 4. So the formula goes in cell B4.

Next, click inside cell B3 and highlight to cell E4. Your spreadsheet should now look like this one:

Excel is going to use our formula in cell B4. It will then look at the new values on Row 3 (not counting the zero), and then insert the new totals for us. To create a Data Table then, do the following:

 From the Excel menu bar, click on Data

 Locate the Data Tools panel

 Click on the "What if Analysis" item

 Select Data Table from the menu

Just like last time, you'll get the Data Table dialogue box. The one we want now, though, is Row

Input Cell. But what is the Input Cell this time?

Ask yourself what you are trying to work out, and what you want Excel to recalculate. You want to work out the new prices. The formula you entered was:

= B1 * B2

Excel is going to be changing this formula. You only need to decide if you want Excel to alter the

B1 or the B2. B1 contains the number of items; B2 contains the price of each item. Since we're trying to work out the profits we'd get if we change the price, we need Excel to change B2. So enter B2 for the Row Input Cell:

When you click OK, Excel will work out the new profits:

So setting a price of £3.50 per item, you'd make £875 profit. You would make £1,000 at

£4.00 per item, and £1,125 if you sell for £4.50.

Subtotals

Once you have sorted a data range into groups, Excel's Subtotal command can create a subtotal for each group, automatically inserting a new row and displaying the total for each line.

Adding Subtotals

Adjusting Views with Subtotals

Removing Subtotals

Re-sorting the Data

Adding Subtotals

Subtotals are added using the Subtotal dialog box, which is accessed from the Data command tab.

1.

Sort your data range

2.

Select a cell within the data range

3.

From the Data command tab, in the Outline group, click SUBTOTAL

The Subtotal dialog box appears.

HINT: The following two graphics show a data range that has been sorted according to days of the week, and its corresponding Subtotal dialog box, which has been set to display subtotals for the total hours worked on each day of the week.

4.

From the At each change in pull-down list, select the column containing data groups for which you want subtotals

NOTE: It is important to have this column sorted into data groups, because the Subtotal command subtotals numerical data at every change in the column.

EXAMPLE: Select Day

5.

From the Use function pull-down list, select the function that will be used to create subtotals

EXAMPLE: Select Sum

Sum adds the values for each data group

Count determines the number of records in each data group

Average finds the average value in each data group

Max finds the highest value in each data group

Min finds the lowest value in each data group

Product multiplies compounding values in each data group

6.

From the Add subtotal to scroll box, select the column with the numerical data you want subtotaled

7.

OPTIONAL: At the bottom of the dialog box, select the appropriate options

8.

Click OK

Subtotal lines are added according to the column you specified in the At each change in pull-down list and the data groups you sorted in that column.

Adjusting Views with Subtotals

When you have applied subtotals to your data range, an outline bar appears to the left of the row numbers.

The represents levels of detail in the view

Level 1: grand total

Level 2: subtotals and grand total

Level 3: all data in range

To expand the view one level:

1.

Click the plus sign

To collapse the view one level:

1.

Click the minus sign

Removing Subtotals

If you no longer need subtotals, you can easily remove them without deleting rows.

1.

Select a cell within the data range

2.

From the Data command tab, in the Outline group, click SUBOTOTAL

The Subtotal dialog box appears.

3.

Click REMOVE ALL

The subtotals are removed.

Re-sorting the Data

If you re-sort a data range containing subtotals, Excel will warn you that it will remove the subtotals before re-sorting. After the data range has been re-sorted, you can add subtotals again.

Pivot Table

A Pivot Table is way to present information in a report format. The idea is that you can click drop down lists and change the data that is being displayed. For example, choose just one student from a drop down list and view only his or her scores. Pivot tables are a lot easier to grasp when you see them in action. Here's the one we're going to create in this section:

Look at Row 4. This shows that the student is Elisa. If we click Elisa's drop down arrow, we'll see this:

Now we have another student to select (we'll only use two students, for this tutorial). We could untick Lisa, and tick Mary instead. Then her scores would display.

The Subject and Month cells also have drop down lists. So we could view only January's scores, and just for Art and English, for example.

So this is a Pivot Table - a report that we can manipulate by selecting items from drop down lists.

The first thing you need for a Pivot Table is some data to go in it. Type the data.

Highlight the data that will be going in to your Pivot Table (cells A1 to D37). On the Excel 2007 menu bar, click Insert . From the Insert menu, locate the Tables Panel :

On the Tables panel click Pivot Tables . The Create Pivot Tables dialogue box appears:

In the dialogue box above, the data that we highlighted is in the Table/Range textbox. You can select different cells by clicking the icon to the right of the Table/Range textbox. You can also specify an external data source, such as a text file, for the data in your Pivot Table.

We have selected a New Worksheet as the place where the Pivot Table will be placed. Click OK.

When you click OK, Excel 2007 presents you with a rather complex layout. The area on the right should look something like this one below:

It helps to have a look again at what we're trying to create. Here's the completed Pivot Table again:

Now take a look at the Pivot Table Field List image again, the one above the completed pivot table. It has tick boxes for Month, Subject, Student, and Score. These are column headings from the original spreadsheet data. We've put the Month in cell A7 on our Pivot Table, Subject is in cell B6, Student is in cell B4, and Score is the Average scores in cells C8 to G10.

The idea is that you tick a box in the Pivot Table Field List, and then drag it to the four areas below. Excel 2007 will take care of the rest.

So, tick all four boxes in the field list:

Excel will create a basic (and messy) Pivot Table for you. But we're going to put our 4 fields into the 4 areas below. Here is the 4 areas we can drag to:

For the Report Filter , we want the name of a Student. For the Column Labels , we want the

Subject, and for the Row Labels , we'll just have the Month. The Values will be the Average scores.

If you look at the Field areas after you have ticked all four boxes, however, you may see something like this:

Month, Subject and Student have all been grouped under Row Labels . You can drag and drop these, though.

So click on Student in the Row Labels box. Hold down your left mouse button, and then drag it in to the Report Filter box. If you don't fancy dragging and dropping, simply click the Student item with your left button. From the menu that appears, select Move to Report Filter :

Your Field areas will then look like this:

Move Subject from Row Labels to the Column Labels area:

Your Field areas will then look like this:

The Pivot Table on your spreadsheet will look a lot different, too. It should be looking like this:

Our Pivot Table is coming along, but the scores are all wrong, and it needs tidying up a bit.

The reason why the scores from our Pivot Table are so strange is because Excel 2007 is using the wrong formula. It's using a Sum total when we want it to use an Average.

The numbers have all been added up. But we want averages, instead. To change the formula, click on Sum of Score under the Values field area:

You'll see the following menu:

Select, Field Settings to see the following dialogue box:

Change the Formula from Sum to Average , and then click OK. Your Average formula won't be formatted to any decimal places. So highlight you data. On the Home menu in Excel 2007, locate the Number panel. Format your Averages so that it has no decimal places. Your Pivot Table will then look like this:

Look at cells A3, B3 and A4 above. These all have the not very descriptive names of Average of

Score, Column Labels, and Row Labels. You can click inside of these cells and type your own headings, in exactly the same way as you would to enter text in a normal cell.

In the new version of the Pivot Table below, we have renamed these cells. We've also centred the data.

Only one thing left to do - spruce up the table by adding a bit of colour.

Click anywhere on your Pivot Table to highlight it. Now look at the menu bar at the top of Excel

2007. You'll notice a Design menu. Click on this to see the various design options.

The Pivot Table Style Options panel is interesting.

Select Banded Rows and see what happens. Now click Banded Columns .

Next to this panel, there are lots of Pivot Table Styles to chose from. Select one that catches your eye. Here's our finished Pivot Table again, only with a different Style:

This table is original:

4.5. Charting Data and Printing

Creating a Chart

To create a chart:

Select the cells that contain the data you want to use in the chart

Click the Insert tab on the Ribbon

Click the type of Chart you want to create

Modify a Chart

Once you have created a chart you can do several things to modify the chart.

To move the chart:

Click the Chart and Drag it another location on the same worksheet, or

Click the Move Chart button on the Design tab

Choose the desired location (either a new sheet or a current sheet in the workbook)

To change the data included in the chart:

Click the Chart

Click the Select Data button on the Design tab

To reverse which data are displayed in the rows and columns:

Click the Chart

Click the Switch Row/Column button on the Design tab

To modify the labels and titles:

Click the Chart

On the Layout tab, click the Chart Title or the Data Labels button

Change the Title and click Enter

Chart Tools

The Chart Tools appear on the Ribbon when you click on the chart. The tools are located on three tabs: Design, Layout, and Format.

Within the Design tab you can control the chart type, layout, styles, and location.

Within the Layout tab you can control inserting pictures, shapes and text boxes, labels, axes, background, and analysis.

Within the Format tab you can modify shape styles, word styles and size of the chart.

Printing

Printing allows you to get your work on paper. Data available on your worksheets can be explored and exploited in various areas including visual presentation or book review. Since printing involves different issues, it is important to know what Microsoft Excel offers .

Before printing, make sure that a printer is available for your computer. This means that, a printer could be attached to your computer or you may use a networked shared printer.

Print Preview

Print preview allows you to open a special window that would present the document as it would appear when it is printed. In Microsoft Excel, the Print Preview window is not just used to preview the printed document. There are other configurations you can perform. To access the

Print Preview window, you can click the Office Button, position the mouse on Print, and click

Print Preview.

In Print Preview, Microsoft Excel displays a Ribbon with only one tab:

Next Page

Previous Page Shows the previous section of the printed page

Allows you to preview the next page of a document that spreads more than one page

Zoom

Print

Page Setup

Since the Print Preview window is not used to read the document but to preview it, if you want to take a closer look at the document, click the Zoom button to zoom in. If you click the zoom button again, the preview area would come back to the previous display.

Used to call the Print dialog box

Would call the Page Setup dialog. The Page Setup dialog box will be reviewed in another section

Besides displaying a preview of the printed paper, the Print Preview window allows you to "enlarge" or "squeeze" the document. To do this, you can first click the Show Margins check box. Once clicked the window would display lines around the document Show Margins

The Print Dialog Box

Based on this, to shrink, enlarge, heighten or narrow the printed document, you can click one of the lines and drag in the desired direction.

The printing process in Microsoft Excel offers all the classic default features of any other application and other specific issues related to a spreadsheet. To control printing on your worksheet, you use the Print dialog box. To access the Print dialog box:

 Click the Office Button, position the mouse on Print, and click Print

 Click the Office Button, position the mouse on Print, and click Print Preview. In the Print section of the Ribbon, click Print

The Print dialog box displays some of the items you are already familiar with such as the title bar, the Context Sensitive Help, the close, the OK, and the Cancel buttons.

When a printer (at least one) is available for your computer, it is usually set as the default printer and it displays on the Printer Name combo box. If more than one printer is available, when you decide to print, click the Printer Name combo box and select the desired printer from the list.

If your worksheet is long, sometimes very long, and expand on various pages, you have the option of printing all the pages or a range of pages. This can be configured in the Print Range section of the dialog box.

You can also select some sections of the worksheet and print just that. This is configured in the

Print What section. By default, Microsoft Excel prints the active worksheet, that is, the worksheet that is displaying in the background. If you decide to print everything on your workbook, you can click the Entire Workbook radio button.

For a presentation or any other purpose, if you want to print various copies of the worksheet, increase the number by using the Number Of Copies spin button.

One thing you should always do is to preview your worksheet before the actual printing, this allows you to have an idea of what your worksheet would look like on a piece of paper. You can preview your job by clicking the Preview button.

Page Orientation

Whenever you ask Microsoft Excel to print the contents of a worksheet, by default, it prints the document vertically, this is considered as Portrait. Some and many of the worksheets spread wider than taller. Therefore, you should check or change their page orientation before printing.

That is why you have the option of choosing one of the orientation radio buttons. If the document or the section to be printed is wider than taller, you can change the Orientation to

Landscape.

To specify that whether the document would print in Portrait or landscape:

 In the Page Layout tab of the Ribbon, in the Page Setup section, click Orientation and click one of the options

On the Ribbon of the Print Preview window, click the Page Setup button. In the

General tab and in the Orientation section, click either Portrait or Landscape

Page Scaling

By default, the printed document will adjust itself to 100% fitting a ratio of 1/1 page wide and tall. To change any of these settings:

 In the Page Layout tab of the Ribbon, in the Scale To Fit section, click Orientation and click one of the options in the Width combo box, the Height combo box, or the Scale spin button

 Display the Page Setup dialog box. In the General tab and in the Scale section, use the appropriate spin buttons. The options are Adjust To, Fits To, and Page(s) Wide By

Paper Size

By default, the paper size is set to the Letter paper dimension, which is 8.5 by 11 inches. If this doesn’t fit your desire, you can change it:

 In the Page Setup section of the Page Layout tab of the Ribbon, click the Size button to show a list of the available options

 Display the Page Setup dialog box. In the Page tab, click the arrow of the Paper Size combo box and select the desired size from the list

Print Quality

The result of the printed-paper depends on your printer. If your printer allows more configuration, in the General tab of the Page Setup dialog box, click the Print Quality combo box and select a different setting.

The Print button would call the Print dialog that we saw earlier.

If you want to take a look at the printed result before actually printing, click the Print Preview button.

Although the Print button would call the Print dialog box, the Options button allows you to change or configure the properties of the printer.

MODULE 4

Unit 1

BASICS OF MICROSOFT POWERPOINT

Structure :

1.1.

1.2.

1.3.

1.4.

1.5.

1.6.

1.7.

1.8.

1.9.

1.10.

Introduction to PowerPoint,

PowerPoint Screen

Creating New Presentation,

Slides

Themes

Slide Views

Formatting

Customising Presentation

Slide Transition

Animation

1.1. Introduction to PowerPoint,

PowerPoint is used to give presentation. It is a collection of data and information that is to be delivered to a specific audience. A PowerPoint presentation is a collection of electronic slides that can have text, pictures, graphics, tables, sound and video. This collection can run automatically or can be controlled by a presenter.

One way you can open Microsoft PowerPoint by clicking Start -> (All) Programs ->

Microsoft Office -> Microsoft Office PowerPoint .

1.2. Microsoft PowerPoint Screen

Getting started with PowerPoint 2007 you will notice that there are many similar features to previous versions. You will also notice that there are many new features that yo u’ll be able to utilize. There are three features that you should remember as you work within

PowerPoint 2007: the Microsoft Office Button, the Quick Access Toolbar, and the

Ribbon.

Microsoft Office Button

The Microsoft Office Button performs many of the functions that were located in the File menu of older versions of PowerPoint. This button allows you to create a new presentation, Open an existing presentation, save and save as, print, send, or close.

Ribbon

Ribbon is the panel at the top portion of the document It has seven tabs: Home, Insert,

Design, Animations, Slide Show, Review and View. Each tab is divided into groups.

The groups are logical collections of features designed to perform function that you will utilize in developing or editing your PowerPoint slides.

Commonly utilized features are displayed on the Ribbon. To view additional features within each group, click the arrow at the bottom right corner of each group.

Home : Clipboard, Slides, Font, Paragraph, Drawing, and Editing

Insert : Tables, Illustrations, Links, Text, and Media Clips

Design : Page Setup, Themes, Background

Animations : Preview, Animations, Transition to this Slide

Slide Show : Start Slide Show, Set Up, Monitors

Review : Proofing, Comments, Protect

View : Presentation Views, Show/Hide, Zoom, Window, Macros

Quick Access Toolbar

The quick access toolbar is a customizable toolbar that contains commands that you may want to use. You can place the quick access toolbar above or below the ribbon. To change the location of the quick access toolbar, click on the arrow at the end of the toolbar and click Show Below the Ribbon .

You can also add items to the quick access toolbar. Right click on any item in the Office

Button or the Ribbon and click Add to Quick Access Toolbar and a shortcut will be added.

Mini Toolbar

A new feature in Office 2007 is the Mini Toolbar. This is a floating toolbar that is displayed when you select text or right-click text. It displays common formatting tools, such as Bold, Italics, Fonts, Font Size and Font Color.

Navigation

Navigation through the slides can be accomplished through the Slide Navigation menu on the left side of the screen. Also, an outline appears from materials that have been entered in the presentation. To access the outline, click the outline tab.

1.3. Creating New Presentation

You can start a new presentation from a blank slide, a template, existing presentations, or a Word outline.

To create a new presentation from a blank slide:

Click the Microsoft Office Button

Click New

Click Blank Presentation

To create a new presentation from a template:

Click the Microsoft Office Button

Click New

Click Installed Templates or Browse through Microsoft Office Online Templates

Click the template you choose

To create a new presentation from an existing presentation:

Click the Microsoft Office Button

Click New

Click New from Existing

Browse to and click the presentation

To create a new presentation from a Word outline:

Click the slide where you would like the outline to begin

Click New Slide on the Home tab

Click Slides from Outline

Browse and click the Word Document that contains the outline

Save a Presentation

When you save a presentation, you have two choices: Save or Save As .

To save a document:

Click the Microsoft Office Button

Click Save

You may need to use the Save As feature when you need to save a presentation under a different name or to save it for earlier versions of PowerPoint. Remember that older versions of PowerPoint will not be able to open PowerPoint 2007 presentation unless you save it as a PowerPoint 97-2003 Format. To use the Save As feature:

Click the Microsoft Office Button

Click Save As

Type in the name for the Presentation

In the Save as Type box, choose Excel 97-2003 Presentation

1.4.

Add Slides

Slides

There are several choices when you want to add a new slide to the presentation: Office

Themes, Duplicate Selected Slide, or Reuse Slides.

To create a new slide from Office Themes:

Select the slide immediately BEFORE where you want the new slide

Click the New Slide button on the Home tab

 Select any one

To create a slide as a duplicate of a slide in the presentation, Click Duplicate Selected

Slides

To create a new slide from another presentation:

 Select the slide immediately BEFORE where you want the new slide

Click the New Slide button on the Home tab

Click Reuse Slides

Click Browse

Click Browse File

Locate the slide show and click on the slide to import

Deleting Slides

 Select the slide

Click on Home Tab

Click on Delete

1.5. Themes

Themes are design templates that can be applied to an entire presentation that allows for consistency throughout the presentation. To add a theme to a presentation:

Click the Design tab

Choose one of the displayed Themes or click the Galleries button

To apply new colors to a theme:

 Click the Colors drop down arrow

 Choose a color set or click Create New Theme Colors

To change the background style of a theme

 Click the Background Styles button on the Design tab

1.6. Slide Views

Presentations can be viewed in a variety of manners. On the View tab, the Presentation

Views group allows you to view the slides as Normal, Slide Sorter, Notes Page, Slide

Show, Slide Master, Handout Master, and Notes Master.

To access all the different slide views, click on the View ribbon at the top of the screen.

On the left end of the View tab of the ribbon you will see the different choices for methods to view your slides.

PowerPoint 2007 offers a wide range of customizable options that allow you to make

PowerPoint work the best for you. To access these customizable options:

Click the Office Button

Click PowerPoint Options include picture of Office Button menu.

Slide View

Normal View or Slide View as it is more commonly known, is the most often used view while creating a PowerPoint presentation.

When you first start the program, the screen opens in Normal View. Working on a large version of a slide is helpful when designing your presentation.

Slide Sorter View

Slide Sorter view shows miniature versions of all the slides in horizontal rows. These miniature versions of the slides are called thumbnails.

Slides can be easily deleted or rearranged quickly in this view. Effects such as transitions and sounds can be added to several slides at the same time in Slide Sorter view .

Notes Page

Notes Pages show a smaller version of the slide with an area below for printed notes.

Each slide is created on its own notes page. The speaker can print these pages out to use as a reference while making his presentation. The notes do not show on the screen during the presentation.

Slide Show

Slide Show view is the view used for presenting to an audience. The whole screen is taken over by your PowerPoint 2007 presentation.

Slide Show view can be accessed from the View tab of the ribbon or by pressing the F5 key on the keyboard. To exit Slide Show view at any time, press the Esc key on the keyboard.

Outline View

Outline View can be accessed by selecting the Outline tab on the Slides/Outline pane at the left of the PowerPoint 2007 screen. Outline view shows all the text of all slides in a list. No graphics are shown in Outline view.

Outline view is useful for quick editing purposes and can be exported out as a Word document to use as a summary handout.

Quick Access Buttons

At the bottom right corner of the PowerPoint screen, you will see the quick access buttons. These handy buttons are the same as in previous versions of PowerPoint. The screen shot above indicates the different buttons and their uses. You can quickly switch to Slide Sorter view, Normal (or Slide) view, or Slide Show view. You can also zoom in or out on the current view, or fit the current slide to the window.

1.7. Formatting

Change Font Typeface and Size

To change the font typeface:

 Click the arrow next to the font name and choose a font.

 Remember that you can preview how the new font will look by highlighting the text, and hovering over the new font typeface.

To change the font size:

Click the arrow next to the font size and choose the appropriate size, or

Click the increase or decrease font size buttons.

Font Styles and Effects

Font styles are predefined formatting options that are used to emphasize text. They include: Bold, Italic, and Underline. To add these to text:

Select the text and click the Font Styles included on the Font group of the Home

 tab or

Select the text and right click to display the font tools

Change Text Color

T o change the text color:

Select the text and click the Colors button included on the Font Group of the

Ribbon, or

Highlight the text and right click and choose the colors tool.

Select the color by clicking the down arrow next to the font color button.

Bulleted Lists

To add bullets to your text, click on the arrow next to the button with bullets, then choose one of the symbols. If you would like to use a different symbol, click on Bullets and

Numbering and customize your bullets.

Numbering

To add numbering to your text, click on the arrow next to the button with numbering, then choose one of the styles listed. If you would like to change the style, click on Bullets and

Numbering and customize your bullets.

Clear All Text Formatting

This button allows you to clear all changes you have made to the text.

Background

The Design ribbon contains the features you will need to add or change a background in

PowerPoint.

Click on the background button on the right end of the Design ribbon .

This will open the Format Background dialog box.

Use the Solid Fill Option

Solid color choices are shown in the Fill section of the Format Background dialog box.

 Click the Color drop down button to reveal standard color choices.

 Select a color shown or click on the More Colors...

option to reveal more color choices.

Use a Preset Gradient

PowerPoint has several preset gradient fills available for you to choose as a background for your slides. Gradient colors can be effective as a PowerPoint background if chosen wisely. Be sure to consider the audience clientele when you choose the preset gradient background colors for your presentation.

Click the option for Gradient fill.

Click the drop down Preset colors button

Select a preset gradient fill

Click the Close button or the Apply to All button.

Background Textures

 Use textured backgrounds in PowerPoint carefully. They are often busy and

 make text difficult to read. This can detract from your message.

When opting to choose a textured background for your PowerPoint presentation, choose a subtle design and make sure that there is good contrast between the background and the text.

Clip Art or Photographs as PowerPoint Backgrounds

Photographs or clip art can be added as a background for your PowerPoint presentations. When you insert a picture or clip art as the background,

PowerPoint will stretch it to cover the whole slide, if the object is small. This can often cause distortion to the graphic object and therefore some photos or

 graphics can be poor choices for backgrounds.

If the graphic object is small, it can be tiled over the slide. This means that the picture or clip art object will be placed repeatedly across the slide in rows to completely cover the slide.

1.8. Customising Presentation

Inserting Picture

To add a picture:

Click the Insert Tab

Click the Picture Button

Browse to the picture from your files

Click the name of the picture

Click insert

To move the graphic, click it and drag it to where you want it

Adding Clip Art

To add Clip Art:

Click the Insert Tab

Click the Clip Art Button

Search for the clip art using the search Clip Art dialog box

Click the clip art

 To move the graphic, click it and drag it to where you want it

Editing Pictures and Clip Art

When you add a graphic to the presentation, an additional Tab appears on the Ribbon.

The Format Tab allows you to format the pictures and graphics. This tab has four groups:

Adjust: Controls the picture brightness, contrast, and colors

Picture Style: Allows you to place a frame or border around the picture and add effects

Arrange: Controls the alignment and rotation of the picture

Size: Cropping and size of graphic

Adding a Shape

To add Shapes:

Click the Insert Tab

Click the Shapes Button

Click the shape you choose

Adding SmartArt

SmartArt is a feature in Office 2007 that allows you to choose from a variety of graphics, including flow charts, lists, cycles, and processes. To add SmartArt:

Click the Insert Tab

Click the SmartArt Button

Click the SmartArt you choose

Click the SmartArt

Drag it to the desired location in the slide

To format the SmartArt:

Click the SmartArt

Click either the Design or the Format tab

Click the SmartArt to add text and pictures .

Adding a Photo Album

The photo album feature is new in PowerPoint 2007 and allows you to easily create a photo album to share pictures. To create a photo album:

Click the Photo Album button on the Insert tab

Click New Photo Album

Click File/Disk to add pictures to the photo album

Move the pictures up and down in the order of the album but clicking the up/down arrows

Tables are used to display data in a table format.

Adding Video

Video clips can be added to the presentation. To add a video clip:

Click the Movie button on the Insert tab

Choose Movie from File or Movie from Clip Organizer

To edit the video options:

Click the movie icon

Click the Format tab

Adding Audio

Audio clips can be added to the presentation. To add an audio clip:

Click the Audio button on the Insert tab

Choose Sound from File, Sound from Clip Organizer, Play CD Audio Track, or Record Sound

To edit the audio options:

Click the audio icon

Click the Format tab

1.9. Slide Transitions

Transitions are effects that are in place when you switch from one slide to the next. To add slide transitions:

Select the slide that you want to transition

Click the Animations tab

Choose the appropriate animation or click the Transition dialog box

To adjust slide transitions:

Add sound by clicking the arrow next to Transition Sound

Modify the transition speed by clicking the arrow next to Transition Speed

To apply the transition to all slides:

Click the Apply to All button on the Animations tab

To select how to advance a slide :

Choose to Advance on Mouse Click , or

Automatically after a set number of seconds

1.10. Slide Animation

Slide animation effects are predefined special effects that you can add to objects on a slide. To apply an animation effect:

Select the object

Click the Animations tab on the Ribbon

Click Custom Animation

Click Add Effect

Choose the appropriate effect

Slide Show Options

The Slide Show tab of the ribbon contains many options for the slide show. These options include:

Preview the slide show from the beginning

Preview the slide show from the current slide

Set up Slide Show

Set Up Slide Show

This option allows you to set preferences for how the slide show will be presented. The options include:

Whether the show will run automatically or will be presented by a speaker

The looping options

Narration options

Monitor resolutions

Print a Presentation

There are many options for printing a presentation. They are:

Slides: These are slides that you would see if you were showing the presentation, one slide per page

Handouts: 1, 2, 3, 4, 6 or 9 per page, this option allows for more slides per page

Notes Page: This includes the slides and the speaker notes

Outline View: This will print the outline of the presentation

To access the print options:

Click the Microsoft Office Button

Click Print

In the Print Dialog Box, click the arrow next to Print what

Choose the format and click OK to print

Unit 2 : MICROSOFT ACCESS

Structure:

2. 1. Introduction to Database

2. 2. Starting of Access

2. 1. Introduction to Database

Defining a Database, Understanding RDBMS, Objects of a Relational Database,

Functions of a Database Management System (DBMS),

A database management system (DBMS), sometimes just called a database manager, is a program that lets one or more computer users create and access data in a database. The

DBMS manages user requests (and requests from other programs) so that users and other programs are free from having to understand where the data is physically located on storage media and, in a multi-user system, who else may also be accessing the data. In handling user requests, the DBMS ensures the integrity of the data (that is, making sure it continues to be accessible and is consistently organized as intended) and security (making sure only those with access privileges can access the data). The most typical DBMS is a relational database management system (RDBMS). A standard user and program interface is the Structured Query Language (SQL). A newer kind of DBMS is the objectoriented database management system (ODBMS).

2. 2. Starting of Access

Microsoft Access is a computer application used to create and manage computer-based databases on desktop computers and/or on connected computers (a network). Microsoft Access can be used

for personal information management, in a small business to organize and manage data, or in an enterprise to communicate with servers.

To start this program, you could click Start -> (All) Programs -> Microsoft Office -> Microsoft

Office Access 2007. You can also launch it from Windows Explorer or My Computer. To do this, locate its shortcut in Windows Explorer or My Computer. By default, Microsoft Access 2007 is located in C:\Program Files\Microsoft Office\Office12 and its shortcut is called

MSACCESS.EXE. Once you have located it, you can then double-click it.

The Office Button

When Microsoft Access opens, it displays an interface divided in various sections. The top section displays a long bar also called the title bar.

The title bar starts on the left side with the Office Button tool tip would appear:

. If you position the mouse on it, a

The menu of the Office Button allows you to perform the routine Windows operations of a regular application, including creating a new database, opening an existing database, saving an object, etc.

The Quick Access Toolbar

On its right side, the Office Button displays a (short) object called the Quick Access Toolbar

. If you want to hide the Quick Access toolbar, you can right-click it and click

Remove Quick Access Toolbar.

By default the Quick Access toolbar is equipped with three buttons: Save, Undo, and Redo.

On the right side of the Quick Access toolbar, there is the Customize button with a down-pointing arrow.

The role of this button is to help you decide what buttons to display on the Quick Access toolbar.

To add a button to the Quick Access toolbar, click the Customize button and click the desired button from the menu. If the available buttons are not enough or if you do not see a button you want:

You can click the More Commands button

You can right-click the Quick Access toolbar and click Customize Quick Access Toolbar

Any of these actions would open the Access Options dialog box:

To add a command, click it in the middle list and click Add. Once you have selected the desired options, click OK.

The main or middle area of the top section displays the name of the application: Microsoft

Access.

On the right side of the title bar, there are three system buttons that allow you to minimize, maximize, restore, or close Microsoft Access.

Under the title bar, there is another bar with a Help button on the right side.

The Work Area

The main area of the Microsoft Access interface is divided in three sections. The left side displays a column of various links with the top labeled Online Templates.

The middle section itself is made of two sides. The top part allows you to create a database. The bottom section displays some promotional information from Microsoft.

The right side displays as a column with the top title labeled Open Recent Database with a More button under it. By default, the area under the More button is empty:

The bottom section of the Microsoft Access interface displays a status bar.

The Database as a File

A Microsoft Access database is primarily a Windows file. It must have a location, also called a path, which indicates how the file can be retrieved and made available. Although you can create a database on the root directory such as the C: drive, it is usually a good idea to create your files, including your databases, in an easily recognizable folder.

When you installed the computer (or when it was installed), it (the operating system) might have created a folder called My Documents that provides a convenient place for you to create your files. If various people use the same computer, there is a different My Documents folder for each one. When you log in, the computer (the operating system) locates your corresponding My

Documents folder and makes it available.

Creating a Database

In our lessons, we will learn different techniques of creating a database. For now, a database is first of all a Windows file. It is mainly created from Microsoft Access. If you have just started

Microsoft Access, to create a database, you can use one of the links in the main (middle section of the interface).

You can proceed from one of these options. Like every file in the computer, a database must have a name that identifies it. This name must be specified when creating the database.

In our description of the Microsoft Access interface, we saw that the right section displayed an empty area. If you start creating a database as we will see in the next sections and lessons, the right side gets filled with some options, such as prompting you to name your database.

The Database Wizard

Many techniques allow you to create a database, the fastest of which consists of using one of the provided templates. To create a database using one of the samples, in the left section, first click

Local Templates, then, in the main section, select one of the samples under Local Templates:

The templates are organized in categories. To access a sample by category, in the left section, you can click Business, Personal, or Education. When you click a category, its templates display under Local Templates and you can choose one. After selecting a template, in the right section, you must give a name to your new database in the File Name text box. By default, Microsoft

Access suggests a name you can use. If you do not like it, you can provide your own. Also, by default, Microsoft Access suggests that the database be created in the My Documents folder. If you want it located in another folder, you can click the Browse button . This would open the

File New Database dialog box where you can select an existing folder or create a new one using the Create New Folder button . Display the folder in the Save In combo box and click OK.

Once you have specified the name of the database and its location, you can click Create.

After you have created a database, as a computer file, it becomes part of what are known in

Microsoft Windows as the most recently used (MRU) documents. As such, when you click the

Office Button, the right side of the menu shows a list of MRUs. Here is an example of clicking the Office Button after a few databases have been created:

Database Properties

Database project has its own properties that you can use to find out some details about your file, to enter some notes about the project, or to give directives to other people who have access to the database. This can be done using a dialog box called the Database Properties. To access it, you can click the Office Button -> Manage -> Database Properties.

Opening a Database

To open a database, you can click the Office Button. If you see your intended database in the right column, you can click it to open it.

Deleting a Database

If you have a database you do not need anymore, you can delete it. To delete a database, in My

Documents, in Windows Explorer or another file management application:

You can click it to select it and press Delete

You can right-click it and click Delete

The Ribbon

Under the title bar, Microsoft Access displays a long bar called the ribbon:

\

The ribbon is a type of property sheet made of various property pages. Each page is represented with a tab. To access a tab:

You can click its label or button, such as Home or Create

You can press Alt or F10. This would display the access key of each tab.

The Navigation Pane

After creating or opening a database, unless the product is setup otherwise, the left section is occupied by a rectangular object called the Navigation Pane. The Navigation Pane is the central

point of a database. It allows you to review the objects that are part of a database. You also use it to change the way the objects display, whether the objects should appear in categories, and what categories.

By default, the Navigation Pane appears as a rectangular box with a title on top, a yellow downpointing button and a Shutter Bar Open/Close Button . If you want to minimize the

Navigation button, you can click the Shutter Bar Open/Close Button . If you click it, the

Shutter Bar Open/Close Button changes and the Navigation Pane becomes a vertical bar:

To expand the Navigation Pane again, you can click the Shutter Bar Open/Close Button or you can click the bar itself.

Tables and Objects in the Navigation Pane

The top section of the Navigation Pane displays an All Tables label:

When you create a table, a section gets created for that table. The top section of the table holds its name. The name of a table is followed by a colon and Table:

In the same way, as you create more tables, each gets its own section in the Navigation Pane.

By default, when you create the objects that are associated with a table, the objects of the category display. This means the category is expanded. If you want to minimize a section, which is referred to as collapsing, you can click its bar.

If you want to hide a category, right-click its category header and click Hide.

Access Objects

To view or hide the objects on the Navigation pane:

 You click the double down-arrows to view objects. The double down-arrows change to double up-arrows .

 You click the double up-arrows to hide objects. The double up-arrows change to double down-arrows .

As stated earlier, the Navigation pane stores the objects in your database: tables, queries, forms, reports, macros, and modules. Objects always display with an icon to the right.

The icon tells you the object type: table, query, form, report, macro, and module.

Tables

Queries

Forms

Reports

Macros

Modules

Objects

In Access, data is stored in tables. A table is a set of columns and rows, with each column referred to as a field. Each value in a field represents a single type of data. Each row of a table is referred to as a record.

You use queries to retrieve specific data from your database and to answer questions about your data. For example, you can use a query to find the names of the employees in your database who live in a particular state.

Forms give you the ability to choose the format and arrangement of fields. You can use a form to enter, edit, and display data.

Reports organize or summarize your data so you can print it or view it onscreen. You often use reports when you want to analyze your data or present your data to others.

Macros give you the ability to automate tasks. You can use a macro to add functionality to a form, report, or control.

Like macros, modules give you the ability to automate tasks and add functionality to a form, report, or control. Macros are created by choosing from a list of macro actions, whereas modules are written in

Visual Basic for Applications.

Microsoft Access Exit

Since Microsoft Access shares the same functionality you are probably familiar with from using other applications, you can close it easily.

To close Microsoft Access, you can click the Office Button and then click Exit Accecss

To close Microsoft Access from its title bar, you can click its Close button

To close Microsoft Access like any regular window of the Microsoft Windows applications, you can press Alt + F4

To close Microsoft Access using mnemonics, you can press Alt, F, X.

Introduction to Tables

Imagine you create a list of people. The list can be started with names as follows:, James, Walton,

Raj, and Suraj. This type of list is considered one-dimensional because all of its items fit in one category. In order to create a more detailed list, you may want to include these people’s email addresses and probably other related information. If you include these additional pieces of information in the same category, the list may become confusing. To arrange the list, you would divide it in categories. Here is an example:

Name

James

Email Address

Phone

Number

James@yahoo.com 243-5434

Walton Walton@dell.com 399-2893

Raj raj@gmail.com 646-8384

Suraj Su_raj@gmail.com 547-6766

Relationship

Friend

Brother

Friend

Friend

This type of display allows you to refer to a piece of information by its category. This is the basis of a table. A table is a two-dimensional list of items so that the items are arranged by categories.

A complete or incomplete series of items that represent each category is called a record.

Therefore, a table can be represented as follows:

In database development, a category is represented as a column. Sometimes it is also called a field. A record is represented as a row.

Database Object and Fields

A field is an object used to host, hold, or store a piece of information of a database. Before such an object can perform its function, it must first be created. In the strict sense, the most important or the most regularly used fields of a database are created on a table, but tables may not be the friendliest windows to present to a user for data entry. For this reason, fields can and should also be created on other windows.

The process of making a field available depends on the type of object it will reside on and probably how the field will be used. This leads to two categories of fields: those that can receive or store information and those that can only present or display it. All fields of a table, especially in Microsoft Access, are created to store data. On the other hand, fields on almost all of the other objects (queries, forms, reports) can be made of combinations of already existing fields of a table, or they can be created independently of any existing or non-existing data.

The Table Tab

A table is presented as a rectangular window represented in the middle of the screen with a tab.

As an option, you can remove the tabs and let the tables display without them. To do this, you can click the Office Button and click Access Options. In the left frame, click Current Database and, in the right frame, in the Document Window Options section, click the Tabbed Documents radio button and remove the check mark on the Display Document Tabs check box:

After making the selection, you can click OK. You will be asked to close and reopen the database:

On the left side of its top section, a table presents an icon . The table icon provides a menu you can access when you right-click. The menu allows you to save or change the view of the table.

When a table displays with a tab, the extreme right section of the tab(s) shows a close button you can click to close the table.

A Table as a Datasheet

Although a table is primarily recognized as an arranged list of columns and rows, each column and each row intersect to create a rectangular box called a cell:

The cell is actually the object that holds data of a table. A cell holds only one piece of information. The database developer decides what type of information would go into a cell but the user types that information. Based on its arrangement of cells, a table is said to display in a datasheet layout or simply a datasheet (because its view is made of cells).

Table Creation

To have a table as part of your database, you must create it and you have various options. If you create a database using one of the local templates, it would provide you with a few tables that you can start using. To use a sample table, after starting a database, on the ribbon, you can click

Create. Then, in the Tables section, click Table Templates to see a list of sample tables:

From the menu that appears, you can click one. This would open a new table for you. You can then save it.

Saving a Table

To save a table you have just created:

You can click the Office Button and click Save

You can right-click the Table_X (such as Table1) tab and click Save

You can press Ctrl + S

If a table has not been saved before, you will be asked to provide a name for the table. A table can have almost any name: Employees, Library Data, Students etc. Because you and your users should be able to easily identify a table.

Primary key

A primary key is a field or combination of fields that uniquely identify each record in a table. A foreign key is a value in one table that must match the primary key in another table. You use primary keys and foreign keys to join tables together—in other words, you use primary keys and foreign keys to create relationships.

Opening a Table

To open a table, first locate it in the Navigation Pane then:

You can double-click the table

You can right-click the table and click Open

Closing a Table

After using a table, you can close it. Before closing a table, first select its tab. Then, to close a table:

You can click the close button on the right side of the tabs

You can press Ctrl + Shift + F4

Modify a Table

After you create a table, you may need to modify it. You can delete columns, insert columns, or move columns.

Delete Columns

The Delete option permanently deletes columns and all the data contained in them. You cannot undo a column deleted.

Click and drag to select the columns you want to delete.

Activate the Datasheet tab.

Click Delete in the Fields & Columns group. A prompt appears.

Click Yes. Access deletes the columns you selected.

Insert Columns

The Insert option inserts a column before the selected column.

To insert a column:

1.

Click the column head of the column before which you want to insert a column.

2.

Activate the Datasheet tab.

3.

Click Insert in the Fields & Columns group. Access inserts a new column.

If you right-click a column label, you can use the menu that appears to insert or delete columns.

To delete a column:

1.

Right-click the column head you want to delete. A menu appears.

2.

Click Delete Column.

To insert a column:

1.

Right-click the column head before which you want to insert a column. A menu appears.

2.

Click Insert Column.

To move a column:

1.

Move your mouse pointer over the horizontal line under the column label. Your mouse pointer turns into a four sided arrow.

2.

Press your left mouse button

3.

Click and drag the field to the new location. A dark line appears at the new location.

4.

Release you left mouse button. Access moves the column.

Move around a Table

Access provides several methods for moving around a table. On the Home tab, there is a

Go To button. When you click it, a menu of options appears. You can use the menu to go to the first, last, previous, or next record in your table. You can click the New option to add a new record. You can also use special keys and the navigation bar in Access to move around a table. The navigation bar appears at the bottom of the table.

Using a Template Field

Microsoft Access provides many pre-configured fields you can add to your table, whether you create the table based on a template or from scratch. These ready-made sample columns are referred to as a Field Templates.

Before using a field template, from the ribbon, click Create and click New Table. This would start a new table and the ribbon would switch to the Datasheet tab. In the Fields &

Columns section, click New Field. This would display the Field Templates window.

By default, the Field Templates window is positioned on the right side of the screen. This is a dockable window: you can move it to the left or place it somewhere else on the screen.

The items in the Field Templates window are organized in categories identified with a labeled node, such as Basic Fields, Assets, or Contacts. Each node displays a - button.

This - button indicates that the node is expanded. To collapse a node, you can click its - button, which becomes a + button. To expand a node, you can click its + button. Here is an example where the Assets, the Contacts, and the Issues nodes are collapsed while the

Events and the Projects nodes are expanded.

To use a field from the Field Templates window:

You can double-click the field

You can click and drag the field from the Field Templates and drop it on the table to the left of Add New Field:

Database Strings

Introduction to Strings

A string is text made of a readable letter, a symbol, a word or a group of words. When creating a table in Datasheet View, to apply a Text field:

 If you create a new field by replacing the Add New Field column header with a name, the new field automatically receives the Text data type

 To change the data type of a field, click its column header or a cell under its column. In the Data Type & Formatting section of the Datasheet tab of the ribbon, click the arrow of the Data Type combo box and select Text

 You can also use the Field Templates window:

 Under Basic Fields, you can drag Single Line of Text and drop it on the table

Integer-Based Data Types

Introduction

A number is a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), a combination of digits, or a combination of one or more digits, a separator, and one or more digits. Microsoft Access supports three categories of numbers and there are various ways you can apply one to a field. You can work in either the Datasheet View or the Design View of a Table.

An Integer

An integer is a natural number larger than the Byte. It can hold a value between -32,768 and

32,767. Examples of such ranges are: the number of pages of a book. Because an integer can hold as much value as the Byte, you can apply the integer type wherever you would use a byte type. Microsoft Access supports the integer type through a data type called

Integer. Like the byte, you can specify an Integer data type only in the Design View of a table.

To apply the Integer data type to a field, in the Design View of the table, after specifying the field's Data Type as Number, in the bottom section of the table, set its Field Size to

Integer

A Long Integer

A long integer is a natural number whose value is between –2,147,483,648 and

2,147,483,642. Examples are the population of a city, the distance between places of different countries, the number of words of a book. Microsoft Access supports the long integer type through a data type called Long Integer (in reality the data type is called

Long). You can apply the long integer type in either the Datasheet View or the Design

View of a table:

In the Datasheet View, after specifying the name of the field, in the Data Type &

Formatting section of the Datasheet tab, click the arrow of the Data Type combo box and select Number. The Long Integer would automatically be applied to the field

Also in the Datasheet View, from the Field Templates, under the Basic Fields category, you can drag Number and drop it on the table.

In the Design View of the table, after specifying the field's Data Type as Number, in the bottom section of the table, the Field Size would automatically be set to

Long Integer

Currency

To support currency values, Microsoft Access provides the Currency data type. You can apply it to a field in either the Datasheet View or the Design View.

To apply the Currency data type to a field:

 In the Datasheet View, using the Field Templates, you can drag one of the following fields and drop it on the table: o From Basic Fields: Currency

o From Assets: Current Value o From Projects: Budget

 Still in the Datasheet View, after naming the column or clicking a cell under that column, in the Data Type & Formatting section of the Datasheet tab of the ribbon, first set the Data Type combo box to Number. Then click the arrow of the Format combo box and select Currency

 In the Design View, after setting the column's data type to Number, in the bottom section of the table, set the Field Size to Double, click the arrow of the Format property and select Currency

Percentage Values

A number is referred to as percentage if it represents a fraction of a 100. Examples of percentage values are the density of a population or the interest rate of a loan. In most cases, a percentage value is written with the percent symbol, which is %. To apply a percent value to a field, you can work from either the Datasheet View or the Design

View.

To set up a field for percentage values:

 In the Datasheet View, using the Field Templates, you can drag the % Complete field from the Tasks category and drop it on the table

 In the Datasheet View, after naming the column or clicking a cell under that column, in the Data Type & Formatting section of the Datasheet tab of the ribbon, first set the Data Type combo box to Number. Then click the arrow of the Format combo box and select Percent

 In the Datasheet View, after naming the column or clicking a cell under that column, in the Data Type & Formatting section of the Datasheet tab of the ribbon,

first set the Data Type combo box to Number and click the Apply Percent Format button . Then click the arrow of the Format combo box and select Percent.

 In the Design View, after setting the column's data type to Number, in the bottom section of the table, set the Field Size to Double, then click the arrow of the

Format property and select Percent

Scientific Notations

Besides the regular format we are used to using to represent a number, another technique consists of writing the number as an exponent. Using this technique, instead of using

1000 to represent a thousand, you can use 1.00e3. This is referred to as scientific notation.

To apply the scientific notation to a field:

In the Datasheet View, after naming the column or clicking a cell under that column, in the Data Type & Formatting section of the Datasheet tab of the ribbon, first set the Data

Type combo box to Number. Then click the arrow of the Format combo box and select

Scientific

In the Design View, after setting the column's data type to Number, in the bottom section of the table, click the arrow of the Format property and select Scientific.

Unit 3

Advanced Features of MS ACCESS

Structure:

3.1. Creating Relationship between Tables

3.2. Queries

3.3. Forms

3.4. Reports

3.5. Printing Reports

3.1. Creating Relationships between Tables

In Access, you store data in multiple tables and then use relationships to join the tables.

After you have created relationships, you can use data from all of the related tables in a query, form, or report.

A primary key is a field or combination of fields that uniquely identify each record in a table. A foreign key is a value in one table that must match the primary key in another table. You use primary keys and foreign keys to join tables together—in other words, you use primary keys and foreign keys to create relationships.

There are two valid types of relationships: one-to-one and one-to-many. In a one-to-one relationship, for every occurrence of a value in table A, there can only be one matching occurrence of that value in table B, and for every occurrence of a value in table B, there can only be one matching occurrence of that value in table A. One-to-one relationships are rare because if there is a one-to-one relationship, the data is usually stored in a single table. However, a one-to-one relationship can occur when you want to store the information in a separate table for security reasons, when tables have a large number of fields, or for other reasons. In a one-to-many relationship, for every occurrence of a value in table A, there can be zero or more matching occurrences in table B, and for every one occurrence in table B, there can only be one matching occurrence in table A.

When tables have a one-to-many relationship, the table with the one value is called the primary table and the table with the many values is called the related table. Referential integrity ensures that the validity of the relationship between two tables remains intact. It prohibits changes to the primary table that would invalidate an entry in the related table.

For example, a school has students. Each student can make several payments, but each payment can only be from one student. The Students table is the primary table and the

Payments table is the related table.

1

2

3

Students

Roll_No

Primary Key

Last Name

Rodrigus

Smith

Dalton

First Name

John

Adams

John

3

4

5

1

2

Payments

Payment ID

Primary key

3

2

3

1

2

Roll_No

Foreign key

Amount Due

2000

2500

3000

2500

1000

Amount Paid

1500

1000

500

1000

2500

If you delete Roll_NO 1 from the Students table, Roll_NO 1 is no longer valid in the

Payments table. Referential integrity prevents you from deleting Roll_NO 1 from the

Students table. Also, if the only valid Roll_NOs are 1, 2, and 3, referential integrity prevents you from entering a value of 4 in the Roll_NO field in the Payments table. A foreign key without a primary key reference is called an orphan. Referential integrity prevents you from creating orphans.

To create relationships:

1.

Close all tables and forms. (Right-click on the tab of any Object. A menu appears. Click

Close All.)

2.

Activate the Database Tools tab.

3.

Click the Relationships button in the Show/Hide group. The Relationships window appears.

4.

If anything appears in the relationships window, click the Clear Layout button in the Tools group. If you are prompted, click Yes.

5.

Click the Show Table button in the Relationships group. The Show Table dialog box appears.

6.

Activate the Tables tab if your relationships will be based on tables, activate the Queries tab if your relationships will be based on queries, or activate the Both tab if your relationships will be based on both.

7.

Double-click each table or query you want to use to build a relationship. The tables appear in the Relationships window.

8.

Click the Close button to close the Show Table dialog box.

9.

Drag the Primary table’s primary key over the related table’s foreign key. After you drag the primary key to the related table’s box, the cursor changes to an arrow. Make sure the arrow points to the foreign key. The Edit Relationships

Dialog box appears.

10.

Click the Enforce Referential Integrity checkbox.

11.

Click Create. Access creates a one-to-many relationship between the tables.

12.

Click the Save button on the Quick Access toolbar to save the relationship.

Note: When you create a relationship, you can view the related table as a subdatasheet of the primary table. Open the primary table and click the plus (+) in the far left column.

The plus sign turns into a minus (-) sign. If the Insert Subdatasheet dialog box opens, click the table you want to view as a subdatasheet and then click OK. Access displays the subdatasheet each time you click the plus sign in the far left column. Click the minus sign to hide the subdatasheet.

Note: After a relationship has been created between two tables, you must delete the relationship before you can make modifications to the fields on which the relationship is based. To delete a relationship:

1.

Click the line that connects the tables.

2.

Press the Delete key.

3.2. Queries

You can use a query to view a subset of your data or to answer questions about your data.

For example, if you want to view a list of student names and email addresses, but you do not want to see addresses and other data, you can create a query that displays the student’s first name, last name, and email address only. Alternatively, if you want to know which students live in DE, you can restrict your list to those students. This lesson teaches you how to create a query.

Open Tables or Queries in Query Design View

A query can be based on tables or on other queries. To create a query, you open the tables or queries on which you are going to base your query in Query Design view, and then use the options in Design view to create your query. You then click the Run button to display the results. You can save queries for later use.

To open tables or queries in Query Design view:

1.

Activate the Create tab.

2.

Click the Query Design button in the Other group. The Show Table dialog box appears.

3.

Activate the Tables tab if you want to base your query on tables, activate the

Queries tab if you want base your query on queries or activate the Both tab if you want to base your query on both tables and queries.

4.

Click to choose the table or query on which you want to base your query.

5.

Click Add. The table appears in the window. a.

Click to choose the next table or query on which you want to base your query. b.

Continue clicking tables or queries until you have all the tables and queries you plan to use.

6.

Click Close. Access changes to Query Design view.

Display All Records and All Fields

In Query Design view, each table has an option that allows you to display all of the fields and all of the records in a table. This option appears on the field line on the drop-down menu as the table name followed by a period and an asterisk ( tablename.*).

1.

Open a table or query in Query Design view.

2.

Click the down-arrow in the first field on the Field row and then select the tablename.* option. The table name appears on the table line.

3.

Click the Run button. Access retrieves all of the fields and records for the table and displays them in Datasheet view.

Retrieve a Single Column

You can use an Access query to retrieve a single column of data. Instead of choosing the tablename.* option on the Field line in Query Design view, choose the name of the field you want to retrieve.

1.

Open a table or query in Query Design view.

2.

Choose the field name you want to display in the field line.

3.

Click the Run button. Access retrieves the column you chose.

Sort a Query

When creating a query, you can sort the rows you retrieve in ascending or descending order by choosing the option you want on the Sort row in Query Design view.

To perform a sort:

1.

Open a table or query in Query Design view.

2.

Choose the field names you want to retrieve in the order you want to retrieve them.

3.

Under the field you want to sort, click the down-arrow and then choose

Ascending or Descending.

4.

Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Retrieve Specific Records

In the examples so far, you have been retrieving all of the records (rows) in your table.

You can, however, specify which records you wish to retrieve. For example, you can retrieve only those students who live in DE, only the student whose student number is 5, or only those students whose birth date is 2/16/88.

You use logical operators such as = (equal), <> (not equal), > (greater than), or < (less than) to restrict the records you retrieve. For example, if you only want to display students who live in DE, enter = "DE" in the State column on the Criteria line. Access will only retrieve records where the value in the State column is equal to DE. Selection criteria are not case-sensitive, so Access will retrieve records where the entry is DE, de,

De, or dE.

Logical Operators

Operator Meaning

=

<>

>

>=

<

<=

In

Field Type

Equal to Character

Number

Date

Not equal to Character

Number

Date

Greater than Character

Number

Date

Greater than or equal to

Character

Number

Date

Less than

Less than or equal to

Equal to any item in a list

Character

Number

Date

Character

Number

Date

Character

Number

Date

Not In

Between

Not equal to any item in a list

Character

Number

Date

Between two values, greater than or equal to one and less than or

Character

Number

Date

Entry Format

= "DE"

= 5

= #2/16/88#

<> "DE"

<> 5

<> #2/16/88#

> "DE"

> 5

> #2/16/88#

> = "DE"

> = 5

> = #2/16/88#

< "DE"

< 5

< #2/16/88#

<= "DE"

<= 5

<= #2/16/88#

In ("DE", "NJ")

In (5, 9, 17)

In (#2/16/88#, #2/3/90#,

#12/15/88#)

Not In ("DE", "NJ")

Not In (5, 9, 17)

Not In (#2/16/88#, #2/3/90#,

#12/15/88#)

Between "C" And "F"

Between 5 And 10

Between #1/1/88# And

#12/31/88#

Not Between

Is Null

Is Not Null

Like

Not Like equal to the other

Not between two values

Character

Number

Date

The value is missing from the field

Character

Number

Date

The value is not Character missing from the field

Number

Date

Like a specified Character pattern. Number

Date * means any series of characters.

? means any single character.

Not like a specified pattern.

* means any series of characters.

? means many single character.

Character

Number

Date

Not Between "C" And "F"

Not Between 5 And 10

Not Between #1/1/88# And

#12/31/88#

Is Null

Is Null

Is Null

Is Not Null

Is Not Null

Is Not Null

Like "S*"

Like "1*"

Not Applicable

Like "S*"

Like "1*"

Not Applicable

Create a Query That Uses Two or More Tables

If you want to view data from two or more tables or queries, you can create a query that pulls the data from multiple tables or queries. The tables and queries from which you pull your data should have a relationship.

To create a query that uses two or more tables:

Open the tables and/or queries you want to use in Query Design view.

Choose the field names you want to retrieve in the order you want to retrieve them.

Choose the field names you want to sort by in the order you want to sort. Under the fields you want to sort by, choose Ascending or Descending.

Enter your selection criteria, if necessary (Not applicable in this example).

Deselect the Show button for columns you do not want to display (Not applicable in this example).

Click the Run button. Access retrieves the columns you chose and displays the rows in the order you specified.

Save a Query

After you create a query, you can save it. You can rerun a saved query at any time. If you change the data on which the saved query is based, you will see the changes when you rerun the query.

1.

Click the Save button on the Quick Access toolbar. Access saves the query unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears.

2.

Type the name you want to give your query.

3.

Click OK. Access saves the query. You can now access the query by using the

Navigation pane.

Note: You can also save by right-clicking a query’s tab and then selecting Save from the menu that appears. Access saves the query unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears. Type the name you want to give the query and then click OK. Access saves the query. You can now access the query by using the Navigation pane.

Modify a Query

Once created, a query can be modified. Simply open the query in Query Design view and make the changes. You can add columns, change the sort order, change the criteria, and make other changes.

In Query Design view, the Query Setup group offers several options that can assist you.

Use the Insert Rows button to insert a row in the criteria area. Click anywhere in the row before which you want to insert a new row and then click the Insert Rows button.

Use the Insert Columns button to insert a column. Click anywhere in the column before which you want to insert a column and then click the Insert Column button .

Use the Delete Rows button to delete a row in the criteria area. Click anywhere in the row you want to delete and then click the Delete Row button.

Use the Delete Columns button to delete a column. Click anywhere in the column you want to delete and then click the Delete Column button .

Make a Table Query

You can use a query to create a table. This is useful when you want to create a new table that includes the fields and data from an existing table.

To create a table:

Open the table or query on which you want to base your new table on in Query

Design view.

Enter the criteria on which you want to base your new table.

Click the Make Table button. The Make Table dialog box appears.

Type the name you want to give your new table.

Click OK.

Click Run.

Click Yes.

Close the query. (Right-click the query’s tab and then click Close.)

Double-click the new table’s name in the Navigation pane to view the new table.

Parameter Query

If instead of entering predetermined criteria, you want to prompt users when a query runs, you can create a parameter query. You create a parameter query by enclosing a question in square brackets ([]). For example, if you want to create a parameter query that asks users which State they want to use from the Student’s table, you would type [Which

State?] on the Criteria line under the State column. When the query runs, Access will prompt the user for the answer to your question.

To create a parameter query:

Open a table or query in Query Design view.

Create your query.

On the Criteria line, type the prompt within square brackets.

Click the Run button. Access prompts you.

Respond to the prompt.

Click OK. Access displays the results of your query in Datasheet view.

Like "*" & [Prompt] & "*"

Like "*" & [

Like [

Prompt

Prompt

]

] & "*"

Returns all records that contain the value you enter.

Example: If you enter ad, Access returns all records that include the sequence ad anywhere in the field.

Returns all records that end with the value you enter.

Example: If you enter S, Access returns all records that end with S .

Returns all records that begin with the value you enter.

Example: If you enter S, Access returns

> [Prompt]

Note: You can also use < (less than) ,<=

(less than or equal to) >=, >= (greater than or equal to), or <> (not equal)

3.3. Forms

all records that begin with S .

Find all records with a value greater than the value you enter.

Example: If you enter 5, Access returns all records that are greater than 5 .

Access forms are much like paper forms: you can use them to enter, edit, or display data.

They are based on tables. When using a form, you can choose the format, the arrangement, and which fields you want to display.

Using the Form Button

Access can automatically create several types of forms. For example, when you click the

Form button on the Create tab, Access places all fields in the selected table on a form. If the table has a one-to-many relationship with one other table or query, Access creates a stacked form (the records are displayed in a column) for the primary table and a datasheet for the related table. If there are several tables with a one-to-many relationship, Access does not create the datasheet.

To create a form:

1.

Open the Navigation pane.

2.

Click the table or query on which you want to base your form.

3.

Activate the Create tab.

4.

Click Form in the Forms group. Access creates a form.

To save a form:

1.

Click the Save button on the Quick Access toolbar. Access saves the form unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears.

2.

Type the name you want to give the form.

3.

Click OK. Access saves the form. You can now access the form by using the

Navigation pane.

You can also save by right-clicking a form’s tab and then selecting Save from the menu that appears. Access saves the form unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears. Type the name you want to give the form and then click OK. Access saves the form. You can now access the form by using the Navigation pane.

To apply an AutoFormat:

The AutoFormat option on the Format tab enables you to apply formats quickly, such as background colors, field colors, field label colors, and fonts.

1.

Activate the Format tab.

2.

Click AutoFormat. The AutoFormat menu appears.

3.

Click the format you want to apply.

To change a form title:

When you create a form, by default, Access uses the form name as the title. You can change the title.

1.

Activate the Format tab.

2.

Click the Title button.

3.

Type the new title.

To add the date and time:

You can easily add the date and time to your form.

1.

Activate the Format tab.

2.

Click the Date and Time button. The Date and Time dialog box appears. Select the date and time format you want. The date and time appear on your form. item you want to select. A box surrounds selected items.

3.4. Reports

Reports are organize and summarize data for viewing or for printing. A detail report displays all of the selected records. You can include summary data such as totals, counts, and percentages in a detail report. A summary report does not list the selected records but instead summarizes the data and presents totals, counts, percentages, or other summary data only. Access has several report generation tools that you can use to create both detail and summary reports quickly. This lesson teaches you how to create reports.

Creating Reports

Use the Report Button

The Report button creates a simple report that lists the records in the selected table or query in a columnar format.

1.

Open the Navigation pane.

2.

Click the table or query on which you want to base your report.

3.

Activate the Create tab.

4.

Click the Report button in the Reports group. Access creates your report and displays your report in Layout view. You can modify the report.

Note: After you create a report, you can save it.

1.

Click the Save button on the Quick Access toolbar. Access saves the report unless you are saving for the first time. If you are saving for the first time, the Save As dialog box appears.

2.

Type the name you want to give your report.

3.

Click OK. Access saves the report. You can now access the report by using the

Navigation pane.

As with other objects, you can also save a report by right-clicking the reports tab and selecting Save. Saved reports appear in the Navigation pane.

Access reports created simply by using the Report button have several sections. They are detailed in the following table.

Report Header

Page Header

Page Footer

Detail Section

Report Footer

Sections of a Report

Appears at the top of the first page and displays the report title.

Appears at the top of every page and displays the headings (field labels) for each column.

Appears at the bottom of every page and displays the page number and total number of pages.

Appears between the page header and page footer and displays the records from the table or query.

This section is optional. Appears on the last page of the report and displays summary information such as grand totals.

The Report Wizard

Report Wizard is used to create a report. The Report Wizard provides you with more flexibility than you get by using the Report button. You can choose the tables and fields, group the data, sort the data, summarize the data, choose a layout and orientation, apply a style, and title your report. Follow the steps shown here to create a report by using the

Report Wizard:

To create a report by using the Report Wizard:

Open the Report Wizard

1.

Activate the Create tab.

2.

Click Report Wizard in the Reports group. The Report Wizard appears.

Select tables, queries and fields

When using the Report Wizard, you can use fields from multiple tables and/or queries if the tables/queries have a relationship.

1.

Click the down-arrow next to the Table/Queries field and then click the table from which you want to select fields.

2.

Click a field and then click the single-right arrow to select a single field, click the double-right arrows to select all fields, click a field and then click the single-left arrow to deselect a single field, or click the double-left arrow to deselect all fields.

3.

Repeat steps 1 and 2 for each table from which you want to select fields.

4.

Click Next. The Report Wizard moves to the next page.

Group

When using the Report Wizard, you can group data. Grouping puts all of the values in a field into a group based on the field’s value. For example, if your data is grouped by the

Department field and the records in the Department field have values such as

Administration, Computer Science, and English. Access will group all of the data for the

Administration department together, all of the data for the Computer Science department together, and all of the data for the English department together.

1.

Click to select the field by which you want to group your data. You may not see this page of the wizard if you are selecting data from a single table.

2.

Click Next. The Report Wizard moves to the next page.

3.

Click a field you want to group by.

4.

Click the right-arrow to select a field; click a field and then click the left arrow to deselect a field. Use the up- and down-arrows to change the order of the groupings. If you are only using one table, this may be your first opportunity to select a field to group by.

5.

Repeat steps 3 and 4 for each field you want to group by.

6.

Click Next. The Report Wizard moves to the next page.

Sort and summarize

By using the Report Wizard, you can create up to four levels of sort. Access sorts the first level, and then sorts the second level within that sort, and so on. If you have grouped your data, you can summarize it by displaying the sum, average, and minimum or maximum value for each numeric field. You can choose to have your report display just the summary data or each detail line and the summary data. There is also an option that allows you to display the percent the sum of each group is of the grand total. All of the

fields in your report may not fit on a single page. You can have Access automatically adjust the size of the font so that every field fits.

1.

Click the down-arrow and then select the field you want to sort by.

2.

Click the button to choose ascending or descending order. Clicking the button toggles between Ascending and Descending. You can sort up to four levels.

3.

Click the Summary Options button. The Summary Options window appears.

4.

Click to select the summary data you want.

5.

Click to select whether you want detail and summary data or if you want summary data only.

6.

Click if you want to calculate the percent to the total for sums.

7.

Click OK. The Summary Options window closes.

8.

Click Next. The Report Wizard moves to the next page.

Layout and orientation

You can choose the layout and orientation of your report. The layout determines where each field appears on the page. Access provides three options to choose from: Stepped,

Block, and Outline. When you choose an option, the left side of the window displays a graphic of the layout.

Orientation determines whether Access creates the report in portrait or landscape. Most paper, such as paper sized 8 1/2 by 11, is longer on one edge than it is on the other. If you print in Portrait, the shortest edge of the paper becomes the top of the page. Portrait is the default option. If you print Landscape, the longest edge of the paper becomes the top of the page.

1.

Click to select a layout.

2.

Click to select a page orientation.

3.

Choose the Adjust The Field Width So All Fields Fit On A Page option if you want all fields to fit on a single page.

4.

Click Next. The Report Wizard moves to the next page.

Style

A style is a set of formats consisting of such things as background colors, fonts, font colors, and font sizes. Access supplies predesigned styles that format titles, labels, and more. When you choose a style, the left side of the window displays a preview.

1.

Click to select a style.

2.

Click Next. The Report Wizard moves to the next page.

Create a title

On the final page of the Report Wizard, you can title your report. The title appears at the top of the report and on the Navigation pane.

1.

Type the title you want to give the report.

2.

Click Finish. Access creates, saves, and opens your report in Layout view.

Group Header

Group Footer

Sections of a Report

Appears before a group and displays information about the group.

Appears after a group and summarizes the group data.

3.5. Printing Report

Often, the people who use Access data only see a printed report. In Print Preview, you can see exactly how your report will look when printed, you can make changes to it, and you can print it. To print, click the Print button in the Print group. The Print dialog box opens and you can select your print options.

To change to Print Preview:

1.

Open your report.

2.

Activate the Home tab.

3.

Click the down-arrow under the View button. A menu appears.

4.

Click Print Preview. Access changes to Print Preview.

Several options are available to you in Print Preview.

Print Preview Options

Print

Size

Displays the Print dialog box. You can select such options as the printer, print range, and number of copies.

Use this option to print your report.

Click the Size button to set the size of

Portrait

Landscape

Margins

Print Data Only

Zoom

One Page

Two Page

More Pages the paper you are going to print on.

Click the Portrait button to print with the shortest side of the paper as the top.

Click the Landscape button to print with the longest side of the paper as the top.

Click the Margins button to select a margin size of Normal, Wide, or

Narrow. Margins define the amount of white space that surrounds your report.

Prints the report data without other elements such as titles, and labels.

Changes the cursor to a magnifying glass. When the magnifying glass displays a minus sign (–), you can zoom out. When the magnifying glass displays a plus sign (+), you can zoom in.

Click the down-arrow under the

Zoom button to display a menu and choose a zoom level.

Displays one page of the report in the

Access window.

Displays two pages of the report in the Access window.

Displays a menu from which you can

choose the number of page you want to display.

Unit 4

HTML and Web Page Design

Structure :

4.1. Introduction to Website Design and the Basics

4.2. HTML Tags

4.3. Java Script

4.4. FrontPage

4.1. An Introduction to website design & the basics

You may be asking why do I need a website. ?

What are the advantages of a website to me and my business?

Websites can have many purposes and the aim of your website , i.e. what you wish to achieve with your website, is important to establish before any work is begun.

A website is a tool. A tool that potentially has many functions. It is the aim of the website that defines its function.

Your website may be an informational website , aiming to educate and stimulate, and an on-line community where people share ideas and thoughts in forums or chat rooms. You may be thinking about a website for your business to increase cost efficiency and increase your client base.

Your website may have a particular functional aspect such as allowing people to order products securely online. This has the benefit of creating customer convenience and repeat orders and your business can reap the benefit of lower overheads with more competitive product pricing .

A big plus is that it allows your company or service to work more efficiently and effectively by improving communications. This could be a private area of your public website with staff only access or a dedicated site for your staff.

Your website may have global appeal or it may focus on a particular geographical area .

It may be aimed a particular niche or section of society or it may be of wider interest.

Who the website is aimed at - your audience is crucial.

Your website may be public or may have restricted access. The content may be free to anyone or people may use their credit card to pay for a subscription to your sites content.

It may be commercial or non-profit . You may wish to have sponsors who advertise on the site in the form of banners or pop-up "web-verts".

You may simply wish to have an online brochure giving people basic information about your company, from which you can expand and build your web presence, paving the way for future developments .

The purpose and aim of your website is only limited by your imagination.

There are three main elements to websites: the organization of the content and how it is navigated , the appearance of the WebPages and the content in it's own right . This is very much a pre-production planning stage which is very important if we are to implement a website effectively within a timescale & budget.

If you want your website tying in with any existing promotions, color schemes, or decor, let us know as we can integrate your website into your existing image.

If you have a logo or any graphics you use, or wish to use, let us know, so we can incorporate them to create a branding for your website. We can also create you a logo should you need one.

WebPages can also be interactive , that is they respond to a user's actions. The third element of a webpage, the content , is up to you. We can incorporate most kinds of digital media into a website or digitize analogue information for you, for inclusion in the

website, e.g. scanning of photos & recording of audio or video. This includes written text, video, audio, music, speech, pictures, animation, diagrams, photos, PDF documents,

Microsoft Office documents, CAD files, anything you can think of really.

We can capture your content if you do not have the facilities such as video shooting and editing, taking photos, scanning hardcopy drawings/diagrams, recording audio and encoding. We can also create interactive animations and product presentations. Please see our other services section for further details.

We will also optimize your media for the Internet so that it has a smaller filesize and therefore downloads faster. This applies to video, pictures and sound.

As a word of warning the larger the filesize of your content, the longer it will take to download when some one is browsing your website. Most people have dial-up internet connections which run at a maximum of 56kbps and realistically this is not a fast enough connection to stream video of significant quality, even when optimized. However sound, pictures and animation are fine if optimized correctly. If wish to use extensive video streaming it is better to aim at broadband users

Introduction to HTML

HTML is a language for describing web pages.

HTML stands for Hyper Text Markup Language

HTML is not a programming language, it is a markup language

A markup language is a set of markup tags

HTML uses markup tags to describe web pages

4.2. HTML Tags

HTML markup tags are usually called HTML tags

HTML tags are keywords surrounded by angle brackets like <html>

HTML tags normally come in pairs like <b> and </b>

The first tag in a pair is the start tag,

Start and end tags are also called

the second tag is the opening tags and end tag closing tags .

HTML Documents = Web Pages

HTML documents describe web pages

HTML documents contain HTML tags

HTML documents are also

and plain text called web pages

The purpose of a web browser (like Internet Explorer or Firefox) is to read HTML documents and display them as web pages. The browser does not display the HTML tags, but uses the tags to interpret the content of the page:

<html>

<body>

<h1>My

<p>My

</body>

</html>

The Minimal HTML Document

First first

Heading</h1> paragraph</p>

Every HTML document should contain certain standard HTML tags. Each document consists of head and body text. The head contains the title, and the body contains the actual text

that is made up of paragraphs, lists, and other elements. Browsers expect specific information because they are programmed according to HTML and SGML specifications.

Required elements are shown in this sample bare-bones document:

<html>

<head>

<TITLE>A Simple HTML Example</TITLE>

</head>

<body>

<H1>HTML is Easy To Learn</H1>

<P>Welcome to the world of HTML.

This is the first paragraph. While short it is still a paragraph!</P>

<P>And this is the second paragraph.</P>

</body>

</html>

The required elements are the <html>, <head>, <title>, and <body> tags (and their corresponding end tags). Because you should include these tags in each file, you might want to create a template file with them. (Some browsers will format your HTML file correctly even if these tags are not included. But some browsers won't! So make sure to include them.)

Basic Html tags

Markup Tags

> HTML

This element tells your browser that the file contains HTML-coded information. The file extension .html also indicates this as an HTML document and must be used. (If you are restricted to 8.3 filenames (e.g., LeeHome.htm, use only .htm for your extension.)

> HEAD

The head element identifies the first part of your HTML-coded document that contains the title. The title is shown as part of your browser's window (see below).

> TITLE

The title element contains your document title and identifies its content in a global context.

The title is typically displayed in the title bar at the top of the browser window, but not inside the window itself. The title is also what is displayed on someone's hotlist or bookmark list, so choose something descriptive, unique, and relatively short. A title is also used to identify your page for search engines (such as HotBot or Infoseek ).

> BODY

The second--and largest--part of your HTML document is the body, which contains the content of your document (displayed within the text area of your browser window). The tags explained below are used within the body of your HTML document.

> Headings

HTML has six levels of headings, numbered 1 through 6, with 1 being the largest. Headings are typically displayed in larger and/or bolder fonts than normal body text. The first heading in each document should be tagged <H1>.

The syntax of the heading element is: <H y > Text of heading </H y > where y is a number between 1 and 6 specifying the level of the heading.

Do not skip levels of headings in your document. For example, don't start with a level-one heading (<H1>) and then next use a level-three (<H3>) heading.

> Paragraphs

Unlike documents in most word processors, carriage returns in HTML files aren't significant.

In fact, any amount of whitespace -- including spaces, linefeeds, and carriage returns -- are automatically compressed into a single space when your HTML document is displayed in a browser. So you don't have to worry about how long your lines of text are.

Word wrapping can occur at any point in your source file without affecting how the page will be displayed.

In the bare-bones example shown in the Minimal HTML Document section, the first paragraph is coded as:

<P>Welcome to the world of HTML.

This is the first paragraph.

While short it is still a paragraph!</P>

In the source file there is a line break between the sentences. A Web browser ignores this line break and starts a new paragraph only when it encounters another <P> tag.

Important : You must indicate paragraphs with <P> elements. A browser ignores any indentations or blank lines in the source text. Without <P> elements, the document becomes one large paragraph. (One exception is text tagged as "preformatted," which is explained below.) For example, the following would produce identical output as the first bare-bones HTML example:

<H1>Level-one heading</H1>

<P>Welcome to the world of HTML. This is the first paragraph. While short it is still a paragraph! </P> <P>And this is the second paragraph.</P>

To preserve readability in HTML files, put headings on separate lines, use a blank line or two where it helps identify the start of a new section, and separate paragraphs with blank lines

(in addition to the <P> tags). These extra spaces will help you when you edit your files

(but your browser will ignore the extra spaces because it has its own set of rules on spacing that do not depend on the spaces you put in your source file).

NOTE : The </P> closing tag may be omitted. This is because browsers understand that when they encounter a <P> tag, it means that the previous paragraph has ended.

However, since HTML now allows certain attributes to be assigned to the <P> tag, it's generally a good idea to include

Using the <P> and </P> as a paragraph container means that you can centre a paragraph by including the ALIGN= alignment attribute in your source file.

<P ALIGN=CENTER>

This is a centred paragraph.

> Formatting Character TAGS

> Font tags

<FONT> … </FONT>

With the addition of new attributes the <FONT> tag has become a very powerful tool for styling your Web page. This simple tag controls the font of the text.

> COLOR

The COLOR attribute changes the color of the selected text. This attribute may not be supported on older browsers.

Syntax: COLOR="#rrggbb"

> FACE

The FACE attribute will change the font for your text. This attribute may not be supported on older browsers. When entering the FACE value use lowercase lettering, capitalize the first letter, and separate words by commas. Some fonts may not be supported by some browsers.

Syntax: FACE="fontname"

> SIZE

This attribute will change the size of your text. Absolute values range from 1 to 7, or you may enter values like "+1" or "-2" to adjust from the previous size.

Syntax: SIZE="1"…"7", or "+/-integer"

FONT SIZE=4> <B>This is bold</B> <I>This is italic</I> <U>This is underlined</U> text<SUP>superscript</SUP> text<SUB>subscript</SUB> <TT>This is teletype</TT>

</FONT>

> Bold tags:

<B> … </B>

This tag will display the selected text as bold.

> Italic tags:

<I> … </I>

This tag will display the selected text as italic.

> Underline tags:

<U> … </U>

This tag will display the selected text as underlined.

> Strikethrough tags:

<S> … </S>

This tag will display the selected text with a line through the words.

> Superscript tags:

<SUP> … </SUP>

This tag will display the selected text in a smaller font and raised above the text line.

> Subscript tags:

<SUB> … </SUB>

This tag will display the selected text in a smaller font and sunken below the text line.

Extracted

4.3. JavaScript

JavaScript is a programming language that allows the website owner to make a webpage a bit more dynamic from the client's perspective. Remember from the Perl CGI lesson that there are two ends to a web transaction: the client side (you and your browser), and the server side (the host who has the URL). Whereas Perl was used on the server side to produce dynamic HTML content, JavaScript is executed by the browser and is useful for dynamic effects that result from the client's actions (clicking, moving the mouse, changing a window size, etc.)

JavaScript often makes the client's experience more personable and more fun. If you think of it in those terms, you'll be able to figure out pretty quickly whether you need JavaScript or Perl in order to achieve your current website goal. This isn't a hard-and-fast rule, of course, but it's a handy guideline for beginners.

It's requisite at this point to say that JavaScript has no relationship to the programming language

Java. They're independent entities that share nomenclature only by coincidence. It's simply not true that JavaScript is a subset of that bigger language, Java — no matter what that diminutive appendage "Script" would allow you to believe.

JavaScript is also a great way to get an introduction to object-oriented programming — and we'll see a little bit about objects, properties and methods below. If you're used to other languages like

Perl or Fortran or Pascal — languages that use subroutines (computer scientists call them

"procedures") and so are classified as "procedural languages" — object-oriented code can be a bit odd at first glance. If, on the other hand, you've not done any programming at all, then you should be able to take to JavaScript like a duck to water because you don't have to rethink all those procedural language syntaxes that you've already taken to heart.

One difficulty that can arise with JavaScript is the issue of compatibility. Will all browsers be able to run your JavaScript successfully? What if someone with an ancient browser visits your website and nothing works properly? That's a nontrivial problem for JavaScript programmers.

JavaScript was originally invented by Netscape and so has been a part of that browser since version 2. Microsoft Internet Explorer's version of the language is called JScript and they've been running it since version 3. While it's unlikely that someone will visit your website with a browser that doesn't understand JavaScript, it's possible that you might use some JavaScript features that not all browsers support and it's quite possible that a client has turned off JavaScript in their browser.

4.4. Microsoft FrontPage

Microsoft's FrontPage editing tool is one of the most commonly used web site editing tools today.

It is easy to learn and offers the flexibility to manage your website as it grows. Novice users are able to create attractive and functional websites; while more advanced users can accomplish more robust tasks such as working with CGI scripts and modifying the actual HTML of the web pages.

FrontPage is designed as a WYSIWYG application. WYSIWYG is an acronym for "What You

See Is What You Get". This means you basically draw your web pages as you want them to look and FrontPage generates the appropriate HTML to accomplish the task. It is a well-designed Web page editor loaded with easy-to-use options and page elements that allow users to create a decent

Web site quickly. The application is designed to mimic Microsoft Office in look and feel, while allowing users to easily build Web pages with drag-and-drop functionality. With the software installed on a user's computer, they can quick develop Web documents with elements drawn from other Office documents.

As your website grows the content contained within the site can become difficult to keep updated.

Keeping your sites content current is a time consuming task, often considerably larger than the initial development of the site. The use of FrontPage provides users with the power to easily manage content on a site-wide basis instead of on a page-by-page basis. This eases the normally daunting task of website management.

What makes FrontPage different from other Web design software is the inclusion of unique server extensions that support special features that are not available with other Web design utilities. For example:

1. 'Publish' your website directly to your account, without any need for FTP.

2. FrontPage's 'Web-Bots' make it easy to place things such as forms and guest books on a site without having to learn complex scripting.

3. Edit your site 'on the fly', meaning that FrontPage can log you directly into your site on the server to edit the pages in real time, rather than having to make changes locally and then reupload all the changed files.

FrontPage Server Extensions are server-side components that let FrontPage interact directly with the Web server . Most web-hosting providers offer FrontPage Server Extensions to their customers as an integrated part of their hosting packages. Server extensions are native to the

Windows hosting platform, but are also widely available on various Unix hosting platforms as well. Through the server extensions, users can upload and maintain their Web site directly through the FrontPage application, without the need for a separate FTP application. Web authors can also utilize other innovative features, such as traffic reports that can be generated daily, weekly or month. These reports can track hits, referred domains, and search strings. The software also allows Web authors to develop database-driven Web sites. The Database Interface Wizard simplifies the creation of dynamic databases that can interact with the Web site.

This is just a touch of what FrontPage can do. By using the features of FrontPage to your advantage, you can easily make the creation and maintenance of your website a much simpler task

Step #1: Getting Started

To Create a New FrontPage Web

1.

On the Windows task bar, click the Start button, point to Programs , and then click

Microsoft FrontPage .

The FrontPage Explorer opens and the Getting Started dialog box is displayed.

2.

In the Getting Started dialog box, choose Create a New FrontPage Web and then click

OK .

The New FrontPage Web dialog box is displayed.

3.

In the New FrontPage Web dialog box, select One Page Web next to the number 1.

This option will create a site consisting of a single home page.

4.

Next to the number 2 in the New FrontPage Web dialog box, type your name into the title field.

" Your Name " is the title of your new FrontPage-based Web site. This title helps you distinguish between multiple Web sites you create with FrontPage.

5.

Click OK .

FrontPage creates a new FrontPage-based Web site called " Your Name ."

Two Main Component

The FrontPage Explorer

The FrontPage Explorer is a main component of Microsoft FrontPage. You use the

FrontPage Explorer to build new Web sites and later to change, maintain, administer, and publish established FrontPage-based Web sites.

The seven buttons on the FrontPage Explorer's Views bar provide different ways of looking at information in your FrontPage-based Web site.

The FrontPage Editor

The FrontPage Editor is used to create, edit, and view pages using a familiar WYSIWYG

(What You See Is What You Get) interface similar to word-processing programs such as

Microsoft Word. All text, styles, and page formatting are based on Hypertext Markup

Language (HTML) standards.

Step #2: Web Page Design in the FrontPage Explorer

To Add New Page as a Child of the Existing Page

1.

On the FrontPage Explorer's Views bar, click the Navigation button.

2.

Highlight the existing page that you want to add its child.

Click the New Page button the menu.

The dialog box asking you to allow FrontPage to create navigation bars will be displayed.

If you allow FrontPage to create navigation bars, you will lose the flexibility in your design.

You can manually add the navigation bars and the page banner on to your Web page.

The suggestion is not to allow FrontPage to automatically create navigation bars.

3.

Based on the design of your Web site, repeat procedure # 2 to add all children pages into your Web site.

To Apply a FrontPage Theme

1.

On the FrontPage Explorer's Views bar, click the Themes button.

2.

Click on different themes in the scrolling list box.

The Theme Preview pane lets you preview the graphical elements as they will appear on your Web pages. You can preview a theme before applying it by selecting it from the list of themes.

Before applying a theme, you can select theme options that affect the appearance of the theme's components. Selecting Vivid Colors will apply brighter colors to text and graphics, selecting Active Graphics will animate certain theme components, and selecting

Background Image will apply a background image to the pages in your current

FrontPage-based Web site.

3.

From the list of themes, select a theme, then click the Apply button.

The selected theme is applied to the pages in your current FrontPage-based Web site.

To Open the Home Page

1.

In the FrontPage Explorer, click the Navigation button on the Views bar.

2.

Double-click the home page in the Navigation pane.

Step #3: Web Page Design in the FrontPage Editor

To Add Page Banner for the Title of Your Web Page

1.

Move cursor to the location where you want to add the page banner.

2.

Select INSERT and FRONTPAGE COMPONENT from the menu.

The dialog box will be displayed to allow you to select and add the page banner onto your Web page.

To Add Navigation Bars

1.

Move cursor to the location where you want to add the navigation bars.

2.

Select INSERT and NAVIGATION BARS from the menu.

3.

The dialog box will be displayed to allow you to select levels included in the hyperlinks and orientation and appearance of the navigation bars for that specific Web page.

To Add Brief Introduction of Yourself

1.

Type Brief Introduction of Yourself on the Home page, then press ENTER.

2.

Pressing ENTER creates a new paragraph on the page.

To Import a photo of you

1.

On the Insert menu, choose Image .

The Image dialog box is displayed. In this dialog box, you can select an image to be inserted from your local file system.

2.

In the Image dialog box, click the File button.

3.

In the Select File dialog box, Select the your photo file, then click OK.

The photograph of you is placed on the page.

4.

Press ENTER to create a new line for the next image.

To Create Hyperlinks From Text

1.

Type name of your favorite Web site.

2.

Click and drag the mouse over the text you just typed to select them.

On the Insert menu, choose Hyperlink.

The Create Hyperlink dialog box is displayed. In this dialog box, you can select the target of the hyperlink. This can be a page or a file in your current FrontPage-based Web site, on your local file system, on another Web server, or on the World Wide Web. You can also create an e-mail hyperlink (which you will learn in Lesson 2).

3.

In the URL field, type the URL of your favorite web site and then click OK.

To Create an E-mail Hyperlink

1.

Type your E-mail address. Double-click the E-mail address to select it.

2.

Click the Create or Edit Hyperlink button on the FrontPage Editor's toolbar.

3.

In the Create Hyperlink dialog box, click the E-mail button.

4.

In the Create E-mail Hyperlink dialog box, type your e-mail address (for example, someone@unlgrad1.unl.edu).

5.

Click OK.

6.

The URL field in the Create Hyperlink dialog box should now contain

"mailto:someone@unlgrad1.unl.edu"

7.

Click OK.

To Preview the Home Page in a Web Browser

1.

On the File menu, choose Preview in Browser.

Preview in Browser displays the active page in your Web browser. This lets you see exactly how the page will appear in your favorite Web browser before you publish your

FrontPage-based Web site.

2.

In the Preview in Browser dialog box, click on the Web browser you want to use, and then click Preview.

Step #4: Save Your Web Site to Diskette

Current Location of Your Web Site

1.

Your Web site will be currently saved in hard drive of the computer. Use the Window

Explorer to access the folder where your Web site is.

In hard drive of the computer, the folder that contains your Web site is WEBSHARE \

WWWROOT and your Web site is the folder called “ Your Name .”

2.

Copy the whole folder caller “ Your Name ” to your diskette or to “J drive” of the CBA network.

To Open Your Web Site from the Diskette

1.

On the Windows task bar, click the Start button, point to Programs , and then click

Microsoft FrontPage .

The FrontPage Explorer opens and the Getting Started dialog box is displayed.

2.

In the Getting Started dialog box, choose Open an Existing FrontPage Web and then click More Webs .

The Open FrontPage Web dialog box is displayed.

3.

In the Select a Web server or disk location , type in the disk location and the folder called “

Your Name

.” For example, J:\Pairin

Then, click List Webs .

The dialog box asking to convert the folder into a new FrontPage web will be displayed.

Click Yes .

4.

The Open FrontPage Web dialog box will be displayed with completed location details.

Click OK .

Step #5: Publishing Your FrontPage Web

To Verify Hyperlinks in the Current FrontPage Web

1.

In the Hyperlink Status view, choose Verify Hyperlinks on the Tools menu.

The Verify Hyperlinks dialog box is displayed. In this dialog box, you can specify whether FrontPage should check all hyperlinks or selected hyperlinks only.

2.

Click Start to begin verifying all hyperlinks.

To Publish the Current FrontPage Web

1.

Close all open pages in the FrontPage Editor.

2.

In the FrontPage Explorer, click the Publish button on the toolbar.

The Publish dialog box is displayed asking where to publish your FrontPage Web.

Click More Webs

The Publish FrontPage Web dialog box will be displayed with the location where

FrontPage will publish your FrontPage-based Web site. Normally, this location will be a subfolder (new subfolder) in your current folder.

For example, J:\Pairin\Copy_of_Pairin

Click OK

FrontPage will create the new subfolder (e.g., Copy_of_Pairin) in your current folder and publish your Web site into this new subfolder.

3.

Copy every file and every subfolder from the new subfolder (e.g., Copy_of_Pairin) that

FrontPage creates and publishes your Web site to YOUR FOLDER (your first initial and your last name) in the SEATTLE SERVER.

Module 5

Unit 1

Programming Language

Structure:

1.1

Introduction to C Programming

1.2

Basic Data Types and Operators

1.3

Statements and Control Flow

1.1

Introduction to C programming

C is a general-purpose programming language with features economy of expression, modern flow control and data structures, and a rich set of operators. C is not a ``very high level'' language, nor a ``big'' one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages.

C was originally designed for and implemented on the UNIX operating system on the DEC PDP-

11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX applications programs are written in C. C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C, although it has been called a ``system programming language'' because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.

Many of the important ideas of C stem from the language BCPL, developed by Martin Richards.

The influence of BCPL on C proceeded indirectly through the language B, which was written by

Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7. BCPL and B are ``typeless'' languages. By contrast, C provides a variety of data types. The fundamental types are characters,

and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be astatement. Pointers provide for machine-independent address arithmetic.

C provides the fundamental control-flow constructions required for well-structured programs: statement

grouping, decision making (if-else),

selecting one of a set of possible values

(switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).

Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically ``automatic'', or created anew with each invocation. Function definitions may not be nested but variables may be declared in a blockstructured fashion. The functions of a C program may exist in separate source files that are compiled separately. Variables may be internal to a function, external but known only within a single source file, or visible to the entire program.

A preprocessing step performs macro substitution on program text, inclusion of other source files, and conditional compilation.

C is a relatively ``low-level'' language. This characterization is not pejorative; it simply means that C deals with the same sort of objects that most computers do namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.

C provides no operations to deal directly with composite objects such as character strings, sets, lists or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is no heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.

Similarly, C offers only straightforward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or co-routines.

Although the absence of some of these features may seem like a grave deficiency, (``You mean I have to call a function to compare two character strings?''), keeping the language down to modest size has real benefits. Since C is relatively small, it can be described in small space, and learned quickly. A programmer can reasonably expect to know and understand and indeed regularly use the entire language.

For many years, the definition of C was the reference manual in the first edition of The C

Programming Language. In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the

ANSI standard, or “ANSIC'', was completed in late 1988. Most of the features of the standard are already supported by modern compilers.

The standard is based on the original reference manual. The language is relatively little changed; one of the goals of the standard was to make sure that most existing programs would remain valid, or, failing that, that compilers could produce warnings of new behavior.

For most programmers, the most important change is the new syntax for declaring and defining functions. A function declaration can now include a description of the arguments of the function; the definition syntax changes to match. This extra information makes it much easier for compilers to detect errors caused by mismatched arguments; in our experience, it is a very useful addition to the language.

There are other small-scale language changes. Structure assignment and enumerations, which had been widely available, are now officially part of the language. Floating-point computations may now be done in single precision. The properties of arithmetic, especially for unsigned types, are clarified. The preprocessor is more elaborate. Most of these changes will have only minor effects on most programmers.

A second significant contribution of the standard is the definition of a library to accompany C. It specifies functions for accessing the operating system (for instance, to read and write files), formatted input and output, memory allocation, string manipulation, and the like. A collection of standard headers provides uniform access to declarations of functions in data types.

Programs that use this library to interact with a host system are assured of compatible behavior.

Most of the library is closely modeled on the ``standard I/O library'' of the UNIX system. This library was described in the first edition, and has been widely used on other systems as well.

Again, most programmers will not see much change.

Because the data types and control structures provided by C are supported directly by most computers, the run-time library required to implement self-contained programs is tiny. The standard library functions are only called explicitly, so they can be avoided if they are not

needed. Most can be written in C, and except for the operating system details they conceal, are themselves portable.

Although C matches the capabilities of many computers, it is independent of any particular machine architecture. With a little care it is easy to write portable programs, that is, programs that can be run without change on a variety of hardware. The standard makes portability issues explicit, and prescribes a set of constants that characterize the machine on which the program is run.

C is not a strongly-typed language, but as it has evolved, its type-checking has been strengthened. The original definition of C frowned on, but permitted, the interchange of pointers and integers; this has long since been eliminated, and the standard now requires the proper declarations and explicit conversions that had already been enforced by good compilers.

The new function declarations are another step in this direction. Compilers will warn of most type errors, and there is no automatic conversion of incompatible data types. Nevertheless, C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.

C, like any other language, has its blemishes. Some of the operators have the wrong precedence; some parts of the syntax could be better. Nonetheless, C has proven to be an extremely effective and expressive language for a wide variety of programming applications.

Getting Started

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages:

Print the word hello, world

In C, the program to print ``hello, world'' is

#include <stdio.h> main()

{ printf("hello, world\n");

} you have a C compiler, the first thing to do is figure out how to type this program in and compile it and run it and see where its output went. (If you don't have a C compiler yet, the first thing to do is to find one.)

The first line is practically boilerplate; it will appear in almost all programs we write. It asks that some definitions having to do with the ``Standard I/O Library'' be included in our program; these definitions are needed if we are to call the library function printf correctly.

The second line says that we are defining a function named main . Most of the time, we can name our functions anything we want, but the function name main is special: it is the function that will be ``called'' first when our program starts running. The empty pair of parentheses indicates that our main function accepts no arguments , that is, there isn't any information which needs to be passed in when the function is called.

The braces { and } surround a list of statements in C. Here, they surround the list of statements making up the function main .

The line printf("Hello, world!\n");

is the first statement in the program. It asks that the function printf be called; printf is a library function which prints formatted output. The parentheses surround printf 's argument list: the information which is handed to it which it should act on. The semicolon at the end of the line terminates the statement. printf 's name reflects the fact that C was first developed when Teletypes and other printing terminals were still in widespread use. Today, of course, video displays are far more common. printf 's ``prints'' to the standard output , that is, to the default location for program output to go. Nowadays, that's almost always a video screen or a window on that screen. If you do have a printer, you'll typically have to do something extra to get a program to print to it.) printf 's first (and, in this case, only) argument is the string which it should print. The string, enclosed in double quotes "" , consists of the words ``Hello, world!'' followed by a special sequence: \n . In strings, any two-character sequence beginning with the backslash

\ represents a single special character. The sequence \n represents the ``new line'' character, which prints a carriage return or line feed or whatever it takes to end one line of output and move down to the next. (This program only prints one line of output, but it's still important to terminate it.)

The second line in the main function is return 0;

In general, a function may return a value to its caller, and main is no exception. When main returns (that is, reaches its end and stops functioning), the program is at its end, and the return value from main tells the operating system (or whatever invoked the program that main is the main function of) whether it succeeded or not. By convention, a return value of 0 indicates success.

This program may look so absolutely trivial that it seems as if it's not even worth typing it in and trying to run it, but doing so may be a big (and is certainly a vital) first hurdle. On an unfamiliar computer, it can be arbitrarily difficult to figure out how to enter a text file containing program source, or how to compile and link it, or how to invoke it, or what

happened after (if?) it ran. The most experienced C programmers immediately go back to this one, simple program whenever they're trying out a new system or a new way of entering or building programs or a new way of printing output from within programs. As

Kernighan and Ritchie say, everything else is comparatively easy.

How you compile and run this (or any) program is a function of the compiler and operating system you're using. The first step is to type it in, exactly as shown; this may involve using a text editor to create a file containing the program text. You'll have to give the file a name, and all C compilers (that I've ever heard of) require that files containing

C source end with the extension .c

. So you might place the program text in a file called hello.c

.

The second step is to compile the program. (Strictly speaking, compilation consists of two steps, compilation proper followed by linking, but we can overlook this distinction at first, especially because the compiler often takes care of initiating the linking step automatically.) On many Unix systems, the command to compile a C program from a source file hello.c

is cc -o hello hello.c

You would type this command at the Unix shell prompt, and it requests that the cc (C compiler) program be run, placing its output (i.e. the new executable program it creates) in the file hello , and taking its input (i.e. the source code to be compiled) from the file hello.c

.

The third step is to run (execute, invoke) the newly-built hello program. Again on a

Unix system, this is done simply by typing the program's name: hello

Depending on how your system is set up (in particular, on whether the current directory is searched for executables, based on the PATH variable), you may have to type

./hello

to indicate that the hello program is in the current directory (as opposed to some `` bin '' directory full of executable programs, elsewhere).

You may also have your choice of C compilers. On many Unix machines, the cc command is an older compiler which does not recognize modern, ANSI Standard C syntax. An old compiler will accept the simple programs we'll be starting with, but it will not accept most of our later programs. If you find yourself getting baffling compilation errors on programs which you've typed in exactly as they're shown, it probably indicates that you're using an older compiler. On many machines, another compiler called acc or gcc is available, and you'll want to use it, instead. (Both acc and gcc are typically invoked the same as cc ; that is, the above cc command would instead be typed, say, gcc

-o hello hello.c

.)

Under MS-DOS, the compilation procedure is quite similar. The name of the command you type will depend on your compiler (e.g. cl for the Microsoft C compiler, tc or bcc for Borland's Turbo C, etc.). You may have to manually perform the second, linking step, perhaps with a command named link or tlink . The executable file which the compiler/linker creates will have a name ending in .exe

(or perhaps .com

), but you can still invoke it by typing the base name (e.g. hello ). See your compiler documentation for complete details; one of the manuals should contain a demonstration of how to enter, compile, and run a small program that prints some simple output, just as we're trying to describe here.

In an integrated or ``visual'' programming environment, such as those on the Macintosh or under various versions of Microsoft Windows, the steps you take to enter, compile, and run a program are somewhat different (and, theoretically, simpler). Typically, there is a way to open a new source window, type source code into it, give it a file name, and add it to the program (or ``project'') you're building. If necessary, there will be a way to specify what other source files (or ``modules'') make up the program. Then, there's a button or menu selection which compiles and runs the program, all from within the programming environment. (There will also be a way to create a standalone executable file which you can run from outside the environment.) In a PC-compatible environment,

you may have to choose between creating DOS programs or Windows programs. (If you have troubles pertaining to the printf function, try specifying a target environment of

MS-DOS. Supposedly, some compilers which are targeted at Windows environments won't let you call printf , because until you call some fancier functions to request that a window be created, there's no window for printf to print to.)

1.2

Basic Data Types and Operators

The type of a variable determines what kinds of values it may take on. An operator computes new values out of old ones. An expression consists of variables, constants, and operators combined to perform some useful computation. In this chapter, we'll learn about C's basic types, how to write constants and declare variables of these types, and what the basic operators are.

As Kernighan and Ritchie say, ``The type of an object determines the set of values it can have and what operations can be performed on it.'' This is a fairly formal, mathematical definition of what a type is, but it is traditional (and meaningful). There are several implications to remember:

1.

The ``set of values'' is finite. C's int type can not represent all of the integers; its float type can not represent all floating-point numbers.

2.

When you're using an object (that is, a variable) of some type, you may have to remember what values it can take on and what operations you can perform on it. For example, there are several operators which play with the binary (bit-level) representation of integers, but these operators are not meaningful for and may not be applied to floating-point operands.

3.

When declaring a new variable and picking a type for it, you have to keep in mind the values and operations you'll be needing.

Types

There are only a few basic data types in C. The first ones we'll be encountering and using are:

 char a character

 int an integer, in the range -32,767 to 32,767

 long int a larger integer (up to +-2,147,483,647)

 float a floating-point number

 double a floating-point number, with more precision and perhaps greater range than float

If you can look at this list of basic types and say to yourself, ``Oh, how simple, there are only a few types, I won't have to worry much about choosing among them,'' you'll have an easy time with declarations. (Some masochists wish that the type system were more complicated so that they could specify more things about each variable, but those of us who would rather not have to specify these extra things each time are glad that we don't have to.)

The ranges listed above for types int and long int are the guaranteed minimum ranges.

On some systems, either of these types (or, indeed, any C type) may be able to hold larger values, but a program that depends on extended ranges will not be as portable. Some programmers become obsessed with knowing exactly what the sizes of data objects will be in various situations, and go on to write programs which depend on these exact sizes.

Determining or controlling the size of an object is occasionally important, but most of the time we can sidestep size issues and let the compiler do most of the worrying.

(From the ranges listed above, we can determine that type int must be at least 16 bits, and that type long int must be at least 32 bits. But neither of these sizes is exact; many systens have 32-bit int s, and some systems have 64-bit long int s.)

You might wonder how the computer stores characters. The answer involves a character set , which is simply a mapping between some set of characters and some set of small numeric codes. Most machines today use the ASCII character set, in which the letter A is represented by the code 65, the ampersand & is represented by the code 38, the digit 1 is represented by the code 49, the space character is represented by the code 32, etc. (Most of the time, of course, you have no need to know or even worry about these particular code values; they're automatically translated into the right shapes on the screen or printer

when characters are printed out, and they're automatically generated when you type characters on the keyboard. Eventually, though, we'll appreciate, and even take some control over, exactly when these translations--from characters to their numeric codes--are performed.) Character codes are usually small--the largest code value in ASCII is 126, which is the ~ (tilde or circumflex) character. Characters usually fit in a byte, which is usually 8 bits. In C, type char is defined as occupying one byte, so it is usually 8 bits.

Most of the simple variables in most programs are of types int , long int , or double .

Typically, we'll use int and double for most purposes, and long int any time we need to hold integer values greater than 32,767.

Constants

A constant is just an immediate, absolute value found in an expression. The simplest constants are decimal integers, e.g. 0 , 1 , 2 , 123 . Occasionally it is useful to specify constants in base 8 or base 16 (octal or hexadecimal); this is done by prefixing an extra 0

(zero) for octal, or 0x for hexadecimal: the constants 100 , 0144 , and 0x64 all represent the same number. (If you're not using these non-decimal constants, just remember not to use any leading zeroes. If you accidentally write 0123 intending to get one hundred and twenty three, you'll get 83 instead, which is 123 base 8.)

We write constants in decimal, octal, or hexadecimal for our convenience, not the compiler's. The compiler doesn't care; it always converts everything into binary internally, anyway. (There is, however, no good way to specify constants in source code in binary.)

A constant can be forced to be of type long int by suffixing it with the letter L (in upper or lower case, although upper case is strongly recommended, because a lower case l looks too much like the digit 1 ).

A constant that contains a decimal point or the letter e (or both) is a floating-point constant: 3.14, 10., .01, 123e4, 123.456e7 . The e indicates multiplication by a power of

10; 123.456e7 is 123.456 times 10 to the 7th, or 1,234,560,000. (Floating-point constants are of type double by default.)

We also have constants for specifying characters and strings. (Make sure you understand the difference between a character and a string: a character is exactly one character; a string is a set of zero or more characters; a string containing one character is distinct from a lone character.) A character constant is simply a single character between single quotes:

'A' , '.' , '%' . The numeric value of a character constant is, naturally enough, that character's value in the machine's character set. (In ASCII, for example, 'A' has the value

65.)

A string is represented in C as a sequence or array of characters. (We'll have more to say about arrays in general, and strings in particular, later.) A string constant is a sequence of zero or more characters enclosed in double quotes: "apple" , "hello, world" , "this is a test" .

Within character and string constants, the backslash character \ is special, and is used to represent characters not easily typed on the keyboard or for various reasons not easily typed in constants. The most common of these ``character escapes'' are:

\n

\b

\r

\'

\"

\\ a ``newline'' character a backspace a carriage return (without a line feed) a single quote (e.g. in a character constant) a double quote (e.g. in a string constant) a single backslash

Declarations

Informally, a variable (also called an object ) is a place you can store a value. So that you can refer to it unambiguously, a variable needs a name. You can think of the variables in your program as a set of boxes or cubbyholes, each with a label giving its name; you might imagine that storing a value ``in'' a variable consists of writing the value on a slip of paper and placing it in the cubbyhole.

A declaration tells the compiler the name and type of a variable you'll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon: char c; int i; float f;

You can also declare several variables of the same type in one declaration, separating them with commas: int i1, i2;

Later we'll see that declarations may also contain initializers , qualifiers and storage classes , and that we can declare arrays , functions , pointers , and other kinds of data structures.

The placement of declarations is significant. You can't place them just anywhere (i.e. they cannot be interspersed with the other statements in your program). They must either be placed at the beginning of a function, or at the beginning of a brace-enclosed block of statements (which we'll learn about in the next chapter), or outside of any function.

Furthermore, the placement of a declaration, as well as its storage class, controls several things about its visibility and lifetime , as we'll see later.

You may wonder why variables must be declared before use. There are two reasons:

1.

It makes things somewhat easier on the compiler; it knows right away what kind of storage to allocate and what code to emit to store and manipulate each variable; it doesn't have to try to intuit the programmer's intentions.

2.

It forces a bit of useful discipline on the programmer: you cannot introduce variables willy-nilly; you must think about them enough to pick appropriate types for them. (The compiler's error messages to you, telling you that you apparently forgot to declare a variable, are as often helpful as they are a nuisance: they're helpful when they tell you

that you misspelled a variable, or forgot to think about exactly how you were going to use it.)

Although there are a few places where declarations can be omitted (in which case the compiler will assume an implicit declaration), making use of these removes the advantages of reason 2 above, so I recommend always declaring everything explicitly.

A declaration for a variable can also contain an initial value. This initializer consists of an equal’s sign and an expression, which is usually a single constant: int i = 1; int i1 = 10, i2 = 20;

Variable Names

Within limits, you can give your variables and functions any names you want. These names (the formal term is ``identifiers'') consist of letters, numbers, and underscores. For our purposes, names must begin with a letter. Theoretically, names can be as long as you want, but extremely long ones get tedious to type after a while, and the compiler is not required to keep track of extremely long ones perfectly. The capitalization of names in C is significant: the variable names variable , Variable , and VARIABLE (as well as silly combinations like variAble ) are all distinct.

A final restriction on names is that you may not use keywords (the words such as int and for which are part of the syntax of the language) as the names of variables or functions

(or as identifiers of any kind).

Arithmetic Operators

The basic operators for performing arithmetic are the same in many computer languages:

*

/

+

addition subtraction multiplication division

% modulus (remainder)

The operator can be used in two ways: to subtract two numbers (as in a - b ), or to negate one number (as in -a + b or a + -b ).

When applied to integers, the division operator / discards any remainder, so 1 / 2 is 0 and 7 / 4 is 1. But when either operand is a floating-point quantity (type float or double ), the division operator yields a floating-point result, with a potentially nonzero fractional part. So 1 / 2.0

is 0.5, and 7.0 / 4.0

is 1.75.

The modulus operator % gives you the remainder when two integers are divided: 1 % 2 is

1; 7 % 4 is 3. (The modulus operator can only be applied to integers.)

An additional arithmetic operation you might be wondering about is exponentiation.

Some languages have an exponentiation operator (typically ^ or ** ), but C doesn't. (To square or cube a number, just multiply it by itself.)

Multiplication, division, and modulus all have higher precedence than addition and subtraction. The term ``precedence'' refers to how ``tightly'' operators bind to their operands (that is, to the things they operate on). In mathematics, multiplication has higher precedence than addition, so 1 + 2 * 3 is 7, not 9. In other words, 1 + 2 * 3 is equivalent to 1 + (2 * 3) . C is the same way.

All of these operators ``group'' from left to right, which means that when two or more of them have the same precedence and participate next to each other in an expression, the evaluation conceptually proceeds from left to right. For example, 1 - 2 - 3 is equivalent to (1 - 2) - 3 and gives -4, not +2. (``Grouping'' is sometimes called associativity , although the term is used somewhat differently in programming than it is in mathematics. Not all C operators group from left to right; a few group from right to left.)

Whenever the default precedence or associativity doesn't give you the grouping you want, you can always use explicit parentheses. For example, if you wanted to add 1 to 2 and then multiply the result by 3, you could write (1 + 2) * 3 .

Assignment Operators

The assignment operator = assigns a value to a variable. For example, x = 1 sets x to 1, and a = b sets a to whatever b 's value is. The expression i = i + 1 is, as we've mentioned elsewhere, the standard programming idiom for increasing a variable's value by 1: this expression takes i 's old value, adds 1 to it, and stores it back into i . (C provides several ``shortcut'' operators for modifying variables in this and similar ways, which we'll meet later.)

We've called the = sign the ``assignment operator'' and referred to ``assignment expressions'' because, in fact, = is an operator just like + or . C does not have

``assignment statements''; instead, an assignment like a = b is an expression and can be used wherever any expression can appear. Since it's an expression, the assignment a = b has a value, namely, the same value that's assigned to a . This value can then be used in a larger expression; for example, we might write c = a = b which is equivalent to c = (a = b) and assigns b 's value to both a and c . (The assignment operator, therefore, groups from right to left.) Later we'll see other circumstances in which it can be useful to use the value of an assignment expression.

It's usually a matter of style whether you initialize a variable with an initializer in its declaration or with an assignment expression near where you first use it. That is, there's no particular difference between

and int a = 10; int a;

/* later... */ a = 10;

Function Calls

We'll have much more to say about functions in a later chapter, but for now let's just look at how they're called. (To review: what a function is is a piece of code, written by you or by someone else, which performs some useful, compartmentalizable task.) You call a function by mentioning its name followed by a pair of parentheses. If the function takes any arguments, you place the arguments between the parentheses, separated by commas.

These are all function calls: printf("Hello, world!\n") printf("%d\n", i) sqrt(144.) getchar()

The arguments to a function can be arbitrary expressions. Therefore, you don't have to say things like int sum = a + b + c; printf("sum = %d\n", sum); if you don't want to; you can instead collapse it to printf("sum = %d\n", a + b + c);

Many functions return values, and when they do, you can embed calls to these functions within larger expressions: c = sqrt(a * a + b * b)

x = r * cos(theta) i = f1(f2(j))

The first expression squares a and b , computes the square root of the sum of the squares, and assigns the result to c . (In other words, it computes a * a + b * b , passes that number to the sqrt function, and assigns sqrt 's return value to c .) The second expression passes the value of the variable theta to the cos (cosine) function, multiplies the result by r , and assigns the result to x . The third expression passes the value of the variable j to the function f2 , passes the return value of f2 immediately to the function f1 , and finally assigns f1 's return value to the variable i .

Reading Input from keyboard:

We'll be using the scanf function to read in a value and then printf to read it back out. Let's look at the program and then pick apart exactly what's going on. You can even compile this and run it if it helps you follow along.

#include <stdio.h> int main()

{

int this_is_a_number;

printf( "Please enter a number: " );

scanf( "%d", &this_is_a_number );

printf( "You entered %d", this_is_a_number );

getchar();

return 0;

}

So what does all of this mean? We've seen the #include and main function before; main must appear in every program you intend to run, and the #include gives us access to printf (as well as scanf). (As you might have guessed, the io in stdio.h stands for

"input/output"; std just stands for "standard.") The keyword int declares this_is_a_number to be an integer.

This is where things start to get interesting: the scanf function works by taking a string and some variables modified with &. The string tells scanf what variables to look for: notice that we have a string containing only "%d" -- this tells the scanf function to read in an integer. The second argument of scanf is the variable, sort of. We'll learn more about what is going on later, but the gist of it is that scanf needs to know where the variable is stored in order to change its value. Using & in front of a variable allows you to get its location and give that to scanf instead of the value of the variable. Think of it like giving someone directions to the soda aisle and letting them go get a coca-cola instead of fetching the coke for that person. The & gives the scanf function directions to the variable.

When the program runs, each call to scanf checks its own input string to see what kinds of input to expect, and then stores the value input into

1.3

Statements and Control Flow

Statements are the ``steps'' of a program. Most statements compute and assign values or call functions, but we will eventually meet several other kinds of statements as well. By default, statements are executed in sequence, one after another. We can, however, modify that sequence by using control flow constructs which arrange that a statement or group of statements is executed only if some condition is true or false, or executed over and over again to form a loop . (A somewhat different kind of control flow happens when we call a function: execution of the caller is suspended while the called function proceeds. We'll discuss functions in chapter 5.)

My definitions of the terms statement and control flow are somewhat circular. A statement is an element within a program which you can apply control flow to; control flow is how you specify the order in which the statements in your program are executed.

(A weaker definition of a statement might be ``a part of your program that does something,'' but this definition could as easily be applied to expressions or functions.)

Expression Statements

Most of the statements in a C program are expression statements . An expression statement is simply an expression followed by a semicolon. The lines i = 0; i = i + 1; and printf("Hello, world!\n"); are all expression statements. (In some languages, such as Pascal, the semicolon separates statements, such that the last statement is not followed by a semicolon. In C, however, the semicolon is a statement terminator; all simple statements are followed by semicolons. The semicolon is also used for a few other things in C; we've already seen that it terminates declarations, too.)

Expression statements do all of the real work in a C program. Whenever you need to compute new values for variables, you'll typically use expression statements (and they'll typically contain assignment operators). Whenever you want your program to do

something visible, in the real world, you'll typically call a function (as part of an expression statement). We've already seen the most basic example: calling the function printf to print text to the screen. But anything else you might do--read or write a disk file, talk to a modem or printer, draw pictures on the screen--will also involve function calls. (Furthermore, the functions you call to do these things are usually different depending on which operating system you're using. The C language does not define them, so we won't be talking about or using them much.)

Expressions and expression statements can be arbitrarily complicated. They don't have to consist of exactly one simple function call, or of one simple assignment to a variable. For one thing, many functions return values, and the values they return can then be used by other parts of the expression. For example, C provides a sqrt (square root) function, which we might use to compute the hypotenuse of a right triangle like this: c = sqrt(a*a + b*b);

To be useful, an expression statement must do something; it must have some lasting effect on the state of the program. (Formally, a useful statement must have at least one side effect .) The first two sample expression statements in this section (above) assign new values to the variable i , and the third one calls printf to print something out, and these are good examples of statements that do something useful. or

(To make the distinction clear, we may note that degenerate constructions such as

0; i; i + 1;

are syntactically valid statements--they consist of an expression followed by a semicolon--but in each case, they compute a value without doing anything with it, so the computed value is discarded, and the statement is useless. But if the ``degenerate'' statements in this paragraph don't make much sense to you, don't worry; it's because they, frankly, don't make much sense.)

It's also possible for a single expression to have multiple side effects, but it's easy for such an expression to be (a) confusing or (b) undefined. For now, we'll only be looking at expressions (and, therefore, statements) which do one well-defined thing at a time. if Statements

The simplest way to modify the control flow of a program is with an if statement, which in its simplest form looks like this: if (x > max) max = x;

Even if you didn't know any C, it would probably be pretty obvious that what happens here is that if x is greater than max , x gets assigned to max . (We'd use code like this to keep track of the maximum value of x we'd seen--for each new x , we'd compare it to the old maximum value max , and if the new value was greater, we'd update max .)

More generally, we can say that the syntax of an if statement is: if ( expression ) statement where expression is any expression and statement is any statement.

What if you have a series of statements, all of which should be executed together or not at all depending on whether some condition is true? The answer is that you enclose them in braces: if ( expression )

{ statement<sub>1</sub>

statement<sub>2</sub> statement<sub>3</sub>

}

As a general rule, anywhere the syntax of C calls for a statement, you may write a series of statements enclosed by braces. (You do not need to, and should not, put a semicolon after the closing brace, because the series of statements enclosed by braces is not itself a simple expression statement.)

An if statement may also optionally contain a second statement, the `` else clause,'' which is to be executed if the condition is not met. Here is an example: if(n > 0) else average = sum / n;

{ printf("can't compute average\n"); average = 0;

}

The first statement or block of statements is executed if the condition is true, and the second statement or block of statements (following the keyword else ) is executed if the condition is

not true. In this example, we can compute a meaningful average only if n is greater than 0; otherwise, we print a message saying that we cannot compute the average.

The general syntax of an if statement is therefore if( expression ) statement<sub>1</sub> else

statement<sub>2</sub>

(where both statement <sub> 1 </sub> and statement <sub> 2 </sub> may be lists of statements enclosed in braces).

It's also possible to nest one if statement inside another. (For that matter, it's in general possible to nest any kind of statement or control flow construct within another.) For example, here is a little piece of code which decides roughly which quadrant of the compass you're walking into, based on an x value which is positive if you're walking east, and a y value which is positive if you're walking north: if(x > 0)

{ else if(y > 0) printf("Northeast.\n"); else

}

{ if(y > 0) printf("Southeast.\n"); else printf("Northwest.\n"); printf("Southwest.\n");

}

When you have one if statement (or loop) nested inside another, it's a very good idea to use explicit braces {} , as shown, to make it clear (both to you and to the compiler) how they're nested and which else goes with which if . It's also a good idea to indent the various levels, also as shown, to make the code more readable to humans. Why do both? You use indentation to make the code visually more readable to yourself and other humans, but the compiler doesn't pay attention to the indentation (since all whitespace is essentially equivalent and is essentially ignored). Therefore, you also have to make sure that the punctuation is right.

Here is an example of another common arrangement of if and else . Suppose we have a variable grade containing a student's numeric grade, and we want to print out the corresponding letter grade. Here is code that would do the job:

if(grade >= 90) printf("A"); else if(grade >= 80) printf("B"); else if(grade >= 70) printf("C"); else if(grade >= 60) printf("D"); else printf("F");

What happens here is that exactly one of the five printf calls is executed, depending on which of the conditions is true. Each condition is tested in turn, and if one is true, the corresponding statement is executed, and the rest are skipped. If none of the conditions is true, we fall through to the last one, printing ``F''.

In the cascaded if / else / if / else /... chain, each else clause is another if statement. This may be more obvious at first if we reformat the example, including every set of braces and indenting each if statement relative to the previous one: else if(grade >= 90)

{ printf("A");

}

{ if(grade >= 80)

{ else printf("B");

}

{ if(grade >= 70)

{ printf("C");

}

} else

} else

}

{ if(grade >= 60)

{ printf("D");

}

{ printf("F");

}

Boolean Expressions

An if statement like if(x > max) max = x; is perhaps deceptively simple. Conceptually, we say that it checks whether the condition x > max is ``true'' or ``false''. The mechanics underlying C's conception of ``true'' and ``false,'' however, deserve some explanation. We need to understand how true and false values are represented, and how they are interpreted by statements like if .

As far as C is concerned, a true/false condition can be represented as an integer. (An integer can represent many values; here we care about only two values: ``true'' and

``false.'' The study of mathematics involving only two values is called Boolean algebra, after George Boole, a mathematician who refined this study.) In C, ``false'' is represented by a value of 0 (zero), and ``true'' is represented by any value that is nonzero. Since there

are many nonzero values (at least 65,534, for values of type int ), when we have to pick a specific value for ``true,'' we'll pick 1.

The relational operators such as < , <= , > , and >= are in fact operators, just like + , , * , and / . The relational operators take two values, look at them, and ``return'' a value of 1 or

0 depending on whether the tested relation was true or false. The complete set of relational operators in C is:

<

<=

>

>= less than less than or equal greater than greater than or equal

==

!= equal not equal

For example, 1 < 2 is 1, 3 > 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.

We've now encountered perhaps the most easy-to-stumble-on ``gotcha!'' in C: the equality-testing operator is == , not a single = , which is assignment. If you accidentally write if(a = 0)

(and you probably will at some point; everybody makes this mistake), it will not test whether a is zero, as you probably intended. Instead, it will assign 0 to a , and then perform the ``true'' branch of the if statement if a is nonzero. But a will have just been assigned the value 0, so the

``true'' branch will never be taken! (This could drive you crazy while debugging--you wanted to do something if a was 0, and after the test, a is 0, whether it was supposed to be or not, but the

``true'' branch is nevertheless not taken.)

The relational operators work with arbitrary numbers and generate true/false values. You can also combine true/false values by using the Boolean operators , which take true/false values as operands and compute new true/false values. The three Boolean operators are:

&& and

||

! or not (takes one operand; `` unary '')

The && (``and'') operator takes two true/false values and produces a true (1) result if both operands are true (that is, if the left-hand side is true and the right-hand side is true). The ||

(``or'') operator takes two true/false values and produces a true (1) result if either operand is true. The !

(``not'') operator takes a single true/false value and negates it, turning false to true and true to false (0 to 1 and nonzero to 0).

For example, to test whether the variable i lies between 1 and 10, you might use if(1 < i && i < 10)

...

Here we're expressing the relation `` i is between 1 and 10'' as ``1 is less than i and i is less than 10.''

It's important to understand why the more obvious expression if(1 < i < 10) /* WRONG */ would not work. The expression 1 < i < 10 is parsed by the compiler analogously to 1 + i

+ 10 . The expression 1 + i + 10 is parsed as (1 + i) + 10 and means ``add 1 to i , and then add the result to 10.'' Similarly, the expression 1 < i < 10 is parsed as (1 < i) < 10 and means ``see if 1 is less than i , and then see if the result is less than 10.'' But in this case,

``the result'' is 1 or 0, depending on whether i is greater than 1. Since both 0 and 1 are less than

10, the expression 1 < i < 10 would always be true in C, regardless of the value of i !

Relational and Boolean expressions are usually used in contexts such as an if statement, where something is to be done or not done depending on some condition. In these cases what's actually checked is whether the expression representing the condition has a zero or nonzero value. As long as the expression is a relational or Boolean expression, the interpretation is just what we want. For example, when we wrote

if(x > max) the > operator produced a 1 if x was greater than max , and a 0 otherwise. The if statement interprets 0 as false and 1 (or any nonzero value) as true.

But what if the expression is not a relational or Boolean expression? As far as C is concerned, the controlling expression (of conditional statements like if ) can in fact be any expression: it doesn't have to ``look like'' a Boolean expression; it doesn't have to contain relational or logical operators. All C looks at (when it's evaluating an if statement, or anywhere else where it needs a true/false value) is whether the expression evaluates to 0 or nonzero. For example, if you have a variable x , and you want to do something if x is nonzero, it's possible to write if(x) statement and the statement will be executed if x is nonzero (since nonzero means ``true'').

This possibility (that the controlling expression of an if statement doesn't have to ``look like'' a Boolean expression) is both useful and potentially confusing. It's useful when you have a variable or a function that is ``conceptually Boolean,'' that is, one that you consider to hold a true or false (actually nonzero or zero) value. For example, if you have a variable verbose which contains a nonzero value when your program should run in verbose mode and zero when it should be quiet, you can write things like if(verbose) printf("Starting first pass\n"); and this code is both legal and readable, besides which it does what you want. The standard library contains a function isupper() which tests whether a character is an upper-case letter, so if c is a character, you might write if (isupper(c))

...

Both of these examples ( verbose and isupper() ) are useful and readable.

However, you will eventually come across code like if(n) average = sum / n; where n is just a number. Here, the programmer wants to compute the average only if n is nonzero (otherwise, of course, the code would divide by 0), and the code works, because, in the context of the if statement, the trivial expression n is (as always) interpreted as ``true'' if it is nonzero, and ``false'' if it is zero.

``Coding shortcuts'' like these can seem cryptic, but they're also quite common, so you'll need to be able to recognize them even if you don't choose to write them in your own code. Whenever you see code like or if(x) if(f()) where x or f() do not have obvious ``Boolean'' names, you can read them as ``if x is nonzero'' or ``if f() returns nonzero.'' while Loops

Loops generally consist of two parts: one or more control expressions which (not surprisingly) control the execution of the loop, and the body , which is the statement or set of statements which is executed over and over.

The most basic loop in C is the while loop. A while loop has one control expression, and executes as long as that expression is true. This example repeatedly doubles the number 2 (2, 4, 8, 16 ...) and prints the resulting numbers as long as they are less than

1000: int x = 2; while(x < 1000)

{ printf("%d\n", x); x = x * 2;

}

(Once again, we've used braces {} to enclose the group of statements which are to be executed together as the body of the loop.)

The general syntax of a while loop is while(expression ) statement

A while loop starts out like an if statement: if the condition expressed by the expression is true, the statement is executed. However, after executing the statement, the condition is tested again, and if it's still true, the statement is executed again. (Presumably, the condition depends on some value which is changed in the body of the loop.) As long as the condition remains true, the body of the loop is executed over and over again. (If the condition is false right at the start, the body of the loop is not executed at all.)

As another example, if you wanted to print a number of blank lines, with the variable n holding the number of blank lines to be printed, you might use code like this: while(n > 0)

{ printf("\n"); n = n - 1;

}

After the loop finishes (when control ``falls out'' of it, due to the condition being false), n will have the value 0.

You use a while loop when you have a statement or group of statements which may have to be executed a number of times to complete their task. The controlling expression represents the condition ``the loop is not done'' or ``there's more work to do.'' As long as the expression is true, the body of the loop is executed; presumably, it makes at least

some progress at its task. When the expression becomes false, the task is done, and the rest of the program (beyond the loop) can proceed. When we think about a loop in this way, we can seen an additional important property: if the expression evaluates to ``false'' before the very first trip through the loop, we make zero trips through the loop. In other words, if the task is already done (if there's no work to do) the body of the loop is not executed at all. (It's always a good idea to think about the ``boundary conditions'' in a piece of code, and to make sure that the code will work correctly when there is no work to do, or when there is a trivial task to do, such as sorting an array of one number.

Experience has shown that bugs at boundary conditions are quite common.) for Loops

Our second loop, which we've seen at least one example of already, is the for loop. The first one we saw was: for (i = 0; i < 10; i = i + 1) printf("i is %d\n", i);

More generally, the syntax of a for loop is for( expr <sub> 1 </sub> ; expr <sub> 2 </sub> ; expr <sub> 3 </sub> )

statement

(Here we see that the for loop has three control expressions. As always, the statement can be a brace-enclosed block.)

Many loops are set up to cause some variable to step through a range of values, or, more generally, to set up an initial condition and then modify some value to perform each succeeding loop as long as some condition is true. The three expressions in a for loop encapsulate these conditions: expr <sub> 1 </sub> sets up the initial condition, expr <sub> 2 </sub> tests whether another trip through the loop should be taken, and expr <sub> 3 </sub> increments or updates things after each trip through the loop and prior to the next one. In our first example, we had i

= 0 as expr <sub> 1 </sub> , i < 10 as expr <sub> 2 </sub> , i = i + 1 as

expr <sub> 3 </sub> , and the call to printf as statement, the body of the loop. So the loop began by setting i to 0, proceeded as long as i was less than 10, printed out i 's value during each trip through the loop, and added 1 to i between each trip through the loop.

When the compiler sees a for loop, first, expr <sub> 1 </sub> is evaluated. Then, expr <sub> 2 </sub> is evaluated, and if it is true, the body of the loop (statement) is executed.

Then, expr <sub> 3 </sub> is evaluated to go to the next step, and expr <sub> 2 </sub> is evaluated again, to see if there is a next step. During the execution of a for loop, the sequence is: expr <sub> 1 </sub> expr <sub> 2 </sub>

statement expr <sub> 3 </sub> expr <sub> 2 </sub>

statement expr <sub> 3 </sub>

... expr <sub> 2 </sub>

statement expr <sub> 3 </sub> expr <sub> 2 </sub>

The first thing executed is expr <sub> 1 </sub> . expr <sub> 3 </sub> is evaluated after every trip through the loop. The last thing executed is always expr <sub> 2 </sub> , because when expr <sub> 2 </sub> evaluates false, the loop exits.

All three expressions of a for loop are optional. If you leave out expr <sub> 1 </sub> , there simply is no initialization step, and the variable(s) used with the loop had better have been

initialized already. If you leave out expr <sub> 2 </sub> , there is no test, and the default for the for loop is that another trip through the loop should be taken (such that unless you break out of it some other way, the loop runs forever). If you leave out expr <sub> 3 </sub> , there is no increment step.

The semicolons separate the three controlling expressions of a for loop. (These semicolons, by the way, have nothing to do with statement terminators.) If you leave out one or more of the expressions, the semicolons remain. Therefore, one way of writing a deliberately infinite loop in C is for(;;)

...

It's useful to compare C's for loop to the equivalent loops in other computer languages you might know. The C loop for(i = x; i <= y; i = i + z) is roughly equivalent to: for I = X to Y step Z do 10 i=x,y,z

(BASIC)

(FORTRAN) for i := x to y (Pascal)

In C (unlike FORTRAN), if the test condition is false before the first trip through the loop, the loop won't be traversed at all. In C (unlike Pascal), a loop control variable (in this case, i ) is guaranteed to retain its final value after the loop completes, and it is also legal to modify the control variable within the loop, if you really want to. (When the loop terminates due to the test condition turning false, the value of the control variable after the loop will be the first value for which the condition failed, not the last value for which it succeeded.)

It's also worth noting that a for loop can be used in more general ways than the simple, iterative examples we've seen so far. The ``control variable'' of a for loop does not have

to be an integer, and it does not have to be incremented by an additive increment. It could be ``incremented'' by a multiplicative factor (1, 2, 4, 8, ...) if that was what you needed, or it could be a floating-point variable, or it could be another type of variable which we haven't met yet which would step, not over numeric values, but over the elements of an array or other data structure. Strictly speaking, a for loop doesn't have to have a ``control variable'' at all; the three expressions can be anything, although the loop will make the most sense if they are related and together form the expected initialize, test, increment sequence.

The powers-of-two example of the previous section does fit this pattern, so we could rewrite it like this: int x; for(x = 2; x < 1000; x = x * 2) printf("%d\n", x);

There is no earth-shaking or fundamental difference between the while and for loops. In fact, given the general for loop for( expr<sub>1</sub> ; expr<sub>2</sub> ; expr<sub>3</sub> ) statement you could usually rewrite it as a while loop, moving the initialize and increment expressions to statements before and within the loop: expr<sub>1</sub> ; while( expr<sub>2</sub> )

{ statement expr<sub>3</sub> ;

}

Similarly, given the general while loop

while( expr ) statement you could rewrite it as a for loop: for(; expr ; ) statement

Another contrast between the for and while loops is that although the test expression

( expr <sub> 2 </sub> ) is optional in a for loop, it is required in a while loop. If you leave out the controlling expression of a while loop, the compiler will complain about a syntax error. (To write a deliberately infinite while loop, you have to supply an expression which is always nonzero. The most obvious one would simply be while(1) .)

If it's possible to rewrite a for loop as a while loop and vice versa, why do they both exist? Which one should you choose? In general, when you choose a for loop, its three expressions should all manipulate the same variable or data structure, using the initialize, test, increment pattern. If they don't manipulate the same variable or don't follow that pattern, wedging them into a for loop buys nothing and a while loop would probably be clearer. (The reason that one loop or the other can be clearer is simply that, when you see a for loop, you expect to see an idiomatic initialize/test/increment of a single variable, and if the for loop you're looking at doesn't end up matching that pattern, you've been momentarily misled break and continue

Sometimes, due to an exceptional condition, you need to jump out of a loop early, that is, before the main controlling expression of the loop causes it to terminate normally. Other times, in an elaborate loop, you may want to jump back to the top of the loop (to test the controlling expression again, and perhaps begin a new trip through the loop) without playing out all the steps of the current loop. The break and continue statements allow you to do these two things. (They are, in fact, essentially restricted forms of goto .)

To put everything we've seen in this chapter together, as well as demonstrate the use of the break statement, here is a program for printing prime numbers between 1 and 100:

#include <stdio.h>

#include <math.h> main()

{ int i, j; printf("%d\n", 2); for(i = 3; i <= 100; i = i + 1)

{

} return 0;

} for(j = 2; j < i; j = j + 1)

{ if(i % j == 0)

} break; if(j > sqrt(i))

{ printf("%d\n", i); break;

}

The outer loop steps the variable i through the numbers from 3 to 100; the code tests to see if each number has any divisors other than 1 and itself. The trial divisor j loops from 2 up to i . j is a divisor of i if the remainder of i divided by j is 0, so the code uses C's ``remainder'' or

``modulus'' operator % to make this test. (Remember that i % j gives the remainder when i is divided by j .)

If the program finds a divisor, it uses break to break out of the inner loop, without printing anything. But if it notices that j has risen higher than the square root of i , without its having found any divisors, then i must not have any divisors, so i is prime, and its value is printed. (Once we've determined that i is prime by noticing that j > sqrt(i) , there's no need to try the other trial divisors, so we use a second break statement to break out of the loop in that case, too.)

The simple algorithm and implementation we used here (like many simple prime number algorithms) does not work for 2, the only even prime number, so the program ``cheats'' and prints out 2 no matter what, before going on to test the numbers from 3 to 100.

Many improvements to this simple program are of course possible; you might experiment with it. (Did you notice that the ``test'' expression of the inner loop for(j = 2; j < i; j = j + 1) is in a sense unnecessary, because the loop always terminates early due to one of the two break statements? ) switch case

Switch case statements are a substitute for long if statements that compare a variable to several

"integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. switch ( <variable> ) {

case this-value:

Code to execute if <variable> == this-value

break; case that-value:

Code to execute if <variable> == that-value

break;

... default:

Code to execute if <variable> does not equal the value following any of the cases

break;

}

Unit 2

DATA STRUCTURES

Structure:

2.1 Arrays

2.2 Functions and Program Structure

2.3 Basic I/O

2.4 Pointers

2.5 Structures

2.6 Strings

2.1 Arrays

So far, we've been declaring simple variables: the declaration int i; declares a single variable, named i , of type int . It is also possible to declare an array of several elements. The declaration int a[10]; declares an array, named a , consisting of ten elements, each of type int . Simply speaking, an array is a variable that can hold more than one value. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.) We can represent the array a above with a picture like this:

In C, arrays are zero-based : the ten elements of a 10-element array are numbered from 0 to 9. The subscript which specifies a single element of an array is simply an integer expression in square brackets. The first element of the array is a[0] , the second element is a[1] , etc. You can use these ``array subscript expressions'' anywhere you can use the name of a simple variable, for example: a[0] = 10; a[1] = 20; a[2] = a[0] + a[1];

Notice that the subscripted array references (i.e. expressions such as a[0] and a[1] ) can appear on either side of the assignment operator.

The subscript does not have to be a constant like 0 or 1 ; it can be any integral expression.

For example, it's common to loop over all elements of an array: int i; for(i = 0; i < 10; i = i + 1)

a[i] = 0;

This loop sets all ten elements of the array a to 0.

Arrays are a real convenience for many problems, but there is not a lot that C will do with them for you automatically. In particular, you can neither set all elements of an array at once nor assign one array to another; both of the assignments and a = 0; int b[10]; b = a; are illegal.

/* WRONG */

/* WRONG */

To set all of the elements of an array to some value, you must do so one by one, as in the loop example above. To copy the contents of one array to another, you must again do so one by one: int b[10]; for(i = 0; i < 10; i = i + 1) b[i] = a[i];

Remember that for an array declared int a[10]; there is no element a[10] ; the topmost element is a[9] . This is one reason that zero-based loops are also common in C. Note that the for loop for(i = 0; i < 10; i = i + 1)

... does just what you want in this case: it starts at 0, the number 10 suggests (correctly) that it goes through 10 iterations, but the less-than comparison means that the last trip through the loop has i set to 9. (The comparison i <= 9 would also work, but it would be less clear and therefore poorer style.)

In the little examples so far, we've always looped over all 10 elements of the sample array a . It's common, however, to use an array that's bigger than necessarily needed, and to use a second variable to keep track of how many elements of the array are currently in use.

For example, we might have an integer variable int na; /* number of elements of a[] in use */

Then, when we wanted to do something with a (such as print it out), the loop would run from 0 to na , not 10 (or whatever a 's size was): for(i = 0; i < na; i = i + 1) printf("%d\n", a[i]);

Naturally, we would have to ensure ensure that na 's value was always less than or equal to the number of elements actually declared in a .

Arrays are not limited to type int ; you can have arrays of char or double or any other type.

Here is a slightly larger example of the use of arrays. Suppose we want to investigate the behavior of rolling a pair of dice. The total roll can be anywhere from 2 to 12, and we want to count how often each roll comes up. We will use an array to keep track of the counts: a[2] will count how many times we've rolled 2, etc.

We'll simulate the roll of a die by calling C's random number generation function, rand() . Each time you call rand() , it returns a different, pseudo-random integer. The values that rand() returns typically span a large range, so we'll use C's modulus (or

``remainder'') operator % to produce random numbers in the range we want. The expression rand() % 6 produces random numbers in the range 0 to 5, and rand() % 6

+ 1 produces random numbers in the range 1 to 6.

Here is the program:

#include <stdio.h>

#include <stdlib.h>

{ main() int i; int d1, d2; int a[13]; /* uses [2..12] */ for(i = 2; i <= 12; i = i + 1) a[i] = 0; for(i = 0; i < 100; i = i + 1)

{ d1 = rand() % 6 + 1; d2 = rand() % 6 + 1; a[d1 + d2] = a[d1 + d2] + 1;

} for(i = 2; i <= 12; i = i + 1) printf("%d: %d\n", i, a[i]); return 0;

}

We include the header <stdlib.h> because it contains the necessary declarations for the rand() function. We declare the array of size 13 so that its highest element will be a[12] .

(We're wasting a[0] and a[1] ; this is no great loss.) The variables d1 and d2 contain the rolls of the two individual dice; we add them together to decide which cell of the array to increment, in the line a[d1 + d2] = a[d1 + d2] + 1;

After 100 rolls, we print the array out. Typically (as craps players well know), we'll see mostly

7's, and relatively few 2's and 12's.

2.1.1 Array Initialization

Although it is not possible to assign to all elements of an array at once using an assignment expression, it is possible to initialize some or all elements of an array when the array is defined. The syntax looks like this: int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

The list of values, enclosed in braces {} , separated by commas, provides the initial values for successive elements of the array.

(Under older, pre-ANSI C compilers, you could not always supply initializers for ``local'' arrays inside functions; you could only initialize ``global'' arrays, those outside of any function. Those compilers are now rare, so you shouldn't have to worry about this distinction any more. We'll talk more about local and global variables later in this chapter.)

If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to 0. For example, int a[10] = {0, 1, 2, 3, 4, 5, 6}; would initialize a[7] , a[8] , and a[9] to 0. When an array definition includes an initializer, the array dimension may be omitted, and the compiler will infer the dimension from the number of initializers. For example, int b[] = {10, 11, 12, 13, 14}; would declare, define, and initialize an array b of 5 elements (i.e. just as if you'd typed int b[5] ). Only the dimension is omitted; the brackets [] remain to indicate that b is in fact an array.

In the case of arrays of char , the initializer may be a string constant: char s1[7] = "Hello,"; char s2[10] = "there,"; char s3[] = "world!";

As before, if the dimension is omitted, it is inferred from the size of the string initializer. (We haven't covered strings in detail yet--we'll do so in chapter 8--but it turns out that all strings in C are terminated by a special character with the value 0. Therefore, the array s3 will be of size 7, and the explicitly-sized s1 does need to be of size at least 7. For s2 , the last 4 characters in the array will all end up being this zero-value character.)

2.1.2 Arrays of Arrays (``Multidimensional'' Arrays)

When we said that ``Arrays are not limited to type int ; you can have arrays of... any other type,'' we meant that more literally than you might have guessed. If you have an

``array of int ,'' it means that you have an array each of whose elements is of type int .

But you can have an array each of whose element is of type x , where x is any type you choose. In particular, you can have an array each of whose elements is another array! We can use these arrays of arrays for the same sorts of tasks as we'd use multidimensional arrays in other computer languages (or matrices in mathematics). Naturally, we are not limited to arrays of arrays, either; we could have an array of arrays of arrays, which would act like a 3-dimensional array, etc.

The declaration of an array of arrays looks like this: int a2[5][7];

You have to read complicated declarations like these ``inside out.'' What this one says is that a2 is an array of 5 somethings, and that each of the somethings is an array of 7 int s. More briefly,

`` a2 is an array of 5 arrays of 7 int s,'' or, `` a2 is an array of array of int .'' In the declaration of a2 , the brackets closest to the identifier a2 tell you what a2 first and foremost is. That's how you know it's an array of 5 arrays of size 7, not the other way around. You can think of a2 as having 5 ``rows'' and 7 ``columns,'' although this interpretation is not mandatory. (You could also treat the ``first'' or inner subscript as ``x'' and the second as ``y.'' Unless you're doing something fancy, all you have to worry about is that the subscripts when you access the array match those that you used when you declared it, as in the examples below.)

To illustrate the use of multidimensional arrays, we might fill in the elements of the above array a2 using this piece of code: int i, j; for(i = 0; i < 5; i = i + 1)

{ for(j = 0; j < 7; j = j + 1) a2[i][j] = 10 * i + j;

}

This pair of nested loops sets a[1][2] to 12, a[4][1] to 41, etc. Since the first dimension of a2 is 5, the first subscripting index variable, i , runs from 0 to 4. Similarly, the second subscript varies from 0 to 6.

We could print a2 out (in a two-dimensional way, suggesting its structure) with a similar pair of nested loops: for(i = 0; i < 5; i = i + 1)

{ for(j = 0; j < 7; j = j + 1) printf("%d\t", a2[i][j]); printf("\n");

}

(The character \t in the printf string is the tab character.)

Just to see more clearly what's going on, we could make the ``row'' and ``column'' subscripts explicit by printing them, too: for(j = 0; j < 7; j = j + 1) printf("\t%d:", j); printf("\n"); for(i = 0; i < 5; i = i + 1)

{

printf("%d:", i); for(j = 0; j < 7; j = j + 1) printf("\t%d", a2[i][j]); printf("\n");

}

This last fragment would print

0: 1: 2: 3: 4: 5: 6:

0: 0 1 2 3 4 5 6

1: 10 11 12 13 14 15 16

2: 20 21 22 23 24 25 26

3: 30 31 32 33 34 35 36

4: 40 41 42 43 44 45 46

Finally, there's no reason we have to loop over the ``rows'' first and the ``columns'' second; depending on what we wanted to do, we could interchange the two loops, like this: for(j = 0; j < 7; j = j + 1)

{ for(i = 0; i < 5; i = i + 1) printf("%d\t", a2[i][j]); printf("\n");

}

Notice that i is still the first subscript and it still runs from 0 to 4, and j is still the second subscript and it still runs from 0 to 6.

2.1.3 Visibility and Lifetime (Global Variables, etc.)

We haven't said so explicitly, but variables are channels of communication within a program. You set a variable to a value at one point in a program, and at another point (or points) you read the value out again. The two points may be in adjoining statements, or they may be in widely separated parts of the program.

How long does a variable last? How widely separated can the setting and fetching parts of the program be, and how long after a variable is set does it persist? Depending on the variable and how you're using it, you might want different answers to these questions.

The visibility of a variable determines how much of the rest of the program can access that variable. You can arrange that a variable is visible only within one part of one function, or in one function, or in one source file, or anywhere in the program. (We haven't really talked about source files yet; we'll be exploring them soon.)

Why would you want to limit the visibility of a variable? For maximum flexibility, wouldn't it be handy if all variables were potentially visible everywhere? As it happens, that arrangement would be too flexible: everywhere in the program, you would have to keep track of the names of all the variables declared anywhere else in the program, so that you didn't accidentally re-use one. Whenever a variable had the wrong value by mistake, you'd have to search the entire program for the bug, because any statement in the entire program could potentially have modified that variable. You would constantly be stepping all over yourself by using a common variable name like i in two parts of your program, and having one snippet of code accidentally overwrite the values being used by another part of the code. The communication would be sort of like an old party line-you'd always be accidentally interrupting other conversations, or having your conversations interrupted.

To avoid this confusion, we generally give variables the narrowest or smallest visibility they need. A variable declared within the braces {} of a function is visible only within that function; variables declared within functions are called local variables . If another function somewhere else declares a local variable with the same name, it's a different variable entirely, and the two don't clash with each other.

On the other hand, a variable declared outside of any function is a global variable , and it is potentially visible anywhere within the program. You use global variables when you do want the communications path to be able to travel to any part of the program. When you declare a global variable, you will usually give it a longer, more descriptive name (not

something generic like i ) so that whenever you use it you will remember that it's the same variable everywhere.

Another word for the visibility of variables is scope .

How long do variables last? By default, local variables (those declared within a function) have automatic duration : they spring into existence when the function is called, and they

(and their values) disappear when the function returns. Global variables, on the other hand, have static duration : they last, and the values stored in them persist, for as long as the program does. (Of course, the values can in general still be overwritten, so they don't necessarily persist forever.)

Finally, it is possible to split a function up into several source files, for easier maintenance. When several source files are combined into one program (we'll be seeing how in the next chapter) the compiler must have a way of correlating the global variables which might be used to communicate between the several source files. Furthermore, if a global variable is going to be useful for communication, there must be exactly one of it: you wouldn't want one function in one source file to store a value in one global variable named globalvar , and then have another function in another source file read from a different global variable named globalvar . Therefore, a global variable should have exactly one defining instance , in one place in one source file. If the same variable is to be used anywhere else (i.e. in some other source file or files), the variable is declared in those other file(s) with an external declaration , which is not a defining instance. The external declaration says, ``hey, compiler, here's the name and type of a global variable

I'm going to use, but don't define it here, don't allocate space for it; it's one that's defined somewhere else, and I'm just referring to it here.'' If you accidentally have two distinct defining instances for a variable of the same name, the compiler (or the linker) will complain that it is ``multiply defined.''

It is also possible to have a variable which is global in the sense that it is declared outside of any function, but private to the one source file it's defined in. Such a variable is visible

to the functions in that source file but not to any functions in any other source files, even if they try to issue a matching declaration.

You get any extra control you might need over visibility and lifetime, and you distinguish between defining instances and external declarations, by using storage classes . A storage class is an extra keyword at the beginning of a declaration which modifies the declaration in some way. Generally, the storage class (if any) is the first word in the declaration, preceding the type name. (Strictly speaking, this ordering has not traditionally been necessary, and you may see some code with the storage class, type name, and other parts of a declaration in an unusual order.)

We said that, by default, local variables had automatic duration. To give them static duration (so that, instead of coming and going as the function is called, they persist for as long as the function does), you precede their declaration with the static keyword: static int i;

By default, a declaration of a global variable (especially if it specifies an initial value) is the defining instance. To make it an external declaration, of a variable which is defined somewhere else, you precede it with the keyword extern : extern int j;

Finally, to arrange that a global variable is visible only within its containing source file, you precede it with the static keyword: static int k;

Notice that the static keyword can do two different things: it adjusts the duration of a local variable from automatic to static, or it adjusts the visibility of a global variable from truly global to private-to-the-file.

To summarize, we've talked about two different attributes of a variable: visibility and duration. These are orthogonal, as shown in this table:

duration: visibility: automatic static local normal local variables static local variables global N/A normal global variables

We can also distinguish between file-scope global variables and truly global variables, based on the presence or absence of the static keyword.

We can also distinguish between external declarations and defining instances of global variables, based on the presence or absence of the extern keyword.

2.1.4 Default Initialization

The duration of a variable (whether static or automatic) also affects its default initialization.

If you do not explicitly initialize them, automatic-duration variables (that is, local, nonstatic ones) are not guaranteed to have any particular initial value; they will typically contain garbage. It is therefore a fairly serious error to attempt to use the value of an automatic variable which has never been initialized or assigned to: the program will either work incorrectly, or the garbage value may just happen to be ``correct'' such that the program appears to work correctly! However, the particular value that the garbage takes on can vary depending literally on anything : other parts of the program, which compiler was used, which hardware or operating system the program is running on, the time of day, the phase of the moon. (Okay, maybe the phase of the moon is a bit of an exaggeration.) So you hardly want to say that a program which uses an uninitialized variable ``works''; it may seem to work, but it works for the wrong reason, and it may stop working tomorrow.

Static-duration variables (global and static local), on the other hand, are guaranteed to be initialized to 0 if you do not use an explicit initializer in the definition.

(Once upon a time, there was another distinction between the initialization of automatic vs. static variables: you could initialize aggregate objects, such as arrays, only if they had static duration. If your compiler complains when you try to initialize a local array, it's probably an old, pre-ANSI compiler. Modern, ANSI-compatible compilers remove this limitation, so it's no longer much of a concern.)

2.1.5 Examples

Here is an example demonstrating almost everything we've seen so far:

{ int globalvar = 1; extern int anotherglobalvar; static int privatevar; f()

} int localvar; int localvar2 = 2; static int persistentvar;

Here we have six variables, three declared outside and three declared inside of the function f() . globalvar is a global variable. The declaration we see is its defining instance (it happens also to include an initial value). globalvar can be used anywhere in this source file, and it could be used in other source files, too (as long as corresponding external declarations are issued in those other source files). anotherglobalvar is a second global variable. It is not defined here; the defining instance for it (and its initialization) is somewhere else.

privatevar is a ``private'' global variable. It can be used anywhere within this source file, but functions in other source files cannot access it, even if they try to issue external declarations for it. (If other source files try to declare a global variable called

`` privatevar '', they'll get their own; they won't be sharing this one.) Since it has static duration and receives no explicit initialization, privatevar will be initialized to 0. localvar is a local variable within the function f() . It can be accessed only within the function f() . (If any other part of the program declares a variable named `` localvar '', that variable will be distinct from the one we're looking at here.) localvar is conceptually ``created'' each time f() is called, and disappears when f() returns. Any value which was stored in localvar last time f() was running will be lost and will not be available next time f() is called. Furthermore, since it has no explicit initializer, the value of localvar will in general be garbage each time f() is called. localvar2 is also local, and everything that we said about localvar applies to it, except that since its declaration includes an explicit initializer, it will be initialized to 2 each time f() is called.

Finally, persistentvar is again local to f() , but it does maintain its value between calls to f() . It has static duration but no explicit initializer, so its initial value will be 0.

The defining instances and external declarations we've been looking at so far have all been of simple variables. There are also defining instances and external declarations of functions, which we'll be looking at in the next chapter.

(Also, don't worry about static variables for now if they don't make sense to you; they're a relatively sophisticated concept, which you won't need to use at first.)

The term declaration is a general one which encompasses defining instances and external declarations; defining instances and external declarations are two different kinds of declarations. Furthermore, either kind of declaration suffices to inform the compiler of the name and type of a particular variable (or function). If you have the defining instance of a global variable in a source file, the rest of that source file can use that variable

without having to issue any external declarations. It's only in source files where the defining instance hasn't been seen that you need external declarations.

You will sometimes hear a defining instance referred to simply as a ``definition,'' and you will sometimes hear an external declaration referred to simply as a ``declaration.'' These usages are mildly ambiguous, in that you can't tell out of context whether a ``declaration'' is a generic declaration (that might be a defining instance or an external declaration) or whether it's an external declaration that specifically is not a defining instance. (Similarly, there are other constructions that can be called ``definitions'' in C, namely the definitions of preprocessor macros, structures, and typedefs, none of which we've met.) In these notes, we'll try to make things clear by using the unambiguous terms defining instance and external declaration . Elsewhere, you may have to look at the context to determine how the terms ``definition'' and ``declaration'' are being used.

2.1.6 Type casting

Typecasting is a way to make a variable of one type, such as an int, act like another type, such as a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.

#include <stdio.h> int main()

{

/* The (char) is a typecast, telling the computer to interpret the 65 as a

character, not as a number. It is going to give the character output of

the equivalent of the number 65 (It should be the letter A for ASCII).

Note that the %c below is the format code for printing a single character

*/

printf( "%c\n", (char)65 );

getchar();

One use for typecasting for is when you want to use the ASCII characters. For example, what if you want to create your own chart of all 256 ASCII characters. To do this, you will need to use to typecast to allow you to print out the integer as its character equivalent.

#include <stdio.h> int main()

{

}

for ( int x = 0; x < 256; x++ ) {

/* Note the use of the int version of x to output a number and the use

* of (char) to typecast the x into a character which outputs the

* ASCII character that corresponds to the current number

*/

printf( "%d = %c\n", x, (char)x );

}

getchar();

If you were paying careful attention, you might have noticed something kind of strange: when we passed the value of x to printf as a char, we'd already told the compiler that we intended the value to be treated as a character when we wrote the format string as %c. Since the char type is just a small integer, adding this typecast actually doesn't add any value!

So when would a typecast come in handy? One use of typecasts is to force the correct type of mathematical operation to take place. It turns out that in C (and other programming languages), the result of the division of integers is itself treated as an integer: for instance, 3/5 becomes 0!

Why? Well, 3/5 is less than 1, and integer division ignores the remainder.

On the other hand, it turns out that division between floating point numbers, or even between one floating point number and an integer, is sufficient to keep the result as a floating point

number. So if we were performing some kind of fancy division where we didn't want truncated values, we'd have to cast one of the variables to a floating point type. For instance, (float)3/5 comes out to .6, as you would expect!

When might this come up? It's often reasonable to store two values in integers. For instance, if you were tracking heart patients, you might have a function to compute their age in years and the number of heart times they'd come in for heart pain. One operation you might conceivably want to perform is to compute the number of times per year of life someone has come in to see their physician about heart pain. What would this look like?

/* magical function returns the age in years */ int age = getAge();

/* magical function returns the number of visits */ int pain_visits = getVisits(); float visits_per_year = pain_visits / age;

The problem is that when this program is run, visits_per_year will be zero unless the patient had an awful lot of visits to the doc. The way to get around this problem is to cast one of the values being divided so it gets treated as a floating point number, which will cause the compiler to treat the expression as if it were to result in a floating point number: float visits_per_year = pain_visits / (float)age;

/* or */ float visits_per_year = (float)pain_visits / age;

This would

cause the correct values to be stored in visits_per_year.

2.2 Functions and Program Structure

A function is a ``black box'' that we've locked part of our program into. The idea behind a function is that it compartmentalizes part of the program and in particular, that the code within the function has some useful properties:

1.

It performs some well-defined task, which will be useful to other parts of the program.

2.

It might be useful to other programs as well; that is, we might be able to reuse it (and without having to rewrite it).

3.

The rest of the program doesn't have to know the details of how the function is implemented. This can make the rest of the program easier to think about.

4.

The function performs its task well. It may be written to do a little more than is required by the first program that calls it, with the anticipation that the calling program (or some other program) may later need the extra functionality or improved performance. (It's important that a finished function do its job well, otherwise there might be a reluctance to call it, and it therefore might not achieve the goal of reusability.)

5.

By placing the code to perform the useful task into a function, and simply calling the function in the other parts of the program where the task must be performed, the rest of the program becomes clearer: rather than having some large, complicated, difficultto-understand piece of code repeated wherever the task is being performed, we have a single simple function call, and the name of the function reminds us which task is being performed.

6.

Since the rest of the program doesn't have to know the details of how the function is implemented, the rest of the program doesn't care if the function is reimplemented later, in some different way (as long as it continues to perform its same task, of course!).

This means that one part of the program can be rewritten, to improve performance or add a new feature (or simply to fix a bug), without having to rewrite the rest of the program.

Functions are probably the most important weapon in our battle against software complexity. You'll want to learn when it's appropriate to break processing out into functions (and also when it's not), and how to set up function interfaces to best achieve the qualities mentioned above: reusability, information hiding, clarity, and maintainability.

2.2.1 Function Basics

So what defines a function? It has a name that you call it by, and a list of zero or more arguments or parameters that you hand to it for it to act on or to direct its work; it has a body containing the actual instructions (statements) for carrying out the task the function is supposed to perform; and it may give you back a return value , of a particular type.

Here is a very simple function, which accepts one argument, multiplies it by 2, and hands that value back: int multbytwo(int x)

{ int retval; retval = x * 2; return retval;

}

On the first line we see the return type of the function ( int ), the name of the function

( multbytwo ), and a list of the function's arguments, enclosed in parentheses. Each argument has both a name and a type; multbytwo accepts one argument, of type int , named x . The name x is arbitrary, and is used only within the definition of multbytwo . The caller of this function only needs to know that a single argument of type int is expected; the caller does not need to know what name the function will use internally to refer to that argument. (In particular, the caller does not have to pass the value of a variable named x .)

Next we see, surrounded by the familiar braces, the body of the function itself. This function consists of one declaration (of a local variable retval ) and two statements. The first statement is a conventional expression statement, which computes and assigns a value to retval , and the second statement is a return statement, which causes the function to return to its caller, and also specifies the value which the function returns to its caller.

The return statement can return the value of any expression, so we don't really need the local retval variable; the function could be collapsed to

} int multbytwo(int x)

{ return x * 2;

How do we call a function? We've been doing so informally since day one, but now we have a chance to call one that we've written, in full detail. Here is a tiny skeletal program to call multby2 :

#include <stdio.h> extern int multbytwo(int); int main()

{ int i, j; i = 3; j = multbytwo(i);

} printf("%d\n", j); return 0;

This looks much like our other test programs, with the exception of the new line extern int multbytwo(int);

This is an external function prototype declaration. It is an external declaration, in that it declares something which is defined somewhere else. (We've already seen the defining instance of the function multbytwo , but maybe the compiler hasn't seen it yet.) The function prototype declaration contains the three pieces of information about the function that a caller needs to know: the function's name, return type, and argument type(s). Since we don't care what name the multbytwo function will use to refer to its first argument, we don't need to mention it.

(On the other hand, if a function takes several arguments, giving them names in the prototype may make it easier to remember which is which, so names may optionally be used in function prototype declarations.) Finally, to remind us that this is an external declaration and not a defining instance, the prototype is preceded by the keyword extern .

The presence of the function prototype declaration lets the compiler know that we intend to call this function, multbytwo . The information in the prototype lets the compiler generate the correct code for calling the function, and also enables the compiler to check up on our code (by making sure, for example, that we pass the correct number of arguments to each function we call).

Down in the body of main, the action of the function call should be obvious: the line j = multbytwo(i); calls multbytwo , passing it the value of i as its argument. When multbytwo returns, the return value is assigned to the variable j . (Notice that the value of main 's local variable i will become the value of multbytwo 's parameter x ; this is absolutely not a problem, and is a normal sort of affair.)

This example is written out in ``longhand,'' to make each step equivalent. The variable i isn't really needed, since we could just as well call j = multbytwo(3);

And the variable j isn't really needed, either, since we could just as well call printf("%d\n", multbytwo(3));

Here, the call to multbytwo is a subexpression which serves as the second argument to printf . The value returned by multbytwo is passed immediately to printf . (Here, as in general, we see the flexibility and generality of expressions in C. An argument passed to a function may be an arbitrarily complex subexpression, and a function call is itself an expression which may be embedded as a subexpression within arbitrarily complicated surrounding expressions.)

We should say a little more about the mechanism by which an argument is passed down from a caller into a function. Formally, C is call by value , which means that a function receives copies of the values of its arguments. We can illustrate this with an example.

Suppose, in our implementation of multbytwo , we had gotten rid of the unnecessary retval variable like this:

int multbytwo(int x)

{ x = x * 2; return x;

}

We might wonder, if we wrote it this way, what would happen to the value of the variable i when we called j = multbytwo(i);

When our implementation of multbytwo changes the value of x , does that change the value of i up in the caller? The answer is no. x receives a copy of i 's value, so when we change x we don't change i .

However, there is an exception to this rule. When the argument you pass to a function is not a single variable, but is rather an array, the function does not receive a copy of the array, and it therefore can modify the array in the caller. The reason is that it might be too expensive to copy the entire array, and furthermore, it can be useful for the function to write into the caller's array, as a way of handing back more data than would fit in the function's single return value.

2.2.2 Function Prototypes

In modern C programming, it is considered good practice to use prototype declarations for all functions that you call. As we mentioned, these prototypes help to ensure that the compiler can generate correct code for calling the functions, as well as allowing the compiler to catch certain mistakes you might make.

Strictly speaking, however, prototypes are optional. If you call a function for which the compiler has not seen a prototype, the compiler will do the best it can, assuming that you're calling the function correctly.

If prototypes are a good idea, and if we're going to get in the habit of writing function prototype declarations for functions we call that we've written (such as multbytwo ), what

happens for library functions such as printf ? Where are their prototypes? The answer is in that boilerplate line

#include <stdio.h> we've been including at the top of all of our programs. stdio.h

is conceptually a file full of external declarations and other information pertaining to the ``Standard I/O'' library functions, including printf . The #include directive (which we'll meet formally in a later chapter) arranges that all of the declarations within stdio.h

are considered by the compiler, rather as if we'd typed them all in ourselves. Somewhere within these declarations is an external function prototype declaration for printf , which satisfies the rule that there should be a prototype for each function we call. (For other standard library functions we call, there will be other ``header files'' to include.) Finally, one more thing about external function prototype declarations. We've said that the distinction between external declarations and defining instances of normal variables hinges on the presence or absence of the keyword extern . The situation is a little bit different for functions. The ``defining instance'' of a function is the function, including its body

(that is, the brace-enclosed list of declarations and statements implementing the function). An external declaration of a function, even without the keyword extern , looks nothing like a function declaration. Therefore, the keyword extern is optional in function prototype declarations. If you wish, you can write int multbytwo(int); and this is just as good an external function prototype declaration as extern int multbytwo(int);

(In the first form, without the extern , as soon as the compiler sees the semicolon, it knows it's not going to see a function body, so the declaration can't be a definition.) You may want to stay in the habit of using extern in all external declarations, including function declarations, since `` extern = external declaration'' is an easier rule to remember.

2.2.3 Function Philosophy

What makes a good function? The most important aspect of a good ``building block'' is that have a single, well-defined task to perform. When you find that a program is hard to

manage, it's often because it has not been designed and broken up into functions cleanly.

Two obvious reasons for moving code down into a function are because:

1. It appeared in the main program several times, such that by making it a function, it can be written just once, and the several places where it used to appear can be replaced with calls to the new function.

2. The main program was getting too big, so it could be made (presumably) smaller and more manageable by lopping part of it off and making it a function.

These two reasons are important, and they represent significant benefits of well-chosen functions, but they are not sufficient to automatically identify a good function. As we've been suggesting, a good function has at least these two additional attributes:

3. It does just one well-defined task, and does it well.

4. Its interface to the rest of the program is clean and narrow.

Attribute 3 is just a restatement of two things we said above. Attribute 4 says that you shouldn't have to keep track of too many things when calling a function. If you know what a function is supposed to do, and if its task is simple and well-defined, there should be just a few pieces of information you have to give it to act upon, and one or just a few pieces of information which it returns to you when it's done. If you find yourself having to pass lots and lots of information to a function, or remember details of its internal implementation to make sure that it will work properly this time, it's often a sign that the function is not sufficiently well-defined. (A poorly-defined function may be an arbitrary chunk of code that was ripped out of a main program that was getting too big, such that it essentially has to have access to all of that main function's local variables.)

The whole point of breaking a program up into functions is so that you don't have to think about the entire program at once; ideally, you can think about just one function at a time.

We say that a good function is a ``black box,'' which is supposed to suggest that the

``container'' it's in is opaque--callers can't see inside it (and the function inside can't see

out). When you call a function, you only have to know what it does, not how it does it.

When you're writing a function, you only have to know what it's supposed to do, and you don't have to know why or under what circumstances its caller will be calling it. (When designing a function, we should perhaps think about the callers just enough to ensure that the function we're designing will be easy to call, and that we aren't accidentally setting things up so that callers will have to think about any internal details.)

Some functions may be hard to write (if they have a hard job to do, or if it's hard to make them do it truly well), but that difficulty should be compartmentalized along with the function itself. Once you've written a ``hard'' function, you should be able to sit back and relax and watch it do that hard work on call from the rest of your program. It should be pleasant to notice (in the ideal case) how much easier the rest of the program is to write, now that the hard work can be deferred to this workhorse function.

(In fact, if a difficult-to-write function's interface is well-defined, you may be able to get away with writing a quick-and-dirty version of the function first, so that you can begin testing the rest of the program, and then go back later and rewrite the function to do the hard parts. As long as the function's original interface anticipated the hard parts, you won't have to rewrite the rest of the program when you fix the function.)

What I've been trying to say in the preceding few paragraphs is that functions are important for far more important reasons than just saving typing. Sometimes, we'll write a function which we only call once, just because breaking it out into a function makes things clearer and easier.

If you find that difficulties pervade a program, that the hard parts can't be buried inside black-box functions and then forgotten about; if you find that there are hard parts which involve complicated interactions among multiple functions, then the program probably needs redesigning.

For the purposes of explanation, we've been seeming to talk so far only about ``main programs'' and the functions they call and the rationale behind moving some piece of code down out of a ``main program'' into a function. But in reality, there's obviously no

need to restrict ourselves to a two-tier scheme. Any function we find ourself writing will often be appropriately written in terms of sub-functions, sub-sub-functions, etc.

(Furthermore, the ``main program,'' main() , is itself just a function.)

2.3 Basic I/O

2.3.1 printf printf 's name comes from print f ormatted. It generates output under the control of a format string (its first argument) which consists of literal characters to be printed and also special character sequences-format specifiers --which request that other arguments be fetched, formatted, and inserted into the string. Our very first program was nothing more than a call to printf , printing a constant string: printf("Hello, world!\n");

Our second program also featured a call to printf : printf("i is %d\n", i);

In that case, whenever printf ``printed'' the string "i is %d" , it did not print it verbatim; it replaced the two characters %d with the value of the variable i .

There are quite a number of format specifiers for printf . Here are the basic ones :

%d print an int argument in decimal

%ld print a long int argument in decimal

%c

%s

%f

%e

%g

%o

%x print a character print a string print a float or double argument same as %f, but use exponential notation use %e or %f, whichever is better print an int argument in octal (base 8) print an int argument in hexadecimal (base 16)

%% print a single %

It is also possible to specify the width and precision of numbers and strings as they are inserted

(somewhat like FORTRAN format statements); we'll present those details in a later chapter.

(Very briefly, for those who are curious: a notation like %3d means to print an int in a field at least 3 spaces wide; a notation like %5.2f

means to print a float or double in a field at least 5 spaces wide, with two places to the right of the decimal.)

To illustrate with a few more examples: the call printf("%c %d %f %e %s %d%%\n", '1', 2, 3.14, 56000000., "eight", 9); would print

The call

1 2 3.140000 5.600000e+07 eight 9% printf("%d %o %x\n", 100, 100, 100); would print

100 144 64

Successive calls to printf just build up the output a piece at a time, so the calls printf("Hello, "); printf("world!\n"); would also print Hello, world!

(on one line of output).

Earlier we learned that C represents characters internally as small integers corresponding to the characters' values in the machine's character set (typically ASCII). This means that there isn't really much difference between a character and an integer in C; most of the difference is in whether we choose to interpret an integer as an integer or a character. printf is one place where we get to make that choice: %d prints an integer value as a string of digits representing its decimal value, while %c prints the character corresponding to a character set value. So the lines

char c = 'A'; int i = 97; printf("c = %c, i = %d\n", c, i); would print c as the character A and i as the number 97. But if, on the other hand, we called printf("c = %d, i = %c\n", c, i); we'd see the decimal value (printed by %d ) of the character 'A' , followed by the character

(whatever it is) which happens to have the decimal value 97.

You have to be careful when calling printf . It has no way of knowing how many arguments you've passed it or what their types are other than by looking for the format specifiers in the format string. If there are more format specifiers (that is, more % signs) than there are arguments, or if the arguments have the wrong types for the format specifiers, printf can misbehave badly, often printing nonsense numbers or (even worse) numbers which mislead you into thinking that some other part of your program is broken.

Because of some automatic conversion rules which we haven't covered yet, you have a small amount of latitude in the types of the expressions you pass as arguments to printf .

The argument for %c may be of type char or int , and the argument for %d may be of type char or int . The string argument for %s may be a string constant, an array of characters, or a pointer to some characters (though we haven't really covered strings or pointers yet).

Finally, the arguments corresponding to %e , %f , and %g may be of types float or double .

But other combinations do not work reliably: %d will not print a long int or a float or a double ; %ld will not print an int ; %e , %f , and %g will not print an int .

2.3.2 Character Input and Output

Unless a program can read some input, it's hard to keep it from doing exactly the same thing every time it's run, and thus being rather boring after a while.

The most basic way of reading input is by calling the function getchar . getchar reads one character from the ``standard input,'' which is usually the user's keyboard, but which

can sometimes be redirected by the operating system. getchar returns (rather obviously) the character it reads, or, if there are no more characters available, the special value EOF

(``end of file'').

A companion function is putchar , which writes one character to the ``standard output.''

(The standard output is, again not surprisingly, usually the user's screen, although it, too, can be redirected. printf , like putchar , prints to the standard output; in fact, you can imagine that printf calls putchar to actually print each of the characters it formats.)

Using these two functions, we can write a very basic program to copy the input, a character at a time, to the output:

}

{

#include <stdio.h>

/* copy input to output */ main() int c; c = getchar(); while(c != EOF)

{ putchar(c); c = getchar();

} return 0;

This code is straightforward, and I encourage you to type it in and try it out. It reads one character, and if it is not the EOF code, enters a while loop, printing one character and reading another, as long as the character read is not EOF . This is a straightforward loop, although there's one mystery surrounding the declaration of the variable c : if it holds characters, why is it an int ?

We said that a char variable could hold integers corresponding to character set values, and that an int could hold integers of more arbitrary values (up to +-32767). Since most character sets contain a few hundred characters (nowhere near 32767), an int variable can in general comfortably hold all char values, and then some. Therefore, there's nothing wrong with declaring c as an int . But in fact, it's important to do so, because getchar can return every character value, plus that special, non-character value EOF , indicating that there are no more characters. Type char is only guaranteed to be able to hold all the character values; it is not guaranteed to be able to hold this ``no more characters'' value without possibly mixing it up with some actual character value. (It's like trying to cram five pounds of books into a four-pound box, or 13 eggs into a carton that holds a dozen.) Therefore, you should always remember to use an int for anything you assign getchar 's return value to.

When you run the character copying program, and it begins copying its input (your typing) to its output (your screen), you may find yourself wondering how to stop it. It stops when it receives end-of-file (EOF), but how do you send EOF? The answer depends on what kind of computer you're using. On Unix and Unix-related systems, it's almost always control-D. On MS-DOS machines, it's control-Z followed by the RETURN key.

Under Think C on the Macintosh, it's control-D, just like Unix. On other systems, you may have to do some research to learn how to send EOF.

(Note, too, that the character you type to generate an end-of-file condition from the keyboard is not the same as the special EOF value returned by getchar . The EOF value returned by getchar is a code indicating that the input system has detected an end-of-file condition, whether it's reading the keyboard or a file or a magnetic tape or a network connection or anything else. In a disk file, at least, there is not likely to be any character in the file corresponding to EOF ; as far as your program is concerned, EOF indicates the absence of any more characters to read.)

Another excellent thing to know when doing any kind of programming is how to terminate a runaway program. If a program is running forever waiting for input, you can usually stop it by sending it an end-of-file, as above, but if it's running forever not

waiting for something, you'll have to take more drastic measures. Under Unix, control-C

(or, occasionally, the DELETE key) will terminate the current program, almost no matter what. Under MS-DOS, control-C or control-BREAK will sometimes terminate the current program, but by default MS-DOS only checks for control-C when it's looking for input, so an infinite loop can be unkillable. There's a DOS command, break on which tells DOS to look for control-C more often, and I recommend using this command if you're doing any programming. (If a program is in a really tight infinite loop under MS-DOS, there can be no way of killing it short of rebooting.) On the Mac, try command-period or commandoption-ESCAPE.

Finally, don't be disappointed (as I was) the first time you run the character copying program. You'll type a character, and see it on the screen right away, and assume it's your program working, but it's only your computer echoing every key you type, as it always does. When you hit RETURN, a full line of characters is made available to your program.

It then zips several times through its loop, reading and printing all the characters in the line in quick succession. In other words, when you run this program, it will probably seem to copy the input a line at a time, rather than a character at a time. You may wonder how a program could instead read a character right away, without waiting for the user to hit RETURN. That's an excellent question, but unfortunately the answer is rather complicated, and beyond the scope of our discussion here. (Among other things, how to read a character right away is one of the things that's not defined by the C language, and it's not defined by any of the standard library functions, either. How to do it depends on which operating system you're using.)

Stylistically, the character-copying program above can be said to have one minor flaw: it contains two calls to getchar , one which reads the first character and one which reads

(by virtue of the fact that it's in the body of the loop) all the other characters. This seems inelegant and perhaps unnecessary, and it can also be risky: if there were more things going on within the loop, and if we ever changed the way we read characters, it would be

easy to change one of the getchar calls but forget to change the other one. Is there a way to rewrite the loop so that there is only one call to getchar , responsible for reading all the characters? Is there a way to read a character, test it for EOF , and assign it to the variable c , all at the same time?

There is. It relies on the fact that the assignment operator, = , is just another operator in C.

An assignment is not (necessarily) a standalone statement; it is an expression, and it has a value (the value that's assigned to the variable on the left-hand side), and it can therefore participate in a larger, surrounding expression. Therefore, most C programmers would write the character-copying loop like this: while((c = getchar()) != EOF) putchar(c);

What does this mean? The function getchar is called, as before, and its return value is assigned to the variable c . Then the value is immediately compared against the value EOF .

Finally, the true/false value of the comparison controls the while loop: as long as the value is not EOF , the loop continues executing, but as soon as an EOF is received, no more trips through the loop are taken, and it exits. The net result is that the call to getchar happens inside the test at the top of the while loop, and doesn't have to be repeated before the loop and within the loop (more on this in a bit).

Stated another way, the syntax of a while loop is always while( expression ) ...

A comparison (using the != operator) is of course an expression; the syntax is expression != expression

And an assignment is an expression; the syntax is expression = expression

What we're seeing is just another example of the fact that expressions can be combined with essentially limitless generality and therefore infinite variety. The left-hand side of the !=

operator (its first expression) is the (sub)expression c = getchar() , and the combined expression is the expression needed by the while loop.

The extra parentheses around

(c = getchar()) are important, and are there because because the precedence of the != operator is higher than that of the = operator. If we (incorrectly) wrote while(c = getchar() != EOF) the compiler would interpret it as

/* WRONG */ while(c = (getchar() != EOF))

That is, it would assign the result of the != operator to the variable c , which is not what we want.

(``Precedence'' refers to the rules for which operators are applied to their operands in which order, that is, to the rules controlling the default grouping of expressions and subexpressions. For example, the multiplication operator * has higher precedence than the addition operator + , which means that the expression a + b * c is parsed as a + (b

* c) . We'll have more to say about precedence later.)

The line while((c = getchar()) != EOF) epitomizes the cryptic brevity which C is notorious for. You may find this terseness infuriating

(and you're not alone!), and it can certainly be carried too far, but bear with me for a moment while I defend it.

The simple example we've been discussing illustrates the tradeoffs well. We have four things to do:

1.

call getchar ,

2.

assign its return value to a variable,

3.

test the return value against EOF , and

4.

process the character (in this case, print it out again).

2.4 Pointers

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them.

Pointer is a variable which contains the address of the another variable.

The cool thing is that once can talk about the address of a variable, you'll then be able to go to that address and retrieve the data stored in it. If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function that to copy every element of the data! Moreover, if you need more memory for your program, you can request more memory from the system--how do you get "back" that memory? The system tells you where it is located in memory; that is to say, you get a memory address back. And you need pointers to store the memory address.

A note about terms: the word pointer can refer either to a memory address itself, or to a variable that stores a memory address. Usually, the distinction isn't really that important: if you pass a pointer variable into a function, you're passing the value stored in the pointer--the memory address. When I want to talk about a memory address, I'll refer to it as a memory address; when I want a variable that stores a memory address, I'll call it a pointer. When a variable stores the address of another variable, I'll say that it is "pointing to" that variable.

2.4.1 Pointer Syntax

Pointers require a bit of new syntax because when you have a pointer, you need the ability to both request the memory location it stores and the value stored at that memory location.

Moreover, since pointers are somewhat special, you need to tell the compiler when you declare your pointer variable that the variable is a pointer, and tell the compiler what type of memory it points to.

The pointer declaration looks like this:

<variable_type> *<name>;

For example, you could declare a pointer that stores the address of an integer with the following syntax: int *points_to_integer;

Notice the use of the *. This is the key to declaring a pointer; if you add it directly before the variable name, it will declare the variable to be a pointer. Minor gotcha: if you declare multiple pointers on the same line, you must precede each of them with an asterisk:

/* one pointer, one regular int */ int *pointer1, nonpointer1;

/* two pointers */ int *pointer1, *pointer2;

As I mentioned, there are two ways to use the pointer to access information: it is possible to have it give the actual address to another variable. To do so, simply use the name of the pointer without the *. However, to access the actual memory location, use the *. The technical name for this doing this is dereferencing the pointer; in essence, you're taking the reference to some memory address and following it, to retrieve the actual value. It can be tricky to keep track of

when you should add the asterisk. Remember that the pointer's natural use is to store a memory address; so when you use the pointer: call_to_function_expecting_memory_address(pointer); then it evaluates to the address. You have to add something extra, the asterisk, in order to retrieve the value stored at the address. You'll probably do that an awful lot. Nevertheless, the pointer itself is supposed to store an address, so when you use the bare pointer, you get that address back.

Pointing to Something: Retrieving an Address

In order to have a pointer actually point to another variable it is necessary to have the memory address of that variable also. To get the memory address of a variable (its location in memory), put the & sign in front of the variable name. This makes it give its address. This is called the address-of operator, because it returns the memory address. Conveniently, both ampersand and address-of start with a; that's a useful way to remember that you use & to get the address of a variable.

For example:

#include <stdio.h> int main()

{

int x; /* A normal integer*/

int *p; /* A pointer to an integer ("*p" is an integer, so p

must be a pointer to an integer) */

p = &x; /* Read it, "assign the address of x to p" */

scanf( "%d", &x ); /* Put a value in x, we could also use p here */

printf( "%d\n", *p ); /* Note the use of the * to get the value */

getchar();

}

The printf outputs the value stored in x. Why is that? Well, let's look at the code. The integer is called x. A pointer to an integer is then defined as p. Then it stores the memory location of x in pointer by using the address operator (&) to get the address of the variable. Using the ampersand is a bit like looking at the label on the safety deposit box to see its number rather than looking inside the box, to get what it stores. The user then inputs a number that is stored in the variable x; remember, this is the same location that is pointed to by p. In fact, since we use an ampersand to pass the value to scanf, it should be clear that scanf is putting the value in the address pointed to by p. (In fact, scanf works becuase of pointers!)

The next line then passes *p into printf. *p performs the "dereferencing" operation on p; it looks at the address stored in p, and goes to that address and returns the value.

Notice that in the above example, the pointer is initialized to point to a specific memory address before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the program. For instance, the operating system will probably prevent you from accessing memory that it knows your program doesn't own: this will cause your program to crash. If it let you use the memory, you could mess with the memory of any running program--for instance, if you had a document opened in Word, you could change the text! Fortunately, Windows and other modern operating systems will stop you from accessing that memory and cause your program to crash. To avoid crashing your program, you should always initialize pointers before you use them.

It is also possible to initialize pointers using free memory. This allows dynamic allocation of memory. It is useful for setting up structures such as linked lists or data trees where you don't know exactly how much memory will be needed at compile time, so you have to get memory during the program's execution. We'll look at these structures later, but for now, we'll simply examine how to request memory from and return memory to the operating system.

The function malloc, residing in the stdlib.h header file, is used to initialize pointers with memory from free store (a section of memory available to all programs). malloc works just like any other function call. The argument to malloc is the amount of memory requested (in bytes), and malloc gets a block of memory of that size and then returns a pointer to the block of memory allocated.

Since different variable types have different memory requirements, we need to get a size for the amount of memory malloc should return. So we need to know how to get the size of different variable types. This can be done using the keyword sizeof, which takes an expression and returns its size. For example, sizeof(int) would return the number of bytes required to store an integer.

#include <stdlib.h> int *ptr = malloc( sizeof(int) );

This code set ptr to point to a memory address of size int. The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage lest the memory be lost to the operating system for the duration of the program (this is often called a memory leak because the program is not keeping track of all of its memory).

Note that it is slightly cleaner to write malloc statements by taking the size of the variable pointed to by using the pointer directly: int *ptr = malloc( sizeof(*ptr) );

What's going on here? sizeof(*ptr) will evaluate the size of whatever we would get back from dereferencing ptr; since ptr is a pointer to an int, *ptr would give us an int, so sizeof(*ptr) will

return the size of an integer. So why do this? Well, if we change ptr to point to something else like a float, then we don't have to go back and correct the malloc call to use sizeof(float). Since ptr would be pointing to a float, *ptr would be a float, so sizeof(*ptr) would still give the right size!

The free function returns memory to the operating system. free( ptr );

After freeing a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer, the pointer becomes a null pointer, in other words, it points to nothing. By doing this, when you do something foolish with the pointer (it happens a lot, even with experienced programmers), you find out immediately instead of later, when you have done considerable damage.

The concept of the null pointer is frequently used as a way of indicating a problem--for instance, malloc returns 0 when it cannot correctly allocate memory. You want to be sure to handle this correctly--sometimes your operating system might actually run out of memory and give you this value!

2.5 Structures

We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create a structure called person which is made up of a string for the name and an integer for the age. Here is how you would create that person structure in C: struct person

{

char *name;

int age;

};

The above is just a declaration of a type. You must still create a variable of that type to be able to use it. Here is how you create a variable called p of the type person:

#include<stdio.h> struct person

{

char *name;

int age;

}; int main()

{

struct person p;

return 0;

}

To access the string or integer of the structure you must use a dot between the structure name and the variable name.

#include<stdio.h>

struct person

{

char *name;

int age;

}; int main()

{

struct person p;

p.name = "John Smith";

p.age = 25;

printf("%s",p.name);

printf("%d",p.age);

return 0;

}

2.5.1 Type definitions

You can give your own name to a variable using a type definition. Here is an example of how to create a type definition called intptr for a pointer to an integer.

#include<stdio.h> typedef int *intptr; int main()

{

intptr ip;

return 0;

}

2.5.2 Type definitions for a structure

If you don't like to use the word struct when declaring a structure variable then you can create a type definition for the structure. The name of the type definition of a structure is usually all in uppercase letters.

#include<stdio.h>

typedef struct person

{

char *name;

int age;

} PERSON; int main()

{

PERSON p;

p.name = "John Smith";

p.age = 25;

printf("%s",p.name);

printf("%d",p.age);

return 0;

}

2.5.3 Pointers to structures

When you use a pointer to a structure you must use -> instead of a dot.

#include<stdio.h>

typedef struct person

{

char *name;

int age;

} PERSON; int main()

{

PERSON p;

PERSON *pptr;

PERSON pptr = &p;

pptr->name = "John Smith";

pptr->age = 25;

printf("%s",pptr->name);

printf("%d",pptr->age);

return 0;

}

2.5.4 Unions

Unions are like structures except they use less memory. If you create a structure with a double that is 8 bytes and an integer that is 4 bytes then the total size will still only be 8 bytes. This is because they are on top of each other. If you change the value of the double then the value of the integer will change and if you change the value of the integer then the value of the double will change. Here is an example of how to declare and use a union:

#include<stdio.h>

typedef union num

{

double d;

int i;

} NUM; int main()

{

NUM n;

n.d = 3.14;

n.i = 5;

return 0;

}

2.6 Strings

A string is an array of characters. Strings must have a 0 or null character after the last character to show where the string ends. The null character is not included in the string.

There are 2 ways of using strings. The first is with a character array and the second is with a string pointer.

A character array is declared in the same way as a normal array. char ca[10];

You must set the value of each individual element of the array to the character you want and you must make the last character a 0. Remember to use %s when printing the string. char ca[10]; ca[0] = 'H'; ca[1] = 'e'; ca[2] = 'l'; ca[3] = 'l'; ca[4] = 'o';

ca[5] = 0; printf("%s",ca);

String pointers are declared as a pointer to a char. char *sp;

When you assign a value to the string pointer it will automatically put the 0 in for you unlike character arrays. char *sp; sp = "Hello"; printf("%s",sp);

You can read a string into only a character array using scanf and not a string pointer. If you want to read into a string pointer then you must make it point to a character array. char ca[10],*sp; scanf("%s",ca); sp = ca; scanf("%s",sp);

2.6.1 String handling functions

The strings.h header file has some useful functions for working with strings. Here are some of the functions you will use most often: strcpy(destination, source)

You can't just use string1 = string2 in C. You have to use the strcpy function to copy one string to another. strcpy copies the source string to the destination string. s1 = "abc"; s2 = "xyz"; strcpy(s1,s2); // s1 = "xyz"

strcat(destination, source)

Joins the destination and source strings and puts the joined string into the destination string. s1 = "abc"; s2 = "xyz"; strcat(s1,s2); // s1 = "abcxyz" strcmp(first, second)

Compares the first and second strings. If the first string is greater than the second one then a number higher than 0 is returned. If the first string is less than the second then a number lower than 0 is returned. If the strings are equal then 0 is returned. s1 = "abc"; s2 = "abc"; i = strcmp(s1,s2); // i = 0

strlen(string)

Returns the amount of characters in a string. s = "abcde"; i = strlen(s); // i = 5

UNIT 3

OBJECT ORIENTED PROGRAMMING CONCEPTS

Structure :

3.1 Introduction

3.2 Object-Oriented Programming

3.3 Properties of Abstract Data Types

3.4 Object-Oriented Concepts

3.5 Implementation of Abstract Data Types

3.1 Introduction

3.1.1 A Survey of Programming Techniques

This chapter is a short survey of programming techniques. We use a simple example to illustrate the particular properties and to point out their main ideas and problems.

Roughly speaking, we can distinguish the following learning curve of someone who learns to program:

 Unstructured programming,

 procedural programming,

 modular programming and

 Object-oriented programming.

3.1.2 Unstructured Programming

Usually, people start learning programming by writing small and simple programs consisting only of one main program. Here ``main program'' stands for a sequence of commands or

statements which modify data which is global throughout the whole program. We can illustrate this as shown in Fig. 1.1

.

Figure 1.1: Unstructured programming. The main program directly operates on global data.

As you should all know, this programming techniques provide tremendous disadvantages once the program gets sufficiently large. For example, if the same statement sequence is needed at different locations within the program, the sequence must be copied. This has lead to the idea to extract these sequences, name them and offering a technique to call and return from these procedures .

3.1.3 Procedural Programming

With procedural programming you are able to combine returning sequences of statements into one single place. A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made (Fig. 1.2

).

Figure 1.2: Execution of procedures. After processing flow of controls proceed where the call was made.

With introducing parameters as well as procedures of procedures ( subprocedures ) programs can now be written more structured and error free. For example, if a procedure is correct, every time it is used it produces correct results. Consequently, in cases of errors you can narrow your search to those places which are not proven to be correct.

Now a program can be viewed as a sequence of procedure calls . The main program is responsible to pass data to the individual calls, the data is processed by the procedures and, once the program has finished, the resulting data is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a tree , as shown in Fig. 1.3

for a program with no subprocedures.

Figure 1.3: Procedural programming. The main program coordinates calls to procedures and hands over appropriate data as parameters.

To sum up: Now we have a single program which is divided into small pieces called procedures. To enable usage of general procedures or groups of procedures also in other programs, they must be separately available. For that reason, modular programming allows grouping of procedures into modules.

3.1.4 Modular Programming

With modular programming procedures of a common functionality are grouped together into separate modules. A program therefore no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls and which form the whole program (Fig. 1.4

).

Figure 1.4: Modular programming. The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters.

Each module can have its own data. This allows each module to manage an internal state which is modified by calls to procedures of this module. However, there is only one state per module and each module exists at most once in the whole program.

3.2 Object-Oriented Programming

Object-oriented programming solves some of the problems just mentioned. In contrast to the other techniques, we now have a web of interacting objects, each house-keeping its own state

(Fig. 1.6

).

Figure 1.6: Object-oriented programming. Objects of the program interact by sending messages to each other.

Consider the multiple lists example again. The problem here with modular programming is, that you must explicitly create and destroy your list handles. Then you use the procedures of the module to modify each of your handles.

In contrast to that, in object-oriented programming we would have as many list objects as needed. Instead of calling a procedure which we must provide with the correct list handle, we would directly send a message to the list object in question. Roughly speaking, each object implements its own module allowing for example many lists to coexist.

Each object is responsible to initialize and destroy itself correctly. Consequently, there is no longer the need to explicitly call a creation or termination procedure.

You might ask: So what? Isn't this just a more fancier modular programming technique?

You were right, if this would be all about object-orientation. Fortunately, it is not.

Beginning with the next chapters additional features of object-orientation are introduced which makes object-oriented programming to a new programming technique.

3.2.1 Abstract Data Types

Some authors describe object-oriented programming as programming abstract data types and their relationships. Within this section we introduce abstract data types as a basic concept for object-orientation and we explore concepts used in the list example of the last section in more detail.

3.2.2 Handling Problems

The first thing with which one is confronted when writing programs is the problem .

Typically you are confronted with ``real-life'' problems and you want to make life easier by providing a program for the problem. However, real-life problems are nebulous and the first thing you have to do is to try to understand the problem to separate necessary from unnecessary details: You try to obtain your own abstract view, or model , of the problem. This process of modeling is called abstraction and is illustrated in Figure 2.1

.

Figure 3.1: Create a model from a problem with abstraction.

The model defines an abstract view to the problem. This implies that the model focuses only on problem related stuff and that you try to define properties of the problem. These properties include by the problem.

 the data which are affected and the operations which are identified

As an example consider the administration of employees in an institution. The head of the administration comes to you and ask you to create a program which allows to administer the employees. Well, this is not very specific. For example, what employee information is needed by the administration? What tasks should be allowed? Employees are real persons who can be characterized with many properties; very few are:

 name,

 size,

 date of birth,

 shape,

 social number,

 room number,

 hair color,

 Hobbies.

Certainly not all of these properties are necessary to solve the administration problem.

Only some of them are problem specific . Consequently you create a model of an employee for the problem. This model only implies properties which are needed to fulfill the requirements of the administration, for instance name, date of birth and social number. These properties are called the data of the (employee) model. Now you have described real persons with help of an abstract employee.

Of course, the pure description is not enough. There must be some operations defined with which the administration is able to handle the abstract employees. For example, there must be an operation which allows you to create a new employee once a new person enters the institution. Consequently, you have to identify the operations which should be able to be performed on an abstract employee. You also decide to allow access to the employees' data only with associated operations. This allows you to ensure that data elements are always in a proper state. For example you are able to check if a provided date is valid.

To sum up, abstraction is the structuring of a nebulous problem into well-defined entities by defining their data and operations. Consequently, these entities combine data and operations. They are not decoupled from each other.

3.3 Properties of Abstract Data Types

The example of the previous section shows, that with abstraction you create a welldefined entity which can be properly handled. These entities define the data structure of a set of items. For example, each administered employee has a name, date of birth and social number.

The data structure can only be accessed with defined operations . This set of operations is called interface and is exported by the entity. An entity with the properties just described is called an abstract data type (ADT).

Figure 2.2

shows an ADT which consists of an abstract data structure and operations.

Only the operations are viewable from the outside and define the interface.

Figure 2.2: An abstract data type (ADT).

Once a new employee is ``created'' the data structure is filled with actual values: You now have an instance of an abstract employee. You can create as many instances of an abstract employee as needed to describe every real employed person.

Let's try to put the characteristics of an ADT in a more formal way:

Definition (Abstract Data Type) An abstract data type (ADT) is characterized by the following properties:

1. It exports a type.

2. It exports a set of operations. This set is called interface.

3. Operations of the interface are the one and only access mechanism to the type's data structure.

4. Axioms and preconditions define the application domain of the type.

3.3.1 Importance of Data Structure Encapsulation

The principle of hiding the used data structure and to only provide a well-defined interface is known as encapsulation . Why is it so important to encapsulate the data structure?

To answer this question consider the following mathematical example where we want to define an ADT for complex numbers. For the following it is enough to know that complex numbers consists of two parts: real part and imaginary part . Both parts are represented by real numbers. Complex numbers define several operations: addition, subtraction, multiplication or division to name a few. Axioms and preconditions are valid as defined by the mathematical definition of complex numbers. For example, it exists a neutral element for addition.

To represent a complex number it is necessary to define the data structure to be used by its ADT. One can think of at least two possibilities to do this:

Both parts are stored in a two-valued array where the first value indicates the real part and the second value the imaginary part of the complex number. If x denotes the real part and y the imaginary part, you could think of accessing them via array subscription:

x=c[0] and y=c[1].

Both parts are stored in a two-valued record. If the element name of the real part is r and that of the imaginary part is i, x and y can be obtained with: x=c.r and y=c.i.

Point 3 of the ADT definition says that for each access to the data structure there must be an operation defined. The above access examples seem to contradict this requirement. Is this really true?

Let's look again at the two possibilities for representing imaginary numbers. Let's stick to the real part. In the first version, x equals c [0]. In the second version, x equals c .r. In both cases x equals ``something''. It is this ``something'' which differs from the actual data structure used. But in both cases the performed operation ``equal'' has the same meaning

to declare x to be equal to the real part of the complex number c : both cases archive the same semantics.

If you think of more complex operations the impact of decoupling data structures from operations becomes even more clear. For example the addition of two complex numbers requires you to perform an addition for each part. Consequently, you must access the value of each part which is different for each version. By providing an operation ``add'' you can encapsulate these details from its actual use. In an application context you simply ``add two complex numbers'' regardless of how this functionality is actually archived.

Once you have created an ADT for complex numbers, say Complex , you can use it in the same way like well-known data types such as integers.

Let's summarize this: The separation of data structures and operations and the constraint to only access the data structure via a well-defined interface allows you to choose data structures appropriate for the application environment.

3.3.2 Generic Abstract Data Types

ADTs are used to define a new type from which instances can be created. As shown in the list example, sometimes these instances should operate on other data types as well.

For instance, one can think of lists of apples, cars or even lists. The semantical definition of a list is always the same. Only the type of the data elements change according to what type the list should operate on.

This additional information could be specified by a generic parameter which is specified at instance creation time. Thus an instance of a generic ADT is actually an instance of a particular variant of the ADT. A list of apples can therefore be declared as follows:

List<Apple> listOfApples;

The angle brackets now enclose the data type for which a variant of the generic ADT List should be created. listOfApples offers the same interface as any other list, but operates on instances of type Apple .

3.3.3 Notation

As ADTs provide an abstract view to describe properties of sets of entities, their use is independent from a particular programming language. Each ADT description consists of two parts:

Data: This part describes the structure of the data used in the ADT in an informal way.

Operations: This part describes valid operations for this ADT, hence, it describes its interface. We use the special operation constructor to describe the actions which are to be performed once an entity of this ADT is created and destructor to describe the actions which are to be performed once an entity is destroyed. For each operation the provided arguments as well as preconditions and postconditions are given.

As an example the description of the ADT Integer is presented. Let k be an integer expression:

ADT Integer is

Data

A sequence of digits optionally prefixed by a plus or minus sign. We refer to this signed whole number as N.

Operations

constructor

Creates a new integer.

add(k)

Creates a new integer which is the sum of N and k.

Consequently, the postcondition of this operation is sum = N + k . Don't confuse this with assign statements as used in programming languages! It is rather a mathematical equation which yields ``true'' for each value sum , N and k after add has been performed.

sub(k)

Similar to add, this operation creates a new integer of the difference of both integer values. Therefore the postcondition for this operation is sum = N-k.

set(k)

Set N to k. The postcondition for this operation is N = k.

...

end

The description above is a specification for the ADT Integer . Please notice, that we use words for names of operations such as ``add''. We could use the more intuitive ``+'' sign instead, but this may lead to some confusion: You must distinguish the operation ``+'' from the mathematical use of ``+'' in the postcondition. The name of the operation is just syntax whereas the semantics is described by the associated pre- and postconditions.

However, it is always a good idea to combine both to make reading of ADT specifications easier.

Real programming languages are free to choose an arbitrary implementation for an ADT.

For example, they might implement the operation add with the infix operator ``+'' leading to a more intuitive look for addition of integers.

3.3.4 Abstract Data Types and Object-Orientation

ADTs allows the creation of instances with well-defined properties and behavior. In object-orientation ADTs are referred to as classes . Therefore a class defines properties of objects which are the instances in an object-oriented environment.

ADTs define functionality by putting main emphasis on the involved data, their structure, operations as well as axioms and preconditions. Consequently, object-oriented programming is ``programming with ADTs'': combining functionality of different ADTs to solve a problem. Therefore instances (objects) of ADTs (classes) are dynamically created, destroyed and used.

3.4 Object-Oriented Concepts

The previous sections already introduce some ``object-oriented'' concepts. However, they were applied in an procedural environment or in a verbal manner. In this section we investigate these concepts in more detail and give them names as used in existing object-oriented programming languages.

3.5 Implementation of Abstract Data Types

The last section introduces abstract data types (ADTs) as an abstract view to define properties of a set of entities. Object-oriented programming languages must allow to implement these types. Consequently, once an ADT is implemented we have a particular representation of it available.

Consider again the ADT Integer . Programming languages such as Pascal, C, Modula-2 and others already offer an implementation for it. Sometimes it is called int or integer .

Once you've created a variable of this type you can use its provided operations. For example, you can add two integers:

int i, j, k; /* Define three integers */

i = 1; /* Assign 1 to integer i */

j = 2; /* Assign 2 to integer j */

k = i + j; /* Assign the sum of i and j to k */

Let's play with the above code fragment and outline the relationship to the ADT Integer .

The first line defines three instances i , j and k of type Integer . Consequently, for each instance the special operation constructor should be called. In our example, this is internally done by the compiler. The compiler reserves memory to hold the value of an integer and ``binds'' the corresponding name to it. If you refer to i you actually refer to this memory area which was ``constructed'' by the definition of i . Optionally, compilers might choose to initialize the memory, for example, they might set it to 0 (zero).

The next line

i = 1; sets the value of i to be 1. Therefore we can describe this line with help of the ADT notation as follows:

Perform operation set with argument 1 on the Integer instance i. This is written as follows: i.set(1).

We now have a representation at two levels. The first level is the ADT level where we express everything that is done to an instance of this ADT by the invocation of defined operations. At this level, pre- and postconditions are used to describe what actually happens. In the following example, these conditions are enclosed in curly brackets.

{ Precondition: i = n where n is any Integer } i.set(1)

{ Postcondition: i = 1 }

Don't forget that we currently talk about the ADT level! Consequently, the conditions are mathematical conditions.

The second level is the implementation level, where an actual representation is chosen for the operation. In C the equal sign ``='' implements the set() operation. However, in

Pascal the following representation was chosen:

i := 1;

In either case, the ADT operation set is implemented.

Let's stress these levels a little bit further and have a look at the line

k = i + j;

Obviously, ``+'' was chosen to implement the add operation. We could read the part ``i + j'' as ``add the value of j to the value of i '', thus at the ADT level this results in

{ Precondition: Let i = n 1 and j = n 2 with n 1, n 2 particular Integers } i.add(j)

{ Postcondition: i = n 1 and j = n 2 }

The postcondition ensures that i and j do not change their values. Please recall the specification of add . It says that a new Integer is created the value of which is the sum.

Consequently, we must provide a mechanism to access this new instance. We do this with the set operation applied on instance k :

{ Precondition: Let k = n where n is any Integer } k.set(i.add(j))

{ Postcondition: k = i + j }

As you can see, some programming languages choose a representation which almost equals the mathematical formulation used in the pre- and postconditions. This makes it sometimes difficult to not mix up both levels.

3.5.1 Class

A class is an actual representation of an ADT. It therefore provides implementation details for the data structure used and operations. We play with the ADT Integer and design our own class for it:

class Integer {

attributes:

int i

methods:

setValue(int n)

Integer addValue(Integer j)

}

In the example above as well as in examples which follow we use a notation which is not programming language specific. In this notation class {...} denotes the definition of a class. Enclosed in the curly brackets are two sections attributes: and methods: which define the implementation of the data structure and operations of the corresponding ADT.

Again we distinguish the two levels with different terms: At the implementation level we speak of ``attributes'' which are elements of the data structure at the ADT level. The same applies to ``methods'' which are the implementation of the ADT operations.

In our example, the data structure consists of only one element: a signed sequence of digits. The corresponding attribute is an ordinary integer of a programming language .

We only define two methods setValue() and addValue() representing the two operations set and add .

Definition (Class) A class is the implementation of an abstract data type (ADT). It defines attributes and methods which implement the data structure and operations of the

ADT, respectively. Instances of classes are called objects . Consequently, classes define properties and behavior of sets of objects.

3.5.2 Object

Recall the employee example of previous chapter We have talked of instances of abstract

employees. These instances are actual ``examples'' of an abstract employee, hence, they contain actual values to represent a particular employee. We call these instances objects.

Objects are uniquely identifiable by a name . Therefore you could have two distinguishable objects with the same set of values. This is similar to ``traditional'' programming languages where you could have, say two integers i and j both of which equal to ``2''. Please notice the use of ``i'' and ``j'' in the last sentence to name the two integers. We refer to the set of values at a particular time as the state of the object.

Definition (Object) An object is an instance of a class. It can be uniquely identified by its

name and it defines a state which is represented by the values of its attributes at a particular time.

The state of the object changes according to the methods which are applied to it. We refer to these possible sequence of state changes as the behavior of the object:

Definition (Behavior) The behavior of an object is defined by the set of methods which can be applied on it.

We now have two main concepts of object-orientation introduced, class and object.

Object-oriented programming is therefore the implementation of abstract data types or, in more simple words, the writing of classes. At runtime instances of these classes, the objects, achieve the goal of the program by changing their states. Consequently, you can think of your running program as a collection of objects. The question arises of how these objects interact ? We therefore introduce the concept of a message in the next section.

3.5.3 Message

A running program is a pool of objects where objects are created, destroyed and interacting.

This interacting is based on messages which are sent from one object to another asking the recipient to apply a method on itself.. In our pseudo programming language we could create new objects and invoke methods on them. For example, we could use

Integer i; /* Define a new integer object */

i.setValue(1); /* Set its value to 1 */ to express the fact, that the integer object i should set its value to 1. This is the message

``Apply method setValue with argument 1 on yourself.'' sent to object i . We notate the sending of a message with ``.''. This notation is also used in C++; other object-oriented languages might use other notations, for example ``''.

Sending a message asking an object to apply a method is similar to a procedure call in

``traditional'' programming languages. However, in object-orientation there is a view of autonomous objects which communicate with each other by exchanging messages.

Objects react when they receive messages by applying methods on themselves. They also may deny the execution of a method, for example if the calling object is not allowed to execute the requested method.

In our example, the message and the method which should be applied once the message is received have the same name: We send ``setValue with argument 1'' to object i which applies ``setValue(1)''.

Definition (Message) A message is a request to an object to invoke one of its methods. A message therefore contains

 the name of the method and the arguments of the method.

Consequently, invocation of a method is just a reaction caused by receipt of a message.

This is only possible, if the method is actually known to the object.

Definition (Method) A method is associated with a class. An object invokes a method as a reaction to receipt of a message.

Structure :

UNIT 4

ADVANCED FEATURES OF OOPS

4.1 Encapsulation

4.2 Relationships

4.3 Inheritance

4.4 Abstract Classes

4.5 Static and Dynamic Binding

4.1 Encapsulation

Encapsulation is the procedure of covering up of data and functions into a single unit. Encapsulation

(also information hiding) consists of separating the external aspects of an object which are accessible to other objects, from the internal implementation details of the object, which are hidden from other objects.

A process, encapsulation means the act of enclosing one or more items within a (physical or logical) container (Class). Object-oriented programming is based on encapsulation. When an objects state and behavior are kept together, they are encapsulated. That is, the data that represents the state of the object and the methods (Functions and Subs) that manipulate that data are stored together as a cohesive unit.

The object takes requests from other client objects, but does not expose its the details of its data or code to them. The object alone is responsible for its own state, exposing public messages for clients, and declaring private methods that make up its implementation. The client depends on the (hopefully) simple public interface, and does not know about or depend on the details of the implementation.

For example, a HashTable object will take get() and set() requests from other objects, but does not expose its internal hash table data structures or the code strategies that it uses.

4.2 Relationships

A-Kind-Of relationship

Consider you have to write a drawing program. This program would allow drawing of various objects such as points, circles, rectangles, triangles and many more. For each object you provide a class definition. For example, the point class just defines a point by its coordinates:

class Point {

attributes:

int x, y

methods:

setX(int newX)

getX()

setY(int newY)

getY()

}

You continue defining classes of your drawing program with a class to describe circles. A circle defines a center point and a radius:

class Circle {

attributes:

int x, y,

radius

methods:

setX(int newX)

getX()

setY(int newY)

getY()

setRadius(newRadius)

getRadius()

}

Comparing both class definitions we can observe the following:

Both classes have two data elements x and y. In the class Point these elements describe the position of the point, in the case of class Circle they describe the circle's center.

Thus, x and y have the same meaning in both classes: They describe the position of their associated object by defining a point.

Both classes offer the same set of methods to get and set the value of the two data elements x and y.

Class Circle ``adds'' a new data element radius and corresponding access methods.

Knowing the properties of class Point we can describe a circle as a point plus a radius and methods to access it. Thus, a circle is ``a-kind-of'' point. However, a circle is somewhat more ``specialized''. We illustrate this graphically as shown in Figure 4.1

.

Figure 4.1: Illustration of ``a-kind-of'' relationship.

In this and the following figures, classes are drawn using rectangles. Their name always starts with an uppercase letter. The arrowed line indicates the direction of the relation, hence, it is to be read as ``Circle is a-kind-of Point.''

Is-A relationship

The previous relationship is used at the class level to describe relationships between two similar classes. If we create objects of two such classes we refer to their relationship as an

``is-a'' relationship.

Since the class Circle is a kind of class Point , an instance of Circle , say a circle , is a point . Consequently, each circle behaves like a point. For example, you can move points in x direction by altering the value of x . Similarly, you move circles in this direction by altering their x value.

Figure 4.2

illustrates this relationship. In this and the following figures, objects are drawn using rectangles with round corners. Their name only consists of lowercase letters.

Figure 4.2: Illustration of ``is-a'' relationship.

Part-Of relationship

You sometimes need to be able to build objects by combining them out of others. You already know this from procedural programming, where you have the structure or record construct to put data of various types together.

Let's come back to our drawing program. You already have created several classes for the available figures. Now you decide that you want to have a special figure which represents your own logo which consists of a circle and a triangle. (Let's assume, that you already have defined a class Triangle .) Thus, your logo consists of two parts or the circle and triangle are part-of your logo:

class Logo {

attributes:

Circle circle

Triangle triangle

methods:

set(Point where)

}

We illustrate this in Figure 4.3

.

Figure 4.3: Illustration of ``part-of'' relationship.

Has-A relationship

This relationship is just the inverse version of the part-of relationship. Therefore we can easily add this relationship to the part-of illustration by adding arrows in the other direction (Figure 4.4

).

Figure 4.4: Illustration of ``has-a'' relationship.

4.3 Inheritance

With inheritance we are able to make use of the a-kind-of and is-a relationship. As described there, classes which are a-kind-of another class share properties of the latter. In our point and circle example, we can define a circle which inherits from point:

class Circle inherits from Point {

attributes:

int radius

methods:

setRadius(int newRadius)

getRadius()

}

Class Circle inherits all data elements and methods from point. There is no need to define them twice: We just use already existing and well-known data and method definitions.

On the object level we are now able to use a circle just as we would use a point, because a circle is-a point. For example, we can define a circle object and set its center point coordinates:

Circle acircle

acircle.setX(1) /* Inherited from Point */

acircle.setY(2)

acircle.setRadius(3) /* Added by Circle */

``Is-a'' also implies, that we can use a circle everywhere where a point is expected. For example, you can write a function or method, say move() , which should move a point in x direction:

move(Point apoint, int deltax) {

apoint.setX(apoint.getX() + deltax)

}

As a circle inherits from a point, you can use this function with a circle argument to move its center point and, hence, the whole circle:

Circle acircle

...

move(acircle, 10) /* Move circle by moving */

/* its center point */

Let's try to formalize the term ``inheritance'':

Definition (Inheritance) Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say ``A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance.

Definition (Superclass/Subclass) If class A inherits from class B, then B is called

superclass of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the superclass.

In the literature you may also find other terms for ``superclass'' and ``subclass''.

Superclasses are also called parent classes . Subclasses may also be called child classes or just derived classes .

Of course, you can again inherit from a subclass, making this class the superclass of the new subclass. This leads to a hierarchy of superclass/subclass relationships. If you draw this hierarchy you get an inheritance graph .

A common drawing scheme is to use arrowed lines to indicate the inheritance relationship between two classes or objects. In our examples we have used ``inherits-

from''. Consequently, the arrowed line starts from the subclass towards the superclass as illustrated in Figure 4.5

.

Figure 4.5: A simple inheritance graph.

In the literature you also find illustrations where the arrowed lines are used just the other way around. The direction in which the arrowed line is used, depends on how the corresponding author has decided to understand it.

Anyway, within this tutorial, the arrowed line is always directed towards the superclass.

In the following sections an unmarked arrowed line indicates ``inherit-from''.

Multiple Inheritance

One important object-oriented mechanism is multiple inheritance. Multiple inheritance does not mean that multiple subclasses share the same superclass. It also does not mean that a subclass can inherit from a class which itself is a subclass of another class.

Multiple inheritance means that one subclass can have more than one superclass. This enables the subclass to inherit properties of more than one superclass and to ``merge'' their properties.

As an example consider again our drawing program. Suppose we already have a class

String which allows convenient handling of text. For example, it might have a method to append other text. In our program we would like to use this class to add text to the possible drawing objects. It would be nice to also use already existing routines such as move() to move the text around. Consequently, it makes sense to let a drawable text have a point which defines its location within the drawing area. Therefore we derive a new class DrawableString which inherits properties from Point and String as illustrated in

Figure 4.6

.

Figure 4.6: Derive a drawable string which inherits properties of Point and String.

In our pseudo language we write this by simply separating the multiple superclasses by comma:

class DrawableString inherits from Point, String {

attributes:

/* All inherited from superclasses */

methods:

/* All inherited from superclasses */

}

We can use objects of class DrawableString like both points and strings. Because a drawablestring is-a point we can move them around

DrawableString dstring

...

move(dstring, 10)

...

Since it is a string , we can append other text to them:

dstring.append("The red brown fox ...")

Now it's time for the definition of multiple inheritance:

Definition (Multiple Inheritance) If class A inherits from more than one class, ie. A inherits from B1, B2, ..., Bn, we speak of multiple inheritance. This may introduce

naming conflicts in A if at least two of its superclasses define properties with the same name.

The above definition introduce naming conflicts which occur if more than one superclass of a subclass use the same name for either attributes or methods. For an example, let's assume, that class String defines a method setX() which sets the string to a sequence of

``X'' characters . The question arises, what should be inherited by DrawableString ? The

Point , String version or none of them?

These conflicts can be solved in at least two ways:

The order in which the superclasses are provided define which property will be accessible by the conflict causing name. Others will be ``hidden''.

The subclass must resolve the conflict by providing a property with the name and by defining how to use the ones from its superclasses.

The first solution is not very convenient as it introduces implicit consequences depending on the order in which classes inherit from each other. For the second case, subclasses must explicitly redefine properties which are involved in a naming conflict.

A special type of naming conflict is introduced if a class D multiply inherits from superclasses B and C which themselves are derived from one superclass A . This leads to an inheritance graph as shown in Figure 4.7

.

Figure 4.7: A name conflict introduced by a shared superclass of superclasses used with multiple inheritance.

The question arises what properties class D actually inherits from its superclasses B and

C . Some existing programming languages solve this special inheritance graph by deriving

D with

 the properties of A plus

 the properties of B and C without the properties they have inherited from A.

Consequently, D cannot introduce naming conflicts with names of class A . However, if B and C add properties with the same name, D runs into a naming conflict.

Another possible solution is, that D inherits from both inheritance paths. In this solution,

D owns two copies of the properties of A : one is inherited by B and one by C .

Although multiple inheritance is a powerful object-oriented mechanism the problems introduced with naming conflicts have lead several authors to ``doom'' it. As the result of multiple inheritance can always be achieved by using (simple) inheritance some objectoriented languages even don't allow its use. However, carefully used, under some conditions multiple inheritance provides an efficient and elegant way of formulating things.

4.4 Abstract Classes

With inheritance we are able to force a subclass to offer the same properties like their superclasses. Consequently, objects of a subclass behave like objects of their superclasses.

Sometimes it make sense to only describe the properties of a set of objects without knowing the actual behavior beforehand. In our drawing program example, each object should provide a method to draw itself on the drawing area. However, the necessary steps to draw an objects depends on its represented shape. For example, the drawing routine of a circle is different from the drawing routine of a rectangle.

Let's call the drawing method print() . To force every drawable object to include such method, we define a class DrawableObject from which every other class in our example inherits general properties of drawable objects:

abstract class DrawableObject {

attributes:

methods:

print()

}

We introduce the new keyword abstract here. It is used to express the fact that derived classes must ``redefine'' the properties to fulfill the desired functionality. Thus from the abstract class' point of view, the properties are only specified but not fully defined . The full definition including the semantics of the properties must be provided by derived classes.

Now, every class in our drawing program example inherits properties from the general drawable object class. Therefore, class Point changes to:

class Point inherits from DrawableObject {

attributes:

int x, y

methods:

setX(int newX)

getX()

setY(int newY)

getY()

print() /* Redefine for Point */

}

We are now able to force every drawable object to have a method called print which should provide functionality to draw the object within the drawing area. The superclass of all drawable objects, class DrawableObject , does not provide any functionality for drawing itself. It is not intended to create objects from it. This class rather specifies properties which must be defined by every derived class. We refer to this special type of classes as abstract classes :

Definition (Abstract Class) A class A is called abstract class if it is only used as a superclass for other classes. Class A only specifies properties. It is not used to create objects. Derived classes must define the properties of A.

Abstract classes allow us to structure our inheritance graph. However, we actually don't want to create objects from them: we only want to express common characteristics of a set of classes.

Even More Object-Oriented Concepts

We continue with our tour through the world of object-oriented concepts by presenting a short introduction to static versus dynamic binding. With this, we can introduce polymorphism as a mechanism which let objects figure out what to do at runtime. But first, here is a brief overview about generic types.

4.5 Static and Dynamic Binding

In strongly typed programming languages you typically have to declare variables prior to their use. This also implies the variable's definition where the compiler reserves space for the variable. For example, in Pascal an expression like

var i : integer; declares variable i to be of type integer . Additionally, it defines enough memory space to hold an integer value.

With the declaration we bind the name i to the type integer . This binding is true within the scope in which i is declared. This enables the compiler to check at compilation time for type consistency. For example, the following assignment will result in a type mismatch error when you try to compile it:

var i : integer;

...

i := 'string';

We call this particular type of binding ``static'' because it is fixed at compile time.

Definition (Static Binding) If the type T of a variable is explicitly associated with its name N by declaration, we say, that N is statically bound to T. The association process is called static binding.

There exist programming languages which are not using explicitly typed variables. For example, some languages allow to introduce variables once they are needed:

... /* No appearance of i */

i := 123 /* Creation of i as an integer */

The type of i is known as soon as its value is set. In this case, i is of type integer since we have assigned a whole number to it. Thus, because the content of i is a whole number, the type of i is integer.

Definition (Dynamic Binding) If the type T of a variable with name N is implicitly associated by its content, we say, that N is dynamically bound to T. The association process is called dynamic binding.

Both bindings differ in the time when the type is bound to the variable. Consider the following example which is only possible with dynamic binding:

if somecondition() == TRUE then

n := 123

else

n := 'abc'

endif

The type of n after the if statement depends on the evaluation of somecondition() . If it is

TRUE, n is of type integer whereas in the other case it is of type string.

Polymorphism

Polymorphism allows an entity (for example, variable, function or object) to take a variety of representations. Therefore we have to distinguish different types of polymorphism which will be outlined here.

The first type is similar to the concept of dynamic binding. Here, the type of a variable depends on its content. Thus, its type depends on the content at a specific time:

v := 123 /* v is integer */

... /* use v as integer */

v := 'abc' /* v "switches" to string */

... /* use v as string */

Definition (Polymorphism (1)) The concept of dynamic binding allows a variable to take different types dependent on the content at a particular time. This ability of a variable is called polymorphism. Another type of polymorphism can be defined for functions. For example, suppose you want to define a function isNull() which returns

TRUE if its argument is 0 (zero) and FALSE otherwise. For integer numbers this is easy:

boolean isNull(int i) {

if (i == 0) then

return TRUE

else

return FALSE

endif

}

However, if we want to check this for real numbers, we should use another comparison due to the precision problem:

boolean isNull(real r) {

if (r < 0.01 and r > -0.99) then

return TRUE

else

return FALSE

endif

}

In both cases we want the function to have the name isNull . In programming languages without polymorphism for functions we cannot declare these two functions because the name isNull would be doubly defined. Without polymorphism for functions, doubly defined names would be ambiguous. However, if the language would take the parameters of the function into account it would work. Thus, functions (or methods) are uniquely identified by:

 the name of the function (or method) and

 the types of its parameter list.

Since the parameter list of both isNull functions differ, the compiler is able to figure out the correct function call by using the actual types of the arguments:

var i : integer

var r : real

i = 0

r = 0.0

...

if (isNull(i)) then ... /* Use isNull(int) */

...

if (isNull(r)) then ... /* Use isNull(real) */

Definition (Polymorphism (2)) If a function (or method) is defined by the combination of

 its name and

 the list of types of its parameters

we speak of polymorphism. This type of polymorphism allows us to reuse the same name for functions (or methods) as long as the parameter list differs. Sometimes this type of polymorphism is called overloading.

The last type of polymorphism allows an object to choose correct methods. Consider the function move() again, which takes an object of class Point as its argument. We have used this function with any object of derived classes, because the is-a relation holds.

Now consider a function display() which should be used to display drawable objects. The declaration of this function might look like this:

display(DrawableObject o) {

...

o.print()

...

}

We would like to use this function with objects of classes derived from DrawableObject :

Circle acircle

Point apoint

Rectangle arectangle

display(apoint) /* Should invoke apoint.print() */

display(acircle) /* Should invoke acircle.print() */

display(arectangle) /* Should invoke arectangle.print() */

The actual method should be defined by the content of the object o of function display() .

Since this is somewhat complicated, here is a more abstract example:

class Base {

attributes:

methods:

virtual foo()

bar()

}

class Derived inherits from Base {

attributes:

methods:

virtual foo()

bar()

}

demo(Base o) {

o.foo()

o.bar()

}

Base abase

Derived aderived

demo(abase)

demo(aderived)

In this example we define two classes Base and Derived . Each class defines two methods foo() and bar() . The first method is defined as virtual . This means that if this method is invoked its definition should be evaluated by the content of the object.

We then define a function demo() which takes a Base object as its argument.

Consequently, we can use this function with objects of class Derived as the is-a relation holds. We call this function with a Base object and a Derived object, respectively.

Suppose, that foo() and bar() are defined to just print out their name and the class in which they are defined. Then the output is as follows:

foo() of Base called.

bar() of Base called.

foo() of Derived called.

bar() of Base called.

Why is this so? Let's see what happens. The first call to demo() uses a Base object. Thus, the function's argument is ``filled'' with an object of class Base . When it is time to invoke method foo() it's actual functionality is chosen based on the current content of the corresponding object o . This time, it is a Base object. Consequently, foo() as defined in class Base is called.

The call to bar() is not subject to this content resolution. It is not marked as virtual .

Consequently, bar() is called in the scope of class Base .

The second call to demo() takes a Derived object as its argument. Thus, the argument o is filled with a Derived object. However, o itself just represents the Base part of the provided object a derived .

Now, the call to foo() is evaluated by examining the content of o , hence, it is called within the scope of Derived . On the other hand, bar() is still evaluated within the scope of

Base .

Definition (Polymorphism (3)) Objects of superclasses can be filled with objects of their subclasses. Operators and methods of subclasses can be defined to be evaluated in two contexts.:

1. Based on object type, leading to an evaluation within the scope of the superclass.

2. Based on object content, leading to an evaluation within the scope of the contained subclass.

The second type is called polymorphism.

Download