22bpops103 203 Lab Manual
22bpops103 203 Lab Manual
************************************************
APS College of Engineering
Somanahalli, Kanakapura Road, Bangalore-82
Department of Computer Science and Engineering
Name
USN
Section
Lab Batch
Day / Time
************************************************
Expected output: The program performs the operation based on the given operator and prints the
result of addition, subtraction, multiplication & division.
Algorithm
Step1: [Begin]
Start
Step 2: [Input two numbers]
Read num1, num2
Step 3: [Input operator]
Read operator
Step 4: [Switch to the relevant case]
case ‘+’: result num1+num2
display result
goto step 4
case ‘-’: result num1-num2
display result
goto step 4
case ‘*’: result num1*num2
display result
goto step 4
case ‘/’: if(num2!=0)
result num1/num2
display result
else
div 1
end if
goto step 4
default: Display “ Error in Operation”
goto step 4
if (div equals 1)
display “Divide by Zero Error”
end if
Step 5: [Finished]
Stop
Flowchart
#include<stdio.h>
void main()
{
char operator;
float num1,num2,result,div;
printf("Simulation of a Simple Calculator\
n");
printf("*********************************\n");
printf("Enter two numbers \n"); scanf("%f
%f",&num1,&num2);
printf("Enter the operator [+,-,*,/] \
n"); scanf("%s",&operator);
switch(operator)
{
case '+': result = num1 + num2;
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2,
result); break;
case '-': result = num1 - num2;
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2,
result); break;
case '*': result = num1 * num2;
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2,
result); break;
case '/': if(num2!=0)
{
result = num1 / num2;
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}
else
{
div=1;
}
break;
default : printf("Error in operation\n");
}
if(div==1)
{
printf("Divide by Zero Error\n");
}
}
Lab Program 2: Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
Objective: To understand the decision making constructs and find the roots of a quadratic equation
Expected output: This program computes all possible roots for a given set of coefficients with
appropriate messages. The possible roots are: Linear equation and its roots, Real & equal roots, Real &
distinct roots and Imaginary roots.
Algorithm
Step 1: [Begin]
Start
Step 2: [Input the coefficients of quadratic equation]
Read a, b, c.
Step 3:[Check if a equals 0 and b equals
0] Display “invalid Coefficients”
goto step8
Step 4: [Check if a equals 0]
Display “ Linear Equation”
root1 -c/b
display root1
goto step8
step 5: [Compute disc]
disc b*b - (4*a*c)
Step 6: [Check whether disc is equal to 0]
if disc equals 0
display “ Roots are real and equal”
compute root1 root2 -b/(2*a)
display “ root1 and root2”
goto step8
Step 7: [Check whether disc is greater than 0]
if disc greater than 0
Display “ Roots are real and distinct”
compute root1 (-b+sqrt( disc))/(2*a)
compute root2 (-b-sqrt( disc))/(2*a)
Display “ root1 and root2”
goto step8
Step 8: [Otherwise display imaginary roots]
Display “ Roots are imaginary”
compute real -b/(2*a)
compute imag sqrt(fabs( disc))/(2*a)
Display “ root1 and root2 with real and imaginary values in a+bi format”
goto step8
Step 9: [Finished]
Stop
Flowchart
#include<stdio.h>
#include<math.h>
void main()
{ float a,b,c,disc,root1,root2,real,imag;
printf("Enter value for a : ");
scanf("%f",&a);
printf("Enter value for b : ");
scanf("%f",&b);
printf("Enter value for c : ");
scanf("%f",&c);
if((a==0)&&(b==0))
{
printf("Invalid coefficients \n");
printf("\n Try again with valid inputs");
}
else if(a==0)
{
}
else
{ printf("Linear equation\n");
root1=-c/b;
printf("Root = %.3f\n",root1);
disc=b*b-(4*a*c);
if(disc==0)
{
printf("The roots are real and equal \n");
root1=root2=-b/(2*a);
printf("Root 1 = %.3f\n Root 2 = %.3f\n",root1,root2);
}
else if(disc>0)
{
printf("The roots are real and distinct \n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root 1 = %.3f\n Root 2 = %.3f\n ",root1,root2);
}
else
Algorithm
Step 1: [Start]
Begin
Flowchart
#include<stdio.h>
void main()
{
char name[20];
int n;
float amount;
printf("Enter the consumer name\n");
scanf("%s", &name);
printf("Enter no. of units consumed \n");
scanf("%d",&n);
if (n<=200)
{
amount=n*80;
}
else if(n>200 && n<=300)
{
amount=200*80;
amount=amount+(n-200)*90;
}
else
{
amount=(n-300)*100;
amount=amount+100*90;
amount=amount+200*80;
}
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n); for(i=0;i<=n;i+
+)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
printf(" ");
/* Display number in ascending order upto middle*/ for(j=1;j<=i;j+
+)
printf("%d",j);
printf("\n");
}
}
Lab Program 5 Implement Binary Search on Integers
Start
end for
low 0
high n-1
Step 6: [Do until low<=high]
Step 8: [Finished]
Stop
Program
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
void main()
{
Int number[10]
[20],key[20]; int
n,i,low,high,mid;
printf("\nEnter the number of names to read : ");
scanf("%d",&n);
printf("\nEnter the number in ascending order
\n");for(i=0;i<n;i++)
scanf("%s",&number[i]); printf("\
nEnter the number to be searched :
");scanf("%s",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if (strcmp(number[mid],key)==0)
{
printf("\nnumber found in position :
%d\n",mid+1);exit (0);
}
else if(strcmp(number
[mid],key)<0)low=mid+1;
else
}
high=mid-1;
printf("\n Number not found. \n");
}
Lab Program 6 Implement Matrix multiplication and validate the rules of multiplication
Objective: To find the product of two matrices
Algorithm
Step1: [Initialize]
Start
end for
read b[i][j]
end for
end for
Step 7: [Perform multiplication of matrix a and b]
end for
end for
Step 8: [Print matrix A]
end for
end for
do
}
else
{
printf("Matrix multiplication not possible as Columns of A not equal to Rows of B.\n");
for(j=0;j<n;j++)
{
printf("Enter the element for A[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\t\t Enter elements for matrix B in Column Major order.\n");
for(j=0;j<q;j++)
{
for(i=0;i<p;i++)
{
{
for(j=0;j<n;j++)
{
printf(" %d \t",a[i][j]);
}
printf("\n ");
}
printf("The Matrix B is : \n ");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(" %d \t",b[i][j]);
}
printf("\n ");
}
APSCE, Dept. of CSE 2022-23
printf(" The multiplied Matrix C is :\n ");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(" %d \t",c[i][j]);
}
printf("\n ");
}
}
printf(" \n \t\t....Press Any Key to EXIT ... \n\n");
}
Lab 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 inferences.
Objective: To understand the implementation of sin(x) series using Taylor series
Algorithm
Step 1: [Begin]
Start
Step 2: Set pi to 3.142
Step 3: [Initialize sum to 0]
Sum 0
Step 4: [Input
degree]
Read degree
Step 5: [Convert degree to
radian] x
degree*(pi/180)
Step 6: [Assign x value to numerator]
nume x
i 2
Step 9: [Repeat until true]
term nume/deno
nume -nume *x*x
deno deno * i * (i+1)
sum sum+term
i i+2
do until while (fabs(term)>=0.00001)
Step 10: [Print sin of degree]
Display sum
Step 11: [Print sin(degree) using built-in function for comparison]
Display sin(x)
Step 12: [Finished]
Stop
Flowchart
#include <stdio.h>
#include <math.h>
#define pi 3.142
void main()
{
int i,degree;
float x,sum=0,term,nume,deno;
printf("\n Enter the value of degree : ");
scanf("%d", °ree);
x=degree*(pi/180);
nume=x;
deno=1;
i=2;
do
{
term=nume/deno;
nume=-nume*x*x;
deno=deno*i*(i+1);
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.00001);
printf("\n The sine of %d is %f",degree,sum);
printf("\n The sine of %d using inbuilt function sin(%d) is %f",degree,degree,sin(x)); printf("\
nPress any key to EXIT...\n");
Lab Program 8 Sort the given set of N numbers using Bubble sort.
Algorithm
Flowchart
Program
#include<stdio.h>
int main()
{
int num[20],n,i,j,temp;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers one by one: ");
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
printf("The Sorted items are: \n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
printf("\n");
return 0;
}
Lab Program 9 Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques.
Objective: To compare two strings, to concatenate two strings and to find string length using
functions.
Input: Choice of the user to perform string operations and two strings.
Output: Based on users choice programs outputs two strings and displays whether they are equal or
not, programs concatenates two strings, programs finds the length of the given string.
else
endif
i=i+1
return -1
endif
Flowchart
Program
#include <stdio.h>
void stringlength(char []);
void concatenate(char [], char []);
int stringcmp(char [], char []);
int main()
{
char p[100], q[100],temp;
int choice;
printf("Enter Your Choice\n1-String Length\n2-String Concatenate\n3-String Comparision\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Input a string to find its length:");
scanf("%s",p);
stringlength(p);
break;
case 2:printf("Input a string:");
scanf("%s",p);
printf("Input a string to concatenate:");
scanf("%s",q);
concatenate(p, q);
printf("String obtained on concatenation: \"%s\"\n", p);
break;
case 3:printf("Comparision of two strings\n");
printf("Input string1\n");
scanf("%s",p);
printf("Input string2 \n");
scanf("%s",q);
if (stringcmp(p,q) == 0)
printf("The strings are equal.\n");
else
printf("The strings are not equal.\n");
break;
default: printf("Invalid Choice");
}
return 0;
}
void stringlength(char p[])
{
int len=0;
while (p[len] != '\0')
len++;
printf("The Length of the given String is %d\n",len);
}
void concatenate(char p[], char q[])
{
int c, d;
c = 0;
while (p[c] != '\0')
{
c++;
}
d = 0;
while (q[d] != '\0')
{
p[c] = q[d]; d+
+;
c++;
}
p[c] = '\0';
}
else
return -1;
Lab Program 10 Implement structures to read, write and compute average- marks of the students, list the
students scoring above and below the average marks for a class of N students.
Expected Output: Average marks of the student, students scoring above & below the average marks.
Algorithm
Algorithm:
Step 1: [Initialize]
Total_marks=0
Step 2: [Read number of students]
Read n
Step 3: [Input the n Students details-rollno,name&marks]
For i
0 to n-1 in steps of 1
Read s[i].rollno,s[i].name,s[i].marks
End for
Step 4: [Find average marks]
For i 0
to n-1 in steps of 1
Total_marks=Total_marks+1
End for
Avg=Total_marks / n
s
[
i
]
.
m
a
r
k
s
<
A
v
g
Flowchart
Program
#include<stdio.h>
struct student
{
int rollno,marks;
char name[20];
};
int main()
{
int i,n;
float total=0,avg;
struct student s[10];
char sname[20];
printf("\n Enter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter details of student %d ",i+1);
printf("\n\n Enter the roll number : ");
scanf("%d",&s[i].rollno);
printf("\n Enter the name: ");
scanf("%s",s[i].name);
printf("\n Enter the marks : ");
scanf("%d",&s[i].marks);
total=total+s[i].marks; printf("\
n");
}
avg=total/n;
printf("\nThe average marks is %f\n",avg);
printf("\n Details of students scoring above average marks are : \n\n");
printf("\n Roll no.\tName\t\tMarks\n");
for(i=0;i<n;i++)
{
if(s[i].marks>=avg)
printf("\n %d\t\t%s\t\t%d\n",s[i].rollno,s[i].name,s[i].marks);
}
printf("\n Details of students scoring below average marks are : \n\n");
printf("\n Roll no.\tName\t\tMarks\n");
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
printf("\n %d\t\t%s\t\t%d\n",s[i].rollno,s[i].name,s[i].marks);
}
return 0;
Lab 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.
Mean: is the average of the numbers. i.e. the sum of a collection of numbers divided by the number of
numbers in the collection
Standard deviation (SD): measure the amount of variation or dispersion from the average. For a finite
set of numbers, the standard deviation is found by taking the square root of the average of the squared
differences of the values from their average value.
Expected Output: Compute and display sum, mean & standard deviation
Algorithm
Step 1: [Start]
Begin
Step 2:
[Initialize]
sum=0, sumstd=0
Step 2: [Input the number of elements in the array]
Read n
Step 3: [Input the n real numbers]
0
For i
to n-1 in steps of 1
Read a[i]
End for
Step 4: [Compute sum, mean, standard deviation]
ptr=a
For i←0 thru n in steps of 1
Sum=sum+*ptr
ptr++
End for
Mean=sum/n
ptr=a
For i←0 thru n in steps of 1
sumstd=sumstd+pow((*ptr-mean),2)
ptr++
End for
std= sqrt(sumstd/n)
Step 5: [Output]
Print sum, mean, standard deviation
Step 6: [Stop]
End
Princip les of Progamming Using C (BPOPS103)
Flowchart
sum=O,sumstd=-0
Plr-a
Fa For ( Tr
sum=sum+
mean=s i
lse 0 ;i<n;i+
ue
'PIT
+)wnn
ptr-a1
ptr-ptr+
F sumstd=sumst
std
For
al d +(<n;i++)
i=O ;t
sqrt(su
pow(\ptr -
s mstdln)
mean),2)
e Print
ptr-ptr+1
sum.mean,
standard
deviation
Program
#include<stdio.h>
#include<math.h>
void main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd + pow((*ptr - mean),2);
ptr++;
}
std= sqrt(sumstd/n); printf("Sum=%.3f\
t",sum); printf("Mean=%.3f\t",mean);
printf("Standard deviation=%.3f\n",std);
Lab Program 12. Write a C program to copy a text file to another, read both the input file name and
target file name.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
INTRODUCTION
An operating system is a program that controls the system’s hardware and interacts with user and
application software. The commands are translated into binary code that the machine understands. The
OS facilitates display of results on screen or printed. It manages processing, memory, devices, files and
security etc.
About Linux:
Linux is an operating system, a software program that controls our computer. Most vendors
load an operating system onto the hard drive of a PC before delivering the PC, so, unless the hard
driveof our PC has failed, we may not understand the function of an operating system.
Linux is not public domain, nor is it `shareware'. It is `free' software, commonly called
freeware or Open Source Software. Linux runs on 386/486/Pentium machines. Linux is only the
kernelof the operating system, the part that controls hardware, manages files, separates processes, and
so forth. There are several combination of Linux with sets of utilities and applications to form a
complete operating system. Each of these combination is called a distribution of Linux. The word
Linux, though it in its strictest form refers specifically to the kernel, is also widely and correctly to
refer to an entire operating system built around the Linux kernel.
One thing to be aware of is that Linux is developed using an open and distributed model,
instead of a closed and centralized model like much other software. This means that the current
development version is always public, so that anybody can use it. The result is that whenever a
version with new functionality is released, it almost always contains bugs, but it also results in a very
rapid development so that the bugs are found and corrected quickly, often in hours, as many people
work to fix them.
In contrast, the closed and centralized model means that there is only one person or team
working on the project, and they only release software that they think is working well. Often this leads
to long intervals between releases, long waiting for bug fixes, and slower development. The latest
release of such software to the public is sometimes of higher quality, but the development speed is
generally much slower.
Linux Features
Multiuser: Several users can use the same machine at the same time
Multithreading: Has native kernel support for multiple independent threads of control within a
single process memory space.
Demand loads executables: Linux only reads from disk those parts of a program that are actually used.
Shared copy-on-write pages among executables. This means that multiple process can use the same
memory to run in. When one trie7s to write to that memory, that page is copied somewhere else.
Copy-on-write has two benefits: increasing speed and decreasing memory use.
All source code is available, including the whole kernel and all drivers, the development tools and
all user programs; also, all of it is freely distributable. Plenty of commercial programs are being
provided for Linux without source, but everything that has been free, including the entire base
operating system, is still free.
I. Linux is a cross-platform operating system that runs on many computer models. Only Unix,
an ancestor of Linux, rivals Linux in this respect. In comparison, Windows 95 and Windows
98 run only on CPUs having the Intel architecture. Windows NT runs only on CPUs having
the Intel architecture or the DEC Alpha.
II. Linux is free, in two senses. First, We may pay nothing to obtain and use Linux. On the
other hand, we may choose to purchase Linux from a vendor who bundles Linux with
special documentation or applications, or who provides technical support. However, even
inthis case,
the cost of Linux is likely to be a fraction of what we'd pay for another operating system. So,
Linux is free or nearly free in an economic sense.
Second, and more important, Linux and many Linux applications are distributed in source
form. This makes it possible for us and others to modify or improve them. we're not free to
do this with most operating systems, which are distributed in binary form.
Example: Linux will likely be the first operating system to support Intel's forthcoming
Merced 64-bit CPU.
III. Linux has attractive features and performance. Free access to Linux source code lets
programmers around the world implement new features, and tweak Linux to improve its
performance and reliability. The best of these features and tweaks are incorporated in the
standard Linux kernel or made available as kernel patches or applications. Not even
Microsoft can mobilize and support a software development team as large and dedicated as
the volunteer Linux software development team, which numbers in the hundreds of
thousands, including programmers, code reviewers, and testers.
LINUX COMMANDS
1.cd(Change Directory)
cd [dir]:Change the current directory to dir. The variable HOME is the default dir.
2.cd ..
option "lm" is used to link all the supporting functions(ex: pow, sqrt etc function in
math.h).
8. ./a.out
9. exit
:
esc enter
i
C
M
D
M
o
d
e
vi :
m x
,
:
She y
ll
&
^
Z
Fig 1: Switching between modes
Algorithm:
Step 1. [Initialise] Start
Step 2. [Input an integer number] Read num
Step 3. res =isprime(num)
Step 4. [Output] If (res = 1) Print(“The entered number is a prime”)
Else
Print(“The entered number is not prime”)
Step 5. [Stop] End