0% found this document useful (4 votes)
7K views48 pages

BCS402 Lab Manual

Microcontroller lab Manual for 4th sem CSE VTU

Uploaded by

Anitha T G Raj
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 (4 votes)
7K views48 pages

BCS402 Lab Manual

Microcontroller lab Manual for 4th sem CSE VTU

Uploaded by

Anitha T G Raj
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/ 48

SAPTHAGIRI COLLEGE OF ENGINEERING

(Affiliated to VTU, Belagavi, Approved bu AICTE, New Delhi)


Accredited by NAAC with ‘A’ grade & Accredited by NBA
#14/5, Chikkasandra, Hesaraghatta Main Road, Bangalore – 560057
Phone:080-28372800/1/2 www.sapthagiri.edu.in Fax: 080-
28372797

LAB MANUAL
(2023-24) EVEN SEM

BPOPS103
MICROCONTROLLERS
LABORATORY

IV Semester

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING

Name:

USN:

Batch:
VISVESVARAYA TECHNOLOGICAL UNIVERSITY
JnanaSangama, Belagavi, Karnataka–590018

IV SEMESTER / B.E.
BCS402
MICROCONTROLLER LABORATORY

Prepared By:
DR.ANITHA T G PROF.KOKILA B P
PROFESSOR ASSISTANT PROFESSOR
VISION AND MISSION OF INSTITUTE

VISION
To be a best institution imparting quality engineering education to deal with community
needs through learning and performance.

MISSION
 To implement path breaking student centric education methods.
 To augment talent, nurture teamwork to transform to develop individual as
responsible citizen.
 To educate the students and faculties about entrepreneurship to meet vibrant
requirements of the society.
 Strengthen Industry-Institute Interaction for knowledge sharing

VISION AND MISSION OF DEPARTMENT

Vision

 To be a potential quality source of expert computer engineers, addressing


the requirements of industry and civic demands at large.

Mission
 To enable learning in emerging technologies adopting innovative methods.
 To enhance ability, cultivate collaboration to bring change in building
students approach towards society.
 To make available platform for harnessing entrepreneurial and leadership qualities.
 To partner continuously with industry to inculcate practical knowledge

PROGRAM EDUCATIONAL OBJECTIVES (PEO)

The program educational objectives of Bachelor of Engineering in Computer Science &


Engineering at Sapthagiri College of Engineering are broadly defined on following four
counts.

PEO 1: Graduates will have the expertise in analyzing real time problems and providing
appropriate solutions related to Computer Science & Engineering.

PEO 2: Graduates will have the knowledge of fundamental principles and innovative
technologies to succeed in higher studies, and research.

PEO 3: Graduates will continue to learn and to adapt technology developments combined
with deep awareness of ethical responsibilities in profession
PROGRAM SPECIFIC OUTCOMES(PSO)

The graduates of Computer Science and Engineering program at Sapthagiri College of


engineering will be able to attain the following at the time of graduation.

PSO 1: Apply the knowledge gained from Mathematics, Basic Computing, Basic Sciences
and Social Sciences in general and all computer science courses in particular to
identify, formulate and solve real life engineering problems.

PSO2: Identify the various analysis & design methodologies for facilitating development
of high quality system software products with focus on performance optimization.

PROGRAM OUTCOMES(POs)

Graduation of Bachelor of Computer Science and Engineering program at Sapthagiri


College of Engineering will attain the following program outcomes in the field of Computer
Science & Engineering.

PO1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex
engineering problems.

PO2. Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.

PO3. 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.

PO4. 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.

PO5. 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.

PO6. 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.

PO7. 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.
PO8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.

PO9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.

PO10. 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.

PO11. 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.

PO12. 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.

Course objectives: This course will enable students to

Course Outcomes: The students should be able to:


DO’S AND DON’TS
DO’S

 Do wear ID card and follow dress code. Do log off the computers when youfinish.
 Be on time to LAB sessions.
 Do ask for assistance if you need help.
 Do keep your voice low when speaking to others in the LAB.
 Do make suggestions as to how we can improve the LAB.
 In case of any hardware related problem, ask LAB in charge for solution.
 If you are the last one leaving the LAB, make sure that the staff in charge of theLAB
is informed to close the LAB
 Do keep the LAB as clean as possible.
 Do prepare for lab programs before entering the lab

Don’ts
 Do not use mobile phone inside the lab.
 Don’t do anything that can make the LAB dirty (like eating, throwing wastepapers
etc).
 Do not carry any external devices without permission.
 Don’t move the chairs of the LAB.
 Don’t interchange any part of one computer with another.
 Don’t leave the computers of the LAB turned on while leaving the LAB.
 Do not install or download any software or modify or delete any system files onany
lab computers.
 Do not damage, remove, or disconnect any labels, parts, cables, or equipment.
 Don’t attempt to bypass the computer security system.
 Do not read or modify other user’s file.
 If you leave the lab, do not leave your personal belongings unattended.
 We are not responsible for any theft.
INDEX
Sl. Program Title
No

1 Module – 1.1. Using Keil software, observe the various Registers, Dump, CPSR, with a simple
Assembly Language Programs (ALP).
3. Develop an ALP to multiply two 16-bit binary numbers.
4. Develop an ALP to find the sum of first 10 integer numbers.
5. Develop an ALP to find the largest/smallest number in an array of 32 numbers.
6. Develop an ALP to count the number of ones and zeros in two consecutive memory locations.
Module – 3 7. Simulate a program in C for ARM microcontroller using KEIL to sort the
numbers in ascending/descending order using bubble sort.
8. Simulate a program in C for ARM microcontroller to find factorial of a number.
9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of
characters from upper to lowercase and lower to uppercase.
Module – 4 and 5
10. Demonstrate enabling and disabling of Interrupts in ARM.
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow exceptions in
ARM. Course outcomes

2 Module – 2 2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical
operations (Demonstrate with the help of a suitable program).

5
6
7

10

11

12
COMPUTER PROGRAMMING LABORATORY (BPOPS103)

1. Simulation of a Simple
Calculator. #include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,result;
char op;
clrscr();
printf("\nEnter arithmetic operation [operand(3) operator(+ * / -) operand(4)] \n"); scanf("%d%c
%d",&n1,&op,&n2);
switch(op)
{
case '+': result=n1+n2;
printf("\n Sum=%d",result);
break;
case '-': result=n1-n2;
printf("\nDifference=%d",result);
break;
case '*': result=n1*n2;
printf("\nProduct=%d",result);
break;
case '/': if(n2!=0)
{
float result1=(float)n1/n2; printf("\nQuotient=
%.3f",result1);
}
else
{
printf("\nDivide error!");
}
break;
case '%': result=n1%n2;
printf("\nReminder=%d",result);

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 8


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

break;
default : printf("\nInvalid operator!");
break;
}
getch();
}

SAMPLE OUTPUT:

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3+4

Sum=7

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3-4

Difference=-1

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3/4

Quotient=0.750000

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3*4

Product=12

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3%4

Reminder=3

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 9


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3/0

Divide error!

Enter arithmetic operation [operand(3) operator(+ * / -) operand(4)]


3^4

Invalid operator!

ALGORITHM:
Start
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 the choice is equal to plus add num1 and num2 print result.
Step 5: [To perform subtraction] if the choice is equal to minus subtracts num2 from num1 print
result.
Step 6: [To perform multiplication] if the choice is equal to multiplication multiply num1 and
num2 print result.
Step 7: [To perform division] if the choice is equal to division divide num1 by num2 print
result. Step 8: [To perform Modulus] if the choice is equal to modulus divide num1 by num2
print result (remainder)
STOP

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 10


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 11


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

2. Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("Enter the Co-Efficients\
n"); scanf("%f%f%f",&a,&b,&c);
if(a==0||b==0||c==0)
{
printf("Invalid Inputs\n");
}
else
{
d=b*b-4*a*c;
if(d>0)
{
printf("The roots are Real and Distinct\
n"); r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\nr1=%f",r1);
printf("\nr2=%f",r2);
}
else if(d<0)
{
printf("The roots are Real and Imaginary\
n"); r1=-b/(2*a);
r2=sqrt(fabs(d))/(2*a);
printf("\nr1=%f+i%f",r1,r2);
printf("\nr2=%f-i%f",r1,r2);
}
else
{
printf("The roots are Real and Equal\
n"); r1=r2=-b/(2*a);
printf("\nr1=r2=%f",r1);
}
}
getch();

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 12


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

SAMPLE OUTPUT:

Enter the Co-Efficients0


0
0
Invalid Inputs

Enter the Co-Efficients1 2 1


The Roots are Real and Equalr1=r2=-

1.000000

Enter the Co-Efficients2 3 1


The roots are Real and
Distinct

r1=-0.500000r2=-
1.000000

Enter the Co-Efficients2 3 2


The roots are Real and Imaginary

r1=-0.750000+i0.661438r2=-
0.750000-i0.661438

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 13


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

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

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 14


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

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:
[Finished] STOP

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 15


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 16


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

3. An electricity board charges the following rates for the use of electricity: for the first200
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.
#include<stdio.h>
#include<conio.h>
void main()
{
int total,temp;
char name[20];
int units;
float charge;
clrscr();
printf("\nEnter the name of the consumer: ");
scanf("%s",name);
printf("\nEnter the units consumed: ");
scanf("%d",&units);
temp=units;
charge=100;

if(temp<200)
charge+=(0.8*temp);
else
{
charge+=(0.8*200);
temp-=200;
if(temp<100)
charge+=(0.9*temp);
else
{
charge+=(0.9*100);
temp-=100;
if(temp>0)
charge+=(temp);
if(charge>400)
charge+=(15*charge/100);
}
}

printf("\nNAME\t\tUNITS\t\tCHARGE");

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 17


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

printf("\n%s\t\t%d\t\t%f",name,units,charge);
getch();
}

SAMPLE OUTPUT:

Enter the name of the consumer: aff

Enter the units consumed: 50

NAME UNITS CHARGE


Sapthagiri admin 50 140.000000

Enter the name of the consumer: sapthagiri

Enter the units consumed: 300

NAME UNITS CHARGE


sapthagiri 300 350.000000

ALGORITHM

Step 1. [Initialise] Start


Step 2. [input the consumer name]
Read name
Step 3. [input the number of units consumes]
Read units
Step 4. [Calculate the Electricity bill amount]
If (units=0) mincharge=100
else if(units<=200)
billamount =mincharge+noofunits*0.8else
if((units>200) and (units<=300))
amt=mincharge+200*0.8+ (units -200)*0.9
else
amt=mincharge+200*0.8+100*0.9+(units-300)*1

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 18


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Step 5. [ Calculate the surcharge]


if(amt>400) surcharge= 15/100.0*billamount
Step 6. [Output the result]
Print “Consumer name, bill amount.

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 19


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

4. Write a C Program to display the following by reading the number of rows as input,
1
1 2 1
1 2 3 2 1
12 3 4 3 2 1
---------------------------
nth row

#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);

/* Display number in reverse order after middle */


for(j=i-1;j>=1;j--)
printf("%d",j);

printf("\n");
}
}

SAMPLE OUTPUT:

Input number of rows : 5

1
121
12321
1234321
123454321

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 20


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

5. Implement Binary Search on Integers / Names


#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],key,low,high,mid,flag=0;
clrscr();
printf("\n Enter the no. of elements : ");
scanf("%d",&n);
printf("\n Enter %d elements in ascending order ",n);
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("\n Enter the key element to search : ");
scanf("%d",&key);
low = 0;
high = n-1;
while(low <= high)
{
mid=(low+high)/2;
if(key==a[mid])
{
flag=1;
break;
}
if(key > a[mid])
low=mid+1;
else
high = mid-1;
}
if(flag==1)
printf("\n Successful Search %d at position
%d",key, mid+1);

else
printf("\n Unsuccessful Search");

getch();

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 21


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

SAMPLE OUTPUT:

Enter the no. of elements : 5

Enter 5 elements in ascending order 1 3 5 7

9 Enter the key element to search : 9

Successful Search 9 at position 5

Enter the no. of elements : 7

Enter 7 elements in ascending order 10 17 35 45 67 90 100

Enter the key element to search : 67

Successful Search 67 at position 5

Enter the no. of elements : 5

Enter 5 elements in ascending order 1 3 5 7 9

Enter the key element to search : 0

Unsuccessful Search

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 22


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Algorithm
Step 1: Start
Step 2: read the value of n
STEP 3: initialize low=0, high=n-1
STEP 4: read the array elements
using loop and read key
STEP 5: repeat STEP 6 to STEP 9
until low <= high
STEP 6: calculate mid=(low+high)/2
STEP 7: if key=a[mid] set flag=1 goto STEP 9
Step 8: If key > a[mid]
low= mid+1
Step 9: if key < a[mid]
high=mid-1
Step 10: if(flag=1)
Print “ Element found”
else
print “Element not found”
Step 11: Stop

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 24


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

6. Implement Matrix multiplication and validate the rules of multiplication.


#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
clrscr();
printf("\n Enter the order of the matrix A :"); scanf("%d
%d",&m,&n);
printf("\n Enter the order of the matrix B :"); scanf("%d
%d",&p,&q);
if(n==p)
{
printf("\n 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("\n Enter the elements of matrix B \n");
for(i = 0 ; i < p ; i++)
{
for(j = 0 ; j < q ; j++)
scanf("%d",&b[i][j]);
}
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];
}
}
printf("\n MATRIX A \n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf(" %d \t", a[i][j]);
}

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 25


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

printf("\n");
}
printf("\n MATRIX B \n");
for(i = 0 ; i < p ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", b[i][j]);
}
printf("\n");
}
printf("\n MATRIX C \n");
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix A & B is not multiplicable");

getch();
}

SAMPLE OUTPUT:

Enter the order of the matrix A :2


2

Enter the order of the matrix B :2 2

Enter the elements of matrix A


12
34

Enter the elements of matrix B


12
12

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 26


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

MATRIX
A1 2
3 4

MATRIX
B1 2
1 2

MATRIX
C3 6
7 14

Algorithm

Step1: Start
Step2: read matrix A order m and n Step3: read matrix B order p and q Step4: if n not equal to p
print “multiplication not possible” goto Step11. Otherwise goto Step5
Step5: read the elements of matrix A, using loops for i=0 to m-
1 for j= to n-1
Step6: read the elements of matrix B, using loops for i=0 to p-1
for j=0 to q-1
Step7: calculate resultant matrix C using nested loops for i 0 to m-1
for j 0 to q-1
c[i][j]=0 c[i][j]=c[i][j]+a[i][k]*b[k]
[j]
Step8: print elements of matrix A using nested
loops for i to m-1
for j 0 to n-1
Step9: print elements of matrix B using nested
loops for i for j to p-1 to q-1
Step 10: print elements of matrix C using nested
loops for i 0 to n-1

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 27


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

for j 0 to q-1
Step 11: Stop

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 28


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 29


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

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.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.142
void main()
{
int degree,i;
float x,nume,denom,sum=0,term;
clrscr();
printf("enter the value of degree\n");
scanf ("%d", &degree);
x=(degree*pi)/180;
nume=x;
denom=1;
i=2;

do
{
term=nume/denom;
nume= -nume*x*x;
denom=denom*i*(i+1);
sum=sum+term;
i=i+2;
}
while(fabs(term)>0.00001);

printf("sin(%d)=%f without using built in function \n",degree,sum);


printf("sin(%d)=%f using built in function \n",degree,sin(x));
getch();
}

SAMPLE OUTPUT:

enter the value of degree

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 30


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

30
sin(30)=0.500059 without using built in function
sin(30)=0.500059 using built in function

enter the value of degree


60
sin(60)=0.866093 without using built in function
sin(60)=0.866093 using built in function

Algorithm
Step 1: Start
Step 2: initialize sum to 0
Step 3: read the value of degree
Step 4: calculate
x=(degree*PI)/180 Step 5: initialize
nume to x
denom to 1 i=2
Step 6: do term=nume/denom; nume=nume*x*x; denom=denom*i*(i+1) sum=sum+term
i=i+2 while(fabs(term)>0.00001)
Step 7: print (sin(x) without built-in function)
Step 8: print sin(x) using built-in function
Step 9: Stop.

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 31


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 32


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 33


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

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

#include <stdio.h>
# #include<conio.h>
void main()
{
int n,i,j,a[10],b[10],temp;
clrscr();
printf("\n Enter the no. of elements : ");
scanf("%d",&n);
printf("\n Enter %d elements\n",n);
for(i = 0 ; i < n ; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
for(i = 0 ; i < n-1 ; i++)
{
for(j = 0 ; j < n-i; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("\nThe original elements are\n ");
for(i = 0 ; i < n ; i++)
printf("%d \n",b[i]);
printf("\nThe Sorted elements are\n ");
for(i = 0 ; i < n ; i++)
printf("%d \n",a[i]);
getch();
}

SAMPLE OUTPUT:

Enter the no. of elements : 5

Enter 5 elements
12 45 76 3 0

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 34


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

The original elements are


12
45
76
3
0

The Sorted elements are


0
3
12
45
76

Algorithm

Step1: Start
Step2: Read the value of n
Step3: Read the array elements using loop
for i to n-1
Step4: Repeat steps using nested loop
Step5: for i=0 to n-1 for j=0 to n-i
if(a[j] > a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
Step6: print the sorted elements using loop for i=0 to n-1
Step7: Stop

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 35


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

9.Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
#include<stdio.h>
int slength(char str[])
{
int len; //int len=0;
for(len=0;str[len]!='\0';len++); //while(str[len]!='\0')
return len; //len++;
//return len;
}

void scompare(char str1[],char str2[])


{
int i,flag=0;
if(slength(str1) != slength(str2))
flag=1;
else
for(i=0;i< slength(str1);i++) if(str1[i]!
=str2[i])
{
flag=1;
break;
}
if(flag==1)
printf("\nStrings are Not Equal\n");
else
printf("\nStrings are Equal.\n");
}

void sconcat(char str1[],char str2[],char conc[])


{
int i,j;
for(i=0; i< slength(str1);i++)

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 36


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

conc[i]=str1[i];
for(j=0; j<slength(str2);j++)
conc[i++]=str2[j]; conc[i]='\
0';
}

void main()
{
char str1[50],str2[50],conc[100];
int len,choice;
clrscr();
while(1)
{
printf("\n\n\nMAIN MENU\n");
printf("\n1.Compare strings\n2.Concatenate Strings\n3.String Length\n4.Exit\n\n");

printf("\nEnter your choice:");


scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:printf("\nEnter the First string:");
gets(str1);
printf("\nEnter the Second string:");
gets(str2);
scompare(str1,str2);
break;
case 2:printf("\nEnter the First string:");
gets(str1);
printf("\nEnter the Second string:");
gets(str2);
sconcat(str1,str2,conc);
printf("\nConcatenated string is:%s\n\n",conc);
break;
case 3:printf("\nEnter the String:");
gets(str1);
len=slength(str1);
printf("\n\nThe Length of the string is: %d\n\n",len);
break;
case 4:printf("\nExiting the program...!!!");
exit(0);
default: printf("\nInvalid choice..!!!");

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 37


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

} // End of Switch

}} //End of While

SAMPLE OUTPUT:

MAIN MENU

1.Compare strings
2.Concatenate Strings
3.String Length
4.Exit

Enter your choice:1

Enter the First

string:saptha Enter the

Second string:giri Strings

are Not Equal

MAIN MENU

1.Compare strings
2.Concatenate Strings
3.String Length
4.Exit

Enter your choice:4

Exiting the program...!!!

Algorithm:

Step 1. [Initialise] Start

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 38


COMPUTER PROGRAMMING LABORATORY (BPOPS103)

Step 2. Input the two strings] read s1, s2

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 39


COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

Step 3. [perform two string compare using a function] Call the function Str2cmp(s1,s2)
Step 4. [perform two string concatenation] Call the function Str2cat(s1,s2)
Step 5. Find the length of two string using a function] Call the function length(s1), length(s2)
Step 6. [Finished] Stop

Flowchart

34
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

10.Implement structures to read, write, and compute average- marks and the students
scoring above and below the average marks for a class of N students. #include<stdio.h>

#include<string.h>
struct student
{
int rnumber;
char name[20];
int marks;
char grade;
}stud[60];

void main()
{
int i,n;
float sum=0,avg=0;
clrscr();
printf("Enter the number of students");
scanf("%d",&n);
printf("\nEnter the roll number, Name , Marks and Grade of the student \n");
for(i=1;i<=n;i++)
{
printf("\nStudent %d details\n",i);
scanf("%d",&stud[i].rnumber);
scanf("%s",stud[i].name);
scanf("%d",&stud[i].marks);
fflush(stdin);
stud[i].grade=getchar();
sum+=stud[i].marks;
}
avg=sum/n;
printf("\n Avg marks is %f",avg); printf("\
nStudent Details are:"); printf("\nRoll_number\
tName\tMarks\tGrade"); for(i=1;i<=n;i++)
printf("\n%d\t\t%s\t%d\t%c\t",stud[i].rnumber,stud[i].name,stud[i].marks,stud[i].grade);
printf("\n Details of Students who scored Above avarage Marks\n"); printf("\nRoll_number\
tName\tMarks\tGrade\n");
for(i=1;i<=n;i++)
{
if(stud[i].marks >= avg) printf("\n%d\t\t%s\
t%d\t%c\t",
stud[i].rnumber,stud[i].name,stud[i].marks,stud[i].grade);
}
SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 41
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

printf("\n Details of Students who scored Below avarage Marks\n"); printf("\nRoll_number\tName\


tMarks\tGrade\n");

for(i=1;i<=n;i++)
{
if(stud[i].marks < avg) printf("\n%d\t\t%s\t
%d\t%c\t",
stud[i].rnumber,stud[i].name,stud[i].marks,stud[i].grade);
}
getch();
}

SAMPLE OUTPUT:

Enter the number of students 3

Enter the roll number, Name , Marks and Grade of the student

Student 1 details
10

pradeep
50
A

Student 2 details
15
manju
60
B

Student 3 details
20
govind
70
A

Avg marks is 60.000000


Student Details are:
Roll_number Name Marks Grade
10 pradeep 50 A
15 manju 60 B
20 govind 70 A
Details of Students who scored Above avarage
SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 42
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

Marks Roll_number Name Marks Grade

15 manju 60 B
20 govind 70 A
Details of Students who scored Below avarage

Marks Roll_number Name Marks Grade

10 pradeep 50 A

Algorithm

Step 1: Start
Step 2: Define the structure with required variables Step 3: Read the value of n
Step 4: Read the student details using loop for i= 1 to n read rnumber, name marks, grade
Step 5: Calculate sum Sum=sum+marks

step 6: Calculate average Average=sum/n


Step 7: print the result using loop for i=1 to n
print rnumber,name,marks,grade,average
Step 8: for=1 to n
if(marks>=average)
print rnumber,name,marks and grade
Step 9: if(marks<=average)
print rnumber, name, marks and grade
Step 10: Stop

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 43


COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

Flowchart

SAPTHAGIRI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 44


COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

11.Develop a program using pointers to compute the sum, mean and standard deviation of all
e elements stored in an array of n real numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
intn,i;
clrscr();
printf("enter the no of elements\n");
scanf("%d",&n);
printf("enter the array elemnts\
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=%f\
t",sum); printf("mean=%f\t",mean);
printf("standard deviation=%f\t",std);
getch();
}

SAMPLE OUTPUT:
Enter the no of elements
5
Enter the array elements
2
3
4
5
6
Sum=20.000 Mean=4.000 Standard deviation=1.414
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

Algorithm

Step 1. [Initialise] Start Step 2. [Input the elements] Read n, a[i]


Step 3. [Initialize]
sum=0, sumstd=0
Step 4. [Compute sum, mean, standard deviation] Ptr=a For i = 0 thru n in steps of
1 sum=sum+*ptr , Ptr++ , mean=sum/n , Ptr=a For i= 0 thru n in steps of 1
sumstd=sumstd+pow((*ptr-mean),2)
Ptr++
Step 5 . std=sqrt(sumstd/n)
Step 6. [Output sum, mean and standard deviation] Print sum, mean, std
Step 7. [ Finished ] Stop

FLOWCHART
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

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;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);

// Open one file for reading


fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);

// Open another file for writing


fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

// Read contents from file


c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);

return 0;
}
COMPUTER PROGRAMMING LABORATORY (21CPL27/17)

You might also like