chapter 5 - welcome to publishererperp.techmaxbooks.com/uploadedfiles/samplechapter/g... · web...

30
Computer Programming & Utilization (GTU) 1-1 Introduction to Computer Programming Chapter 1 Introduction to Computer & Programming Syllabus : Programming Languages : Machine language, Assembly language, High level language, Compilers and interpreters Problem Solving using computers : Algorithms and flowcharts 1.1 Introduction : GTU - June 2010 . A Computer is divided into 2 categories - software and hardware. Hardware is the physical component of the computer, where as software is the logical component of the computer. The computer is intelligent device, where intelligence depends on the software. The comparison between hardware and software is given below. Hardware Vs Software Sr. No. Hardware Software 1. It is a physical component of the computer. It is logical component of the computer system. 2. User can see and feel the hardware components. User cannot see the logical components but can feel it. 3. Hardware is divided into 3 part that are microprocessor, mother board, and peripherals. Software is divided into two categories that are system software and application

Upload: doanque

Post on 08-May-2018

220 views

Category:

Documents


4 download

TRANSCRIPT

Computer Programming & Utilization (GTU) 1-1 Introduction to Computer Programming

Chapter 1

Introduction to Computer & Programming

Syllabus : Programming Languages : Machine language, Assembly language, High level language, Compilers and interpretersProblem Solving using computers : Algorithms and flowcharts

1.1 Introduction : GTU - June 2010.

A Computer is divided into 2 categories - software and hardware. Hardware is the physical component of the computer, where as software is the logical component of the computer. The computer is intelligent device, where intelligence depends on the software. The comparison between hardware and software is given below. Hardware Vs Software

Sr. No.

Hardware Software

1. It is a physical component of the computer.

It is logical component of the computer system.

2. User can see and feel the hardware components.

User cannot see the logical components but can feel it.

3. Hardware is divided into 3 part that are microprocessor, mother board, and peripherals.

Software is divided into two categories that are system software and application software.

4. Hardware is the electronics components. So it is designed by the electronics engineer.

Software is the collection of programs. So it is designed by computer engineer.

5. For maintenance of the computer hardware , screw driver, tester, brush is required.

For maintenance of the computer software , testing software using set of input and output is required. Or they

Computer Programming & Utilization (GTU) 1-2 Introduction to Computer Programming

Sr. No.

Hardware Software

follow white box testing and Black box testing procedures.

6. For design of hardware, it requires special software(chip designing software).

For design of the software, programmer requires set of system software.

7. Hardware is like car. Software is like driver.8. Example are keyboard mouse, screen

,CPU , light pen, printer etcSoftware are Microsoft word, paint brush , loader, linker etc.

1.1.1 Computer Hardware :

GTU - June 2009, Jan. 2010, June 2010.It includes all physical components which are touchable and sensible. It is mainly

divided into 4 parts - CPU, input device, output device and auxiliary memory. CPU is the Central Processing Unit, which contains electronics circuits and used to

execute instructions and also controls the operations of other input output devices. CPU is again divided into three parts ALU, CU, and memory . ALU (Arithmetic and Logical Unit) performs arithmetical operations like addition,

subtraction, multiplication, division as well as logical operations like AND, OR,NOT operations.

Computer Programming & Utilization (GTU) 1-3 Introduction to Computer Programming

Fig. 1.1 : Block diagram of the computer Hardware system

CU (Control Unit) controls each and every activity of processing of all connected devices. It acts as a brain. Its main function is to fetch instructions from main memory and execute it. Normally it executes instructions sequentially. But again this depends on specified instructions.

Memory is also called as a main memory or primary memory or internal memory. It is mainly used to store instructions or information in the form of zero’s and one’s. Control unit fetches instructions from the main memory. This memory is also called as a semiconductor memory because it is constructed by the semiconductor material like silicon. This memory is again divided into various types that are RAM, ROM, PROM, EPROM, EEPROM.

o RAM (Random Access Memory) can hold data till power is on. So it is called as a volatile memory (that means temporary memory).

o ROM (Read Only Memory) can hold data permanently. Ordinary user can not store his information into the ROM only manufacturer of the ROM chip can store data into it. It consist of programs which can control IO devices (BIOS), booting procedures, programs of some mathematical functions like sin, cos, tan etc. So it is called as a non volatile memory (that means permanent memory).

o PROM(Programmable Read Only Memory) can hold data permanently. Programmer can store his information into the PROM but only once. After that modification is not allowed. So it is called as a non volatile memory (that means permanent memory).

o EPROM (Erasable Programmable Read Only Memory) can hold data permanently. User can store his information into the EPROM but only once. After that bit modification is not allowed. User can delete all the contents of the chip using ultra violet rays and again rewrite information on the EPROM chip. So it is called as a non volatile memory (that means permanent memory).

o EEPROM (Electrically Erasable Programmable Read Only Memory) can hold data permanently. User can store his information into the EEPROM and bit modification is allowed. So it is called as a non volatile memory (that means permanent memory). But till now it is not used as a secondary memory because of its cost.

CPU accepts information from user through input device. There are many input devices like keyboard, mouse, mice, tracker ball, touch screens etc.

CPU conveys information to user through output device. There are many output devices like monitor, LCD, printers, plotters, speakers etc.

Computer Programming & Utilization (GTU) 1-4 Introduction to Computer Programming

Auxiliary memory is also called as a secondary memory or external memory or magnetic memory. This is permanent memory. User can store data (programs or documents or pictures etc.) permanently on this type of memory. It can be modified. There are many types of secondary memory. That are hard disk, floppy disk, CDROM, pen drive etc.

1.1.2 Computer Software : GTU - June 2009.

Program is the collection of instructions, which performs particular task, and collection of programs which accomplishes application (that means number of tasks) is called as a software. This software is classified into two categories - System software and Application software. System software directly interacts with computer hardware, whereas Application software is only designed to solve user problems as per users requirement. Following block diagram describes the roll of computer software and hardware.

Fig. 1.2 : Layers of the computer software

System Software :

System software is classified into 3 categories : (1) Operating system (2) System support software and (3) System development software.

Operating system manages hardware as well as interacts with user, and provides different services to users. There are many operating systems like DOS, Windows-XP,VISTA, LINUX etc.

System support software manages hardware more efficiently, for example drivers of the IO devices or antivirus software’s

Computer Programming & Utilization (GTU) 1-5 Introduction to Computer Programming

o Drivers of the IO device consists of interfacing programs which can interacts with IO device. Every IO devices has its own driver programs.

o Viruses are the programs which can start misuse of the resources thus there is mismanagement in the system. Virus infected systems lose their data as well as systems start doing some illegal operations.

o Antivirus programs remove viruses from the systems and some time recover systems from major data loss.

System development software supports programming development environment to user. It include editor, pre-processors, compiler, interpreter, loader etc.o Editor is the software , which is used to create programs as well as it is used for

modification of existing programs.o Pre-processor is the software which is used before translators (that means

compiler or editors) , which is used to replace some segments of code with some another segment. It is also called as an expansion.

o Compilers are the software which can translate high level programs into low level programs.

o Interpreters are the software which can translate line by line high level programs into low level programs.

o Loaders are the software which can load object codes into the main memory and execute it.

Compilers Vs Interpreters

Sr. No.

Interpreter Compiler

1. Translates and executes line by line of source code.

Translates whole source code into object code.

2. User can see the result of program at translation time.

User cannot see the result of program at translation time.

3. Interpreter is suitable for beginners. Compiler is not suitable for beginners.4. It requires more memory because at the

time of translation it places object code into main memory and executes it and produce the result.

It requires less main memory because at the time of translation it place object code into secondary memory. Execution is not the job of compiler. This is the job of loader.

5. At first time the process of getting the results from source code is much faster than compilation because it not require loading time it requires only translation time.

At first time the process of getting the results from source code is slower than interpretation because it requires loading time for loading object code from secondary device to main memory for execution.

6. It cannot produce any object code. Thus, it needs translation process for every execution.

Once object code is created then does not require translation, it only requires to load object code into the memory. That is loading process.

7. So this process is lengthy and time consuming.

This process is faster than interpreter.

Computer Programming & Utilization (GTU) 1-6 Introduction to Computer Programming

Sr. No.

Interpreter Compiler

8. Debugging facilities can be easily provided by the interpreter.

When compiler provides debugging facility then logic becomes more complicated.

9. Size of Interpreter program is larger than compiler because it contain two tasks translation as well as loading.

Size of compiler program is smaller than interpreter because it contain only translation procedure.

10. Now a days, there are two types of interpreter that is pure interpreter and impure interpreter. Impure interpreter becomes more popular because of their portability.

Here portability can be achieved through bootstrapping process i.e. cross compilation of the object code.

11. Interpreters easily supports dynamic allocation of memory.

Compiler easily supports static allocation of memory.

12. Examples : Basic interpreter, P code interpreter.

Example : javac, gcc etc.

Application Software :

Application software is classified into two categories : General purpose software and Special purpose software.

General purpose software is used for many number of tasks, and provides many features for example word processor, oracle, excel etc.

Special purpose software is designed for specific purposes only. User programs comes under special purpose software for example pay roll system for specific company, tax calculation software etc.

Comparison of System Software with Application Software

Sr. No.

System software Application software

1. Is primarily concerned with the efficient management of the computer system.

Is primarily concerned with the solution of some problems, using the computer as a tool.

2. However system programs are intended to support the operation and use of the computer itself, rather than any particular application.

The focus is on the application, not on the computing system.

3. Is usually related to the architecture of the machine on which that is to run.system programmer should have knowledge of computer architecture.

Is usually related to the detailed knowledge of the Language and environment. So that Application programmer should have detailed knowledge of high level programming language which is used to develop application

Computer Programming & Utilization (GTU) 1-7 Introduction to Computer Programming

Sr. No.

System software Application software

program and environment i.e. operating system on which they are to run.

4. Is machine dependent. Is machine independent.5. Used to develop new system programs

and using Bootstrapping we can make them portable.

Used to develop new application programs and using cross compiler i.e. again system programs, we can make them portable.

6. Examples of system software are Assembler, linker, loader, compiler etc.

Examples of Application software are Paint, Photoshop, Microsoft access etc.

Computer Programming & Utilization (GTU) 1-8 Introduction to Computer Programming

1.2 Computer Languages :

Language is the media of communication. Natural languages contains many words with specific meaning and sentences with grammatical rules. Languages are used on computer system called as computer languages. Computer languages are divided into 2 categories - low level language, and high level language. Low level language is further divided into 2 types - machine language and assembly language.

1.2.1 Machine Language :

Computer directly understands this language. It is the language of 0’s and 1’s (which is also called as a binary number system i.e. binary pattern). Every instruction consists of order, data, address and all are in terms of 0’s and 1’s, thus it is very difficult to understand as well as remember.

1.2.1.1 Advantages of Machine Level Language :

The programs are compact and small. The programs are stored in very less memory. Programmer can take care of each

and every bits and bytes. Once programs are entered into main memory computer can directly execute it

without using any translator. Suitable for low volume application, small simple program where the prototype is the

final product. It does not require any extra system program to run(like assembler).

1.2.1.2 Disadvantages of Machine Level Language :

The programs are difficult to understand or debug. (Address, instruction, and operands all are in machine language i.e. in binary pattern)

The programs are slow to enter since you must enter each bit individually The programs do not describe the task, which you want to perform in anything

resembling a human readable format. The programs are long and tiresome to write. The programmer often makes careless error. Which are subsequently very difficult to

find (binary pattern all look the same.) Programmer must have detailed knowledge of the architecture of particular

microprocessor. Machine language program are not portable.1.2.2 Assembly Language :

Every machine language instruction is assigned to English word mnemonic such that it should describe function of instruction. Such language of mnemonics is called as Assembly language. Programs written in the assembly languages is called as an assembly language programs. Computer requires assembler to translate assembly level programs into machine level and then only it is become executable.

Computer Programming & Utilization (GTU) 1-9 Introduction to Computer Programming

1.2.2.1 Advantages of Assembly Level Language :

The assembly language instruction is called as mnemonic or memory jogger, which should describe in some way what the instruction does. (if we compare it with machine instruction.)

The programs are very easy to understand or debug. The programs are fast to enter using alphanumeric keys of keyboard.

1.2.2.2 Disadvantages of Assembly Level Language :

Assembly language programs require assembler to translate assembly language program to machine level.

Programmer must have detailed knowledge of the architecture of particular microprocessor.

Programmers need to learn structure of assembly language program and syntax of every statement.

It requires assembler and loader for translation Assembly language programs are not portable.

1.2.3 High level Language :

This language contains set of statements with each having their own syntactical (grammatical) rules. It is very similar to natural languages. Thus it is very easy for program development purpose. They requires set of system program for the execution of high level language program.

Example :- C, C++, JAVA, .NET (VB.Net, ASP.NET, C#.Net)

1.2.3.1 Advantages of High Level Language :

The High level instructions are easy sentences so it is very easy to learn. (if we compare it with machine instruction.)

Not need to learn computer architecture. The programs are very easy to understand or debug. The programs are fast to enter using alphanumeric keys of keyboard. The programs are portable.

1.2.3.2 Disadvantages of High Level Language :

High level language program requires compiler or interpreter to translate high level language program to machine level that means object code.

Object code needs loader to load code into the main memory and execute that program.

Programmers need to learn structure of high level language program and syntax of every statement.

Computer Programming & Utilization (GTU) 1-10 Introduction to Computer Programming

Some high level languages are environment dependent (That means dependent on operating system.)

1.3 System Development Life Cycle :

Computer only understands the machine language, it is very difficult to write down the programs, so now a days only high level languages are used to develop a program. But, directly writing programs in the high level language is also a tedious work as they may produce wrong result. To overcome such situation we need to follow program development process called as a system development life cycle. This process is distributed in 6 phases, it is also called as a water fall model which is shown in Fig. 1.3.

Fig. 1.3 : Waterfall model

System Requirement : This phase includes understanding the problem, and think on what exactly has to be done. Gather all information which is essential to solve problem.

Analysis : In this phase all gathered information is analyzed by different alternatives depending upon the available resources.

Design : In this phase, start to solve problem using various tools like flowcharts and algorithms which are explained in the section 1.4. It is very important step which helps in showing the flow of the program as well as detecting mistakes if any.

Coding : Finally all the design is to be converted into the coding (i. e. programs). Testing : It is again very important phase, which is used to test whether output of

the program is correct or not? This can be tested with various set of inputs. Maintenance : In this phase, program is compared with initial requirement, and as

per requirements, new features are added into it.1.4 Algorithms :

.GTU – Jan. 2010, June 2010.

Computer Programming & Utilization (GTU) 1-11 Introduction to Computer Programming

An algorithm is the finite set of sequential instructions to accomplish a task, where instruction are written in the natural languages like English. It is also called as a step by step solution of the problem.Note : Always remember that algorithm and flowcharts are to be developed first and then programs are to be

written.

1.4.1 Properties/Characteristics of the Algorithm : Algorithm has four properties, that are finiteness, definiteness, effectiveness, input

and output. They are explained below. Finiteness : Total number of steps used in algorithm should be finite. Definiteness : Each step of algorithm must be clear and un ambiguous. Effectiveness : Every step must be basic and essential. Input and output : The algorithm must accept zero or more input and must

produce at least one output.

1.4.2 Advantages :

Very easy to write. Easy technique to understand logic. Easy identification of the mistakes by non computer person.

1.4.3 Disadvantages :

Time consuming. Difficult to show branching and looping. Big tasks are difficult to put in algorithm.1.4.4 Examples of Algorithm :

Simple Flow Problem : These are very simple problems, and the procedure of flow is in sequential manner, means after first step, it automatically goes to second step.

Example 1 : Write an algorithm to calculate area of rectangle.Algorithm

Step 1 : Accept the value of length i.e. lStep 2 : Accept the value of Breadth i.e. bStep 3 : Calculate area i.e. a=l*bStep 4 : Print Area i.e. Print aStep 5 : Stop procedure

Computer Programming & Utilization (GTU) 1-12 Introduction to Computer Programming

Example 2 : Write an algorithm to calculate percentage of the student.Algorithm Step 1 : Accept the name and marks in 6 subject, i.e.

n,m1,m2,m3,m4,m5,m6Step 2 : Total=m1+m2+m3+m4+m5+m6Step 3 : Calculate percentage i.e. per=Total/600*100Step 4 : Print percentage i.e. Print perStep 5 : Stop procedure Branching Flow problems : These sentences are not simple. It consists of

condition, it tests that condition , according to it transfers the flow. Here flow is not sequential that means after branching step, it automatically goes to step mentioned in the statement.

Example 3 : Write an algorithm to check whether the given number is positive or negative?

AlgorithmStep 1 : Accept the number i.e. nStep 2 : Check if n<0 ? If yes go to Step 3 otherwise go to Step 5Step 3 : Print n is negativeStep 4 : Stop procedureStep 5 : Print n is positiveStep 6 : Stop procedure

Example 4 : Write an algorithm to find out smallest number out of two numbers.Algorithm

Step 1 : Accept two numbers i.e. a , b Step 2 : Check if a<b ? If yes go to Step 3 otherwise go to Step 5Step 3 : Print a is smallest numberStep 4 : Stop procedureStep 5 : Print b is smallest numberStep 6 : Stop procedure

Example 5 : Write an algorithm to find out largest number out of four numbers.Algorithm

Step 1 : Accept four numbers i.e. a , b , c ,dStep 2 : Check if a>b ? If yes Go to Step 3 otherwise go to Step 14Step 3 : Check if a>c ? If yes Go to Step 4 otherwise go to Step 9Step 4 : Check if a>d ? If yes Go to Step 5 otherwise go to Step 7Step 5 : Print a is largest number

Computer Programming & Utilization (GTU) 1-13 Introduction to Computer Programming

Step 6 : Stop procedureStep 7 : Print d is largest numberStep 8 : Stop procedureStep 9 : Check if c>d ? If yes Go to Step 10 otherwise go to Step 12Step 10 : Print c is largest numberStep 11 : Stop procedureStep 12 : Print d is largest numberStep 13 : Stop procedureStep 14 : Check if b>c ? If yes Go to Step 15 otherwise go to Step 20Step 15 : Check if b>d ? If yes Go to Step 16 otherwise go to Step 18Step 16 : Print b is largest numberStep 17 : Stop procedureStep 18 : Print d is largest numberStep 19 : Stop procedureStep 20 : Check if c>d ? If yes Go to Step 5 otherwise go to Step 23Step 21 : Print c is largest numberStep 22 : Stop procedureStep 23 : Print d is largest numberStep 24 : Stop procedure Above algorithm is structured algorithm (that means nesting). We can eliminate

repetitive steps using unstructured way, which is described in following algorithm.Step 1 : Accept four numbers i.e. a , b , c ,dStep 2 : Check if a>b ? If yes Go to Step 3 otherwise go to Step 12Step 3 : Check if a>c ? If yes Go to Step 4 otherwise go to Step 9Step 4 : Check if a>d ? If yes Go to Step 5 otherwise go to Step 7Step 5 : Print a is largest numberStep 6 : Stop procedureStep 7 : Print d is largest numberStep 8 : Stop procedureStep 9 : Check if c>d ? If yes Go to Step 10 otherwise go to Step 7Step 10 : Print c is largest number

Computer Programming & Utilization (GTU) 1-14 Introduction to Computer Programming

Step 11 : Stop procedureStep 12 : Check if b>c ? If yes Go to Step 13 otherwise go to Step 9Step 13 : Check if b>d ? If yes Go to Step 14 otherwise go to Step 7Step 14 : Print b is largest numberStep 15 : Stop procedure

Dear students, You can see that both algorithms produce accurate and same result but algorithm 2 requires less number of steps. When you are solving any problem first time then do not think about steps only try to produce accurate result and then try to reduce unnecessary as well as repetitive steps. LOOPS : There are 2 types of loops : Finite loop and Infinite loops

When number of steps are repeated many number of times using same sequence is called as a loop and set of repeated steps are called as a body of loop.

Finite Loops : When body of loop is executed finite number of times called as finite loop. For construction of finite loops following steps are important

Step 1 : Initialize (count) running variable with initial value I=1 Body of loopStep 2 : Increment running variable by step value. I=I+1 Body of loopStep 3 : Check and transfer control to step 2 or in between step 1 and 2 (i.e. body of

loop) If I<10(some final value) then step 2Note : When initial value is less than final value then step value should be positive.

Note : When initial value is greater than final value then step value should be negative.

Note : Step value should not be zero.

Note : Running variable should not be initialised by some constant within the body of loop.

Note : Running variable should not be same in nested loops (i.e. loop within loop)

Example 6 : Write an algorithm to print first 50 odd numbers.There are two possible algorithms

Algorithm1 Algorithm2 (Last odd number is 99)

Step 1 : I=1Step 2 : ODD=1Step 3 : Print ODDStep 4 : ODD=ODD+2

Step 1 : I=1Step 2 : Print IStep 3 : I=I+2Step 4 : If I <=99 go to step 2

Computer Programming & Utilization (GTU) 1-15 Introduction to Computer Programming

Algorithm1 Algorithm2 (Last odd number is 99)

Step 5 : I=I+1Step 6 : If I <=50 go to Step 3Step 7 : Stop procedure

Step 5 : Stop procedure

Computer Programming & Utilization (GTU) 1-16 Introduction to Computer Programming

Example 7 : Write an algorithm to find sum and average of given N numbers.

Example 8 : Write an algorithm to find out factorial of given number.

Algorithm

Step 1 : Accept N

Step 2 : I=1

Step 3 : SUM=0

Step 4 : Accept any number i.e. A

Step 5 : SUM=SUM+A

Step 6 : I=I+1

Step 7 : AVG=SUM/N

Step 8 : Print SUM

Step 9 : Print AVG

Step 10 : Stop procedure

Algorithm

Step 1 : Accept N

Step 2 : I=1

Step 3 : FACT=1

Step 4 : FACT=FACT*I

Step 5 : I=I+1

Step 6 : If I<=N go to Step 4

Step 7 : Print FACT

Step 8 : Stop procedure

Example 9 : Write an algorithm to solve following series. 1-x+x2-x3---xn

Algorithm

Step 1 : Accept N, XStep 2 : I=1Step 3 : SUM=1Step 4 : SIGN=-1Step 5 : TERM=SIGN * XI

Step 6 : SUM=SUM+TERMStep 7 : SIGN=SIGN*-1Step 8 : I=I+1Step 9 : If I<=N go to Step 5Step 10 : Print SUMStep 11 : Stop procedure Infinite Loop : When the body of loop is executed finite number of time called as

finite loop. Following algorithm is the example of infinite loop.

Computer Programming & Utilization (GTU) 1-17 Introduction to Computer Programming

Example 10 : Write an algorithm to print “Tech Max Publication Jindabad” infinite times.AlgorithmStep 1 : Print “Tech Max Publication Jindabad”Step 2 : Go to Step 1Step 3 : Stop procedure

Here step 3 never executes, so it is called as dead step

1.5 Flowcharts :.GTU – Jan. 2010, June 2010.

A Flowchart is pictorial representation of algorithm.Symbol Name Uses

Start and Stop Symbol

Used to start flow chart as well as to terminate the flow chart.

Input /Output Used for IO operation

ProcessUsed to mention ALU and data transfer task (i.e. arithmetic or assignment task)

Decision For branching condition

Flow lines Indicates flow

Loop Indicates finite loop

Fig. 1.4 contd..

Computer Programming & Utilization (GTU) 1-18 Introduction to Computer Programming

Symbol Name Uses

Connector Connecting flow lines from different places

Multiple selection For multiple branching condition

Block Indicates block of statements or body of loop

Module call Calling the subroutine or function

Fig. 1.4 : Flowcharting symbols

1.5.1 Advantages :

Easy to draw. Easy technique to understand logic. Easy to identify the mistakes by non computer person. Easy for branching and looping. 1.5.2 Disadvantages :

Time consuming.

Difficult for modification.

Difficult task to draw flow charts for big programs.

Computer Programming & Utilization (GTU) 1-19 Introduction to Computer Programming

1.5.3 Examples of Flowchart :

Example 11 : Draw a flowchart to calculate area of rectangle.

Example 12 : Draw a flowchart to calculate percentage of the student.

Flowchart :

Fig. 1.5

Flowchart :

Fig. 1.6

Example 13 : Draw a flowchart to check whether the given number is positive or negative ?

Example 14 : Draw a flowchart to find out smallest number out of two numbers.

Flowchart :

Fig. 1.7

Flowchart :

Fig. 1.8

Computer Programming & Utilization (GTU) 1-20 Introduction to Computer Programming

Example 15 : Draw a flowchart to find out largest number out of four numbers..GTU - Dec. 2008.

Flowchart :

Fig. 1.9

Example 16: Draw a flowchart to find out sum and average of given N numbers. .GTU - Dec. 2008.

Example 17 : Draw a flowchart to find out factorial of given N numbers.

GTU - April 2010.

Computer Programming & Utilization (GTU) 1-21 Introduction to Computer Programming

Flowchart :

Fig. 1.10

Flowchart :

Fig. 1.11

Example 18 : Draw a flowchart to solve following series. 1-x+x2-x3 +---xn

Example 19 : Draw a flowchart to print first 50 odd numbers. .GTU –

April 2010.

Flowchart : Flowchart :

Fig 1.13

Example 20 : Draw a flowchart to print “Techmax jindabad” infinite times.

Computer Programming & Utilization (GTU) 1-22 Introduction to Computer Programming

Fig. 1.12

Flowchart :

Fig 1.14

Computer Programming & Utilization (GTU) 1-23 Introduction to Computer Programming

Example 21 : Write an algorithm and draw flowchart for finding sum digits of a number.

Algorithm : Flowchart :

Step 1 : Accept N

Step 2 : N1=N,SUM=0

Step 3 : D=N%10

Step 4 : SUM=SUM*D

Step 5 : N=N/10

Step 6 : If N !=0 go to Step 3

Step 7 : Print SUM

Step 8 : Stop procedure

Fig. 1.15

Computer Programming & Utilization (GTU) 1-24 Introduction to Computer Programming

Example 22 : Write an algorithm and draw the flowchart of a program to find the maximum of 'n' numbers.

Algorithm : Flowchart :

Step 1 : Accept NStep 2 : Accept FIRSTNUM Step 3 : I=2 , MAX=FIRSTNUMStep 4 : Accept any number i.e. AStep 5 : Check if A>MAX Then MAX=AStep 6 : I=I+1Step 7 : If I<=N go to Step 4Step 8 : Print Maximum number is MAXStep 9 : Stop procedure

Fig. 1.16

Example 23 : Write an algorithm and draw the flowchart to accept N numbers and count how many of them where odd and also compute sum of all these odd. .GTU – June 2010.

Ans. :

Step 1 : COUNT=1 : ODD=0: SUM=0Step 2 : Accept NStep 3 : Accept AStep 4 : If A%2==1

Then

Computer Programming & Utilization (GTU) 1-25 Introduction to Computer Programming

SUM=SUM+AODD=ODD+1

Step 5 : COUNT=COUNT+1Step 6 : If COUNT<=N Then Go to step 2Step 7 : Print COUNT and SUMStep 8 : Stop ProcedureFlowchart :

Exercise

Q. 1 Explain the block diagram of the computer.

Q. 2 Differentiate between system programs and application program.

Q. 3 Differentiate between compilers with interpreter.

Q. 4 What are the advantages and disadvantages of machine languages?

Q. 5 What are the advantages and disadvantages of high level languages?

Q. 6 Differentiate between software and hardware.

Q. 7 What is algorithm and flowchart ? and why it is used ?

Q. 8 What is SDLC ? Why it is essential ?

Computer Programming & Utilization (GTU) 1-26 Introduction to Computer Programming

1.6 University Questions and Answers :

Dec. 2008

Q. 1 Draw a flow chart to find the maximum number out of three given numbers. (Pg. 1-23, Example 15 : Say d = – 32768 (Smallest integer number)) (5 Marks)

Q. 2 Draw a flow chart to do the sum of 10 elements read from the user.(Pg. 1-24, Example 16 : say n = 10) (5 Marks)

June 2009

Q. 3 Draw block diagram of computer and explain each block. (Section 1.1.1) (7 Marks)

Q. 4 Define following :

(i) Operating system(ii) Compiler(iii) Application software(iv) System software (Section 1.1.2) (4 Marks)

Jan. 2010

Q. 5 Draw the Block Diagram of Computer Architecture and explain each block with its functions. (Section 1.1.1) (7 Marks)

Q. 6 Give comparison of Flow charts and Algorithms (Sections 1.4 and 1.5) (4 Marks)

April 2010

Q. 7 Draw a flow chart to find factorial of a given number. (Section 1.5) (5 Marks)

June 2010

Q. 8 Provide two major differences between

(a) Software and Hardware (Section 1.1)

(b) Flowchart and Algorithm (Section 1.4 and 1.5)

Q. 9 Name the major components of computer and give their function in brief. (Section 1.1.1) (2 Marks)

Q. 10 Write an algorithm and draw the flowchart to accept N numbers and count how many of them where odd and also compute sum of all these odd. (Example 23) (10 Marks)

Computer Programming & Utilization (GTU) 1-27 Introduction to Computer Programming

Note