0% found this document useful (0 votes)
16 views50 pages

BPOPS103203 (1)

Uploaded by

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

BPOPS103203 (1)

Uploaded by

panchalsoham2626
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

KarnatakLawSociety’s

VishwanathraoDeshpande Institute of
TechnologyHaliyal–581329

PRINCIPLES OF PROGRAMMING
USING C [BPOPS103/203]
for
I/IISemesterB.E.
Asprescribedby
VISVESVARAYA TECHNOLOGICAL
UNIVERSITY,BELAGAVI–590014

(For the Academic Year 2022-2023)

Prepared by
Prof. Bheerappa S
Prof.Pranesh Kulkarni

DepartmentofComputerScience&Engineering

Dept. of CSE, KLS VDIT Haliyal. Page 1


Index
Sl.No. Content PageN
o.

1 Simulation of a Simple Calculator.


14
2 Compute the roots of a quadratic equation by accepting the
coefficients. Print appropriate messages. 17
3 An electricity board charges the following rates for the use of
electricity: for the first 200 units 80 paise per unit: 20
for the next 100 units 90 paise per unit: beyond 300 units Rs 1
per unit. All users are charged a minimum of Rs.
100 as meter charge. If the total amount is more than Rs 400,
then an additional surcharge of 15% of total amount
is charged. Write a program to read the name of the user,
number of units consumed and print out the charges.

4 Write a C Program to display the following by reading the


number of rows as input, 22
1
121
12321
1234321
---------------------------
nth row

5 Implement Binary Search on Integers.


23
6 Implement Matrix multiplication and validate the rules of
multiplication. 26
7 Compute sin(x)/cos(x) using Taylor series approximation.
Compare your result with the built-in library function. Print 31
both the results with appropriate inferences.

8 Sort the given set of N numbers using Bubble sort.


33
9 Write functions to implement string operations such as
compare, concatenate, and find string length. Use the 35
parameter passing techniques.

Dept. of CSE, KLS VDIT Haliyal. Page 2


10 Implement structures to read, write and compute average-
marks of the students, list the students scoring above 38
and below the average marks for a class of N students.

11 Develop a program using pointers to compute the sum, mean


and standard deviation of all elements stored in 41
an array of N real numbers.

12 Write a C program to copy a text file to another, read both the


input file name and target file name. 44
VirtualLabExperiments

1 Evaluation Flow of Expression 45


2 Understanding of Arrays
46

Dept. of CSE, KLS VDIT Haliyal. Page 3


PROGRAM OUTCOMES(POs)
Program Outcomes as defined by NBA (PO) Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project management and
finance: Demonstrate knowledge and
understanding of the engineering and
management principles and apply
these to one’s own work, as a member
and leader in a team, to manage
projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the
need for, and have the preparation and
ability to engage in independent and
life-long learning in the broadest
context of technological change.

Dept. of CSE, KLS VDIT Haliyal. Page 4


Vision (College)
To nurture talent and enrich society through excellence in technical education, research and
innovation.
Mission (College)
1.To augment innovative Pedagogy, kindle quest for interdisciplinary learning & to enhance
conceptual understanding.
2.To build competence,professional ethics & develop entrepreneurial thinking.
3.To strengthen Industry Institute Partnership & explore global collaborations.
4.To inculcate culture of socially responsible citizenship.
5.To focus on Holistic & Sustainable development.
Vision (Dept)

To achieve excellence in technical education, research, and innovation in computer science


and Engineering by emphasizing on global trending technologies.

Mission (Dept)
To train students with conceptual understanding through innovative pedagogies.
To imbibe professional, research, and entrepreneurial skills with a commitment to the
nation’s development at large.
To strengthen the industry-institute Interaction.
To promote life–long learning with a sense of societal & ethical responsibilities.
Program Educational Objectives ( PEO )
To develop an ability to identify and analyze the requirements of Computer
PEO1
Science and Engineering in design and providing novel engineering solutions.
To develop abilities to work in team on multidisciplinary projects with effective
PEO2
communication skills, ethical qualities and leadership roles.
To develop abilities for successful Computer Science Engineer and achieve
PEO3
higher career goals.
Program Specific Outcomes ( PSO )
To develop the ability to model real-world problems using appropriate data
PSO 1
structure and suitable algorithms in the area of Data Processing, System
Engineering, and Networking for varying complexity.
To develop an ability to use modern computer languages, environments and
PSO 2
platforms in creating innovative career.

Dept. of CSE, KLS VDIT Haliyal. Page 5


CO’s And PO’s Mapping Chart

Subject with code: PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203] AY: 2022-23

Semester: 1/II Sem


P
SL. PO PO PO PO PO PO PO PO PO PO PO PS PS
Description 1 2
O
4 5 6 7 8 9 10 11 12 O1 O2
No. 3
Elucidate the basic
architecture and
functionalities of a
1 3
computer and also
recognize
the hardware parts.
Apply programming
constructs of C language
2 3 2
to solve the real world
problem
Explore user-defined data
structures like arrays in
3 implementing solutions to 3
problems like searching
and sorting
Explore user-defined data
structures like structures,
4 3
unions and pointers in
implementing solutions
Design and Develop
Solutions to problems
5 using modular 3 2
programming constructs
using functions

Degree of compliance Low:1 Medium:2 High:3

Evaluation:

Particulars Marks Total


Performance 10
CIA Journal 03 15
Viva-voce 02
Lab IA 05 05
Grand Total 20

Dept. of CSE, KLS VDIT Haliyal. Page 6


Mapping of Experiments with CO, PO and PSO
Sl. Experiment Details CO PO
No
.
1 Simulation of a SimpleCalculator. 1 1
Compute the roots of a quadratic equation by accepting the coefficients. Print
2 1 1
appropriate messages.
An electricity board charges the following rates for the use of electricity: for
the first 200 units 80 paise per unit:
for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All
users are charged a minimum of Rs.
3 2 2
100 as meter charge. If the total amount is more than Rs 400, then an additional
surcharge of 15% of total amount
is charged. Write a program to read the name of the user, number of units
consumed and print out the charges.
Write a C Program to display the following by reading the number of rows as
input,
1
121
4 3 2
12321
1234321
---------------------------
nth row
5 Implement Binary Search on Integers. 3 2
6 Implement Matrix multiplication and validate the rules of multiplication. 3 2
Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with 3
7 5
the built-in library function. Print both the results with appropriate inferences.
8 Sort the given set of N numbers using Bubble sort. 4 2
Write functions to implement string operations such as compare, concatenate,
5 3
9 and find string length. Use theparameter passing techniques.
Implement structures to read, write and compute average- marks of the
10 students, list the students scoring aboveand below the average marks for a class 4 2
of N students.
Develop a program using pointers to compute the sum, mean and standard
11 deviation of all elements stored in 4 2
an array of N real numbers.
Write a C program to copy a text file to another, read both the input file name
12 4 2
and target file name.

Dept. of CSE, KLS VDIT Haliyal. Page 7


EXPERIMENT WISE LESSON PLAN
Experiment No.1
Name Simulation of a Simple Calculator.
Objective Demonstrating usage of switch statements.
s
Experiment No.2
Compute the roots of a quadratic equation by accepting the coefficients. Print
Name
appropriate messages.
Objective Demonstrating usage of decision making statements.
s
Experiment No.3
An electricity board charges the following rates for the use of electricity: for the first
200 units 80 paise per unit:for the next 100 units 90 paise per unit: beyond 300 units
Name Rs 1 per unit. All users are charged a minimum of Rs.100 as meter charge. If the
total amount is more than Rs 400, then an additional surcharge of 15% of total
amountis charged. Write a program to read the name of the user, number of units
consumed and print out the charges.
Objective Demonstrating usage of if statement.
s
Experiment No.4
Write a C Program to display the following by reading the number of rows as input,
1
121
Name 12321
1234321
---------------------------
nth row
Objective Discuss about usage of loop statements.
s
Experiment No. 5
Name Implement Binary Search on Integers.
Objective Demonstrate the usage of loops and array concepts.
s
Experiment No.6
Name Implement Matrix multiplication and validate the rules of multiplication.
Objective Demonstrate the usage of loops and array concepts.
s
Experiment No.7
Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with
Name
the built-in library function. Print both the results with appropriate inferences.
Objective Demonstration of the concepts of Built in functions and user defined functions.
s
Experiment No.8
Dept. of CSE, KLS VDIT Haliyal. Page 8
Name Sort the given set of N numbers using Bubble sort.
Objective Demonstrate the usage of loops and array concepts.
s
Experiment No.9
Write functions to implement string operations such as compare, concatenate, and find string length.
Name Use theparameter passing techniques.
Objective Demonstration of working of modular programming feature.
s
Experiment No.10
Implement structures to read, write and compute average- marks of the students, list
Name
the students scoring above and below the average marks for a class of N students.
Objective Demonstration of working with Structure datatype.
s
Experiment No.11
Develop a program using pointers to compute the sum, mean and standard deviation
Name
of all elements stored inan array of N real numbers.
Objective Demonstration of working with pointer.
s
Experiment No.12
Write a C program to copy a text file to another, read both the input file name and
Name
target file name.
Objective Demonstration of working usage of files and its functions.
s

Dept. of CSE, KLS VDIT Haliyal. Page 9


Problem Solving Techniques:
The process of working through details of a problem to reach a solution. There are three
approaches to problem solving:
Algorithm
Flowchart
Pseudo Code

Algorithm: The algorithm is a step-by-step procedure to be followed in solving a problem. It


provides a scheme to solve a particular problem in finite number of unambiguous steps. It
helps in implementing the solution of a problem using any of the programming languages.
In order to qualify as an algorithm, a sequence of instructions must possess the following
characteristics:
Definiteness: Instructions must be precise and unambiguous i.e. each and every instruction
should be clear and should have only one meaning.
Finiteness: Not even a single instruction must be repeated infinitely. i.e., each instruction
should be performed in finite time.
Termination: After the algorithm gets executed, the user should get the desired result

Key features of an algorithm:


Any algorithm has a finite number of steps and some steps may involve decision making,
repetition. Broadly speaking, an algorithm exhibits three key features that can be given as:
Sequence: Sequence means that each step of the algorithm is executed in the specified order.
Decision: Decision statements are used when the outcome of the process depends on some
condition.
Repetition: Repetition which involves executing one or more steps for a number of times can
be implemented using constructs like the while, do-while and for loops. These loops executed
one or more steps until some condition is true.
Example: To compute the Area of Rectangle
ALGM: AREA_of_RECTANGLE [This algorithm takes length and breadth, the sides of the
rectangle as input and computes the area of rectangle using the formula area=length * breadth.
Finally it prints the area of rectangle]

STEPS:
Step 1:[Initialize]
Start
Step 2: [Input the sides of Rectangle]
Read length, breadth Step 3:[Compute the area of rectangle]
Area length*breadth
Step 4:[Display the Area]
Print Area Step 5: [Finished]
Stop
Flowcharts: A flowchart is a graphical or symbolic representation of an algorithm. They are
basically used to design and develop complex programs to help the users to visualize the logic
of the program so that they can gain a better understanding of the program and find flaws,
bottlenecks, and other less- obvious features within it. Basically, a flowchart depicts the
“flow” of a program. The following table shows the symbols used in flowchart along with its
descriptions.

Dept. of CSE, KLS VDIT Haliyal. Page 10


Symbol Name Description
oval Represents the terminal point

Rectangle Represents the process steps defined in


algorithm

Indicate the reading Operation used for


Parallelogram input/output or

data or information from/to any device

Indicates the decisions (questions) and


Diamond consequently
branch points or the paths to be followed
based on the
result of the question

Arrows Shows the flowchart direction and


connects the various flow chart symbols.

Small circle Shows the continuation from one point


in the process flow to another.

Represents
Looping structures
Hexagon

Subroutine function
Process

Advantages of Flowcharts:
A flowchart is a diagrammatic representation that illustrates the sequence of steps that must
be performed to solve a problem. They are usually drawn in the early stages of formulating
computer solutions to facilitate communication between programmers and business people.
Flowcharts help programmers to understand the logic of complicated and lengthy problems.
They help to analyze the problem in a more effective manner
Flowchart can be used to debug programs that have error(s).
E.g.: To compute the Area of Rectangle
Limitations of using Flowcharts:
Drawing flowcharts is a laborious and a time consuming activity. Flowchart of a complex
program becomes, complex and clumsy. At times, a little bit of alteration in the solution may
require complete re- drawing of the flowchart Essentials of what is done may get lost in the
technical details of how it is done. There are no well-defined standards that limits the details
that must be incorporated in a flowchart

Dept. of CSE, KLS VDIT Haliyal. Page 11


start

Readlength&breadth

Area= length * breadth

Print Area

Stop

Pseudo code: It is a form of structured English that describes algorithms. It facilitates the
designers to focus on the logic of the algorithm without getting bogged down by the details of
language syntax. Pseudocode is a compact and informal high-level description of an algorithm
that uses the structural conventions of a programming language. It is meant for human reading
rather than machine reading, so it omits the details that are not essential for humans. Such
details include keywords, variable declarations, system-specific code and subroutines. There
are no standards defined for writing a pseudocode because it is not an executable program.
Flowcharts can be considered as a graphical alternative to pseudocode, but are more spacious
on paper.
E.g.: To compute the area of Rectangle Begin
Input length, breadth Area=length*breadth Print Area
End

Dept. of CSE, KLS VDIT Haliyal. Page 12


Familiarization with computer hardware and programming environment, concept of naming
the program files, storing, compilation, execution and debugging. Taking any simple C- code.

gcc compilation process

Dept. of CSE, KLS VDIT Haliyal. Page 13


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 1: Simulation of simple Calculator

PURPOSE : To simulate a calculator using arithmetic expressions


INPUT: Enter num1 and num2
OUTPUT: Print the result of addition or subtraction or multiplication or division or modulus.
ALGORITHM

Step 1: [Enter first number] read num1


Step 2: [Enter Second number] read num2
Step 3:[Enter Choice]
read choice
Step 4:[To perform addition]
if choice is equal to plus add num1 and num2 print result
Step 5: [To perform subtraction] if choice is equal to minus subtract num2 from num1 print
result
Step 6: [To perform multiplication]
if choice is equal to multiplication multiply num1 and num2
print result

Dept. of CSE, KLS VDIT Haliyal. Page 14


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART
START

Read num1 and


num2

Read choice

ifchoice=?

case'+'
result=num1+num2;

case '-'
result=num1-num2;

case '*'
result=num1*num2;

case'/' result=(float)num1/
(float)num2;

case '%' result=num1%num2;

default Invalidoperation.

Result

STOP

***************************************************************************
OUTPUT:
First run:
Enter first number: 10 Enter second number: 20
Choose operation to perform (+,-,*,/,%): + Result: 10 + 20 = 30.000000

Second run:
Enter first number: 10 Enter second number: 3
Choose operation to perform (+,-,*,/,%): / Result: 10 / 3 = 3.333333
Dept. of CSE, KLS VDIT Haliyal. Page 15
PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Third run:
Enter first number: 10 Enter second number: 3
Choose operation to perform (+,-,*,/,%): > Invalid operation.
Result: 10 > 3 = 0.000000
****************************************************************************
VIVA QUESTIONS:

What is Switch statement?


How many cases can you have in switch statement?
How switch statement works?
What does break in switch statement indicate?
What is a case in a switch statement?

Dept. of CSE, KLS VDIT Haliyal. Page 16


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 2

Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.

Purpose : To find roots of a given quadratic equation.


Input: Coefficients of quadratic equation a, b, c
Output: Root1 and Root2
ALGORITHM
START
STEP 1: [Input the values of a, b, c] read a, b, c
STEP 2: [Calculate the determinant] determinant = b*b-4*a*c
STEP 3: [Check for validity]
If a is equal to 0 and b is equal to 0
print “Invalid Inputs”
STEP 4: [Check for different roots]
If a is equal to 0
print “Linear equation”
Root1=-c/b
Print “Root1”
STEP 5: [Check for real and equal roots] If determinant is equal to 0 print “Roots are real and
equal” Root1= -b/(2*a)
Root2 = -b/(2*a)
Print “Root1 & Root2”
STEP 6: [Check for real and distinct roots] If determinant is greater than 0
Then print “Roots are real and distinct” Root1= (-b+ (sqrt (fabs (determinant))))/(2*a) Root2=
(-b-(sqrt (fabs (determinant))))/(2*a)
Print “root1 and root2”
End if
STEP 7: [Check for imaginary roots] print “Roots are imaginary” Real=-b/ (2*a)
Imaginary=sqrt (fabs (determinant))/ (2*a)
print “Root1 and R oot2”
STEP 8:STOP

Dept. of CSE, KLS VDIT Haliyal. Page 17


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART

Dept. of CSE, KLS VDIT Haliyal. Page 18


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

****************************************************************************
Output 1:
Enter the Co-efficient of Quadratic Equation 000
Invalid Coefficients

Output 2:
Enter the Co-efficient of Quadratic Equation
0 21
Root1=-0.5
Output 3:
Enter the Co-efficient of Quadratic Equation 121
Roots are Real and Equal Root1=-1.0000
Root2=-1.0000
Output 4:
Enter the Co-efficient of Quadratic Equation 189
Roots are Real and Distinct Root1=-1.354
Root2=-6.646
Output 5:
Enter the Co-efficient of Quadratic Equation 123
ROOTS ARE IMAGINARY ROOT1=-1.000+i1.414 ROOT2=-1.000-i1.414
****************************************************************************
Viva Questions:
What is quadratic Equation?
What is math.h?
What are decision making capabilities of C language?
Write the syntax of “ if ”statement?
Difference between if and switch statements?
Write an unconditional control statement in C.
Write a flowchart for „ if‟ conditional construct?

Dept. of CSE, KLS VDIT Haliyal. Page 19


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 3

An electricity board charges the following rates for the use of electricity: for the first 200 units
80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All
users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs
400, then an additional surcharge of 15% of total amount is charged. Write a program to read
the name of the user, number of units consumed and print out the charges.

PURPOSE: Read The Name Of The User, Number Of Units Consumed And Print
INPUT: name [10], unit
OUTPUT: Print the charges for total number of units consumed
ALGORITHM

START
STEP 1: [Input a name and units] read name and unit
STEP 2: [Initialization]
Metercharge=100
STEP3: [To check electricity unit is less than or equal to 200 and calculate metercharge] If unit
less than or equal to 200
metercharge= metercharge+(unit*.80)
STEP 4: [Else check unit is greater than 200 and greater than 300 and calculate metercharge] If
unit greater than 200 and unit greater than or equal to 300 metercharge=
metercharge+(200*0.80)+((unit-200)*0.90))
STEP 5: [Else check unit is greater than 300 and calculate metercharge]
If unit is greater than 300
metercharge= metercharge+(200*0.80)+(300*0.90)+((unit-300)*1)) STEP 6: [To check and
calculate if meter charge is greater than 400 ]
If metercharge greater than or equal to 400 metercharge=metercharge+(metercharge*0.15);
STEP 7: [Finished] STOP

Dept. of CSE, KLS VDIT Haliyal. Page 20


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOW CHART

****************************************
Output 1:
Enter your name and unit Consumed:Suresh 200 Name: Suresh
Number of unit consumed: 200 MeterCharge : 260.000
****************************************************************************
Viva Questions:
What is else-if ladder?

Dept. of CSE, KLS VDIT Haliyal. Page 21


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

What are the possible outputs of if statement?


What is conditional branching statement?

Laboratory Program 4
Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
nth row

#include <stdio.h>

int main() {
int n, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
for (k = i-1; k >= 1; k--) {
printf("%d ", k);
}
printf("\n");
}
return 0;
}

Output:
Enter the number of rows: 4
1
121
12321
1234321

Dept. of CSE, KLS VDIT Haliyal. Page 22


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 5

Implement Binary search on integers/names

PURPOSE: Implement Binary search


INPUT: n, a[50], key
OUTPUT: key is present at the position or key not found START
ALGORITHM

STEP 1: [Read no of elements] Read n


STEP 2: [Read the elements of the array]
for
i=0 to n
Read a[i]
End ith for loop
STEP 3: [Read the elements to be searched] Read key
STEP 4: [Initiliazation]
low = 0; high = n-1;
STEP 5: [ Checkwhearther low is less than of equal to high and calculate mid)
while
low<=high
mid= (low+ high)/2;
STEP 6: [Check if mid is equal to key ) If a[mid] is equal to key
STEP 7: [print the key position]
Print key is present at the position mid+1 STEP 8: [if key found initialize found is 1]
found=1
STEP 9: [check if mid is greater than key and calculate]
If mid is greater than key high = mid-1;
STEP 10: [Else]
low = mid+1;
STEP 11: [Check if key is not found] If not found
Print key not found STEP 12:[Finished]
STOP

Dept. of CSE, KLS VDIT Haliyal. Page 23


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART

Dept. of CSE, KLS VDIT Haliyal. Page 24


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

*************************************************************** Output 1:
Enter the number of elements in the array: 5 Enter the elements of the array:
10 20 30 40 50
Enter the key to be searched: 30 30 is present at the position = 3

Output 2:
Enter the number of elements in the array: 5 Enter the elements of the array:
10 20 30 40 50
Enter the key to be searched: 60 key not found
****************************************************************************
Viva Questions:
What is searching?
What are the types of searching?
Explain with an example for binary search. Explain with an example for linear search. Which
is better searching technique?

Dept. of CSE, KLS VDIT Haliyal. Page 25


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Lab program 06
Implement Matrix multiplication and validate the rules of multiplication.

PURPOSE : Multiply 2 matrices


INPUT: Two matrices
OUTPUT: Multiplication of 2 matrices

Dept. of CSE, KLS VDIT Haliyal. Page 26


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Dept. of CSE, KLS VDIT Haliyal. Page 27


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Dept. of CSE, KLS VDIT Haliyal. Page 28


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
printf("enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("enter the order of matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p) /*Checks whether n!=p*/
{
printf("Multiplication is not possible\n");
}
else
{
printf("enter the elements of matrix A\n");
for(i=0;i<m;i++) /*Read the elements of matrix A*/
for(j=0;j<n;j++)
{

printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
printf("enter the elements of matrix B\n");
for(i=0;i<p;i++) /*Read the elements of matrix B*/
for(j=0;j<q;j++)
{

printf("b[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}

printf("the read matrix A is\n");


for(i=0;i<m;i++) /*Print the elements of matrix A*/

{
for(j=0;j<n;j++)
{

printf("%d\t",a[i][j]);

} printf("\n");
}
printf("the read matrix B is\n"); /*Print the elements of matrix B*/

for(i=0;i<p;i++)

Dept. of CSE, KLS VDIT Haliyal. Page 29


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

{
for(j=0;j<q;j++)
{

printf("%d\t",b[i][j]);

} printf("\n");
}

for(i=0;i<m;i++)

{
for(j=0;j<q;j++)
{

c[i][j]=0;

for(k=0;k<p;k++)

c[i][j]+=(a[i][k]*b[k][j]); /*Multiply matrix A and B and


store result in C*/

}
}
printf("the resultant matrix is\n");
for(i=0;i<m;i++)

{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]); /*Print Resultant matrix C*/

} printf("\n");
}
}
}
[root@localhost ~]# cc matrixmult.c
[root@localhost ~]# ./a.out
enter the order of matrix A
3
3
enter the order of matrix B
3
3
enter the elements of matrix A
a[0][0]=1
a[0][1]=2
a[0][2]=3
Dept. of CSE, KLS VDIT Haliyal. Page 30
PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

a[1][0]=1
a[1][1]=2
a[1][2]=3
a[2][0]=1
a[2][1]=2
a[2][2]=3
enter the elements of matrix B
b[0][0]=1
b[0][1]=1
b[0][2]=1
b[1][0]=1
b[1][1]=1
b[1][2]=1
b[2][0]=1
b[2][1]=1
b[2][2]=1
the read matrix A is
1 2 3
1 2 3
1 2 3
the read matrix B is
1 1 1
1 1 1
1 1 1
the resultant matrix is
6 6 6
6 6 6
6 6 6
[root@localhost ~]# ./a.out
enter the order of matrix A
3
3
enter the order of matrix B
2
3
Multiplication is not possible
[root@localhost ~]#

Dept. of CSE, KLS VDIT Haliyal. Page 31


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 7

compute Sin(x)/Cos(x) using Taylor series approximation .Compare your result with the built-
in Library function. Print both the results with appropriate messages.

ALGORITHM

Algorithm : To compute Sin(x) using Taylor series approximation Purpose: To compute


sine(x) value using Taylor's series
Input: Degree ie. Value of x
Output : sine value for given degree START
Step 1:[Input value of degree] Read degree
Step 2: [Convert degree to radians] X=(degree *PI)/180
Step 3: [InitialCSE]
term =x sum=term
Step 4: [Calcuate each term and add] term=-term*x*x/((i-1)*i); sum=sum+term;
Step 5: [Print the output]
Print the sine value for the given degree
STOP

Dept. of CSE, KLS VDIT Haliyal. Page 32


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART

****************************************************************************
Output:
Enter the value of degree :90 The sine of 90 is 1.000
The sine function of 90 is 1.000
****************************************************************************
VIVA QUESTIONS:
What is pre-processor directive?
What is difference between const and #define.
What is use of fabs().

Dept. of CSE, KLS VDIT Haliyal. Page 33


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

What is variable initialization and why is it important?


What is the difference between the = symbol and == symbol?
Can the curly brackets { } be used to enclose a single line of code?

Laboratory Program 8
sort the given set of N numbers using Bubble sort.

ALGORITHM : Bubble sort


PURPOSE : Arranging the numbers in ascending order using bubble sort technique
INPUT : N, interger numbers in arrary a[i]
OUTPUT : Numbers are arranged in ascending order START
APPLICATION: Bubble sort can be beneficial to sort the unsorted elements in a specific order.
It can be used to sort the students on basis of their height in a line.
Algorithm:
step 1: start
step 2: [input number of elements]
read n
step 3: [input the elements/numbers into array]
for i=0 to n
read a[i]
end for
step 3 : [sorting the elements in ascending order]
for pass =o to n-1
for i=o to n-1
[compare the adjacent elements]
if(a[i]>a[i+1]) then
[swap these elements]
temp=a[i]
a[i] =a[i+1]
a[i+1] =temp
end if
end for
end for
step 4: [display the sorted elements of array]
for i=0 to n
print a[i]
end for
step 5: STOP

Dept. of CSE, KLS VDIT Haliyal. Page 34


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Flowchart:

Output 1:
Enter the no. of elements:5 5 4 3 2 1
The Sorted elements are 1 2 3 4 5
Dept. of CSE, KLS VDIT Haliyal. Page 35
PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

****************************************************************************
VIVA QUESTIONS:
Why the name bubble sort?
Mention the different types of sorting techniques?
Explain the logic of bubble sort with an example.
What is nested for loop?

Laboratory Program 9

Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.

ALGORITHM

Algorithm: string operations such as compare, concatenate, string length.


Purpose: To find String length, Compare two Strings, Concatenate two strings
Input: Two strings
Output: Length of string1
Concatenated String Comparison of two strings
START

STEP 1: [Store two strings in Str1 and Str2]

Str1=String1 and Str2=String2

STEP 2:[To find String Length] initialize i to 0 while(str[i] not equal to 0) increment i by 1
STEP 3: [To Compare two Strings] initialize i to 0
while (str1[i] is equal to str2[i]) if (str1[i] is equal to null) break;
increment i by 1 return str1[i]-str2[i];
STEP 4: [To Concatenate two Strings]

initialize i to 0 and j to 0 while (str1[i] not equal to null) increment i by 1


while (str2[j] not equal to null) str1[i++]=str2[j++];
STEP 5: [Output the results]

The length of the String res1=Comparision of two Strings res2= Concatenated String
STOP

Dept. of CSE, KLS VDIT Haliyal. Page 36


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART

Dept. of CSE, KLS VDIT Haliyal. Page 37


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Dept. of CSE, KLS VDIT Haliyal. Page 38


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

OUTPUT
The length of the string1=4
RAMA is greater than KRISHNA
concatinated string=RAMAKRISHNA

****************************************************************

VIVA QUESTIONS

What is string?
What are the built-in functions of string?
Difference between user defined functions and built in functions.
What is null character?
Explain the flow of program with example.

Dept. of CSE, KLS VDIT Haliyal. Page 39


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 10

Implement structures to read, write, compute average- marks and the students scoring above
and below the average marks for a class of N students.

ALGORITHM

PURPOSE: To Implement structures to read, write, compute average- marks and the students
scoring above and below the average marks for a class of N students.
Input: number of Students, name ,marks1,marks2,marks3
Output: Average marks and printing the students scoring above and below the average marks
START
STEP 1: [input number of students] Read n
STEP 2: [input details of students ie.name amd marks] Read name,m1,m2,m3
STEP 3: [ Calculate total and average]
for i=0 to n
s[i].total=(s[i].m1+s[i].m2+s[i].m3); T=T+s[i].total;
AVG=T/N;
STEP 4: [Find students above and below average]
for i=0 to n
aboveavg[j]=i;
j++;
else
belowavg[k]=i; k++;
STEP 5:[Finished]
STOP

Dept. of CSE, KLS VDIT Haliyal. Page 40


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOWCHART

Dept. of CSE, KLS VDIT Haliyal. Page 41


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

OUTPUT
Enter number of students. 3 Enter the details of students. Enter the details of student 1 Enter
name Anil
Enter m1,m2,m3 67 84 72
Enter the details of student 2 Enter name Sudhir
Enter m1,m2,m3 76 55 68
Enter the details of student 3 Enter name Suresh
Enter m1,m2,m3 58 79 92
Details of Students.

Name m2 m3
m1
Anil 84.0 72.0 22
000 0000 3.
67.000000
Sudhir 00
55.0 068.0 00
19
76.000000 000 0000 9.
Suresh 00
79.0 092.0 00
22
58.000000 000 0000 9.
AVG = 00 0 00
217.000000

Students scoring above avg. Anil 223.000000


Suresh 229.000000 Students scoring below avg. Sudhir 199.000000

Dept. of CSE, KLS VDIT Haliyal. Page 42


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 11

Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.

Purpose:To find sum,mean and standard deviation of all elements in an array


Input: An array of n elements
Output:Print the sum,mean and standard deviation of all elements in an array
ALGORITHM

Dept. of CSE, KLS VDIT Haliyal. Page 43


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

FLOW CHART:

Dept. of CSE, KLS VDIT Haliyal. Page 44


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

****************************************************************************
Output 1:
Enter the number of elements 5 Enter the array elements are 1 5 9 6 7 Sum=28
Mean=5.6
Standard deviation=2.09

****************************************************************************
Viva Questions:
Define pointer?
How do you declare a pointer variable?
What is * and & in pointer concept.
What are the advantages and disadvantages of using pointer?
Give the difference between static allocation and dynamic allocation of memory space.
What is the effect of the ++ and -- operators on pointer variable
Explain the pointers to arrays concept?

Dept. of CSE, KLS VDIT Haliyal. Page 45


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Laboratory Program 12

Write a C program to copy a text file to another, read both the input file name and target file
name.
Purpose:To copy a text file to another
Input: Read both the input file name and target file name
Output:Copy a text file to another #include <stdio.h>
int main() {
charinputFileName[50], targetFileName[50];
FILE *inputFile, *targetFile;
charch;

printf("Enter the name of the input file: ");


scanf("%s", inputFileName);

// Open input file in read mode


inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("Error opening input file.\n");
return 1;
}

printf("Enter the name of the target file: ");


scanf("%s", targetFileName);

// Open target file in write mode


targetFile = fopen(targetFileName, "w");
if (targetFile == NULL) {
printf("Error opening target file.\n");
fclose(inputFile);
return 1;
}
// Copy input file to target file
while ((ch = fgetc(inputFile)) != EOF) {
fputc(ch, targetFile);
}
printf("File copied successfully.\n");

// Close files
fclose(inputFile);
fclose(targetFile);
return 0;
}

Dept. of CSE, KLS VDIT Haliyal. Page 46


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Virtual Lab Experiments


EXPERIMENT:01
Evaluation Flow of Expression

AIM: To get familiar with expression evaluation.

Procedure for the experiment is as follows.

1. Select the type of operators and the datatype you want to work upon from the top most bar.
2. You can edit the values of variables by pressing the edit button.
3. Select an expression prototype from the menu.
4. You can also edit this expression.
5. You can also edit this expression.
6. Press Next to see the step by step evaluation of the selected expression in the central panel and the
corresponding reasoning in the right panel.
7. Press stop if you want to abort the experiment and start over.
Link:https://cse02-iiith.vlabs.ac.in/exp/cp-expression-evaluation/simulation.html

Fig1: Analysis of expression evaluation

Dept. of CSE, KLS VDIT Haliyal. Page 47


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

EXPERIMENT:02
Understanding of Arrays

AIM: To get familiar with array data type.

For Experiment 1:
Enter the size of array for selected problem and press OK.
Use Enter values to enter your own values in the box provided below for press Generate
random values to generate a set of random values.
Press Start to start the experiment.
Click Next to get a step by step execution of the code.

Simulation Link: https://cse02-iiith.vlabs.ac.in/exp/arrays/simulation.html

Fig1: Analysis of working of 1Darray.

For Experiment 2:
Enter the size of rows and columns for array A and press OK.
Press Generate random values to generate a set of random values.
Enter the size of columns for array B and press OK.
Press Generate random values to generate a set of random values.
Press Start to start the experiment.
Click Next to get a step by step execution of the code.

Dept. of CSE, KLS VDIT Haliyal. Page 48


PRINCIPLES OF PROGRAMMING USING C [BPOPS103/203]

Fig2: Analysis of working of 2D array.

Dept. of CSE, KLS VDIT Haliyal. Page 49

You might also like