Top Banner
GITAM UNIVERSITY (Declared as Deemed to be University U/S 3 of UGC Act, 1956) Lecture Notes of Programming with C [ERUCS 105] INTRODUCTION Lecture-1 Hyderabad Campus, Rudraram HYDERABAD-502329 JULY-NOV 2013
14
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 1

GITAM UNIVERSITY(Declared as Deemed to be University U/S 3 of UGC Act, 1956)

Lecture Notes

of

Programming with C

[ERUCS 105]

INTRODUCTION

Lecture-1

Hyderabad Campus, RudraramHYDERABAD-502329

JULY-NOV 2013

Page 2: Lecture 1

CONTENTS LIST OF TABLES

Contents

1 Computers 31.1 Input Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Arithmetic Logic Unit(ALU) . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 Control Unit(CU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4 Memory Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Output Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.6 Classification of computer software . . . . . . . . . . . . . . . . . . . . . 5

2 Classification of Computers 6

3 Generation of Programming Languages 73.1 First Generation: Machine Language . . . . . . . . . . . . . . . . . . . . 73.2 Second Generation: Assembly Language . . . . . . . . . . . . . . . . . . 73.3 3GL: HLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.4 4GL: Very High Level Languages . . . . . . . . . . . . . . . . . . . . . . 8

4 Sample Programs 94.1 The While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Review Questions 135.1 Long answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135.2 Short answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

6 Snippet Programs 14

List of Figures

1 Parts of Computer System . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Hardware blocks of a computer . . . . . . . . . . . . . . . . . . . . . . . 43 Relationship between hardware and software . . . . . . . . . . . . . . . . 54 While Loop syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 While Loop Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

List of Tables

1 Generations of Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2

Page 3: Lecture 1

1 COMPUTERS1

1 Computers

The Computer is a programmable finite state machine which can perform precisearithmetic and logic operations.

+ A programmable finite state machine is one that can take one of a fixed rangeof values. The computer is said to be programmable because it can instructed toperform tasks within it’s capacity.

When we talk about a computer, we actually mean two things* First is the computer hardware that does all the physical work computers are knownfor.* Second is the computer software that commands the hardware what to do and howto do it.

A list of instructions can be submitted to the computer in the form of a program.

Figure 1: Parts of Computer System

a task is performed by executing the corresponding program. A collection of relatedprograms is referred to as the software. The physical circuitry and components areknown as the hardware. Hardware can be categorized into five blocks.They are:• Input Unit• Arithmetic logic Unit(ALU)• Control Unit(CU)• Memory Unit• Output Unit

1.1 Input Unit

The input unit is the device used to enter data and instructions into the computer. Thedata read from the input device is stored in the computer’s memory.

+ Some common input devices are: Keyboard, mouse, joystick, light pen, etc.

1PROGRAMMING with C[ERUCS 105]

3

Page 4: Lecture 1

1.2 Arithmetic Logic Unit(ALU) 1 COMPUTERS2

1.2 Arithmetic Logic Unit(ALU)

The ALU performs arithmetic and logic operations. The arithmetic operations includeaddition, subtraction, multiplication and division.

Figure 2: Hardware blocks of a computer

1.3 Control Unit(CU)

The control unit coordinates the activities of the various components of a computer.The arithmetic logic unit and the control unit together comprise the Central processingUnit(CPU). The CPUs are called Microprocessor. The speed of a computer dependson the clock frequency of the microprocessor.

+ Some popular microprocessors are: Intel, Pentium, Pentium Pro, IBM, AMD, etc.

1.4 Memory Unit

The information(instructions and data) required is stored is stored in memory. Thecomputer memory is constructed out of semi-conducting material and stores informationin binary form. Binary information is composed of two symbols o and 1, called binarydigits(bits). All information within the computer is represented by two digits. Thememory is organized into equal sized units(usually a collection of 8 bits, called byte).These units are arranged in a sequence and are identified by numbers called addresses.

+ The memory of a computer can be divided into distinct parts. They are:• Registers• Internal Cacahe• External Cache• Main memory• Secondary memory

2PROGRAMMING with C[ERUCS 105]

4

Page 5: Lecture 1

1.5 Output Unit 1 COMPUTERS3

1.5 Output Unit

Just s input devices are used to supply the computer with data, there should be somemeans for the computer to communicate with the user. The information generated bythe computer is displayed using an output device.

+ Some examples of output devices are: Cathod Ray Tube(CRT), Printer, Cardpunch, plotters, etc.

1.6 Classification of computer software

Computer software can be brodely classified into two groups: System software andapplication software.• System software provides a general programming environment in which programmerscan create specific applications to suite their needs. This environment provides newfunctions that are not available at the hardware level and performs tasks related toexecuting the application program.• Application software is designed to solve a particular problem for users. It is generallywhat we think of when we say the word computer programs.

+ Examples of application software include spreadsheets, database systems, desktoppublishing systems, program development software, etc.

Figure 3: Relationship between hardware and software

3PROGRAMMING with C[ERUCS 105]

5

Page 6: Lecture 1

2 CLASSIFICATION OF COMPUTERS4

2 Classification of Computers

* The Abacus was invented around 2500 BC. It is a device made of beads strung onrods and used to perform simple computations.

* Napier invented a device for computing logarithms of natural numbers. It was calledNapier’s Bones and enabled multiplication to be done easily.

* Blaise Pascal invented the addressing machine in 1642 AD.

* charless Babbage designed the difference engine in 1822 AD and analytical enginein 1833 AD.Computers can be classified into five generations, based on the technology and performanceand is shown in Table 1.

Table 1: Generations of ComputerGeneration Year Hardware Software Performance

Zero Pre 1946 Counter wheelswith mechanicalcomponents

−−−− Numerical op-erations at slowspeed

First 1946-1956 Vacuum Tubes Stored Pro-grams, MachineCode, Symboliccode

2 KB Memory, 10Kilo instructionsper second

Second 1956-1963 Transistors High Level Lan-guage(HLL),FORTRAN,ALGOL, etc

32 KB Memory,200 Kilo instruc-tions per second

Third 1964-1981 ICs, Semi-Conductormemory

PASCAL, C, etc 2 MB Memory,5 Million In-structions PerSecond(MIPS)

Fourth 1982-1989 VLSI OOPs Lan-guages: C++,etc

32 KB Memory,30 Million In-structions PerSecond(MIPS)

Fifth 1990-NOW

Parallel Process-ing

JAVA, .NET, etc 1 Giga to 1 Tera in-structions per sec-ond

4PROGRAMMING with C[ERUCS 105]

6

Page 7: Lecture 1

3 GENERATION OF PROGRAMMING LANGUAGES5

3 Generation of Programming Languages

A Programming language is a language specially designed to express computationsthat can be performed by the computer. programming languages are used to createprograms that control the behaviour of a system, to express algorithms, or as a mode ofhuman-computer communication.We now know that programming languages are the primary tools for creating software.As of now hundreds of programming languages exits in the market, some more used thanothers, and each claiming to be the best. However, back in 1940s when computers werebeing developed there was just one language-the machine language.The concept of generations of programming languages (also known as levels), is closelyconnected to the advances, in technology that brought about computer generations. Thefour generations of programming languages include:

• Machine language• Assembly language• High level Languages(HLL) (also known as third generation languages or 3GL)• very High level Languages(HLL) (also known as fourth Generation Languages or 4GL)

3.1 First Generation: Machine Language

Machine language was used to program the first stored program on computer system.This is the lowest level of programming language. The machine language is the only lan-guage that the computer understands. All the commands and data values are expressedusing 1 and 0s, corresponding to the ’on’ and ’off’ electrical states in a computer.The main advantage of machine language is that the code can run very fast and effi-ciently, since it is directly executed by CPU. However, on the down side, the machinelanguage is difficult to learn and is far more difficult to edit if errors occur. Moreover, ifyou want to add some instructions into memory at some location, then all the instruc-tions after the insertion point would have to be moved down to make room in memoryto accommodate the new instructions.

3.2 Second Generation: Assembly Language

The second generation of programming language includes the assembly language. As-sembly languages are symbolic programming languages that use symbolic notation torepresent machine-language instructions. These languages are closely connected to ma-chine language and the internal architecture of the computer system on which they areused. Since they are close to the machine, assembly language is also called low-levellanguage. Assembly language developed in the mid 1950s was a great leap forward. Itused symbolic codes also known as mnemonic codes that are easy-to-remember abbre-viations, rather than numbers, Examples of these code include ADD for add, CMP forcompare, MUL for multiply, etc.

5PROGRAMMING with C[ERUCS 105]

7

Page 8: Lecture 1

3.3 3GL: HLL 3 GENERATION OF PROGRAMMING LANGUAGES6

3.3 3GL: HLL

A third generation programming language(3GL) is a refinement of the second generationprogramming language. The 2GL languages brought logical structure to the software.The third generation language was introduced to make the languages more programmerfriendly.Generally, a statement written in high-level programming language will expand into sev-eral machine language instructions. This is in contrast to assembly languages, where onestatement would generate one machine language instruction. 3GLs made programmingeasier, efficient, and less prone to error.3GL make it easier to write and debug a program and gives the programmer more timeto think about its overall logic. The programs written in such languages are portablebetween machines.

+ A program written in standard C can be compiled and executed on any computerthat has a standard C compiler.

3.4 4GL: Very High Level Languages

with the generation, programming languages started becoming easier to use and more likenatural languages. However, 4GLs is a little different from its prior generation becausethey are basically non-procedural. When writing code using a procedural language,programmer has to tell the computer how a task is done – add this, compare that, dothis if the condition is true, and so on, in a very specific step-by-step manner.+ A typical example of a 4GL is the query language that allows a user to requestinformation from a database with precisely worded English-like sentences.

6PROGRAMMING with C[ERUCS 105]

8

Page 9: Lecture 1

4 SAMPLE PROGRAMS7

4 Sample Programs

4.1 The While Loop

• In ’C’ programming that user want to do something a fixed number of times.

• The while loop is ideally suited for such cases.

The while loop is suited for following cases.CASE-1:• If you want to calculate gross salaries of ten different persons.CASE-2:• If you you want to convert temperatures from centigrade to fahrenheit for 15 differentcities.Example:

1 /∗ Ca l cu l a t i on o f s imple i n t e r e s t f o r 3 s e t s o f p , n and r ∗/2 #include<s t d i o . h>3 main ( )4 {5 int p , n , count ;6 f loat r , s i ;7 count = 1 ;8 while ( count <= 3 )9 {

10 p r i n t f ( ” Enter va lue s o f p , n and r ” ) ;11 s can f ( ”%d %d %f ” , &p , &n , &r ) ;12 s i=p∗n ∗ r / 100 ;13 p r i n t f ( ” Simple i n t e r e s t = Rs . %f ” , s i ) ;14 count=count +1;15 }16 }

example1.c

Sample Output:

Enter values of p, n and r 1000 5 13.5Simple interest = Rs. 675.000000Enter values of p, n and r 2000 5 13.5Simple interest = Rs. 1350.000000Enter values of p, n and r 3500 5 13.5Simple interest = Rs. 2362.500000

7PROGRAMMING with C[ERUCS 105]

9

Page 10: Lecture 1

4.1 The While Loop 4 SAMPLE PROGRAMS8

Analysis:

• The program executes all statements after the while 3 times.

• The logic for calculating the simple interest is written within a pair of bracesimmediately after the while keyword.

• These statements form what is called the body of the while loop.

• The parentheses after the while contain a condition. So long as this condition remainstrue all statements within the body of the while loop keep getting executed repeatedly.

• To begin with the variable count is initialized to 1 and every time the simple interestlogic is executed the value of count is incremented by one.

• The variable count is many a times called either a loop counter or an index variable.

Syntax

initialise loop counter ;while ( test loop counter using a condition ){do this(Statement-1) ;and this(statement-2) ;increment loop counter ;}

Figure 4: While Loop syntax

8PROGRAMMING with C[ERUCS 105]

10

Page 11: Lecture 1

4.1 The While Loop 4 SAMPLE PROGRAMS9

Figure 5: While Loop Flowchart

Note-I

The statements within the while loop would keep on getting executed till the condi-tion being tested remains true. When the condition becomes false, the control passesto the first statement that follows the body of the while loop.

Note-II

The condition being tested may use relational or logical operators as shown in thefollowing examples:while ( i <= 10 )while ( i >= 10 && j <= 15 )

Note-III

The statements within the loop may be asingle line (or) a block of statements.Examples:while ( i <= 10 )i=i+1;

is same aswhile ( i <= 10 ){ i=i+1;}

9PROGRAMMING with C[ERUCS 105]

11

Page 12: Lecture 1

4.1 The While Loop 4 SAMPLE PROGRAMS10

Note-IV

As a rule while must test a condition that will eventually become false. Otherwisethe loop would be executed forever, indefinitly.main(){int i=1;while ( i <= 10 )printf(”%d”,i);}

The correct form would be as under

main(){int i=1;while ( i <= 10 ){ printf(”%d”,i);i=i+1;}}

Note-IV

Instead of incrementing a loop counter, We can even decrement it and still manageto get the body of the loop executed repeatedly.This is shown below:main( ){int i = 5 ;while (i >= 1 ){printf (”Make the computer literate” ) ;i = i - 1 ;}}

Note-VIt is not necessary that a loop counter must only be an int. It can even be a float.main( ){float a = 10.0 ;while (a <= 10.5 ){printf ( ” addition”);a = a + 0.1 ;}}

10PROGRAMMING with C[ERUCS 105]

12

Page 13: Lecture 1

5 REVIEW QUESTIONS11

5 Review Questions

5.1 Long answer Questions

+ Differentiate between computer hardware and software?

+ Explain the role of Operating System?

+ Write a short notes on computer?

+ What are the steps involved in algorithm design?

+ What are the popular features of C language?

5.2 Short answer Questions

+ What is system software?

+ What is a program?

+ What is booting?

+ What is ALU?

+ What is a micro processor?

11PROGRAMMING with C[ERUCS 105]

13

Page 14: Lecture 1

6 SNIPPET PROGRAMS12

6 Snippet Programs

What would be the output of the following programs

(a) main( ){

void slogan( ) ;int c = 5 ;c = slogan( ) ;printf ( ”% d”, c ) ;}void slogan( ){printf ( ”Only He men use C!” ) ;}

(b) main( ){

void slogan( ) ;int c = 5 ;c = slogan( ) ;printf ( ”% d”, c ) ;}void slogan( ){printf ( ”Only He men use C!” ) ;}

(c) main( ){

void slogan( ) ;int c = 5 ;c = slogan( ) ;printf ( ”% d”, c ) ;}void slogan( ){printf ( ”Only He men use C!” ) ;}

12PROGRAMMING with C[ERUCS 105]

14