Lecture 1

212 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
212
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 1

  1. 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, Rudraram HYDERABAD-502329 JULY-NOV 2013
  2. 2. CONTENTS LIST OF TABLES Contents 1 Computers 3 1.1 Input Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Arithmetic Logic Unit(ALU) . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Control Unit(CU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Memory Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 Output Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 Classification of computer software . . . . . . . . . . . . . . . . . . . . . 5 2 Classification of Computers 6 3 Generation of Programming Languages 7 3.1 First Generation: Machine Language . . . . . . . . . . . . . . . . . . . . 7 3.2 Second Generation: Assembly Language . . . . . . . . . . . . . . . . . . 7 3.3 3GL: HLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.4 4GL: Very High Level Languages . . . . . . . . . . . . . . . . . . . . . . 8 4 Sample Programs 9 4.1 The While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5 Review Questions 13 5.1 Long answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5.2 Short answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 6 Snippet Programs 14 List of Figures 1 Parts of Computer System . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Hardware blocks of a computer . . . . . . . . . . . . . . . . . . . . . . . 4 3 Relationship between hardware and software . . . . . . . . . . . . . . . . 5 4 While Loop syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 5 While Loop Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 List of Tables 1 Generations of Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2
  3. 3. 1 COMPUTERS 1 1 Computers The Computer is a programmable finite state machine which can perform precise arithmetic and logic operations.
  4. 4. A programmable finite state machine is one that can take one of a fixed range of values. The computer is said to be programmable because it can instructed to perform 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 known for. Second is the computer software that commands the hardware what to do and how to 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 related programs is referred to as the software. The physical circuitry and components are known 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. The data read from the input device is stored in the computer’s memory.
  5. 5. Some common input devices are: Keyboard, mouse, joystick, light pen, etc. 1 PROGRAMMING with C[ERUCS 105] 3
  6. 6. 1.2 Arithmetic Logic Unit(ALU) 1 COMPUTERS 2 1.2 Arithmetic Logic Unit(ALU) The ALU performs arithmetic and logic operations. The arithmetic operations include addition, 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 processing Unit(CPU). The CPUs are called Microprocessor. The speed of a computer depends on the clock frequency of the microprocessor.
  7. 7. 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. The computer memory is constructed out of semi-conducting material and stores information in binary form. Binary information is composed of two symbols o and 1, called binary digits(bits). All information within the computer is represented by two digits. The memory 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.
  8. 8. The memory of a computer can be divided into distinct parts. They are: • Registers • Internal Cacahe • External Cache • Main memory • Secondary memory 2 PROGRAMMING with C[ERUCS 105] 4
  9. 9. 1.5 Output Unit 1 COMPUTERS 3 1.5 Output Unit Just s input devices are used to supply the computer with data, there should be some means for the computer to communicate with the user. The information generated by the computer is displayed using an output device.
  10. 10. Some examples of output devices are: Cathod Ray Tube(CRT), Printer, Card punch, plotters, etc. 1.6 Classification of computer software Computer software can be brodely classified into two groups: System software and application software. • System software provides a general programming environment in which programmers can create specific applications to suite their needs. This environment provides new functions that are not available at the hardware level and performs tasks related to executing the application program. • Application software is designed to solve a particular problem for users. It is generally what we think of when we say the word computer programs.
  11. 11. Examples of application software include spreadsheets, database systems, desktop publishing systems, program development software, etc. Figure 3: Relationship between hardware and software 3 PROGRAMMING with C[ERUCS 105] 5
  12. 12. 2 CLASSIFICATION OF COMPUTERS 4 2 Classification of Computers The Abacus was invented around 2500 BC. It is a device made of beads strung on rods and used to perform simple computations. Napier invented a device for computing logarithms of natural numbers. It was called Napier’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 engine in 1833 AD. Computers can be classified into five generations, based on the technology and performance and is shown in Table 1. Table 1: Generations of Computer Generation Year Hardware Software Performance Zero Pre 1946 Counter wheels with mechanical components − − −− Numerical op- erations at slow speed First 1946-1956 Vacuum Tubes Stored Pro- grams, Machine Code, Symbolic code 2 KB Memory, 10 Kilo instructions per 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- Conductor memory PASCAL, C, etc 2 MB Memory, 5 Million In- structions Per Second(MIPS) Fourth 1982-1989 VLSI OOPs Lan- guages: C++, etc 32 KB Memory, 30 Million In- structions Per Second(MIPS) Fifth 1990- NOW Parallel Process- ing JAVA, .NET, etc 1 Giga to 1 Tera in- structions per sec- ond 4 PROGRAMMING with C[ERUCS 105] 6
  13. 13. 3 GENERATION OF PROGRAMMING LANGUAGES 5 3 Generation of Programming Languages A Programming language is a language specially designed to express computations that can be performed by the computer. programming languages are used to create programs that control the behaviour of a system, to express algorithms, or as a mode of human-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 than others, and each claiming to be the best. However, back in 1940s when computers were being developed there was just one language-the machine language. The concept of generations of programming languages (also known as levels), is closely connected to the advances, in technology that brought about computer generations. The four 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 expressed using 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 machine language is difficult to learn and is far more difficult to edit if errors occur. Moreover, if you 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 memory to 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 to represent machine-language instructions. These languages are closely connected to ma- chine language and the internal architecture of the computer system on which they are used. Since they are close to the machine, assembly language is also called low-level language. Assembly language developed in the mid 1950s was a great leap forward. It used 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 for compare, MUL for multiply, etc. 5 PROGRAMMING with C[ERUCS 105] 7
  14. 14. 3.3 3GL: HLL 3 GENERATION OF PROGRAMMING LANGUAGES 6 3.3 3GL: HLL A third generation programming language(3GL) is a refinement of the second generation programming language. The 2GL languages brought logical structure to the software. The third generation language was introduced to make the languages more programmer friendly. 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 one statement would generate one machine language instruction. 3GLs made programming easier, efficient, and less prone to error. 3GL make it easier to write and debug a program and gives the programmer more time to think about its overall logic. The programs written in such languages are portable between machines.
  15. 15. A program written in standard C can be compiled and executed on any computer that has a standard C compiler. 3.4 4GL: Very High Level Languages with the generation, programming languages started becoming easier to use and more like natural languages. However, 4GLs is a little different from its prior generation because they 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, do this if the condition is true, and so on, in a very specific step-by-step manner.
  16. 16. A typical example of a 4GL is the query language that allows a user to request information from a database with precisely worded English-like sentences. 6 PROGRAMMING with C[ERUCS 105] 8
  17. 17. 4 SAMPLE PROGRAMS 7 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 different cities. Example: 1 /∗ Calculation of simple i n t e r e s t for 3 s e t s of p , n and r ∗/ 2 #includestdio . h 3 main( ) 4 { 5 int p , n , count ; 6 float r , s i ; 7 count = 1; 8 while ( count = 3 ) 9 { 10 p r i n t f ( ” Enter values of p , n and r ” ) ; 11 scanf ( ”%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.5 Simple interest = Rs. 675.000000 Enter values of p, n and r 2000 5 13.5 Simple interest = Rs. 1350.000000 Enter values of p, n and r 3500 5 13.5 Simple interest = Rs. 2362.500000 7 PROGRAMMING with C[ERUCS 105] 9
  18. 18. 4.1 The While Loop 4 SAMPLE PROGRAMS 8 Analysis: • The program executes all statements after the while 3 times. • The logic for calculating the simple interest is written within a pair of braces immediately 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 remains true 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 interest logic 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 8 PROGRAMMING with C[ERUCS 105] 10
  19. 19. 4.1 The While Loop 4 SAMPLE PROGRAMS 9 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 passes to 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 the following 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 as while ( i = 10 ) { i=i+1; } 9 PROGRAMMING with C[ERUCS 105] 11
  20. 20. 4.1 The While Loop 4 SAMPLE PROGRAMS 10 Note-IV As a rule while must test a condition that will eventually become false. Otherwise the 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 manage to 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-V It 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 ; } } 10 PROGRAMMING with C[ERUCS 105] 12
  21. 21. 5 REVIEW QUESTIONS 11 5 Review Questions 5.1 Long answer Questions
  22. 22. Differentiate between computer hardware and software?
  23. 23. Explain the role of Operating System?
  24. 24. Write a short notes on computer?
  25. 25. What are the steps involved in algorithm design?
  26. 26. What are the popular features of C language? 5.2 Short answer Questions
  27. 27. What is system software?
  28. 28. What is a program?
  29. 29. What is booting?
  30. 30. What is ALU?
  31. 31. What is a micro processor? 11 PROGRAMMING with C[ERUCS 105] 13
  32. 32. 6 SNIPPET PROGRAMS 12 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!” ) ; } 12 PROGRAMMING with C[ERUCS 105] 14

×