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
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<stdio.h>
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;
}
Concept in Programming
Language
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
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 */
{
Syntax provides the structure and how to formulate the phrase or sentence w.r.t grammar of the language. It tells us about which composition is allowed from the character set of the language. Each sentence must be a well formed sentence according to the grammar of the language. The grammar is expressed in a Number of rules that will be finite and these allow the formulation of any number of sentences. A language is defined in the form a quadruplet L(T,N,P,S) where T is set of terminals, N is a set of non terminals, P is set of productions or rules and S is the start symbol. For any language we must have an alphabet/character set, operators and rules. To form a grammar w.r.t a language rules need to be formed. The basic structure of rule is LHS and RHS with some set of terminal and non terminal symbol.
Syntax comprises grammar and vocabulary whereas syntactic analysis is known as parsing. Semantics provides the meaning for the formulated /composed phrase or word or sentence. Semantic function can be incorporated inside the logic of a compiler or interpreter which evaluates ordering of statements for execution.
S-> A
A->Ab | b i.e any word with sequence of any number of occurrence of character b
Start symbol:
Here S is start symbol. Any sentence will start with start symbol only. In respect of BNF notation it is as follows:
L(T,N,P,S)
T={b}
N={A} P={S->A,A->Ab} S=Start sybmol
Example grammar:
Program: statement
Statement: stmt| stmt
Stmt: var=expression
Var:a|b
Expression: term +term | term-term
Term: var/const
e.g x=y+2
Similarly
Sentence: subject,verb,object
Subject: noun/article
e.g Ram ate biscuits.
Here each symbol on the left is described in terms of its components. Thus a program consists of statements, which are of the form of an assignment of a variable to an expression, and so on.
Any number of sentences can be formed with the help of a grammar defined for a language. The grammar should be unambiguous. otherwise during syntactic analysis at some point of time it can have more than one meaning.
WHAT IS THE ELEMENTS OF PROGRAMMING LANGUAGE
Learning a programming language requires understanding of concepts such as representation of different types of data in the computer, the various methods of expressing mathematical and logical relationships among data elements and the methods for controlling the sequence in which operations can be executed for inputting the data, processing it and generating the output data.
Variables, Constants, Data type, Array and Expression
These are the smallest components of a programming language.
For writing a program, one must know how to use the internal memory of a computer. A Computer memory is divided into several locations where each location has its own address. Memory organization can be represented diagrammatically as below:
Each location or cell can hold some information. In order to store, retrieve or manipulate data from a specific memory location, we must have some identifier for a particular location. .
Variable: As referencing memory by its physical address is very tedious, variable names are used. A variable is a symbolic name given to a memory location. Once a variable is assigned to a memory location, the programmer can refer to that location by variable name instead of its address. Variable is the connection between a name and a value.
It is composed of a name, attribute, reference and a value. Attribute means the type of value a variable can hold.
For example the following programming code in C declares variables a & b. int a,b;
char c;
In the above declaration, a & b are the variable name, which refer to a memory location where integer values can be stored. Instead of address of the memory location, variable names a and b will be used to refer to a memory location in order to store or retriever update its value.
Similarly, c is a variable name given to a memory location where a character value can be stored. Further c will be used to refer to the memory location in order to store or retrieve or update its value.
Constant : In contrast to a variable, which is used as identifier for a value and which can change, constants are identifiers that are used for values, which cannot be changed. In other words constants are symbols used to refer to quantities which do not change throughout the life of a program. No assignment can be done to a constant.
A numeric constant stands for a number. This number can be an integer, a decimal fraction, or a number in scientific (exponential) notation. Some of the operations which can be performed with numeric constants are addition, subtraction, multiplication, division and comparison.
A string constant consists of a sequence of characters enclosed in double/single quote marks. Chopping off some of the characters from the beginning or end, adding another string at the end (concatenation), copying are some of the operations that performed on the string constants. All these operations can be done on variables also.
For example in the C programming language -integer constant is declared as:
const int a=2; /* the value of a cannot be changed throughout the program*/ -string constant is declared as:
char const *str; /* str ,string can not be altered by string library functions*/
Data Type: Anything that is processed by a computer is called data. There are different types of data that can be given to the computer for processing. A data type is a classification identifying the typeof data. It determines the
Possible values for that type,
Operations that can be performed on values of that type,
The way values of that type can be stored in memory,
In each programming language there are some primitive data types. For example in the C programming language they are: Please note that these sizes can be compiler or machine dependent in the case of this language. For other languages such as Java, the sizes are defined by the language itself.
int, both signed and unsigned integers, 2 bytes in size.
float, floating point numbers, up to 4 bytes in size.
double, floating point number with double precision. These are organized in 8 bytes (64 bits)
char, character type size of 1 byte (8 bits) It is used to form the strings i.e sequence of characters.
Array : In programming, when large amount of related data needs to be processed and each data element is stored with different a variable name, it becomes very difficult to manage and manipulate. To deal with such situations, the concept of array is used.
An array is a set of elements of same data type that can be individually referenced by an index. Usually these are placed in contiguous memory locations. Generally two types of array are used:
One dimensional array
Two dimensional array
One dimensional array: A one-dimensional array is a structured collection of elements that can be accessed individually by specifying the position of a component with index/ subscript value. The index would let us refer to the corresponding value. value at that .
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
type name [elements];
where type is a valid data type (like int, float...), name is a valid identifier or variable name and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array will contain. Therefore, in order to declare an array named as marks, that will store marks for 5 students,
Two dimensional Arrays : A two-dimensional array is like a table, with a defined number of rows, where each row has a defined number of columns. In some instances we need to have such an array of arrays. It will have two dimensions and data is represented in the form of rows and columns.
Type name [elements] [elements];
For example int a [3] [3]; /* lets one dimension depict location and other dimension represent sales in a day or a week or a month*/
Expression : An expression is a combination of variables, constants and operators written according to the syntax of the programming language. In the C programming language every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Every computer language specifies how operators are evaluated in a given expression. An expression may contain
i) arithmetic operator:
ii) relational operator
iii) logical operator
Assignment : It is composed of variable name, an assignment operator of the language and a value or variable or some expression as per composition allowed based on rules defined in grammar.
e.g temp=5; temp=temp+1;
This means to add 1 to the current value of the variable temp and make that the new contents of the variable temp
temp = a+b ;
Arithmetic : These types of expressions consist of operators, operands or some expression. The following is the list of arithmetic operator.
+(addition), -(subtraction),
*(Multiplication),
/(Division), % (modulo),
++(increment by 1), --(decrement by 1)
Here are some examples of arithmetic expressions. e.g. x=y+z; /* addition of y and z will be stored in x */
i++; /* i will be incremented by 1 i.e i=i+1 */
y=x%2; /* remainder after x divided by 2 will be stored in y */
Logical, relational and equality : these types of expression result in a Boolean representation i.e. each expression will result in either True or False. It is composed of operands and relational/logical/equality operator.
The following is the list of operators in the C programming language
== (equal to)
!= (Not equal to) < (less than)
<= (less than equal to) > (greater than) >=(greater than equal to) && (logical AND)
|| (logical OR) ! (logical NOT)
Relational expressions result in one of the truth value either TRUE or FALSE. They are capable of comparing only two values separated by any valid relational operator.
e.g.
Let x=1, y=3
x==1 /* evaluates to true as x has value 1 */ x!=y /* evaluates to false */
x<y /* evaluates to true */
(x<2) && (y> 5) /* evaluates to true */
Bit Wise: Bitwise operators modify variables considering the bit patterns that represent the values they store.
& AND (Binary operator)
| inclusive OR (OR)
^ exclusive OR (XOR)
<< shift left.
>> shift right.
~ one's complement
e.g. let a=2 (0000 0010),b=5(0000 0101) c=a&b; (0000 0000) /* c=0*/
Enroll for Coaching or Home Class | IGNOU BCA MCA Solved Assignment 2016 2017 on process Join IGNOU BCA MCA Coaching Class | Synopsis & Project Support
Powered by IGNOUFriend | www.ignoufriend.co.in |
ignou bca mca synopsis, ignou class, ignou bca mca project, solved ignou assignment,BCA Coaching Institute in Delhi, MCA Coaching Institute in Delhi, BCA MCA Coaching Institute in Delhi, BCA MCA, Tuation Class in delhi, IGNOU BCA MCA Class, IGNOU BCA MCA Tution,IGNOU Solved Assignment, IGNOU BCA Synopsis , IGNOU MCA Synopsis, ignou bca project, ignou mca project, ignou best coaching in delhi, ignou best coaching in new delhi, ignou best coaching in south delhi, ignou best coaching in north delhi, ignou best coaching in east delhi, ignou best coaching in west delhi
ignou, bca, mca, coaching institute, tution class, bcsp064, mcsp060, cs76, mcs044,bcs011, bcs012, mcs011, mcs012, mcs013, mcs015, bcsl021, bcsl022, mcs014, bcs040, mcs021, mcs023, bcs031, bcsl032, bcsl033, bcsl 034, bcs041, bcs042, mcsl016, bcsl043, bcsl044, bcsl045, bcs051, bcs052, bcs053, bcs054, bcs055, bcsl056, bcsl057, bcsl058, bcs062, mcs022, bcsl063, project
mcsp060, mcse011, mcse004, mcse003, mcsl054, mcs053, mcs052, mcs051, mcsl045, mcs044, mcs043, mcs042, mcs041, mcsl036, mcs035, mcs034, mcs033, mcs032, mcs031, mcsl025, mcs024, mcs023, mcs022, mcs021, mcsl017, mcsl016, mcs015, mcs014, mcs013, mcs012, mcs011
nips institute, nipsar, ignoufriend.in, ignoufriend.co.in, ignouhelp, best coaching institute
|
No comments:
Post a Comment