CPL - Bpops103
CPL - Bpops103
Of CS & BS BPOPS103/203
BELAGAVI – 590018
[ BPOPS103/203 ]
K. S. GROUP OF INSTITUTIONS
( Approved by AICTE, New Delhi ; Affiliated to VTU, Belagavi, Karnataka ; Accredited by NAAC )
www.kssem.edu.in
1
KSSEM Dept. Of CS & BS BPOPS103/203
Program 1
Problem Description
The problem is to simulate a commercial calculator with arithmetic operations such as addition,
subtraction, multiplication and division of two floating point numbers.
Step 1: Start
goto step
Step 9: Stop
2
KSSEM Dept. Of CS & BS BPOPS103/203
Flowchart
3
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to simulate calculator
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
char op;
float a,b,c;
printf("Enter two numbers \n");
scanf("%f%f",&a,&b);
printf("Enter the operator :");
scanf(" %c",&op);
switch(op)
{
case '+' : c = a + b; /* compute addition */
printf("%f + %f = %f\n", a, b,c);
break;
case '-' : c = a - b; /* compute addition */
printf("%f - %f = %f\n", a, b,c);
break;
case '*' : c = a * b; /* compute addition */
printf("%f * %f = %f\n", a, b,c);
break;
4
KSSEM Dept. Of CS & BS BPOPS103/203
else
{
printf("Expression is not valid\n");
}
break;
default : printf("Enter valid operator\n");
}
}
Run 1
./a.out
3.0 4.0
Run 2
./a.out
5.0 2.0
Run 3
./a.out
-5.0 9.0
5
KSSEM Dept. Of CS & BS BPOPS103/203
Run 4
./a.out
4.0 6.0
Run 5
./a.out
4.0 0.0
Run 6
./a.out
5.0 6.0
Conclusion
6
KSSEM Dept. Of CS & BS BPOPS103/203
Program 2
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
Problem Description
−𝑏
• If D = 0 then roots are real and equal𝑥 = 𝑎
.
−𝑏±√𝐷
• If D > 0 then roots are real and distinct. 𝑥 = 2𝑎
Step 6: If discriminant is equal to zero, Display the Roots are Real and
7
KSSEM Dept. Of CS & BS BPOPS103/203
Equal. Compute the roots of quadratic equation and display, otherwise goto next step
(x1=x2= -b/(a*2.0))
Step 7: If discriminant is greater than zero. Display the Roots are Real and Distinct
and Compute the roots of quadratic equation and display, otherwise goto next step
Step 8: If discriminant is lesser than zero, Display the Roots are Imaginary. Compute
the roots of quadratic equation and display, otherwise goto next step
Step 9: Stop
Flowchart
8
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to find roots of quadratic equation
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
9
KSSEM Dept. Of CS & BS BPOPS103/203
void main()
{
float a,b,c,x1,x2,disc,rp,ip;
printf("Enter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0||b==0||c==0)
{
printf("Quadratic equation is not possible\n");
exit(0);
}
disc = b*b - 4*a*c;
if(disc == 0)
{
x1 = x2 = -b/(2*a);
printf("The roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
}
else if(disc > 0)
{
x1 = (-b+sqrt(disc))/(2*a);
x2 = (-b-sqrt(disc))/(2*a);
printf("The roots are real and distinct\n");
printf("x1=%f\n x2= %f\n",x1,x2);
}
else
{
rp = -b/(2*a);
ip = sqrt(fabs(disc))/(2*a);
printf("The roots are complex\n");
printf("First root = %f + i %f\n",rp,ip);
printf("Second root = %f - i %f\n",rp,ip);
10
KSSEM Dept. Of CS & BS BPOPS103/203
}
}
Compilation
Execution
Run1 :
./a.out
121
x1 = -1.000000
x2 = -1.000000
Run2 :
./a.out
142
x1 = -0.585786
x2 = -3.414214
Run3 :
./a.out
11
KSSEM Dept. Of CS & BS BPOPS103/203
123
Run4 :
./a.out
034
a can not be 0
Conclusion
Program 3
An electricity board charges the following rates for the use of electricity: for the first 200 units 80
paisa per unit: for the next 100 units 90 paisa 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
12
KSSEM Dept. Of CS & BS BPOPS103/203
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.
Step 1: Start
Step 2: Declare the required variables
Step 3: Read customer name, units consumed as units
Step 4: metercharge = 100, charge = metercharge
Step 5: if units is less than or equal to 100 then compute charge and goto step 8 otherwise
goto next step
charge = charge + units * 0.8
Step 6: If units is greater than 200 and less than or equal to 300 then compute charge
and goto step 8 otherwise goto next step
charge = charge + 200 * 0.8 +(units – 200) * 0.9
Step 7: charge = charge + 200 * 0.8 + 100 * 0.9 + (units – 300) * 1
Step 8: If charge is greater than 400 add 15% of charge otherwise goto next step
charge = charge + (0.15*charge)
Step 9: Display Customer name, units, charge
Step 10: Stop
Flowchart
13
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to calculate electricity charges based on number of units consumed.
*/
#include<stdio.h>
void main()
{
int units;
float charges;
char name[20];
printf("Enter the name of customer\n");
scanf("%s",name);
printf("Enter the number of units consumed\n");
scanf("%d",&units);
charges = 100;
if(units > 0 && units <= 200)
14
KSSEM Dept. Of CS & BS BPOPS103/203
Compilation
cc electricity_bill.c
Execution
Run1 :
./a.out
Veena
156
Run2 :
./a.out
Vindya
15
KSSEM Dept. Of CS & BS BPOPS103/203
256
Run3 :
./a.out
Yeshas
301
Run4 :
./a.out
Gopal
399
16
KSSEM Dept. Of CS & BS BPOPS103/203
Conclusion
Program to calculate electricity charges based on units consumed is implemented and tested.
Program 5
Problem description:
Problem is to search for an element from a sorted list. Binary Search algorithm requires elements
to be arranged in either ascending or descending order.
Step 1: Start
Step 2: Declare the required variables
Step 3: Read number of elements as n
Step 4: Read n elements arranged in ascending order as a[0]..a[n-1]
Step 5: Read element to be searched as k
Step 6: Initialize variables ]
low = 0, high = n - 1
Step 7: while low is less than high repeat step 8 to 11 else goto step 12
Step 8: Compute mid
mid = (low + high)/2
Step 9: If element at a[mid] = k then goto step 13 else goto next step
Step 10: If element at a[mid] > k then compute high else goto next step
high = mid – 1
Step 11: If element at a[mid] < k then compute low
low = mid + 1
Step 12: Display Unsuccessful search goto step 14
17
KSSEM Dept. Of CS & BS BPOPS103/203
Flowchart
Program
/*
Program to implement Binary Search
18
KSSEM Dept. Of CS & BS BPOPS103/203
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[20],low,mid,high,i,n,k;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("Enter the element to be searched\n");
scanf("%d",&k);
low = 0;
high = n - 1;
while(low <= high)
{
mid = (low + high)/2;
if(a[mid] == k)
{
printf("Successful search, element %d found at %d\n",k,mid+1);
exit(0);
}
else if(a[mid] > k)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
printf("Unsuucessful search\n");
Compilation
cc binarysearch.c
Execution
Run 1 :
./a.out
19
KSSEM Dept. Of CS & BS BPOPS103/203
10 20 30 40 45
10
Run 2 :
./a.out
234
Unsuccessful search
Conclusion
Program 6
20
KSSEM Dept. Of CS & BS BPOPS103/203
Problem Description
An array of arrays is known as 2D array. It is also known as matrix which can be represented as
a table of rows and columns.
Matrix multiplication
1. Check if number of columns in the first matrix is equal to number of rows in the second
matrix.
2. Multiply the elements of each row of the first matrix by the elements of each column in
the second matrix.
3. Add the products.
Step 1: Start
Step 4: Compare is first matrix columns equal to second matrix rows if equal goto next step
c[i][j] = 0
21
KSSEM Dept. Of CS & BS BPOPS103/203
Step 9: Stop
Flowchart
Program
22
KSSEM Dept. Of CS & BS BPOPS103/203
/*
Multiply two matricies
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
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)
{
printf("Matrix multiplication is not possible\n");
exit(0);
}
printf("Enter the elements of matrix A\n");
for(i = 0 ; i < m; i++)
{
for(j = 0 ; j < n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B\n");
}
for(i=0 ; i < m ; i++)
{
for(j=0 ; j < q ; j++)
{
c[i][j] = 0;
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
23
KSSEM Dept. Of CS & BS BPOPS103/203
}
printf("Matrix A is\n");
for(i = 0 ; i < m; i++)
{
for(j = 0 ; j < n; j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Matrix B is\n");
Compilation
cc matrix.c
Execution
Run 1:
24
KSSEM Dept. Of CS & BS BPOPS103/203
./a.out
22
34
Run 2:
./a.out
22
22
12
34
12
34
Matrix A is
12
34
Matrix B is
12
25
KSSEM Dept. Of CS & BS BPOPS103/203
34
7 10
15 22
If we do not include & symbol in scanf function, then we get segmentation fault (core dumped)
error during execution.
Conclusion
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 interferences.
Problem description:
The problem is to find the value of Sin(x) function using Taylor series.
𝑂𝑝𝑝𝑜𝑠𝑖𝑡𝑒
𝑠𝑖𝑛(𝜃) = 𝐻𝑦𝑝𝑜𝑡𝑒𝑛𝑢𝑠𝑒
𝑑𝑒𝑔𝑟𝑒𝑒∗𝜋
𝑟𝑎𝑑𝑖𝑎𝑛 = 180
26
KSSEM Dept. Of CS & BS BPOPS103/203
𝑥3 𝑥5 𝑥7
𝑠𝑖𝑛𝑥 = 𝑥 − + − ±⋯
3! 5! 7!
𝑛! = 𝑛 ∗ (𝑛 − 1) ∗ (𝑛 − 2) ∗ … … ∗ 1
Algorithm: [Sin x]
Step 1: Start
Step 2: Declare the required variables
Step 3: Read the angle as degree
Step 4: Initialize variables
x = (degree * π )/180.0, num = x, den = 1, i = 2, term = num/den
Step 5: while absolute value of term is greater than 0.00001 repeat step 6 else goto step 7
Step 6: Compute the following
term = num/den
num = -num * x *x
den = den * i * (i+1)
sum = sum + term
i=i+2
Step 7: Display sum
Step 8: Stop
Flowchart
27
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to find sine value using Taylor series
*/
#include<stdio.h>
#include<math.h>
void main()
{
float degree,num,den,x,sum=0,term;
int i;
28
KSSEM Dept. Of CS & BS BPOPS103/203
Compilation
cc taylor.c -lm
Execution
Run1 :
./a.out
30
Run2 :
./a.out
45
29
KSSEM Dept. Of CS & BS BPOPS103/203
If function
ns from math library are used in program then compilation process should also include linking
math library files.
Conclusion
Program 9
Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
Problem description
Strings in c is represented as array of characters. The end of a string is recognized by a null (‘\0’)
character.
Counting all characters in a string until a null character gives the length of string.
Concatenation of two strings is appending the second string to the end of first string.
String comparison can be achieved by comparing character of one string with character of other
string.
Step 3: Read choice 1 – String compare, 2 – String length, 3 – String concatenate, 4 - Exit
30
KSSEM Dept. Of CS & BS BPOPS103/203
Step 6: call function to compare two strings, if function returns 0 display identical strings
c = compare(str1,str2)
Step 9: call function to find string length, display string length and goto step 13
len = length(str)
Step 10: if choice is equal to 3 goto next step else goto step 13
Step 12: Call function to concatenate two strings, display concatenated string and goto step 13
str = concatenate(str1,str2)
i=0
i=i+1
Step 1: Count the characters from starting character till null character
i=0
31
KSSEM Dept. Of CS & BS BPOPS103/203
i=i+1
return i
i = 0, j = 0
i=i+1
str1[i] = str2[j]
i=i+1
j=j+1
str1[i] = ‘\0’
return str1
Flowchart
32
KSSEM Dept. Of CS & BS BPOPS103/203
33
KSSEM Dept. Of CS & BS BPOPS103/203
Program
#include<stdio.h>
#include<stdlib.h>
int length(char str[]);
int compare(char str1[],char str2[]);
void concatenate(char str1[],char str2[]);
void main()
{
char str1[30],str2[30];
int choice,a;
printf("Enter 1 - String Comparision\n");
printf("Enter 2 - String Length\n");
printf("Enter 3 - String Conctenation\n");
printf("Enter 4 - Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter String 1 :");
scanf("%s",str1);
34
KSSEM Dept. Of CS & BS BPOPS103/203
}
break;
case 2: printf("Enter String :");
scanf("%s",str1);
a = length(str1);
printf("Length of %s = %d\n",str1,a);
break;
case 3: printf("Enter String 1 :");
scanf("%s",str1);
printf("Enter String 2 :");
scanf("%s",str2);
concatenate(str1,str2);
break;
case 4: exit(0);
break;
default : printf("Enter proper choice\n");
exit(0);
}
}
int length(char str[])
{
int i = 0;
while(str[i] !='\0')
{
i++;
}
return i;
}
int compare(char str1[],char str2[])
{
int i = 0;
35
KSSEM Dept. Of CS & BS BPOPS103/203
while(str1[i] == str2[i])
{
if(str1[i] == '\0' || str2[i] == '\0')
break;
i++;
}
return str1[i] - str2[i];
}
void concatenate(char str1[] ,char str2[])
{
int i = 0, j = 0;
while(str1[i] !='\0')
{
i++;
}
while(str2[j] !='\0')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
printf("Concatenated string is %s \n",str1);
}
Compilation
cc string.c
Execution
Run1:
./a.out
Enter 4 – Exit
36
KSSEM Dept. Of CS & BS BPOPS103/203
Run2:
./a.out
Enter 4 – Exit
Run3:
./a.out
Enter 4 – Exit
Length of Institute = 9
Run4:
./a.out
37
KSSEM Dept. Of CS & BS BPOPS103/203
Enter 4 – Exit
Enter String 2: EM
Run5:
./a.out
Enter 4 – Exit
In C if a function needs to return an array variable, then pointers or struct can be used, directly
arrays cannot be returned.
Printing a string, if %d is used instead of %s “Segmentation fault core dumped” error occurs.
Conclusion
Program to compare strings, finding length of a string and concatenation of strings are
implemented and tested
Program 08
38
KSSEM Dept. Of CS & BS BPOPS103/203
Problem description
Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are not in order.
Step 1: Start
Step 5: Compare the first two numbers, if first no is greater than second number, exchange the
numbers and compare second number and third number, if second number is greater than
third number exchange the numbers, this process continues until the n-1 elements
Step 8: Stop
Flowchart
39
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to implement bubble sort
*/
#include<stdio.h>
void main()
{
int a[20],i,j,temp,n;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("\nThe array before sorting\n");
for(i = 0 ; i < n ; i++)
printf("%d\t",a[i]);
40
KSSEM Dept. Of CS & BS BPOPS103/203
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("\nThe array after sorting\n");
for(i = 0 ; i < n ; i++)
printf("%d\t",a[i]);
Compilation
cc bubble.c
Execution
Run1 :
./a.out
54321
54321
12345
Run2 :
./a.out
41
KSSEM Dept. Of CS & BS BPOPS103/203
673
673
367
Semicolon should not be included at the end of for statement if some operations are done in body
of for loop.
Conclusion
Program 10
Implement structure to read, write and compute average marks and the students scoring above and
below the average marks for a class of N students.
Problem description
Problem is to read, write and compute average marks of students using structures.
A structure is a user defined data type in C/C++. A structure creates a data type that can be used
to group items of possibly different types into a single type.
Step 1: Start
42
KSSEM Dept. Of CS & BS BPOPS103/203
Step 4: for each student read the name, usn and marks and store it in structure stud
stud.name = name
stud.usn = usn
stud.marks = marks
avg = total/n
abooveavg[j] = i
j++
else
belowavg[j] = i
j++
Step 8:Stop
Flowchart
43
KSSEM Dept. Of CS & BS BPOPS103/203
Program
/*
Program to store student details
*/
#include<stdio.h>
struct student
{
char name[20];
char usn[10];
float marks;
44
KSSEM Dept. Of CS & BS BPOPS103/203
};
main()
{
int i,j,k,m,n,aboveavg[10],belowavg[10];
float N,T=0,AVG=0;
struct student s[10];
printf("Enter number of students.\n");
scanf("%d",&n);
N=(float)n;
printf("Enter the details of students.\n");
for (i=0;i<n;i++)
{
printf("Enter the details of student %d \n",i+1);
printf("Enter name \n");
scanf("%s",s[i].name);
printf("Enter USN \n");
scanf("%s",s[i].usn);
printf("Enter marks \n");
scanf("%f",&s[i].marks);
T = T + s[i].marks;
}
printf("Details of Students.\n");
printf("Name\t\t USN\t\t Marks\n");
for (i=0;i<n;i++)
printf("%s\t\t%s\t\t%f\n",s[i].name,s[i].usn,s[i].marks);
AVG=T/N;
printf("AVG = %f \n",AVG);
j=0;
k=0;
for (i=0;i<n;i++)
{
45
KSSEM Dept. Of CS & BS BPOPS103/203
Compilation
cc student.c
Execuation
Run1:
./a.out
46
KSSEM Dept. Of CS & BS BPOPS103/203
Enter name
Veena
Enter usn
1VI14CS01
Enter Marks
14
Enter name
Vinay
Enter usn
1VI14CS05
Enter marks
16
Enter name
Naveen
Enter usn
1VI14CS06
Enter marks
47
KSSEM Dept. Of CS & BS BPOPS103/203
16
Details of students
Veena 1VI14CS01 14
Vinay 1VI14CS05 16
Naveen 1VI14CS06 16
Vinay 16.000000
Naveen 16.000000
Veena 14.000000
Derived data types like structures can be used to store heterogeneous data.
Conclusion
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.
Problem description
Problem is to compute sum, mean and standard deviation of all elements in an array.
A pointer is an address. It is a derived data type that stores the memory address. A pointer can also
be used to refer another pointer, function. A pointer can be incremented/ decremented, i.e., to point
to the next/ previous memory location.
48
KSSEM Dept. Of CS & BS BPOPS103/203
For i = 0 to n
Read a[i]
For i = 0 to n
increment ptr by 1
mean = sum/n
ptr = a
increment ptr by 1
std = sqrt(sumstd/n)
49
KSSEM Dept. Of CS & BS BPOPS103/203
Flowchart
Program
#include <stdio.h>
#include <math.h>
void main()
{
float a[10], *ptr, mean, sum=0, std,sumstd=0;
int n,i;
50
KSSEM Dept. Of CS & BS BPOPS103/203
Compilation
51
KSSEM Dept. Of CS & BS BPOPS103/203
cc stddeviation.c -lm
Execution
Run1:
./a.out
12345
Sum = 15.000000
Mean = 3.000000
6 4 2 8 10 12 14
Sum = 56.000000
Mean = 8.000000
52
KSSEM Dept. Of CS & BS BPOPS103/203
Program 04 :
Write a C program to display the following by reading the number of rows as input :
1 2 1
1 2 3 2 1
Code :
53
KSSEM Dept. Of CS & BS BPOPS103/203
Program 12 :
Write a C program to copy a text file to another, read both the input filename and the
target file name.
54
KSSEM Dept. Of CS & BS BPOPS103/203
Code :
55