0% found this document useful (0 votes)
10 views33 pages

Compititve Progrmming Mannual

The document is a lab manual for the Competitive Programming-I course for T.E. (CSE) students at SBERCT’s Bhagwant Institute of Technology, detailing various programming experiments. It includes aims, theories, program codes, and outputs for multiple problems such as the 3n+1 problem, Jolly Jumper Problem, Stack, and Queue. Additionally, it outlines the vision, mission, program educational objectives, and outcomes related to the Computer Science and Engineering program.

Uploaded by

kml.stbcet
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)
10 views33 pages

Compititve Progrmming Mannual

The document is a lab manual for the Competitive Programming-I course for T.E. (CSE) students at SBERCT’s Bhagwant Institute of Technology, detailing various programming experiments. It includes aims, theories, program codes, and outputs for multiple problems such as the 3n+1 problem, Jolly Jumper Problem, Stack, and Queue. Additionally, it outlines the vision, mission, program educational objectives, and outcomes related to the Computer Science and Engineering program.

Uploaded by

kml.stbcet
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/ 33

COMPUTER SCIENCE & ENGINEERING DEPARTMENT

COMPETITIVE PROGRAMMING
LAB MANUAL
For T.E. (CSE)

Compiled By

.
Competitive Programming - I Laboratory Manual

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

PREPARED BY

Name of Subject:- Competitive Programming-I Year:-TE CSE(2022-


23)
Expt. No. Title
1 Program on 3n+1 problem

2 Program on Jolly Jumper Problem

3 Program on Stack

4 Program on Queue

5 Program on Pattern String

6 Program on Binary Sort

7 Program on Root Finding

8 Program on Revers and Add Number

LAB IN CHARGE HOD

Table of Contents
Sr. No. Topic Page. No.

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

1 How to Use This Manual……………. 6


2 Laboratory Details…………………… 12
2.1 Experiment List…………………… 13
Experiment No.1……………………….. 16
2.2 Aim, theory, Program Code, output, 17
Conclusion…………………………
Experiment No.2……………………….. 20
2.3 Aim, theory, Program Code, output, 21
Conclusion…………………………
Experiment No.3……………………….. 26
2.4 Aim, theory, Program Code, output, 27
Conclusion…………………………
Experiment No.4……………………….. 31
2.5 Aim, theory, Program Code, output, 32
Conclusion…………………………
Experiment No.5……………………….. 35
2.6 Aim, theory, Program Code, output, 36
Conclusion…………………………
Experiment No.6………………………..
2.7 Aim, theory, Program Code, output,
Conclusion…………………………
Experiment No.7………………………..
2.8 Aim, theory, Program Code, output,
Conclusion…………………………

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

1. How to Use This Manual


 This Manual assumes that the facilitators are aware of Collaborative Learning
Methodologies.
 This Manual will only provide them tool they may need to facilitate the session on
Competitive Programming –I in collaborative learning environment.
 The Facilitator is expected to refer this Manual before the session.

Icon of Graduate Attributes

K A D I
Problem Design & Investigation
Applying
Analysis Development of problems
Knowledge

M E E T
Environment
Modern Engineer & Ethics
Sustainability
Tool Usage Society

T O M I
Individual & Communication Project Life Long
Team work Management Learning
& Finance

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Icon of Blooms Taxonomy:-Disk Approach

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Institute Vision and Mission

Vision
Satisfy the ambition of youth, who want to lead & serve our nation
towards prosperity through techno-economic development.

Mission
To provide, faster & sustain a milieu of high academic excellence,
research and entrepreneurship for all those aspiring students
which will prepare them to face global challenges, crafting high
ethical & moral standards

Department Vision and Mission

Vision of Department
To be regionally, nationally and internationally recognized center of
excellence in all fields of Computer Science & Engineering
Education where the best of teaching- learning, state of art
research and consultancy synergize

Mission of Department
1. To empower students with fundamentals of Computer Science
& Engineering to be successful profession.

2. To impart quality education to enable the students for higher


studies, research and entrepreneurship.

3 To cater for the service to society

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

 Program Educational AIMs (PEOs):

PEO1: To provide knowledge of sound mathematical principles underlying


various programming concepts.
PEO2: To develop an ability to understand complex issues in the analysis, design,
implementation and operation of information systems.
PEO3: To provide knowledge of mechanisms for building large-scale computer-
based systems.
PEO4: To develop an ability to provide computer-based solutions to the problems
from other disciplines of science and engineering.
PEO5: To impart skills necessary for adapting rapid changes taking place in the
field of information and communication technologies.
PEO6: To provide knowledge of ethical issues arising due to deployment of
information and communication technologies in the society on large scale.

 Program Outcomes: -
a. An ability to apply knowledge of computing, mathematics, science and
engineering fundamentals appropriate to design, thermal and manufacturing
problems
b. An ability to identify the problems and provide solutions by using first principles,
of engineering science, mathematics, and numerical methods.
c. An ability to design solutions for thermal, hydraulic & machining systems and
design components and processes that meet the specified needs with
appropriate consideration for safety, societal and environmental aspects.
d. An ability to investigate solutions of complex problems by conducting
experiments, simulating, computing and analyzing data to provide valid findings
and conclusions.
e. An ability to use modern engineering tools and technologies necessary for
obtaining quick, economical and accurate solutions of engineering problems.
f. An ability to analyze the local and global impact of mechanical systems on
individuals, organizations and society.
g. An ability to understand the environmental issues and provide the solutions for
sustainable development of the system.

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

h. An ability to understand professional and ethical responsibility through soft skill


development.
i. An ability to function effectively as an individual or as a team member to
accomplish the goal.
j. An ability to communicate effectively at technical and management levels using
both written and oral communication.
k. An ability to apply technical and management skills to develop ones’s own work
as a member and leader in a team to manage projects and in multidisciplinary
environments.
l. An ability to keep up-to-date with contemporary technologies through lifelong
learning.

Course Outcomes
At The End Of Course Student Will Able to:

CO 1: Apply algorithm techniques and methods


CO 2: Calculate processing time and memory space of algorithms.
CO 3: Create good and correct algorithm for problem solving
CO 4: To develop logics which will help them to create programs, applications in
C.
CO 5: Given a computational problem, identify and abstract the programming
task
Involved
CO6: Choose the right data representation formats based on the requirements of
the
Problem

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

2. Laboratory Details
 Useful libraries: CPP, Java, Python

 Input Methods: CPP, Java, Python

 Template: CPP

 Language: CPP, Java, Python

 Setting up Competitive Programming Environment: Sublime: CPP, Visual


Studio: CPP and Python

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Experiment: 1
AIM: Program on 3n+1 problem

THEORY:

Consider the following algorithm to generate a sequence of numbers. Start


with an
integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1.
Repeat this
process with the new value of n, terminating when n = 1. For example, the
following

sequence of numbers will be generated for n = 22:


22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured (but not yet proven) that this algorithm will terminate at n =
1 for
every integer n. Still, the conjecture holds for all integers up to at least 1,
000, 000.
For an input n, the cycle-length of n is the number of numbers generated up
to and
including the 1. In the example above, the cycle length of 22 is 16. Given
any two
numbers i and j, you are to determine the maximum cycle length over all
numbers
between i and j, including both endpoints.

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
int fin;

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

int count=0;
clrscr();
printf("3n+1 problem\n");
printf("enter the no\n");
scanf("%d",&fin);

while(fin!=1)
{
count++;
if(fin%2==0)
{
fin=fin/2;
printf("%d\t",fin);

}
else
{
fin=(fin*3)+1;
printf("%d\t",fin);
}

}
printf("no of elements are %d",++count);
getch();
}

OUTPUT:

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Experiment: 2

AIM: Program on Jolly Jumper Problem

THEORY:

A sequence of n > 0 integers is called a jolly jumper if the absolute values


of the
differences between successive elements take on all possible values 1
through n−1. For
instance,
1423
is a jolly jumper, because the absolute differences are 3, 2, and 1,
respectively. The
definition implies that any sequence of a single integer is a jolly
jumper.Write a program
to determine whether each of a number of sequences is a jolly jumper.

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
int x[20],i,v;
clrscr();
printf("enter the no.");

for(i=0;i<5;i++)
{
scanf("%d",&x[i]);

for(i=0;i<5;i++)
{
printf("%d",x[i]);
}
for(i=4;i<5;i--)
{
v=x[i+1]-x[i];

}
if(v==1)
printf("jolly");
else
printf("not");
getch();
}

OUTPUT:

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Experiment : 3
Aim : Program on Stack

Theory :
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or
FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
 Push: Adds an item in the stack. If the stack is full, then it is said to
be an Overflow condition.
 Pop: Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then it is
said to be an Underflow condition.
 Peek or Top: Returns top element of stack.
 isEmpty: Returns true if stack is empty, else false.

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Program Code:
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\tstack oeration using Array:");
printf("\n\t---------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t Exit point");
break;
}
default:
{
printf("Please enter a valid choice(1\2\3\4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{ printf("\n\tStack is overflow");
}
else
{
printf("Enter the value to be pushed");
scanf("%d",&x);
top++;
stack[top]=x;
}
}

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

void pop()
{
if(top<=-1)
{
printf("\n\tStack is underflow");
}
else
{
printf("\n\t Enter the value be pop",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\tThe element in a stack");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\npress next choice");
}
else
{
printf("\n The stack is empty");
}
}

Output:

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Experiment: 4
Aim : Program om Queue

Theory:
Like Stack, Queue is a linear structure which follows a particular order in
which the operations are performed. The order is First In First Out (FIFO). A

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

good example of queue is any queue of consumers for a resource where


the consumer that came first is served first.
The difference between stacks and queues is in removing. In a stack we
remove the item the most recently added; in a queue, we remove the item
the least recently added.
Operations on Queue:
Mainly the following four basic operations are performed on queue:
Enqueue: Adds an item to the queue. If the queue is full, then it is said to be
an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the
same order in which they are pushed. If the queue is empty, then it is said
to be an Underflow condition.
Front: Get the front item from queue.
Rear: Get the last item from queue.

Applications of Queue:
Queue is used when things don’t have to be processed immediatly, but
have to be processed in First InFirst Out order like Breadth First Search.
This property of Queue makes it also useful in following kind of scenarios.
1) When a resource is shared among multiple consumers. Examples
include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate
as sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.

Program Code :
#include<stdio.h>
#include<conio.h>
#define size 5

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

int front=0;
int rear=0;
int queue[size];
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.enqueue\n2.dequeue\n3.display\n4.exit\n");
printf("enter the choice");
scanf("%d",&ch);

switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit();
break;
}
}
getch();
}
void enqueue()
{
if(rear==size)
{
printf("queue is full");

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

}
else
{
int a;
printf("enter element:");
scanf("%d",&a);
queue[rear]=a;
rear++;
}
}
void dequeue()
{
if(rear==front)
{
printf("queue is empty");
}
else
{
printf("deleted element:%d",queue[front]);
front++;
}
}
void display()
{
if(rear==front)
{
printf("queue is empty");
}
else
{
int i;
for(i=front;i<rear;i++)
{
printf("\n%d",queue[i]);
}
}
}

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Experiment: 5

Aim : Program on Pattern String

Theory:

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

What are Loops?

In looping, a program executes the sequence of statements many times


until the stated condition becomes false. A loop consists of two parts, a
body of a loop and a control statement. The control statement is a
combination of some conditions that direct the body of the loop to execute
until the specified condition becomes false.

Types of Loops

Depending upon the position of a control statement in a program, a loop is


classified into two types:

1. Entry controlled loop

2. Exit controlled loop

In an entry controlled loop, a condition is checked before executing the


body of a loop. It is also called as a pre-checking loop.

In an exit controlled loop, a condition is checked after executing the body


of a loop. It is also called as a post-checking loop.

For loop

A for loop is a more efficient loop structure in 'C' programming. The general
structure of for loop is as follows:

for (initial value; condition; incrementation or decrementation )


{
statements;
}

 The initial value of the for loop is performed only once.


 The condition is a Boolean expression that tests and compares the
counter to a fixed value after each iteration, stopping the for loop
when false is returned.
 The incrementation/decrementation increases (or decreases) the
counter by a set value.

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

While Loop

A while loop is the most straightforward looping structure. The basic


format of while loop is as follows:

while (condition) {
statements;
}

It is an entry-controlled loop. In while loop, a condition is evaluated before


processing a body of the loop. If a condition is true then and only then the
body of a loop is executed. After the body of a loop is executed then
control again goes back at the beginning, and the condition is checked if it
is true, the same process is executed until the condition becomes false.
Once the condition becomes false, the control goes out of the loop.

After exiting the loop, the control goes to the statements which are
immediately after the loop. The body of a loop can contain more than one
statement. If it contains only one statement, then the curly braces are not
compulsory. It is a good practice though to use the curly braces even we
have a single statement in the body.

In while loop, if the condition is not true, then the body of a loop will not be
executed, not even once. It is different in do while loop which we will see
shortly.

Program Code:
#include <stdio.h>
#include <string.h>

int match(char [], char []);

int main() {
char a[100], b[100];
int position;

printf("Enter some text\n");


gets(a);

printf("Enter a string to find\n");

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

gets(b);

position = match(a, b);

if (position != -1) {
printf("Found at location: %d\n", position + 1);
}
else {
printf("Not found.\n");
}

return 0;
}

int match(char text[], char pattern[]) {


int c, d, e, text_length, pattern_length, position = -1;

text_length = strlen(text);
pattern_length = strlen(pattern);

if (pattern_length > text_length) {


return -1;
}

for (c = 0; c <= text_length - pattern_length; c++) {


position = e = c;

for (d = 0; d < pattern_length; d++) {


if (pattern[d] == text[e]) {
e++;
}
else {
break;
}
}
if (d == pattern_length) {
return position;
}
}
return -1;

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output:

Experiment :6

Aim: Program on Binary Sort

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Theory:
Given a sorted array arr[] of n elements, write a function to search a given
element x in arr[].
A simple approach is to do linear search.The time complexity of above
algorithm is O(n). Another approach to perform the same task is using
Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search
interval in half. Begin with an interval covering the whole array. If the value
of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.

Program Code:
#include <stdio.h>
int main()
{int c, first, last, middle, n, search, array[100];
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter %d integers:\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter the value to find:\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d is present at index %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

printf("Not found! %d is not present in the list.\n", search);


return 0;
}

Output:

Experiment: 7

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Aim: Program on Root Finding

Theory:
sqrt () is a C library function. It is mainly associated with programming
language. It is considerd under [math.h] header file.
function:

#include<cmath.h>
double sqrt (double x );
float sqrt (float x );
long double sqrt (long double x );

Description: sqrt computes square root. And returns The square root of x.
In C++, this function is overloaded in <complex> and <valarray> (see
complex sqrt and valarray sqrt). If the argument is negative, a domain error
occurs, setting the global variable errno to the value EDOM.

Return Value: Square root of x.


Portability: In C, only the double version of this function exists with this
name.

Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int num,ans;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
ans=pow(num,0.5);
printf("\n Square root of %d is: %d",num,ans);
getch();
}

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output:

Experiment :8

Aim: Program on Revers and Add Number

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Theory:
A while loop in C programming repeatedly executes a target statement as
long as a given condition is true.

Syntax
The syntax of a while loop in C programming language is −

while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any nonzero value. The
loop iterates while the condition is true.

Program code:
#include<stdio.h>
#include<conio.h>

void main( )
{
clrscr( )
int num,sum=0,rev=0,d;
printf("Enter the number: ");
scanf("%d",&n);

while(num){
d=num%10;
num=num/10;
sum=sum+d;
rev=rev*10+d;
}

printf("Sumof digits = %d",sum);


printf("\nReverse of the number = %d",rev);
getch( );
}

SBERCT’S Bhagwant Institute of Technology, BARSHI


COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Output:
Enter the no. 123
Sum of digits. 6
Reverse no. 321

SBERCT’S Bhagwant Institute of Technology, BARSHI

You might also like