PROBLEM, ALGORITHM, PROGRAM AND PROGRAMMING LANGUAGE

0

 INTRODUCTION

A Programming Language is used to design and describe a set of instructions and computations to be executed by a computer. To do programming, one should have knowledge of i) a particular programming language ii) algorithm to solve the problem. An algorithm is finite number of steps, which perform some computation on the input and produce the desired output in a finite amount of time. Once an algorithm is chosen, the next step for a solution using a computer would be to write a program using the algorithm. A program is defined as a collection of Statements or Sentences written in a particular programming language. Further, to obtain the output from a Program, it is required to be compiled or interpreted so that the computer can understand it..

OBJECTIVES 

After going through this unit, you will be able to :
  • need for Programming;
  • flow chart and Example Program;
  • elements of Programming Languages such as variable, constant, data type, 
  • arrays and expression etc.;
  • describe looping and decisions; and 
  • differentiate between Assembler, Compiler and Interpreter.

PROBLEM, ALGORITHM, PROGRAM AND PROGRAMMING LANGUAGE

A Problem is to carry out a particular task. For solving the problem, some input has to be given to the system. The system will process or manipulate the input and produce the desired output. An algorithm describes the steps required to solve a problem. Once an algorithm is obtained for solving a problem, a Program has to be written which a computer can execute so as to accomplish the given task, thereby solving the problem at hand. The program can be in any suitable programming language and is not dependent on the algorithm in any way.

Algorithm: Once a problem has been defined precisely, a procedure or process must be designed to produce the required output from the given input. Since a computer is a machine that does not possess problem-solving judgmental capabilities, this procedure must be designed as a sequence of simple and unambiguous steps. Such a procedure is known as an algorithm. 

The steps that comprise an algorithm must be organized in a logical, clear manner so that the program that implements this algorithm is similarly well structured. Number of steps in the algorithm should be finite, they should be executed in finite amount of time and they should give the desired output. Algorithms are designed using three basic methods of control:

a) Sequential : Steps are performed in a strictly sequential manner, each step being executed exactly once. 

b) Decision/Selection : One of several alternative actions is selected and executed.

c) Repetition : One or more steps is performed repeatedly.

Any algorithm can be constructed using basic methods of control.

 Programs to implement algorithms on the computer must be written in a language that the computer can understand. It is fruitful, therefore, to describe algorithms in a language that resembles the language used to write computer programs. This is called pseudo code. It is not a programming language with a rigid syntax, but is similar to one. The idea is that it should be easy to write a program by looking at the pseudo code..

Let us take few problems to illustrate how to express the solution to a problem in the form of an algorithm. We will also see how algorithm can be diagrammatically represented using flow chart, and also how a program can be written based on the algorithm.

For providing a solution to any problem some input from the user is required. In the following examples the number n that is expected from the user should be an  integer. In the program, it will be declared of the required type only, referred as data type of the variable. A Detailed description of variable and data type is given in a later section.

Flowcharting: A Flowchart is a graphical representation of an algorithm. It can be compared to the blueprint of a building. Just as a building contractor refers to a blueprint for the construction of a building, similarly a programmer refers to a flowchart for writing the program which describes what operations are to be carried out and in what sequence to solve a problem. Flowcharts are usually drawn using some standard symbols. The Basic flowchart symbols are as below: 


The number n is expected to be an integer
Example 1 
Problem statement: To find out whether a given number is even or odd.
Algorithm:
Step 1 Start
Step 2 INPUT the number n 
Step 3 Find the remainder on dividing the given number by 2
 if (remainder = 0) then
 print “number is even”
 else
 print “number is odd”
Step 4 End
Representing this algorithm through flowchart helps in easier and better 
understanding of the algorithm :


The program to implement this so that it can be run on a computer can be written in any of the known programming languages. For example, here C has been used as the Programming language for writing the program:

#include<stdio.h> /* including header file that has definition of inbuilt functions 
*/
void main()
{ /* To mark beginning of block */
int n; /* variable declaration */

printf(“Enter the number”); /* predefined output function in header file to 
display the message on standard output device */
scanf(“%d”,&n); /* predefined input function for taking an input from the user 
*/
if (n %2 ==0) /* if else condition to check a expression is true or false and 
branch accordingly as per syntax of C programming */
{
printf(“Number %d is even”,n);
}
else
{
printf(“Number %d is odd”,n)
}
} /* to mark the end of block */

Example 2 

Problem: To find the product of first n natural numbers

Step 1 Start
Step 2 Input number n
Step 3 Initialize product=1
Step 4 Initialize the counter, i=1
Step 5 Compute product=product * i
Step 6 Increment counter i by 1 i.e i=i+1
Step 7 Check counter <= n if yes go to step 5
Step 8 Print Product of first n natural numbers as product
Step 9 End

We now express this algorithm as a flowchart for better understanding of the algorithm


Here is the C program corresponding to the above algorithm:

#include 
    void main() 
}
int n,i;
int prod=1;
printf(“Enter number n :”);
scanf(“%d”,&n);
    for(i=1;i<=n;i++) /* For loop construct for repeating a set of statement n 
        number of times */
{
prod=prod * i;

}
printf(“Product of first %d natural numbers is = %d”,n,prod);

}

Example 3

Problem: To find the sum and average of marks obtained by 20 students in some subject of a course.

Algorithm:  

Step 1 Start
Step 2 Initialize an array s for 20 students marks i.e s[20] 
Step 3 initialize sum=0
Step 4 initialize counter=0
Step 5 Compute sum=sum+s[counter]
Step 6 increment counter =counter+1
Step 7 check counter <20
Step 8 if yes goes to step 5
Step 9 avg=(sum/20)
Step 10 Print sum and average
Step 10 End 

Here is the corresponding C program:

#include <stdio.h>

 void main()
{
int i, sum; /* declaring variables */
int s[20]; /* declaring array to refer set of memory locations of same data 
type with one name */
float avg;

sum=0; /* assignment statement */

printf(“Enter marks for 20 students”);
for(i=0;i<20;i++)
{ printf(“%d =”,i+1);
scanf(“%d”,&s[i]);
}

i=0;
while(i<20) /* while loop construct to repeat set of statement till the condition 
is true */
{
sum=sum+s[i]; /* arithmetic statement for addition */
i++; /* increment statement */
}
avg=sum/20;
printf(“Sum of marks for 20 students:= %d”,sum);
printf(“Average marks of 20 students:=%.2f”,avg);
}

Check Your Progress 1 👈

1) Write an algorithm for solving the following problems:
a) To calculate the area of a rectangle.
…………………………………………………………………………
…………………………………………………………………………
b) To find the sum of the first n natural numbers 
…………………………………………………………………………
…………………………………………………………………………
2) Draw a flowchart for a) and b) in Question 1
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

Post a Comment

0Comments
Post a Comment (0)