BPOPS103203 (1)
BPOPS103203 (1)
VishwanathraoDeshpande Institute of
TechnologyHaliyal–581329
PRINCIPLES OF PROGRAMMING
USING C [BPOPS103/203]
for
I/IISemesterB.E.
Asprescribedby
VISVESVARAYA TECHNOLOGICAL
UNIVERSITY,BELAGAVI–590014
Prepared by
Prof. Bheerappa S
Prof.Pranesh Kulkarni
DepartmentofComputerScience&Engineering
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.
Evaluation:
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.
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
Readlength&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
FLOWCHART
START
Read choice
ifchoice=?
case'+'
result=num1+num2;
case '-'
result=num1-num2;
case '*'
result=num1*num2;
case'/' result=(float)num1/
(float)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:
Laboratory Program 2
Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print appropriate messages.
FLOWCHART
****************************************************************************
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?
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
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?
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
Laboratory Program 5
FLOWCHART
*************************************************************** 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?
Lab program 06
Implement Matrix multiplication and validate the rules of multiplication.
#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]);
}
{
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++)
{
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++)
}
}
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 ~]#
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
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().
Laboratory Program 8
sort the given set of N numbers using Bubble sort.
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
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]
The length of the String res1=Comparision of two Strings res2= Concatenated String
STOP
FLOWCHART
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.
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
FLOWCHART
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
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.
FLOW CHART:
****************************************************************************
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?
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;
// Close files
fclose(inputFile);
fclose(targetFile);
return 0;
}
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
EXPERIMENT:02
Understanding of Arrays
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.
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.