Docsity

r16 unit1 computer programming, Exercises of Computer Science

Toggle sidebar
bg1
K.K.R. & K.S.R INSTITUTE OF TECHNOLOGY & SCIENCES DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
1
UNIT-1
Computer:
Computer stands for:
C – Commonly
O- Operating
M- Machine
P- Particularly
U- Used for
T- Technical &
E- Educational
R- Research
A ‘computer’ is a machine that can solve problems by accepting data, performing certain operations &
presenting the results of those operations under the direction of detailed step by step instructions.
A computer is a system made up of two major components:
o Hardware
o Software
The computer hardware is defined as the physical components that are connected to form a computer
system.
The software is the collection of programs/instructions that allow the hardware to do its job/task.
Computer Hardware:
The computer hardware is the physical components that are connected to form a computer system.
The hardware components of a computer system consist of five parts:
Input devices
Central processing unit
Primary storage
Output devices
Secondary/auxiliary storage devices
The input device is usually a keyboard where programs and data are entered into the computer.
The central processing unit is responsible for executing instructions such as arithmetic calculations,
comparisons among data and movement of data inside the system.
Primary memory is also known as main memory or temporary memory, where the programs and data
are stored temporarily during processing.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download r16 unit1 computer programming and more Exercises Computer Science in PDF only on Docsity!

UNIT-

Computer:

 Computer stands for: C – Commonly O- Operating M- Machine P- Particularly U- Used for T- Technical & E- Educational R- Research  A ‘computer’ is a machine that can solve problems by accepting data, performing certain operations & presenting the results of those operations under the direction of detailed step by step instructions.  A computer is a system made up of two major components: o Hardware o Software  The computer hardware is defined as the physical components that are connected to form a computer system.  The software is the collection of programs/instructions that allow the hardware to do its job/task.

Computer Hardware:

 The computer hardware is the physical components that are connected to form a computer system.  The hardware components of a computer system consist of five parts:  Input devices  Central processing unit  Primary storage  Output devices  Secondary/auxiliary storage devices

 The input device is usually a keyboard where programs and data are entered into the computer.  The central processing unit is responsible for executing instructions such as arithmetic calculations, comparisons among data and movement of data inside the system.  Primary memory is also known as main memory or temporary memory, where the programs and data are stored temporarily during processing.

 The data in primary memory are erased when we turn off the personal computer.  The output device is usually a monitor/printer to show output. If the output is shown on the monitor, it is represented as a “soft copy”. If it is printed on the printer, it is represented as a “hard copy”.  Secondary/auxiliary storage is used for both input and output. It is the place where the programs and data are stored permanently.  When we turn-off the computer, the programs and data remain in the secondary storage ready for the next time when we need them.

Computer software:

 The hardware needs software to instruct what has to be done. The set of instructions which control the sequence of operations are known as programs and the set of programs are known as software.  Computer software is divided into two categories:  System software  Application software  System software consists of programs that manage the hardware resources of a computer and perform required information processing tasks.  These programs are divided into three classes: o Operating system o System support software o System development software

Operating system:

 The operating system provides services such as a user interface, file or 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:

 The system support software provides system utilities and other operating services.  Examples of system utilities are sort programs and disk format programs.

System development software:

The 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 that are beyond the scope of this unit.

Application software:

 Application software is broadly classified into two types: o Application-specific software o General-purpose software

Application-specific software:

 The application-specific software can be used only for its intended purpose. They can be used only for the task for which they were designed; they cannot be used for other generalized tasks.  General-purpose software: o The general-purpose software is purchased from a software developer and can be used for more than one application. o Ex: word processors, Database management systems.  The relationship between system and application software is shown below:

 Each number is stored in 1 byte.  To represent the ASCII value in binary form, we need Byte.

Components:

 A computer can perform basically five major operations or functions irrespective of their size.  They are: o Accept input-> both data and instructions. o Display output-> both text and numeric o Store data and instructions o Perform arithmetic and logical operations either on input data or stored data o Monitor, control and direct the overall operation and sequencing of the system  The computer components that support these capabilities collectively form a computer’s hardware. Input devices:  Input devices are used to give the instructions and data to the computer. It is necessary to convert the information or data into a form which can be understood by the computer.  Ex: Keyboard, Mouse, Scanner, Touch Screen, Light pen. Output devices:  Output device is a part of the computer hardware equipment used to display the results of data processing carried out by an information processing system to the outside world.  Ex: Monitor, speakers, printer. Central Processing Unit:  CPU takes all major decisions, makes all sorts of calculations and directs different parts of the computer and functions by activating and controlling the operations.  The CPU has three components: o Control Unit o Arithmetic & Logical Unit o Memory Unit Control Unit:  The control unit controls the overall operations of the computer system. It controls all the other units, directs them to operate in a proper way and coordinates various operations performed. Arithmetic & Logic Unit:  The arithmetic & Logical unit performs two types of operations:  Arithmetic operations  Logical operations  The arithmetic operations include addition, subtraction, multiplication, division and modulo-division.  The logical operations consists of relational and logical operations such as comparison and true or false. Memory Unit:  The memory unit holds the intermediate results during the course of calculations and provides the data as and when required.  Computer memory is divided into two types: o Primary memory o Secondary memory Primary memory:  It stores the data temporarily. The size of primary/main memory is limited in computer.  Ex: RAM Secondary memory:  It stores the data permanently. The data in secondary storage is not directly accessible and has to be routed through the main memory for processing.  Ex: Hard disk, CD/DVD, flash drives etc.

Programming Languages:

A programming language: The language used in the communication of computer instructions is known as the programming language. It is a formal constructed language designed to communicate instructions to a machine, particularly a computer.

Programming languages can be used to create programs to control the behavior of a machine. There are three levels of programming languages are available. They are:

  1. Machine languages (a.k.a Low-level Languages)
  2. Assembly languages (a.k.a Symbolic Languages)
  3. High level languages

Machine Languages:

The machine language is the only language that the computer understands. This is the lowest level of programming language.

All the commands and data values are expressed in 0s and 1s, corresponding to ‘ON’ and ‘OFF’ electrical states.

Advantages of machine language:

The CPU directly understands machine instructions and hence no translations are required.

Because the computer directly starts executing the machine language instructions, it takes less execution time.

Disadvantages of machine language:

Difficult to understand:

It is difficult to understand and develop a program using machine language.

Machine dependent:

The programmer has to remember machine characteristics while preparing a program.

Error prone:

It is hard to understand and remember the various combinations of 1s and 0s representing data and instructions.

This makes it difficult for a programmer to concentrate fully on the logic of the problem thus causing errors.

Difficult to debug and modify:

Checking machine instructions to locate errors is very difficult.

Less Efficient:

The process of high level translation may increase the execution time of the program and programs written in high level languages require more memory space.

Low-level and High-level languages:

Both machine and assembly languages are classified as low-level languages.

Since computers understand only machine language, it is necessary to convert the high level language programs into machine language codes.

For this purpose, translators are used. The two translators that are widely used are:

Compilers Interpreters

Compiler:

A compiler is a program which converts a high level language program/code into binary instructions (machine language) that our computer can interpret, understand.

Interpreter:

An Interpreter is a program which translates statements of a program into machine code. It translates only one statement of the program at a time.

Programming paradigms:

Programming paradigms are the different patterns and models for writing a program.

Basically, programming paradigms are classified as:

Structured programming

Object-oriented programming

Aspect-oriented programming

In olden days, the unstructured style of programming was used, where all actions of a small and

simple program were defined within a single program only.

It is difficult to write and understand a long and complex program using unstructured programming.

Structured programming:

Structured programming involves building programs using small modules. These modules are easy

to read and write.

In structured programming, the problem to be solved is broken down into small tasks that can be

written independently.

Once written, the small tasks are combined together to form the complete task.

Structured programming can be performed in two ways:

Procedural programming

Modular programming

Procedural or procedure-oriented programming:

It requires the given task to be divided into smaller procedures, functions or sub-routines.

A procedural program is largely a ‘single file’ consisting of many procedures and functions including

main() function.

A procedure or a function performs a specific task. The main() function integrates the procedures

and functions by making calls to them in an order that implements the functionality of the program.

When a procedure/function is called, the execution control jumps to the called procedure or

function, the procedure/function is executed and after execution, the control comes back to the

calling procedure/function.

Modular programming:

Modular programming requires breaking down of a program into a group of files, where each file

consists of a program that can be executed independently.

In a modular program, the problem is divided into different independent but related tasks.

For each identified task, a separate program (module) is written, which is a program file that can be

executed independently.

The different files of the program are integrated using a main program file.

The main program file invokes the other files in an order that fulfills the functionality of the problem.

Ex: C, COBOL and PASCAL are examples of structured programming languages.

Object-oriented programming:

In object-oriented programming, subroutines as well as data are locally defined in objects.

Objects are self-contained modules that contain data as well as the functions needed to manipulate

the data within the same module.

The most important object-oriented features are:

Abstraction

Encapsulation and data hiding

Polymorphism

Inheritance

Reusable code

OOP focuses on developing the software based on their component objects.

The components interact with each other to provide the functionality of the software.

The terms that are commonly associated with OOP are as follows:

Class

Object

Ex: C++ and Java are Object-Oriented programming languages.

Aspect oriented programming:

Aspect-oriented programming is a new programming paradigm that handles the cross-cutting

concerns of the software.

It helps the programmer in clearly separating the core concerns and the cross-cutting concerns of

the software.

Aspect oriented programming introduces a new modular unit called “aspect” that encapsulates the

functionality of the cross cutting concerns.

At compile time, the classes of object-oriented programs and the aspects are combined into a final

executable form using an ‘aspect weaver’.

Ex: AspectJ and AspectC are examples are examples of Aspect Oriented Programming languages.

Algorithm:

An algorithm is a step by step procedure to solve a given problem.

(or)

An algorithm is a finite set of instructions that can be executed by a computer to produce desired result.

An algorithm has five important features: Input: An algorithm has certain inputs, which are specified to it initially, before the execution of the algorithm begins.

Flowchart:

Graphical or pictorial representation of algorithm is known as Flowchart. A flowchart is a picture, which shows the sequence in which data are read, computing is performed, decisions are made and results are obtained.

The symbols involved in flowchart are:

All symbols are connected among themselves to indicate the flow of information and processing. Flowcharts are classified into 3 types: Sequential flowchart Conditional flowchart or selection flowchart Iterative or Repetitive flowchart In a sequential flowchart, the steps are executed in linear order one after the other. In conditional flowchart, the step to be executed next is based on a decision taken. In iterative flowchart, some steps are executed repetitively until they fulfill some condition to exit from the repetition.

Flowcharts have their own benefits. However, they have some limitations too.

A complex and long flowchart may run into multiple pages, which become difficult to understand and follow.

Moreover, updating a flowchart with the changing requirements is a challenging job.

The flowchart should be clear, neat and easy to follow.

The usual direction of the flow of a procedure or system is from left to right or top to bottom.

Once the flowchart is drawn, it becomes easy to write the program in any high-level language.

Often flowcharts are helpful in explaining the program to others.

Comparison between flow chart and algorithm S. No. Algorithm Flow chart An algorithm is a finite set of step- Flowchart is a pictorial or symbolic 1 by-step instructions that solve a representation of an algorithm. problem

2

Algorithm gives verbal which is Gives pictorial representation.

almost similar to English language.

3

Suitable for large and modular Suitable for small programs.

programs

4

Easier to understand To understand flowcharts one has to

familiar with symbols. 5 Drawing tools are not required. Required.

6

Algorithm can Be typed so Flowcharts cannot be typed. So

reproduction is easy. reproduction is a problem.

7

It is independent of programming We have to use predefined standard

languages. symbols only. The Software Development Process or Software Development Life Cycle (SDLC):  Software Development Life Cycle is a process used by software industry to design, develop and test high quality softwares. It is also called as Software development process.  The software development life cycle (SDLC) is a framework defining tasks performed at each step in the software development process. What is SDLC?  SDLC is a process followed for a software project, within a software organization.  It consists of a detailed plan describing how to develop, maintain, replace and alter or enhance specific software.  The life cycle defines a methodology for improving the quality of software and the overall development process.  The following figure is a graphical representation of the various stages of a typical SDLC.

 The following are the six phases in every Software development life cycle model:

  1. Requirement gathering and analysis
  2. Design
  3. Implementation or coding
  4. Testing
  5. Deployment
    1. Maintenance Stage1: Requirement gathering and analysis  Business requirements are gathered in this phase.  Meetings with managers and users are held in order to determine the requirements like;