COMPUTER PROGRAMMING LECTURE NOTES, Study notes of Computer Programming

COMPUTER PROGRAMMING. LECTURE NOTES. Year. : 2016 - 2017. Subject Code. : ACS001. Regulations. : R16. Class. : I B.Tech. Branch. : CSE/ ECE/ IT/ EEE.

Typology: Study notes

2021/2022

Uploaded on 08/01/2022

fioh_ji
fioh_ji 🇰🇼

4.5

(70)

814 documents

1 / 171

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
COMPUTER PROGRAMMING
LECTURE NOTES
Year : 2016 - 2017
Subject Code : ACS001
Regulations : R16
Class : I B.Tech
Branch : CSE/ ECE/ IT/ EEE
Team of Instructors : Dr. K Srinivasa Reddy, Professor, CSE,
Dr. G Ramu, Professor, CSE,
Ms. B Padmaja, Associate Professor, CSE,
Ms. P. Ila Chandana Kumari, Associate Professor, IT,
Ms. K. Laxmi Narayanamma, Associate Professor, IT,
Ms. B Rekha, Assistant Professor, IT
Prepared By : Ms. P. Ila Chandana Kumari, Associate Professor, IT
INSTITUTE OF AERONAUTICAL ENGINEERING
(Autonomous)
Dundigal, Hyderabad - 500 043
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download COMPUTER PROGRAMMING LECTURE NOTES and more Study notes Computer Programming in PDF only on Docsity!

COMPUTER PROGRAMMING

LECTURE NOTES

Year : 2016 - 2017

Subject Code : ACS

Regulations : R

Class : I B.Tech

Branch : CSE/ ECE/ IT/ EEE

Team of Instructors : Dr. K Srinivasa Reddy, Professor, CSE, Dr. G Ramu, Professor, CSE, Ms. B Padmaja, Associate Professor, CSE, Ms. P. Ila Chandana Kumari, Associate Professor, IT, Ms. K. Laxmi Narayanamma, Associate Professor, IT, Ms. B Rekha, Assistant Professor, IT

Prepared By : Ms. P. Ila Chandana Kumari, Associate Professor, IT

INSTITUTE OF AERONAUTICAL ENGINEERING

(Autonomous) Dundigal, Hyderabad - 500 043

UNIT-I

INTRODUCTION TO COMPUTERS

COMPUTER SYSTEMS

―A Computer is an electronic device that stores, manipulates and retrieves the data.‖ We can also refer computer computes the information supplied to it and generates data. A System is a group of several objects with a process. For Example: Educational System involves teacher, students (objects). Teacher teaches subject to students i.e., teaching (process). Similarly a computer system can have objects and process. The following are the objects of computer System a) User ( A person who uses the computer) b) Hardware c) Software Hardware: Hardware of a computer system can be referred as anything which we can touch and feel. Example : Keyboard and Mouse. The hardware of a computer system can be classified as Input Devices(I/P) Processing Devices (CPU) Output Devices(O/P)

ALU: It performs the Arithmetic and Logical Operations such as +,-,*,/ (Arithmetic Operators) &&, || ( Logical Operators) CU: Every Operation such as storing , computing and retrieving the data should be governed by the control unit.

KEYBOARD

INPUT

ALU, CU, MU CPU

MONITOR

OUTPUT

The operating system provides services such as a user interface, file and database access, and interfaces to communication systems such as Internet protocols. The primary purpose of this software is to keep the system operating in an efficient manner while allowing the users access to the system. System support software provides system utilities and other operating services. Examples of system utilities are sort programs and disk format programs. Operating services consists of programs that provide performance statistics for the operational staff and security monitors to protect the system and data. The last system software category, system development software , includes the language translators that convert programs into machine language for execution ,debugging tools to ensure that the programs are error free and computer – assisted software engineering(CASE) systems.

Application software Application software is broken in to two classes: general-purpose software and application – specific software. General purpose software is purchased from a software developer and can be used for more than one application. Examples of general purpose software include word processors, database management systems ,and computer aided design systems. They are labeled general purpose because they can solve a variety of user computing problems. Application – specific software can be used only for its intended purpose. A general ledger system used by accountants and a material requirements planning system used by a manufacturing organization are examples of application-specific software. They can be used only for the task for which they were designed they cannot be used for other generalized tasks. The relationship between system and application software is shown below. In this figure, each circle represents an interface point .The inner core is hard ware. The user is represented by the out layer. To work with the system, the typical user uses some form of application software. The application software in turn interacts with the operating system, which is a part of the system software layer. The system software provides the direct interaction with the hard ware. The opening at the bottom of the figure is the path followed by the user who interacts directly with the operating system when necessary.

COMPUTING ENVIRONMENTS

The word ‗compute‘ is used to refer to the process of converting information to data. The advent of several new kinds of computers created a need to have different computing environments. The following are the different kinds of computing environments available

  1. Personal Computing Environment
  2. Time Sharing Environment
  3. Client/Server Environment
  4. Distributed Computing Environment Personal Computing Environment In 1971, Marcian E. Hoff, working for INTEL combined the basic elements of the central processing unit into the microprocessor. If we are using a personal computer then all the computer hardware components are tied together. This kind of computing is used to satisfy the needs of a single user, who uses the computer for the personal tasks. Ex: Personal Computer

Time-Sharing Environment The concept of time sharing computing is to share the processing of the computer basing on the criteria time. In this environment all the computing must be done by the central computer.

Example: Ebay on Internet

COMPUTER LANGUAGES To write a program (tells what to do) for a computer, we must use a computer language. Over the years computer languages have evolved from machine languages to natural languages. The following is the summary of computer languages 1940‘s -- Machine Languages 1950‘s -- Symbolic Languages 1960‘s -- High Level Languages Machine Language In the earliest days of computers, the only programming languages available were machine languages. Each computer has its own machine language which is made of streams of 0‘s and 1‘s. The instructions in machine language must be in streams of 0‘s and 1‘s. This is also referred as binary digits. These are so named as the machine can directly understood the programs Advantages:

  1. High speed execution
  2. The computer can understood instructions immediately
  3. No translation is needed. Disadvantages:
  4. Machine dependent
  5. Programming is very difficult
  6. Difficult to understand
  7. Difficult to write bug free programs
  8. Difficult to isolate an error

Example Additon of two numbers 2  0 0 1 0

  • 3  0 0 1 1

5  0 1 0 1


Symbolic Languages (or) Assembly Language In the early 1950‘s Admiral Grace Hopper, a mathematician and naval officer, developed the concept of a special computer program that would convert programs into machine language. These early programming languages simply mirrored the machine languages using symbols or mnemonics to represent the various language instructions. These languages were known as symbolic languages. Because a computer does not understand symbolic language it must be translated into the machine language. A special program called an Assembler translates symbolic code into the machine language. Hence they are called as Assembly language. Advantages:

  1. Easy to understand and use
  2. Easy to modify and isolate error
  3. High efficiency
  4. More control on hardware Disadvantages:
  5. Machine Dependent Language
  6. Requires translator
  7. Difficult to learn and write programs
  8. Slow development time
  9. Less efficient Example: 2 PUSH 2,A 3 PUSH 3,B
  • ADD A,B 5 PRINT C

c=a+b; printf("%d",c); }

Difference between Machine, Assembly, High Level Languages Feature Machine Assembly High Level Form 0‘s and 1‘s Mnemonic codes Normal English Machine Dependent Dependent Dependent Independent Translator Not Needed Needed(Assembler) Needed(Compiler) Execution Time Less Less High Languages Only one Different Manufactgurers Different Languages Nature Difficult Difficult Easy Memory Space Less Less More

Language Translators These are the programs which are used for converting the programs in one language into machine language instructions, so that they can be excuted by the computer.

  1. Compiler: It is a program which is used to convert the high level language programs into machine language
  2. Assembler: It is a program which is used to convert the assembly level language programs into machine language
  3. Interpreter: It is a program, it takes one statement of a high level language program, translates it into machine language instruction and then immediately executes the resulting machine language instruction and so on. Comparison between a Compiler and Interpreter COMPILER INTERPRETER A Compiler is used to compile an entire program and an executable program is generated through the object program

An interpreter is used to translate each line of the program code immediately as it is entered

The executable program is stored in a disk for future use or to run it in another computer

The executable program is generated in RAM and the interpreter is required for each run of the program The compiled programs run faster The Interpreted programs run slower Most of the Languages use compiler A very few languages use interpreters.

CREATING AND RUNNING PROGRAMS

The procedure for turning a program written in C into machine Language. The process is presented in a straightforward, linear fashion but you shuld recognize that these steps are repeated many times during development to correct errors and make improvements to the code. The following are the four steps in this process

  1. Writing and Editing the program
  2. Compiling the program
  3. Linking the program with the required modules
  4. Executing the program

Compiling Programs The code in a source file stored on the disk must be translated into machine language. This is the job of the compiler. The Compiler is a computer program that translates the source code written in a high-level language into the corresponding object code of the low-level language. This translation process is called compilation. The entire high level program is converted into the executable machine code file. The Compiler which executes C programs is called as C Compiler. Example Turbo C, Borland C, GC etc., The C Compiler is actually two separate programs: The Preprocessor The Translator The Preprocessor reads the source code and prepares it for the translator. While preparing the code, it scans for special instructions known as preprocessor commands. These commands tell the preprocessor to look for special code libraries. The result of preprocessing is called the translation unit. After the preprocessor has prepared the code for compilation, the translator does the actual work of converting the program into machine language. The translator reads the translation unit and writes the resulting object module to a file that can then be combined with other precompiled units to form the final program. An object module is the code in the machine language.

Linking Programs The Linker assembles all functions, the program‘s functions and system‘s functions into one executable program.

Executing Programs To execute a program we use an operating system command, such as run, to load the program into primary memory and execute it. Getting the program into memory is the function of an operating system program known as the loader. It locates the executable program and

reads it into memory. When everything is loaded the program takes control and it begin execution.

ALGORITHM Algorithm is a finite sequence of instructions, each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time. No matter what the input values may be, an algorithm terminates after executing a finite number of instructions. We represent an algorithm using a pseudo language that is a combination of the constructs of a programming language together with informal English statements. The ordered set of instructions required to solve a problem is known as an algorithm. The characteristics of a good algorithm are:  Precision – the steps are precisely stated (defined).  Uniqueness – results of each step are uniquely defined and only depend on the input and the result of the preceding steps.  Finiteness – the algorithm stops after a finite number of instructions are executed.  Input – the algorithm receives input.  Output – the algorithm produces output.  Generality – the algorithm applies to a set of inputs. Example Q. Write a algorithem to find out number is odd or even? Ans. step 1 : start step 2 : input number step 3 : rem=number mod 2 step 4 : if rem=0 then print "number even" else print "number odd" endif step 5 : stop

FLOWCHART Flowchart is a diagrammatic representation of an algorithm. Flowchart is very helpful in writing program and explaining program to others.

Draw flowchart to find the largest among three different numbers entered by user.

INTRODUCTION TO C LANGUAGE

C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972.

The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons.  Easy to learn  Structured language  It produces efficient programs.  It can handle low-level activities.  It can be compiled on a variety of computers. Facts about C  C was invented to write an operating system called UNIX.  C is a successor of B language which was introduced around 1970  The language was formalized in 1988 by the American National Standard Institue (ANSI).  By 1973 UNIX OS almost totally written in C.  Today C is the most widely used System Programming Language.  Most of the state of the art software have been implemented using C Why to use C? C was initially used for system development work, in particular the programs that make-up the operating system. C was adoped as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:  Operating Systems  Language Compilers  Assemblers  Text Editors  Print Spoolers  Network Drivers  Modern Programs  Data Bases  Language Interpreters  Utilities C Program File

  1. Documentation section: The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.
  2. Link section: The link section provides instructions to the compiler to link functions from the system library such as using the #include directive.
  3. Definition section: The definition section defines all symbolic constants such using the #define directive.
  4. Global declaration section: There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.
  5. main () function section: Every C program must have one main function section. This section contains two parts; declaration part and executable part 1. Declaration part: The declaration part declares all the variables used in the executable part. 2. Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon.
  6. Subprogram section: If the program is a multi-function program then the subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order.

PROCESS OF COMPILING AND RUNNING C PROGRAM We will briefly highlight key features of the C Compilation model here.

The C Compilation Model The Preprocessor The Preprocessor accepts source code as input and is responsible for  removing comments  Interpreting special preprocessor directives denoted by #. For example  #include -- includes contents of a named file. Files usually called header files. e.g o #include <math.h> -- standard library maths file. o #include <stdio.h> -- standard library I/O file  #define -- defines a symbolic name or constant. Macro substitution. o #define MAX_ARRAY_SIZE 100 C Compiler The C compiler translates source to assembly code. The source code is received from the preprocessor. Assembler The assembler creates object code. On a UNIX system you may see files with a .o suffix (.OBJ on MSDOS) to indicate object code files. Link Editor If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file.