Labwork.cpp (1)
Labwork.cpp (1)
#include<iostream>
int main()
int array[10],n,i;
cin>>n;
for(i=0;i<n;i++)
cin>>array[i];
cout<<endl;
for(i=0;i<n;i++)
cout<<array[i]<<" ";
for(i=0;i<n;i++)
cout<<array[i]+5<<" ";
}
2. Write a C++ program to insert an element in an array at any index(you must taken the
index number from user) and print the array after inserting the element. The maximum
size of the array is 10.
Sample Input: 10 5 23 11 7 17
Sample Output: 10 5 23 33 11 7 17
#include<iostream>
int main()
int array[20],n,pos,i,num;
cin>>n;
for(i=0;i<n;i++)
cin>>array[i];
}
cout<<"Enter insertion position:"<<endl;
cin>>pos;
cin>>num;
for(i=n-1;i>=pos-1;i--)
array[i+1]=array[i];
n++;
array[pos-1]=num;
cout<<"Inserted array:"<<endl;
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}
}
3. Write a C++ program to delete an element from an array at any index(you must taken
the index number from user) and print the array after deleting the element. The maximum
size of the array is 10.
Sample Input: 10 5 23 11 7 17
Sample Output: 10 23 11 7 17
#include<iostream>
using namespace std;
int main()
{
int array[20],n,i,pos,num;
cout<<"Enter the length of the array:"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cin>>array[i]; // 1 2 3 4 5 6 7 8 9
}
for(i=pos-1;i<n;i++)
{
array[i]=array[i+1];
}
n--;
for(i=0;i<n;i++)
{
cout<<array[i]<<" "; }
}
Lab work 2:
#include<iostream>
using namespace std;
int main()
{
int array[20],n,i,num;
for(i=0;i<n;i++)
{
cin>>array[i]; // 1 2 3 4 5 6 7 8 9
}
cout<<"Enter the number you want to search:"<<endl;
cin>>num;
for(i=0;i<n;i++)
{
if(array[i]==num)
{
int array[10],n,num,i;
int binarysearch(int low,int high)
{
while(low<=high)
{
int mid=(low+high)/2;
if(array[mid]==num)
{
return mid;
}
else if(num >array[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
return -1;
}
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
flag=1;
}
}
if(flag==0)
{
break;
}
}
cout<<"Sorted array"<<endl;
for(i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
}
int main()
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter array elements:"<<endl;
}
bubblesort(array,n);
cout<<"\n Enter the number to search in the array:"<<endl;
cin>>num;
int result= binarysearch(0,n-1);
if(result == -1)
{
cout<<"Elements not found"<<endl;
}
else
{
cout<<"Array found at index:"<<result<<endl;
}
}
Lab work 3:
1. Complete the code of insertion sort and selection sort by taking input from the users .
Insertion sort:
#include <iostream>
using namespace std;
void insertionSort(int array[10], int n) {
for(int i=1;i<n;i++)
{
int current=array[i];
int j=i-1;
while(array[j]>current && j>=0)
{
array[j+1]=array[j];
j--;
}
array[j+1]=current;
}
}
void display(int array[10], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << " ";
cout << endl;
}
int main() {
int array[10],i,n;
cout<<"Enter array size:"<<endl;
cin>>n;
cout<<"Enter array elements:"<<endl;
for(i=0;i<n;i++)
{
cin>>array[i];
}
insertionSort(array, n);
Selection sort:
#include <iostream>
using namespace std;
for(int j=i+1;j<n;j++)
{
if(array[min]>array[j])
{
min=j;
}
}
int temp = array[i];
array[i]=array[min];
array[min]=temp;
}
}
void display(int array[10], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << " ";
int main() {
int array[10],i,n;
cout<<"Enter array size:"<<endl;
cin>>n;
selectionsort(array, n);
}
Lab work 4: Write down a program to traverse a link
list by taking values from users.
Sample Input:
How many nodes user want to insert: 5 (then the link
list will take 5 values from the user)
Sample output:
Shows the values as output.
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next=NULL;
}
};
int main()
{
int n,value;
cout<<"Enter how much number you want to add:"<<endl;
cin>>n;
node* head=NULL;
cout<<"Enter your desire value:"<<endl;
node* tail;
for(int i=0; i<n; i++)
{
cin>>value;
node* one= new node(value);
if(head==NULL)
{
head=one;
tail=head;
}
else
{
node* temp;
temp=new node(value);
tail=tail->next=temp;
}
}
node* sum;
sum=head;
while(sum!=NULL)
{
cout<<sum->data<<" ";
sum=sum->next;
}
}
LAB WORK 5:
1. Complete the link list insert and delete operation. You have to write the code and understand
that.
2. You are given the heads of two sorted linked lists list1 and list2 in the
below-
Input: list1= [1,3,5] & list2= [2,4,6]
Output: [1,2,3,4,5,6]
Write down the C++ code to merge these two sorted link list.
All of you must complete these code. I suggest to give some effort.
Task 1:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next =NULL;
}
};
class linkedlist
{
public:
node* head;
linkedlist()
{
head=NULL;
}
int value;
void insert_at_the_beginning(int value)
{
node* one=new node(value);
if(head==NULL)
{
head=one;
}
else
{
node* temp;
temp=new node(value);
temp->next=head;
head=temp;
}
}
void delete_at_the_head()
{
if(head==NULL)
{
cout<<"Linked list is empty"<<endl;
return;
}
else
{
node* todelete=head;
head=head->next;
delete todelete;
}
}
if (position < 0)
{
cout << "Invalid position" << endl;
return;
}
if (position == 0)
{
delete_at_the_head();
return;
}
void print()
{
node *print;
print=head;
while(print!=NULL)
{
cout<<print->data<<" -> ";
print=print->next;
}
cout<<"NULL";
}
};
int main()
{
linkedlist link;
int n;
cout<<"Enter the number of nodes:"<<endl;
cin>>n;
int position,value;
cout<<"Enter position:"<<endl;
cin>>position;
cout<<"Enter the number you want to insert:"<<endl;
cin>>value;
link.insert_at_any_position(value,position);
link.print();
link.delete_at_any_position(position);
link.print();
}
Task 2:
You are given the heads of two sorted linked lists list1 and list2 in
the below-
Input: list1= [1,3,5] & list2= [2,4,6]
Output: [1,2,3,4,5,6]
Write down the C++ code to merge these two sorted link list.
All of you must complete these code. I suggest to give some
effort.
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next=NULL;
}
};
if(head1!=NULL)
{
tail->next=head1;
}
else
{
tail->next=head2;
}
node* temp=head;
head=head->next;
delete temp;
return head;
}
int main()
{
node* head1=new node(1);
head1->next=new node(3);
head1->next->next=new node(5);
node* temp=mergetwosort(head1,head2);
cout<<"Merged linked list is:"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" -> ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}