0% found this document useful (0 votes)
1K views

CPPS - Lab Manual (22POP13) (1-8)

The document provides instructions for a computer programming laboratory manual. It includes 13 programming assignments to be completed in the C programming language. The first assignment involves simulating a simple calculator and includes an algorithm, flowchart, program code, and sample output to add two numbers. The algorithm uses a switch statement to check the operator and calculate the result accordingly. The flowchart shows the program flow from input to output based on the operator.

Uploaded by

Raghav V Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

CPPS - Lab Manual (22POP13) (1-8)

The document provides instructions for a computer programming laboratory manual. It includes 13 programming assignments to be completed in the C programming language. The first assignment involves simulating a simple calculator and includes an algorithm, flowchart, program code, and sample output to add two numbers. The algorithm uses a switch statement to check the operator and calculate the result accordingly. The flowchart shows the program flow from input to output based on the operator.

Uploaded by

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

RajaRajeswari College of Engineering

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Affiliated to Visvesvaraya Technological University
“Jnana Sangama”, Belgaum – 560 018

LABORATORY MANUAL
Prof. Rashmi B R
“C PROGRAMMING LAB – 22POP13”
Semester: I
Computer Programming Laboratory Manual[22POP13] 2022-23
COMPUTER PROGRAMMING LABORATORY
(Effective from the academic year 2022 -2023)
SEMESTER – I
Subject Code 22POP13 CIE Marks
Number of Contact Hours/Week SEE Marks
Total Number of Lab Contact Exam Hours 3 Hrs
Hours
Credits –
Descriptions (if any):
 The laboratory should be preceded or followed by a tutorial to explain the approach or
algorithm being implemented / implemented for the problems given.
 Every experiment should have algorithm and flowchart be written before writing the program.
 Code should be traced using minimum two test cases which should be recorded.

Laboratory Programs:
1. Simulation of a simple calculator.

2. Compute the roots of a quadratic equation by accepting the coefficients. Print


appropriate messages.

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.
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
1 2 3 4 3 2 1

Nth row
5. Implement Binary Search on Integers.
6. Implement Matrix multiplication and validate the rules of multiplication.
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.
8. Sort the given set of N numbers using Bubble sort.
9. Write functions to implement string operations such as compare, concatenate, and find string
length.Use the parameter passing techniques.
10. Implement structures to read, write and compute the sum, mean and standard deviation of all
elements stored in an array of N real numbers.
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.
12. Write a C program to copy a text file to another, read both the input file name and target file
name.

3
••
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Algorithm:

• An algorithm is a basic tool which is used to express the solution for a given problem
systematically in the form of instructions. This solution is called logic. It takes a set of input
values and produces the desired output.

• An algorithm is defined as unambiguous, step by step procedure (instructions) to solve a given


problem in finite number of steps byaccepting a set of inputs and producing the desired output.
After producing the result, the algorithm should terminate. Usually, Algorithm are written in
simple English like statements along with simple mathematical expressions.

Characteristics of an Algorithm:

• Input: It mayaccept a zero or more inputs.

• Output: It should produce at least one output (result).

Definiteness: Each instruction must be clear, well defined and precise. There should not be
anyambiguity.

Finiteness: It should be a sequence of finite instructions. That is, it should end after a fixed
time. It should not enter into an infinite loop.

Effectiveness: This means that operations must be simple and are carried out in a finite time at
one or more levels of complexity. It should be effective whenever traced manually for the
results.

Algorithmic Notations:

• Name of the Algorithm

• Step number

• ExplanatoryComment

• Termination

4

ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Flowchart:

A flow chart is a pictorial representation of an algorithm. That is flowchart consists of


sequence of instructions that are carried out in an algorithm. All the steps are drawn form of
different shapes of boxes, circle and connecting arrows. Flowcharts are mainly used to help
programmer to understand the logic of the program.

• The various types of geometric shapes, arrows and symbols used while drawing the flowchart
are called flowchart symbols. The symbols used and the meaning associated with each symbol
are shown below.

Symbols used Meaning associated with symbols

Start or end of the flowchart (program)

Input or output operation

Decision making and branching

Connector

Predefined computation or process (used with


functions are used)

Repetition or a loop which is normally used to


execute a group of instructions for a specifies
number of times

Flow of control

5
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

1. Familiarization with programming environment, concept of naming the


program files, storing, compilation, execution and debugging. Taking any simple
C- code.
Algorithm Flowchart

Step 1. [Initialize] Start

Step 2. [Input the length and breadth] Read l, b

Step 3. [Calculate area] area = l * b

Step 4. [Calculate perimeter] peri= 2(l+b)

Step 5. [Display area and perimeter] Print area,

peri

Step 6. [Finished] Stop.

Program Output
#include<stdio.h> Enter length and breadth
{
2
int l, b, area, peri;
printf(“Enter length and breadth\n”); 4
scanf(“%d%d”, &l, &b); The area is 8
area = l * b;
peri = 2 * (l + b); The perimeter is 12
printf(“The area is %d\n”, area);
printf(“The perimeter is %d\n”, peri);
}

6
Algorithm: COMMERCIAL CALCULATOR
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Step 1: Start
Step 2: Read the values of a and b. Step 3:
Read operator
Step4: Verify operator value by using switch statement if operator =+ then goto step 5 or if operator = - then
goto step 6 or if read operator is * then goto step 7 or if read operator is / then goto step 8 or if read
operator is % then goto step 9 or otherwise goto step 10.
Step 5: if operator =+ then compute result=a+b and goto step 11.
Step 6: if operator =- then compute result=a-b and goto step 11.
Step 7: if operator =* then compute result=a*b and goto step 11.
Step 8: if operator =/ then compute result=a/b and goto step 11.
Step 9: if operator =% then compute result=a%b and goto step 11.
Step 10: if operator value is other than above options then print Error in operation then goto
step 12.
Step 11: Print result and goto step 12
Step 12: stop.

1) Simulation of a simple calculator.

7
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Start

Input

Switch(operator)

+ - * / % Default

result=a+b result=a-b result=a*b result=a/b result=a%b Error in operation

Print

Stop

Flowchart

Viva Questions:
a. What is switch statement.
b. What is a case in a switch statement?
c. Is Default necessary in switch case?
d. How many cases can you have in a switch statement?

8
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

// Program 1

//Simulation of a Simple Calculator

#include<stdio.h>
#include<conio.h>

int main()
{
char operator;
float num1, num2, result;

printf("Enter the operator [+,-,*,/] \n");


scanf("%c", &operator);

printf("Enter two numbers \n");


scanf("%f %f", &num1, &num2);
switch(operator)
{
case '+': result = num1 + num2; // add two numbers
break;

case '-': result = num1 - num2; // subtract two numbers


break;

case '*': result = num1 * num2; // multiply two numbers


break;

case '/': if(num2 != 0)


{
result = num1 / num2; // divide two numbers
}
else
{
printf("Divide by zero");
}
break;

default : printf("Error in operation");


break;
}
printf("\n %f %c %f = %f\n", num1, operator, num2, result);
getch();
return 0;
}

9
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Output:
TRACE 1
Enter the operator [+,-,*,/]

+
Enter two numbers
10 20

result = 30

TRACE 2
Enter the operator [+,-,*,/]

/
Enter two numbers
1 0

Divide by zero

10
ALGORITHM: ROOTS OF A QUADRATIC EQUATION
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Step 1: Start

Step 2: Read the values of non-zero coefficients a,b and c.

Step 3: Ifa|b|c is zero goto Step 9. Otherwise Computethe value ofdiscriminant (disc)which is

Equalto (b*b)-(4*a*c).

Step 4: Check if disc is equal to 0. If true, then go to Step 5. Otherwise, goto Step6
Step 5: Compute the roots.

Root1 = (-b)/(2*a)

Root2=Root1

Output the values of roots, Root1 and Root2. Go to Step 9

Step 6: Check if disc is greater than zero or not. Iftrue, then go to Step 7. Otherwise, goto Step 8.

Step 7: Compute the distinct roots,

Root1 = (-b+sqrt(disc))/(2*a)

Root1 = (-b-sqrt(disc))/(2*a)

Out put the values of roots, root1 and root2. Go to Step 9.


Step 8: Compute the complex roots.

Compute the real part, x1 = (-b)/(2*a)

Compute the imaginary part, x2 = sqrt(-disc)/(2*a)

Output roots as

Root1 = x1 + i x2

Root2 = x1 – i x2
Step 9: Stop.
2) Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.

11
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Flow chart

Start

Input a, b, c

if((a*b*c*)==0) True

Roots cannot be
False Determined
Disc=(b*b)-(4*a*c)

if(disc==0) The roots are equal


True

False

if(disc > 0) True The roots are


distinct

False

The roots are complex

Stop

12
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Program 2
#include<stdio.h>
#include<math.h>
#include<conio.h>

void main()
{
float a,b,c,x1,x2,disc;
clrscr();

printf("Enter the co-efficients");


scanf("%f %f %f",&a, &b, &c);

if((a*b*c) = = 0)
{
printf("Roots cannot be Determined\n");
exit(0);
}

disc = b*b-4*a*c;

if(disc > 0)
{
x1 =(-b + sqrt(disc)) / (2*a);

x2 =(-b - sqrt(disc)) / (2*a);

printf("The roots are distinct");

printf("Root1 = %f Root2 = %f", x1,x2);


}

else if (disc == 0)
{
x1 = x2 = -b/(2*a);
printf("The roots are equal");

printf("Root1 = %f Root2 = %f",x1,x2);


}
else
{
x1 = -b/(2*a);
x2 = sqrt(fabs(disc))/(2*a);

printf("The roots are complex");

printf("Root1 = %f + i%f",x1,x2);

printf("Root2= %f - i%f",x1,x2);

}
getch();
}

13
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Output:
First run:
Enter the
coefficients 1
-5
6
The roots are
distinct
Root1=3.0000
Root2=2.0000

Second Run:

Enter the co-


efficients 1
-4
4
The roots are
equal
Root1=Root2=
2.0000
Third Run:
Enter the co-
efficients2
2
2
The roots are complex
Root1=-0.500000+i
0.866025
Root2=-0.500000- i 0.866025

Viva Questions:
a. What is quadratic Equation?
b. What is math.h ? why it is used in C program?
c. What are decision making statements in C language?
d. Write the syntax of “ if ”statement?
e. Difference between if and switch statements?

14
Algorithm:
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Input: Customer name and unit consumed
Output: Customer name, units consumed and total amount to be paid

Step 1: Start

Step 2: Read the name of customer and the unit consumed by the customer.

Step 3: Check if the unit consumed is greater than 1 and less than 200,if true goto step 4 else goto
step 5.
Step 4: Compute: amt=100+(0.8*units).
Step 5: if unit is greater than 200 and less than 300,if true goto step 6 else goto step 7
Step 6: Compute amt=100+(200*0.8)+((units-200)*0.9)
Step 7:Compute amt=100+(200*0.8)+(100*0.9)+((units-300)*1), then goto step 8
Step 8: Check if the amt is less than or equal to 400, if true goto step 9 otherwise goto step 10.
Step 9: Print the amount charged and goto step 11.
Step 10: compute amt=amt*1.15,and print the amount charged.

Step 11: Stop .

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 rupees 1 per unit. All users are charged a minimum of rupees
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.

15
amt=100+(0.8*units);
ComputerProgrammingLaboratoryManual[22POP13]
amt=amt*1.15 2022-23
True
amt=100+(200*0.8)+((units-200)*0.9);
Flow chart

Start

Input name

Input units

If(units<=200)
True

False
If(units>200 True
&&
units<=300)
False

amt=100+(200*0.8)+(100*0.9)+(units-
300)*1;

If(units>400)

True

Print name, units, charge

Stop

16
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Program 3

#include <stdio.h>

void main()
{
char name[10];

float unit, amt;


printf("Enter your name and unit Consumed:");
scanf("%s %f",name,&unit);
if(unit<=200)
amt=unit*0.80+100;
else if((unit>200)&&(unit<=300))
amt=200*0.80+((unit-200)*0.90)+100;
else
amt=200*0.80+100*0.90+((unit-300)*1)+100;
if(amt>400)
amt=1.15*amt;
printf("Name: %s\n Unit=%f \n charge=%f ",name,unit,amt);
}
Output First
Run
Enter your name and unit Consumed: Siri 52
Name: Siri
Unit=52
charge=141.600000
Second Run
Enter your name and unit Consumed: Rajesh 460
Name: Rajesh Unit=460
charge=586.500000

Viva Questions:
a. Difference between float and double datatypes.
b. Write syntax of for loop?
c. What is the use of break statement?
d. Difference between continue and break statement?

17
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

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
1 2 3 4 3 2 1

Nth row
Program 4:

#include <stdio.h>

void main()
{
int row,space,num,col,reverse;
printf("Input number of rows : ");
scanf("%d",&num);
for(row=1;row<=num;row++)
{
/* print blank spaces */
for(space=1;space<=num-row;space++)
printf(" ");

/* Display number in ascending order upto middle*/


for(col=1;col<=row;col++)
printf("%d",col);

/* Display number in reverse order after middle */


for(reverse=row-1;reverse>0;reverse--)
printf("%d",reverse);

printf("\n");
}
}

18
Computer Programming Laboratory Manual[22POP13] 2022-23
Output:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1

19
Computer Programming Laboratory Manual[22POP13] 2022-23

5. Implement Binary Search on Integers.

20
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

21
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Program 5:

#include<stdio.h>
void main()
{
int n, a[100], i, key, high, low, mid, loc;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of array in sorted order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
loc = mid+1;
break;

}
else
{

if(key<a[mid])
high=mid-1;

else
low=mid+1;
}
}

if(loc>0)
printf("\n The element %d is found at %d ",key,loc);
else
printf("\nThe search is unsuccessful");
}

22
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Output
First Run
Enter the size of the array
5
Enter the elements of array in sorted order
10
20
30
40
50
Enter the element to be searched
40
The element 40 is found at 4

Viva Questions:
a. What is an array/definition of array.
b. What are the types of array?
c. What is a multidimensional array?
d. How to declare and initialize one dimensional array?
e. What are the advantages of an array?
f. What is the difference between array & string?
g. Write the syntax of declaring an array.

23
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

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

24
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

25
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Program 6:

26
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

27
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

28
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

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.

29
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

30
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Program 7:
#include<stdio.h>
#include<math.h>
int main( )
{
int i;
float x,t,sum,sum1,y;
printf("Enter the angle\n");
scanf("%f",&x);
y=x;
x=3.1428*(x/180.0);
sum=x;
t=x;
i=1;
do
{
i=i+2;
t=(-t*x*x)/((i-1)*i);
sum=sum+t;
}while(fabs(t)>0.00005);
printf("sin(%f) using taylor series=%f\n",y,sum);
sum1=sin(x);
printf("Using inbuilt function sin(%f)=%f",y,sum1);
}
Output:
Enter the angle 30
sin(30.000000) using taylor series=0.500000
Using inbuilt function sin(30.000000)=0.500000

Viva Questions:
a. What is pre-processor directive?
b. What is difference between const and #define.
c. What is use of fabs().
d. What is variable initialization and why is it important?
e. What is the difference between the = symbol and == symbol?
f. Can the curly brackets { } be used to enclose a single line of code?

31
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

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

32
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

33
ComputerProgrammingLaboratoryManual[22POP13] 2022-23
Program 8:
#include<stdio.h>
int main()
{
int i,j,n,temp;
int a[20];
printf("enter the value of n");
scanf("%d",&n);
printf("Enter the numbers in unsorted order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
// bubble sort logic
for(i=0;i<n;i++)
{
for(j=0;j<(n-i)-1;j++)
{
if( a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
34
ComputerProgrammingLaboratoryManual[22POP13] 2022-23

Output
Enter the value of n
5
Enter the numbers one by one in unsorted order:
20
10
30
50
40
The sorted array is
10
20
30
40
50

Viva Questions:
a. Why the name bubble sort?
b. What are the different types of sorting techniques?
c. Explain the logic of bubble sort with an example.
d. What is nested for loop?

35

You might also like