0% found this document useful (0 votes)
9 views71 pages

Unit I - Part 1

The document outlines the syllabus and course outcomes for a 'Fundamentals of Programming' course, emphasizing skills in problem-solving, coding, and file operations using the C programming language. It includes detailed units covering algorithms, data types, functions, arrays, user-defined data types, and file handling. Additionally, it describes the assessment scheme and provides resources for further reading.

Uploaded by

Tanishq Ikhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views71 pages

Unit I - Part 1

The document outlines the syllabus and course outcomes for a 'Fundamentals of Programming' course, emphasizing skills in problem-solving, coding, and file operations using the C programming language. It includes detailed units covering algorithms, data types, functions, arrays, user-defined data types, and file handling. Additionally, it describes the assessment scheme and provides resources for further reading.

Uploaded by

Tanishq Ikhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course: Fundamentals of Programming

Course Code: 24CS01TP0101

P ro f . N I K I TA K ATA R IYA
D E PA RTM EN T O F C S E
R A M D EOBA BA U N I V E R S IT Y, N A G P U R
Course Outcomes
On successful completion of the course, student shall be able to
[Link] logic for simple problem statements.
[Link] problem statements involving decision-making and loops
[Link] the concept of functions for modular programming
[Link] problems using arrays and structures
[Link] file operations
Course: Fundamentals of Programming
L: 3 Hrs
Syllabus
Unit I: Algorithm and Flowchart for problem-solving, Introduction to C language:
Keywords, Constant, Variable, Data types, Operators, Types of Statements, Decision
Control Statement- if and Conditional operators.

Unit II: Switch case statement, Loops, Pre-processor Directives

Unit III: Concept of functions, User defined and Library Functions, parameter
passing, Recursion, Storage class, Pointers.
Course: Fundamentals of Programming
L: 3 Hrs
Syllabus
Unit IV: Arrays: 1-D, 2-D, Searching, Basic Sorting Algorithms
(Bubble, Insertion and Selection), Pointers to the array, Command line
arguments.

Unit V: User Defined Data Types: Structures, enum, union

Unit VI: File handling Streams in C, Types of Files, File Input/ Output
Operations: Modes of file opening, Reading and writing the file,
Closing the files, using fflush ().
Books
Text Books :
[Link] C Programming Language: B. W. Kernighan and D. M. Ritchie, Second Edition, Pearson,
June 2015
[Link] in ANSI C: E. Balguruswami McGraw Hill

Reference Books
[Link] C: K. R. Venugopal and S. R. Prasad, Tata McGraw Hill
Scheme
Internal External Total

Theory 50 50(ESE) 100


(2 tests = 20M each)
(Continuous
Assessment = 10M)

Practical 25 25 50
Fundamentals of
Programming

UNIT-1
Unit 1
➢Algorithm and Flowchart for problem-solving
➢Introduction to C language
➢ Keywords
➢ Constant
➢Variable
➢Data types
➢Operators
➢Types of Statements
➢Decision Control Statement- if and Conditional operators.
What is Computing?

Computing is an operation that involves processing of data by making use of


computers.

Components of Computer System


What is a Computer
➢An electronic device that is designed to accept data, perform the required mathematical and
logical operations at high speed, and output the result. A computer accepts data, processes it, and
produces information.
➢Computers can also store data for later uses in appropriate storage devices, and retrieve
whenever it is necessary.
➢Modern computers are electronic devices used for a variety of purposes ranging from browsing
the web, writing documents, editing videos, creating applications, playing video games, etc.
➢They are designed to execute applications and provide a variety of solutions by combining
integrated hardware and software components.
Block diagram of Computer
Functional Units
1. Input Unit
2. Memory Unit
1. Primary Memory
2. Secondary Memory
3. Arithmetic and Logical Unit
4. Output Unit
5. Control Unit
Computer Programming
➢ The computer hardware cannot think and make decisions on its own. So, it
cannot be used to analyze a given set of data and find a solution on its own.
➢The hardware needs a software (a set of programs) to instruct what has to be
done.
➢A program is a set of instructions that is arranged in a sequence to guide a
computer to find a solution for the given problem.
➢ The process of writing a program is called programming.
Computer Programming
➢ Computer software is written by computer programmers using a programming
language.
➢ The programmer writes a set of instructions (program) using a specific
programming language. Such instructions are known as the source code.
➢ Another computer program called a compiler is then used on the source code,
to transform the instructions into a language that the computer can understand.
The result is an executable computer program, which is another name for
software.
Computer Language
➢ Digital devices have two stable states, which are referred to as zero
and one by convention
➢The binary number system has two digits, 0 and 1. A single digit (0 or
1) is called a bit, short for binary digit. A byte is made up of 8 bits.
➢Binary Language: Data and instructions (numbers, characters, strings,
etc.) are encoded as binary numbers - a series of bits (one or more bytes
made up of zeros and ones)
Programming Languages
➢ Computers can not use human languages, and programming in the binary
language of computers is a very difficult, tedious process
➢Therefore, most programs are written using a programming language and are
converted to the binary language used by the computer
Three major categories of programming languages:
◦ Machine Language
◦ Assembly Language
◦ High level Language

A programming language is a language specifically designed to express computations


that can be performed by the computer. Programming languages are used to express
algorithms or as a mode of human communication.
Machine Language
➢Natural language of a particular computer
➢Primitive instructions built into every computer
➢The instructions are in the form of binary code
➢Any other types of languages must be translated down to this level
Assembly Languages
➢English-like Abbreviations used for operations (Load R1, R8)
➢Assembly languages were developed to make programming easier
➢The computer cannot understand assembly language - a program called
assembler is used to convert assembly language programs into machine
code

The following instructions are a part of assembly language code to illustrate


addition of two numbers

MOV AX,4 Stores the value 4 in the AX register of CPU


MOV BX,6 Stores the value 6 in the BX register of CPU
ADD AX,BX Add the contents of AX and BX register. Store the result in AX register
High Level Languages
➢English-like and easy to learn and program
➢Common mathematical notation
➢Total Cost = Price + Tax;
➢area = 5 * 5 * 3.1415;

➢Java, C, C++, FORTRAN, VISUAL BASIC, PASCAL

The program written using any programming languages has to be converted


into machine language so that the computer can understand it. There are two
ways to do this: compile the program or interpret the program.
Compiling Source Code
➢A program written in a high-level language is called a source
program (or source code).
➢Since a computer cannot understand a source program. Program
called a compiler is used to translate the source program into a
machine language program called an object program.
➢The object program is often then linked with other supporting library
code before the object can be executed on the machine.

Source File Compiler Object File Linker Excutable File


Compiler
➢A compiler is a special type of program that transforms source code written in a
programming language (the source language) into machine language comprising of just
two digits- 1s and 0s (the target language).

➢The resultant code in 1s and 0s is known as the object code. The object code is the one
which will be used to create an executable program.

➢If the source code contains errors then the compiler will not be able to its intended task.

➢Errors that limit the compiler in understanding a program are called syntax errors. Syntax
errors are like spelling mistakes, typing mistakes, etc.

➢Another type of error is logic error which occurs when the program does not function
accurately. Logic errors are much harder to locate and correct.
Interpreter
➢Like the compiler, the interpreter also executes instructions written in a high-level
language.

➢While the compiler translates instructions written in high level programming


language directly into the machine language; the interpreter on the other hand,
translates the instructions into an intermediate form, which it then executes.

➢Usually, a compiled program executes faster than an interpreted program. However,


the big advantage of an interpreter is that it does not need to go through the
compilation stage during which machine instructions are generated.

➢This process can be time-consuming if the program is long. Moreover, the interpreter
can immediately execute high-level programs.
Difference between Compiler and Interpreter
Problem Solving
 The process of defining a problem, searching for relevant information and
resources about the problem, and of discovering, designing, and evaluating the
solutions for further opportunities.
 Includes:
▪ Finding an Answer to a Question
▪ Figuring out how to Perform a Task
▪ Figure out how to Make Things Work

 Not enough to know a particular programming language… Must be able to


solve problem…
 Very desirable to be a good Problem Solver in any discipline.
Solving Problem
➢Read the Problem: Understand the description of problem or scenario,
identifying the knowns and unkowns
➢Decide how to go about solving the problem: Determine what steps need to be
taken to reach the solution
➢Solve the Problem: Write the solution
➢Test the Answer: Make sure the answer is correct
Solving Computing Problems
In general, when we solve a computing problem we are taking some
inputs, processing (performing some actions on) the inputs, and then
outputting the solution or results.
This is the classic view of computer programming – computation as
calculation.
Four steps (UDIE) can be very effective when applied to solving
computing problems
Four Steps of Problem Solving
◦U – Understand the Problem
◦D – Devise a Good Plan to Solve
◦I – Implement the Plan
◦E – Evaluate the Solution
Step 1 - Understand the Problem
➢What is the Problem to be solved? What is the unknown? What is the condition? What is the
data? What is needed to solve the problem? What actions need to take place?
➢Identify the inputs and outputs
➢Identify the processes needed to produce the outputs from the given inputs
➢Draw a figure. Introduce suitable notation.
➢Isolate Principle parts of the problem.
Step 2 - Devise a Plan
➢Find connections between the knowns and unknowns.
➢Simplify: Break the problem into smaller sub-problems
➢Design a solution
➢Make a plan or list of actions to implement the solution
➢Algorithm / Flowchart / Psuedocode
Step 2 - Devise a Plan (cont.)
Algorithm
◦ A FINITE set of clear, executable steps that will eventually
terminate to produce the desired outcome
◦ Logical design used to solve problems – usually a list of
actions required to perform task
Pseudocode
◦ Written like program code but more “English Like” and
doesn’t have to conform to language syntax
Flowchart
◦ Diagram that visually represents the steps to be performed to
arrive at solution.
Step 3 - Implement the Plan
➢Implement in a Programming Language

➢Carry out the plan checking the preliminary results at each step.

➢Code A Little Test A lot


Step 4 - Evaluate the Solution
➢Run the Code
➢Check results repeatedly and thoroughly
➢Use numerous test cases or data sets
➢Use highly varied test case, including expected as well as and unexpected cases

➢Look for new solutions


➢Is there a better, easier, or more efficient solution

➢Can other problems be solved using these techniques?


Summary
U - Read the Problem Statement
◦ Identify the inputs, outputs, and processes

D - Decide how to Solve the Problem


◦ Create an Algorithm / Flowchart / Psuedocode

I - Program the Code


◦ Implement in Programming Language

E - Test the Solution


◦ Run the Code using numerous, varied test cases
ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into two
phases:
Problem solving phase
◦ produce an ordered sequence of steps that describe solution
of problem
◦ this sequence of steps is called an algorithm
Implementation phase
◦ implement the program in some programming language
Algorithm
➢A finite set of steps that must be followed to solve any problem is called
an algorithm.
➢Algorithm is generally developed before the actual coding is done.
➢It is written using English like language so that it is easily understandable even
by non-programmers.
➢Sometimes algorithms are written using pseudocodes, i.e. a language similar to
the programming language to be used.
Algorithm
Writing algorithm for solving a problem offers these advantages −
➢Promotes effective communication between team members
➢Enables analysis of problem at hand
➢Acts as blueprint for coding
➢Assists in debugging
➢Becomes part of software documentation for future reference during maintenance phase
Characteristics of Algorithm
➢Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and
their inputs/outputs should be clear and must lead to only one meaning.
➢Input − An algorithm should have 0 or more well-defined inputs.
➢Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
➢Finiteness − Algorithms must terminate after a finite number of steps.
➢Feasibility − Should be feasible with the available resources.
➢Independent − An algorithm should have step-by-step directions, which should be independent
of any programming code.
Steps in Problem Solving
First produce a general algorithm (one can use pseudocode)
Refine the algorithm successively to get step by step detailed
algorithm that is very close to a computer language.
Pseudocode & Algorithm
Example 1: Write an algorithm to determine a student’s final grade
and indicate whether he is passing or failing. The final grade is
calculated as the average of marks in 4 subjects.
Pseudocode & Algorithm
Pseudocode:
Input marks of 4 subjects
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
end
Example
Problem − Design an algorithm to add two numbers and display the
result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
The Flowchart
➢A graphical representation of the sequence of operations in a program.
➢Program flowcharts show the sequence of instructions in a single program or
subroutine. Different symbols are used to draw each type of flowchart.
The Flowchart
A Flowchart
◦ shows logic of an algorithm
◦ emphasizes individual steps and their interconnections
◦ e.g. control flow from one action to the next
Flowchart Symbols
Guidelines for Developing Flowcharts

These are some points to keep in mind while developing a flowchart −


➢Flowchart can have only one start and one stop symbol
➢On-page connectors are referenced using numbers
➢Off-page connectors are referenced using alphabets
➢General flow of processes is top to bottom or left to right
➢Arrows should not cross each other
Example-1
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<50

PRINT PRINT
“PASS” “FAIL”

STOP
#include<stdio.h>
void main()
{ int m1,m2,m3,m4;
float grade;
printf("enter marks in 4 subjects \n");
scanf("%d %d %d %d",&m1,&m2,&m3,&m4);
grade = (m1+m2+m3+m4)/4;
if (grade<50)
{ printf("FAIL \n"); }
else { printf("Pass \n"); }
}
Example 2
Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Pseudocode:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with 30
Print length in cm (LCM)
Example 2
Flowchart
Algorithm
Step 1: Input Lft START

Step 2: Lcm  Lft x 30


Input
Step 3: Print Lcm Lft

Lcm  Lft x 30

Print
LCM

STOP
Example 3
Write an algorithm and draw a flowchart that will read the two
sides of a rectangle and calculate its area.
Pseudocode
Input the width (W) and Length (L) of a rectangle
Calculate the area (A) by multiplying L with W
Print A
Example 3
Algorithm
START
Step 1: Input W,L
Step 2: A  L x W Input
W, L
Step 3: Print A

ALxW

Print
A

STOP
Example 4 No. is even or odd.

Pseudocode:

Algorithm:
Example 5
Write an algorithm and draw a flowchart that will calculate
the roots of a quadratic equation
ax2 + bx + c = 0
Hint: d = sqrt (b2 − 4ac ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Example 5
Pseudocode:
Input the coefficients (a, b, c) of the quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Example 5
START

Algorithm:
Input
Step 1: Input a, b, c a, b, c

Step 2: d  sqrt (b  b − 4  a  c )
d  sqrt(b x b – 4 x a x c)
Step 3: x1  (–b + d) / (2 x a)
x1 (–b + d) / (2 x a)
Step 4: x2  (–b – d) / (2 x a)
Step 5: Print x1, x2 X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP
DECISION STRUCTURES
➢The expression A>B is a logical expression
➢it describes a condition we want to test
Y N
➢if A>B is true (if A is greater than B) we take is
the action on left - print the value of A A>B
➢if A>B is false (if A is not greater than B) we
take the action on right- print the value of B PRINT PRINT
A B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
If A>B then
print A
else
Y N
is
print B
A>B
endif

Print Print
A B
Example 6
Write an algorithm that reads two values, determines the largest value and prints the
largest value with an identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
START

Example 6 Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value
is”, MAX

STOP
NESTED IFS
One of the alternatives within an IF–THEN–ELSE statement
◦ may involve further IF–THEN–ELSE statement
Example 7
Write an algorithm that reads three numbers and prints the value of the largest number.
Example 7
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Example 7
Flowchart: Draw the flowchart of the above
Algorithm.

Step 1: Input N1, N2, N3


Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2,
N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1,
N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”,
MAX
Example 7
Write an algorithm that reads
three numbers and prints the
value of the largest number.
Example 8
Write and algorithm and draw a flowchart to
a) read an employee name (NAME), overtime hours worked (OVERTIME),
hours absent (ABSENT) and
b) determine the bonus payment (PAYMENT).
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but  40 hours $40
>20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
else if ((OVERTIME–(2/3)*ABSENT > 30)&&
(OVERTIME–(2/3)*ABSENT<=40)) then
PAYMENT  40
else if ((OVERTIME–(2/3)*ABSENT > 20)&&
(OVERTIME–(2/3)*ABSENT <= 30)) then
PAYMENT  30
else if ((OVERTIME–(2/3)*ABSENT > 10) &&
OVERTIME–(2/3)*ABSENT <= 20)) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Bonus Schedule

Example 8 OVERTIME –
(2/3)*ABSENT
Bonus Paid

>40 hours $50


Flowchart: Draw the flowchart of >30 but  40 hours $40
the above algorithm >20 but  30 hours $30
>10 but  20 hours $20
 10 hours $10

You might also like