Compititve Progrmming Mannual
Compititve Progrmming Mannual
COMPETITIVE PROGRAMMING
LAB MANUAL
For T.E. (CSE)
Compiled By
.
Competitive Programming - I Laboratory Manual
PREPARED BY
3 Program on Stack
4 Program on Queue
Table of Contents
Sr. No. Topic Page. No.
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
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
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.
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.
Course Outcomes
At The End Of Course Student Will Able to:
2. Laboratory Details
Useful libraries: CPP, Java, Python
Template: CPP
Experiment: 1
AIM: Program on 3n+1 problem
THEORY:
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;
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:
Experiment: 2
THEORY:
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:
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.
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:
{
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;
}
}
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:
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
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
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");
}
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]);
}
}
}
Experiment: 5
Theory:
Types of Loops
For loop
A for loop is a more efficient loop structure in 'C' programming. The general
structure of for loop is as follows:
While Loop
while (condition) {
statements;
}
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 main() {
char a[100], b[100];
int position;
gets(b);
if (position != -1) {
printf("Found at location: %d\n", position + 1);
}
else {
printf("Not found.\n");
}
return 0;
}
text_length = strlen(text);
pattern_length = strlen(pattern);
Output:
Experiment :6
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)
Output:
Experiment: 7
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.
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();
}
Output:
Experiment :8
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;
}
Output:
Enter the no. 123
Sum of digits. 6
Reverse no. 321