You are on page 1of 27

Computer Programming & Programming Methodology

Computer Programming & Programming Methodology


Unit-1 Introduction
A computer cannot perform any task of its own and can only understand its own
language which is the language of 0’s and 1’s that means binary number
system, therefore a computer user has to communicate with a computer using
the language which they can understand.
The set of instructions to perform a task in the computer that is known as a
program and the computer languages in which programs are written is known
as programming language. The person developing or writing programs is
known as “programmer”.
A computer cannot understand the natural language like English.
Examples of programming languages are C, C++, C#, Perl, Java, etc.

Introduction to C Programming
History of C Programming

• C is a programming language which was developed at “AT & T’s Bell


Laboratory” of USA in 1972.
• C was written by Dennis Ritchie, that’s why he is also called as father of
C Programming language.
• C was created for specific purpose that is designing the UNIX Operating
system.

Flowchart and Algorithm


• Algorithm and flowchart are two types of tools to explain the process
of a program.

Algorithm:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 1
Computer Programming & Programming Methodology

o A step by step method to solve the problem is called algorithm,


in other words, an algorithm is a procedure for solving
problems.
o Algorithm is a blueprint or plan of our program.
o Features of algorithm:
▪ Proper understanding of the program.
▪ Use of procedures and functions to emphasize
modules.
▪ Choice of variable names.
▪ Documentation of program.

Example:
Write an algorithm to display the sum of two numbers.
Step-1 Start

Step-2 Read two numbers a and b variable.

Step-3 Calculate the sum of a and b and store it in sum


variable.

Step-4 Display the value of sum variable.

Step-5 Stop

Advantages:
▪ It is step-wise representation of a solution to a given
problem, which makes it easy to understand.
▪ It is not dependent on any programming language, so easy
to understand for anyone even without programming
knowledge.
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 2
Computer Programming & Programming Methodology

▪ By using algorithm the problem is broken down into


smaller pieces or steps so it is easier for programmer to
convert it into an actual program.

Disadvantages:
▪ Writing algorithm takes a long time.
▪ An algorithm is not a computer program, it is rather a
concept of how a program should be.

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ Hello World ”);
getch();
}
Practical: (how to do 1st program in C)
1) Open turbo C, by double click in icon.
2) Go to File menu and select New. Blue screen will be display.
3) Include #include<stdio.h> and #include<conio.h> header file.
4) Write void main()
5) Write your program within main().
6) Save your program by click on File menu and select save option or
Press F2 key to save your program.
7) Compile your program by click on Compile menu and select
compile option OR press Alt + F9. (If error occurs then solve
errors at compile time)
8) Run your program by click on Run menu and select Run option
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 3
Computer Programming & Programming Methodology

OR press Ctrl + F9.

Flowchart:
A flowchart is the graphical or pictorial representation of a
problem or program or algorithm with the help of different symbols,
shapes and arrows in order to demonstrate a process or a program.
The main purpose of a flowchart is to analyze different processes.

Symbols Meaning

Start/ End

Input/ Output

Process/ Instruction

Decision

Connector / Arrow

Example:
Draw a flowchart for sum of two numbers.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 4
Computer Programming & Programming Methodology

Start

Input a & b variable

Sum = a + b

Display sum variable

End

Advantages:
▪ The flowchart is an excellent way of communicating the
logic of a program.
▪ It is easy and efficient to analyze problem using
flowchart.
▪ During program development cycle, the flowchart plays
the role to guide or a blueprint, which makes program
development process easier.
▪ It helps the programmer to write the program code.
▪ It is easy to convert the flowchart into any programming
language code as it does not use any specific
programming language concept.

Disadvantages:
▪ The flowchart can be complex when the logic of a
program is quite complicated.
▪ During flowchart is a time consuming task.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 5
Computer Programming & Programming Methodology

▪ Difficult to alter the flowchart, sometimes the designer


needs to redraw the complete flowchart to change the
logic of the flowchart or to alter the flowchart.
▪ It is quite tedious task to develop a flowchart as it requires
special tools to draw the necessary symbols.

Practical Programs:
Program-1 Write a Program to add two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int b;
int sum;
a=10;
b=5;
sum=a + b;
printf("Value of sum is %d",sum);
getch();

}
Program-2 Write a Program to Subtract Two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
float a;
float b;
float sub;

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 6
Computer Programming & Programming Methodology

a=10.5;
b=5.2;
sub=a - b;
printf("subtraction is %f",sub);
getch();

Program-3 Write a program to divide two numbers


#include<stdio.h>
#include<conio.h>
void main()
{

float a;
float b;
float div;
clrscr();
a=10;
b=3;
div=a / b;
printf("division is %f",div);
getch();

Structure Programming
In a structure programming, the program is divided into small parts or
functions and each part performs a specific job. The main importance is on
functions rather than data.
Structured Programming Constructs:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 7
Computer Programming & Programming Methodology

1. Sequence Control Structure ( Sequence Logic Or Sequence


Flow)
2. Selection Control Structure (Selection Logic Or Conditional
Flow)
3. Repetition Control Structure (Iteration Logic Or Repetitive
Flow)

1. Sequence Control Structure (Sequence Logic Or Sequential


Flow)
If nothing is specified then the instructions are executed in the
sequence in which they are written and in it the modules are
executed one after the other.

2. Selection Control Structure (Selection Logic Or Conditional


Flow)
In it there are various conditions on the basis of which one module
is selected out of several given modules. The structures which
implement this logic are called conditional structures.

3. Repetition Control Structure (Iteration Logic Or Repetitive


Flow)
In it loops are implemented. Loop is used to implement those
statements which are to be repeated again and again until some
condition is satisfied. It implements while and for loops. Each of
these begins with repeat statement.

Compiler and Interpreter


A program written in a high level language. A high level language
is only understood by humans. It generally contains phrases from
English language, but computer cannot understand high-level
language.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 8
Computer Programming & Programming Methodology

Computer can only understand program written in 0’s and 1’s in


binary, called the machine code.
A program written in high-level language is called a source code.
Source code must be translated into machine language before it can
be executed. So we need to call the source code into machine code
and this is accomplished by compiler and interpreters.
Compiler or an interpreter is a program that converts program
written in high-level language into machine code understood by
the computer.
Compiler or interpreter is a computer program that accepts a
high-level program (e.g. C Program) as input data and generate
machine-language program as output. The original high-level
program is called the source program and resulting machine-
language program is called the object program.
The difference between an interpreter and compiler

Interpreter Compiler

Translate program one Scan the entire program and


statement at a time. translate the whole program into
machine code.

It takes less amount of time to It takes large amount of time to


analyze the source code but the analyze the source code but the
overall execution time is slower. overall execution time is
comparatively faster.

No intermediate code is Generates intermediate object


generated, hence are memory code which further required
efficient. linking, hence requires more
memory.

Continuous translating the It generates the error message


program until the first error is only after scanning the whole

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 9
Computer Programming & Programming Methodology

met, in which case it stops. program, Hence debugging is


Hence debugging is easy. comparatively hard.

Programming language like Programming language like C,


python, Ruby use interpreter. C++ use compiler.

Editor
An editor refers to any program capable of editing files. The
term editor is commonly used to refer to a text editor, which is a
software program that allows users to create or manipulate plain
text computer files.

Error, Debugging and Testing


Errors:
There are 3 types of program errors:

• Syntax errors

• Run-time errors

• Logical error
Syntax Error:
A syntax error is a violation of the rules of the
programming language.
Example:
Printf(“ hi” )
Syntax errors are relatively easy to find and fix.

Run-time Error:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 10
Computer Programming & Programming Methodology

A run-time error is an error that results from using


invalid operand values for an operation.
The following statement generates a run-time error, if
count has the value zero. Since division by zero is not
mathematically defined.
Example:
int total=100;
int count=0;
Avg = total/count;

Logical Error:
Logical error in program is any code that causes
incorrect output/results even though the program runs to
completion.
A program with logical error may give the correct
answer sometimes and the wrong answer sometimes.
Logical error typically are the most difficult type of the
error to find and correct.
Example:
The following code contains a logic error
because the > and >=.
if( age > 18)
{
printf(“you are eligible for voting”);
}

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 11
Computer Programming & Programming Methodology

Program testing:
• Testing is the process of finding errors in a program.
• Testing can find the presence of errors.
• Testing small program is much easier than testing large programs.
• You should test your program with expected and unexpected input.
o You should know how your program will perform with good
idea as well as bad data.
o Unexpected values may be negative, extremely large, or
extremely small values.

• You should test your program with boundary condition values, i.e
values at and around a value for which the behavior of a program
should change.
o For example, good boundary condition test values for age in the
code above would be 17, 18, 19.

• When testing loops, provide input that cause the loop to repeat zero,
one, and many times.

Program debugging:
Debugging, in computer programming and engineering, is a multistep
process that involves identifying a problem, isolating the source of the
problem, and then either correcting the problem or determining a way to
work around it.
o Debugging is the process of locating and correcting errors in a
program.
o Debugging id problem-solving and often can be very challenging.
o You can help minimize your time spent debugging by:
o Starting with a good program design
SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 12
Computer Programming & Programming Methodology

o Coding carefully
o Testing your program as you write.

Basic Structure of C Program


A C program may contain one or more section.

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. This information gives the program an identity and
basic authority.

Comment can be

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 13
Computer Programming & Programming Methodology

• Single line comment

The single line comment can be written by // (double slash)

• Multi line comment

The multiline comment can be written by

/*………………..

………………….*/

Link section:

The link section provides instruction to the compiler to link or include


the required in-built functions from the system library such as using
the #include directive. This is important, because if we need to use any in-built
system function we must first include the library in which the function has
been defined.

#include<file_name>

The source code of the file “file_name” is included in main()


function C program.

The link section is also called include section, this section is prefixed by
# sign,

#include<stdio.h>

#include<conio.h>

#include<math.h>

Header files Description


#include<stdio.h> It is standard input output header files,
consist of standard input function
scanf() and standard input function
printf().
#include<conio.h> It is console input output file consist
of clrscr() function to clear the screen

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 14
Computer Programming & Programming Methodology

and getch() function to get character


to see the output on screen.
#include<math.h> It is Math header file consist of all
mathematical function.
#include<string.h> It is string header file consist of string
function.

Definition section:

The definition section defines all symbolic constants using the #define
directive. Having the constants being defined here, we can use them elsewhere
in code.

#define pi 3.14

# is preprocessor which gives direction to the compiler that define


pi as a constant value for a program.

Global declaration section:

There are some variables that are used in more than one function; these
variables are called global variables and are declared in this global declaration
section which is outside the definition of any function. This section also
declares all the user-defined functions. As this global scope, these functions
and variables can be used from definition of other functions.

main () function section:

A C program must have one main function section in its structure. This
section contains two parts; declaration part and executable part. However, the
content of these two parts can be mixed.

(a) Declaration part:

The declaration part declares all the variables used in the


executable part.

(b) Executable part:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 15
Computer Programming & Programming Methodology

There is at least one statement in the executable part.

These two parts must appear between the opening and closing braces of
the main function. 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.

Subprogram section:

If the program is a multi-function program then the subprogram section


contains definition of all the user-defined functions which were declared
earlier in the Definition Section. User-defined functions are generally placed
immediately after the main () function, although they may appear in any order.

All section, except the main () function section may be absent when they
are not required.

Character Set

The various categories of characters are called the character set. In C, the
characters are grouped into the following categories:

a) Letters:
a. Uppercase: A………….Z
b. Lowercase: a…………..z
b) Digits:
a. All digits: 0…………..9
c) Special Characters, such as
a. , Comma
b. . Period
c. ; Semicolon
d. : Colon

C tokens

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 16
Computer Programming & Programming Methodology

Tokens in C is the most important element to be used in creating a program in


C. We can define the token as the smallest individual element in C.
C tokens are the basic building blocks in C language which are constructed
together to write a C program.

C token are of six types.


o Keywords (eg. int, while)
o Identifiers (eg. main, total)
o Constants (eg. 10, 20)
o Strings (eg. “total”, “heta”)
o Special Symbols (eg. (), { })
o Operators (eg. +, -, *, / )

Keyword and Identifier


In 'C' every word can be either a keyword or an identifier.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 17
Computer Programming & Programming Methodology

o Keywords:

Keywords have fixed meanings, and the meaning cannot be


changed.

They act as a building block of a 'C' program. There are a


total of 32 keywords in 'C'.

Keywords are written in lowercase letters.

Auto double int struct

Break else long switch

Case enum register typedef

Char extern return union

Const short float unsigned

Continue For signed void

Default goto sizeof volatile

Do If static while

o Identifiers:

An identifier is nothing but a name assigned to an element in


a program.

Example:

name of a variable, function, etc.


SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 18
Computer Programming & Programming Methodology

Identifiers are the user-defined names consisting of 'C'


standard character set.

As the name says, identifiers are used to identify a particular


element in a program.

Each identifier must have a unique name.

Following rules must be followed for identifiers:

• The first character must always be an alphabet or an


underscore.
• It should be formed using only letters, numbers, or
underscore.
• A keyword cannot be used as an identifier.
• It should not contain any whitespace character.
• The name must be meaningful.
• Only first 31 characters are significant.

Constant

Constants are the fixed values that never change during the
execution of a program. Following are the various types of constants:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 19
Computer Programming & Programming Methodology

1) Integer constants

An integer constant is nothing but a value consisting of digits or


numbers. These values never change during the execution of a program.

There are 3 types of integer constant.

a) Decimal Integer
b) Octal Integer
c) Hexadecimal Integer

Decimal Integer:

Consist of a set of digitals from 0 to 9, presented by an


optional + or – sign

Example:

o 123
o -321
o 0
o 89765

Octal Integer:

Octal integer consists of a set of digits from 0 to 7, with


leading 0.

Example:

o 037
o 0
o 043
o 0551

Hexadecimal Integer:

Hexadecimal integer consists of sequence of digits preceded


by 0x or 0X is considered as hexadecimal.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 20
Computer Programming & Programming Methodology

They may also include letters from A to F or from a to f. the


letters represents the numbers from 10 to 15.

Example:

o 0X2
o 0x9F
o 0Xbcd
o 0x

2) Real constants
Real constants are used to represent quantities that are very
continuous such as distances, temperature etc.
These quantities are represented by numbers containing fractional
parts.
Example:
0.00832
-0.75
33.337

3) Single character constant


A single constants contains a single character enclosed within a
pair of single quote marks.
Example:
‘5’
‘X’
‘;’

4) String constant

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 21
Computer Programming & Programming Methodology

A string constant contains series of characters enclosed within a


pair of double quote marks.

Example:

“Hello !”

“1234”

5) Backslash character constants


Constants Meaning

‘\a’ Audible alert

‘\n’ New line

‘\?’ Question mark

Variables:
A variable is a data name that is used to store data values. A variable
may take different values at different times during execution.
Some examples are average, heights, class_strength.

Rules of creating variable:

Variable names may consist of letters, digits and the underscore character,
subject to following conditions.

• Must consist of only letters, digits and underscores.


• First character must be an alphabet. Some system permits underscore as
the first character.
• 31 characters are permitted. But only the first 8 characters are
significant.
• Uppercase and lowercase are significant.
• It should not be a keyword.
• White spaces are not allowed.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 22
Computer Programming & Programming Methodology

Data types:
C language is rich in its data types. ANSI supports three classes of
data types.

1) Primary or fundamental data types.


2) Derived Data types.
3) User-defined data types.

1) Fundamental Data types:

All C compilers support five fundamental data types.

• Integer (int)
• Character (char)
• Floating point (float)
• Double-precision point (double)
• Void

i. Integer types:
Integer types are whole numbers with a range of
values supported by particular machine. If we

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 23
Computer Programming & Programming Methodology

use a 16 bit word length, the size of the integer


value is limited to the range -32768 to +32767.

C has three classes of integer storage namely

• Short int
• Int
• Long int

Short int represents small values than int


and int represents small value than long
int. or we can say that

Short int < int < long int

• Here 1 byte = 8 bits.


• Signed int is able to represent negative values.
• Unsigned int can represent non-negative values (positive
values).

ii. Floating point types:

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 24
Computer Programming & Programming Methodology

• Floating point numbers are stored in 32 bits, with


6 digits of precision.
• Floating point numbers are defined in C by
keyword float.
• When float is not sufficient double can be used
and when double is not sufficient long double
can be used.
• The following table provide the details of
standard floating point types with storage size
and value ranges and their precision,

Type Storage Size Precision

Float 4 bytes 6 decimal place

Double 8 bytes 15 decimal places

Long double 10 bytes 19 decimal Places

iii. Void types:

• The void type has no values. This is usually used


to specify the types of functions.
• The types of function is said to be void when it
does not return any values to the calling
function.

iv. Character types:

• A single character can be defined as character


(char) type data.
• Characters are usually stored in 8 bits (1 byte) of
internal storage.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 25
Computer Programming & Programming Methodology

• While unsigned char have values between 0 to


255, sign char have values from -128 to 127.

Declaring & initialization of C variable:


• Declaration:
▪ Variables must be declared in C program before use.
▪ In C language variable is declared using following sytax:
• Syntax:

Data_type variable_name;

• Example:

int sum;

Here, int is data types and sum is the name of


variable.

Multiple variables can be declare like,

Example:

int sum, total, a;

float p, r;

• Initialization:
o Variable initialization means assigning a value to the variable.

Example:

int sum=30;

Here variable sum is declared and at the same time it is


assigned a value 30.

SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 26
Computer Programming & Programming Methodology

Type Syntax

Variable Declaration Syntax:

Data_type variable_name

Example:

int x, y, z;

char ch;
Variable initialization Syntax:

Data_type variable_name = value;

Example:

int x=50;

char ch=’h’;

Declaring variable as constant


Variable can be declared as constant using keyword const.
Example:
const int sum =30;
OR
int const sum=30;

• Here, Variable sum is declared as const it means that


the value of variable sum cannot be changed latter in
program.

• Any attempt to change the value of sum will result in


error.

• Const keyword can be used before or after data type.


SASCMA English Medium & STERS BCA & BBA College BY: Heta S. Desai Page 27

You might also like