BIT C 1
BIT C 1
BIT C 1
Unit 1
Introduction
Problem Solving
Problem Solving is a systematic approach to find and implement the solution to a problem.
The computer is the symbol manipulating machine that follows the set of instructions called
program.
Program
A set of instructions to solve the problem or the specification of the sequence of
computational steps in a particular programming language is called program. The task of
developing program is called programming.
Algoithm
An algorithm is a step by step descriptions of the procedure written in human understandable
language for solving given problem.
The characteristics of an algorithm are:
- Algorithm must have finite number of steps.
- An algorithm should be simple.
- An algorithm must take at least one or more input values.
- An algorithm must provide at least one or more output values.
Advantages of Algorithms:
- An algorithms are very easy to understand.
- Algorithm is programming language independent.
- Algorithm makes the problem simple, clear, correct.
E.g.
Algorithm to find largest number among three numbers:
step 1: start
step 2: input a,b,c
Step 3: if (a>b) and (a>c) then print “a is greater”.
Else if (b>a) and (b>c) then print “b is greater”.
Else print “c is greater”.
Step 4: stop
Flowchart
- A flow chart is a step by step diagrammatic representation of the logic paths to solve a
given problem.
- A flowchart is graphical representation of an algorithm.
Advantages :
- The flowchart shows the logic of a problem displayed in pictorial fashion.
- It is useful for debugging and testing of programs.
- Program could be coded efficiently using flowcharts.
- The Flowchart is good means of communication to other users.
Disadvantages:
- It is not useful to represent complex program logic
- For any alterations, the flowcharts have to be redrawn completely.
Rules for writing flowcharts :
- The flow chart should be clear, neat and easy to follow.
- It should be drawn from top to bottom.
- A flowchart always begins with start symbol and ends with stop symbol.
- Flow lines are used to join the symbols
- Decision box should have one entry point and two exit points.
- For lengthy flowcharts, connectors are used to join them.
E.g.
Q. Write an algorithm and draw flowchart to test a number for even or odd.
Soln:
Algorithm:
1. Start
2. Input a number which is to be tested for even or odd.
3. If 𝑛𝑢𝑚𝑏𝑒𝑟 % 2 == 0 then print “The number is even”.
Else print “The number is odd”.
4. End
Flowchart:
Q. Write an algorithm and flowchart to find out whether a given integer is zero,
positive, or negative.
Soln:
Algorithm:
1. Start
2. Print”Enter a number”.
3. Read 𝑛
4. If 𝑛 > 0 then print “The number is positive”
Else if 𝑛 < 0 print “The number is negative”
Else “The number is zero”.
5. Stop
Flowchart:
Q. Write an algorithm and flow chart for finding largest of three numbers.
Soln:
Algorithm:
1. Start
2. Input A,B,C
3. If (A>B) and (A>C) then print “A is greater”.
Else if (B>A) and (B>C) then print “B is greater”.
Else print “C is greater”.
4. Stop
Flowchart:
Q. Write the algorithm for obtaining the final grade of student based on mark and draw the
flow chart.
Soln:
Algorithhm:
1. START
2. Input marks of students
3. If marks >= 90
Print “Grade = A”
else if marks >= 80
Print “Grade = B”
else if marks >= 70
Print “Grade = C”
else if marks >= 60
Print “Grade = D”
else
Print “Grade = E”
4. END
Flowchart:
Coding
Coding is the translation of an algorithm or flowchart into a suitable computer language like
c, c++, java etc. Coding is the real job of programmer. The algorithm to solve a problem
which is described by pseudo-code or flow chart is converted into actual programming
language code. The code written by programmer by using any programming language like C,
C++ etc. is called the source code or source program.
So testing is the process of checking the program for its correct functionality by executing the
program with some input data set and observing the output of the program.
Documentation
From the start of the problem solving to the end of the implementation of the program, all the
tasks should be documented i.e. kept for future reference. It is also the important part of the
problem solving or program development. Documentation may be of two types:
a. Technical Documentation known as programmer's documentations which includes the
problem analysis to implementation details for that program. It is needed for future reference
for any modification, update of the program.
b. User manual is the documentation prepared for the end-user of the program that guides the
user how to operate the program.
Introduction to C
C programming,
- Was developed by Dennis Ritchie at AT&T Bell Labs, USA in 1972.
- Is a high-level programming language used to develop applications for high-level
business and low-level system programs.
- Became popular because of its power, simplicity and ease of use.
- Enables system program writing, using pointers.
- It is reliable, simple and easy to use.
Features of C:
- Robust language, which can be used to write any complex program.
- Has rich set of built-in functions and operators.
- Well-suited for writing both system software and business applications.
- Efficient and faster in execution.
- Highly portable.
- Well-suited for structured programming.
- Dynamic memory allocation.
History of C
C language has evolved from three different structured language ALGOL, BCPL and B
language. It uses many concepts from these languages and introduced many new concepts
such as data types, struct, pointer.
Structure of C Program
1. Documentation Section
The documentation section is the part of the program where the programmer gives the
details associated with the program. He usually gives the name of the program, the
details of the author and other details like the time of coding and description. It gives
anyone reading the code the overview of the code.
E.g.
/*
C programming basics & structure of C programs
Author: Jayanta Poudel
Date : 28/09/2019
Update:29/09/2020
*/
2. Link Section
The link section provides instructions to the compiler to link functions from the
system library such as using the include directive.
E.g. #include<stdio.h>
stdio.h is an input output header file which contains the input/output and file handling
functions which are used in C programs. The required header file should be included
using #include preprocessor directive.
3. Definition Section
The definition section defines all symbolic constants such using the #define
directive.
E.g. #define PI 3.14
6. Subprogram Section
User can define their own functions in this section which perform particular task
as per the user requirement. So user create this according their needs.
E.g.
int sum(int a, int b)
{
return a+b;
}
Simple C program
/* C-program to display “Hello, World” */
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Output:
Hello, World!
printf( ):
printf is an output function which has been defined in stdio.h file. Whatever is put inside the
function printf between two double quotes is printed on the screen.
#include <stdio.h>
int main()
{
int number1, number2, sum;
// calculating sum
sum = number1 + number2;
Output: