Untitled
unknown
c_cpp
2 years ago
19 kB
16
Indexable
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<< "Enter the number of elements : ";
int n;
cin>>n;
int maxx = INT_MIN;
int minn = INT_MAX;
int arr[n];
for (int i=0; i<n; i++)
{
cin>>arr[i];
}
//display all the elements array
for (int i=0; i<n; i++)
{
cout<< arr[i];
}
//find the largest number & smallest number
for (int i=0; i<n; i++)
{
if (arr[i]>maxx) maxx = arr[i];
if (arr[i]<minn) minn = arr[i];
}
cout<<maxx<<' '<<minn<<endl;
return 0;
}
//Write a program to create an array of n elements and then insert an element to the list
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<< "Enter the number of elements : ";
int n, item, loc;
cin>>n;
int arr[50];
for (int i=1; i<=n; i++)
{
cin>>arr[i];
}
for (int i=1; i<=n; i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
cout << "Enter the element to insert: ";
cin >> item;
cout << "Enter the location to insert the element (1 to " << n + 1 << "): ";
cin >> loc;
if (loc < 1 || loc > n + 1)
{
cout << "Invalid location. Enter the valid location next time. Exiting program." << endl;
return 0;
}
for (int i = n+1; i > loc; --i)
{
arr[i] = arr[i - 1];
}
arr [loc] = item;
++n;
cout << "Updated array: ";
for (int i = 1; i <= n; ++i)
{
cout << arr[i] << " ";
}
return 0;
}
//Write a program to create an array of n elements and then delete an element from the list.
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<< "Enter the number of elements : ";
int n, item, loc;
cin>>n;
int arr[50];
for (int i=1; i<=n; i++)
{
cin>>arr[i];
}
for (int i=1; i<=n; i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
cout << "Enter the location of the element you want to delete from (1 to " << n << "): ";
cin >> loc;
if (loc < 1 || loc > n)
{
cout << "Invalid location. Enter the valid location next time. Exiting program." << endl;
return 0;
}
if (loc==1)
{
cout << "Updated array: ";
for (int i = 2; i <= n; ++i)
{
cout << arr[i] << " ";
}
}
else
{
cout << "Updated array: ";
for (int i = 1; i <= loc-1; ++i)
{
cout << arr[i] << " ";
}
for (int i = loc+1; i <= n; ++i)
{
cout << arr[i] << " ";
}
}
return 0;
}
//Write a program to sort n numbers using Bubble Sort algorithm.
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++)
{
swapped = false;
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i]<< " " ;
}
int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
//matrix multiplication
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
// If column of first matrix in not equal to row of second matrix,
// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}
cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}
return 0;
}
// Binary Search in C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int array[], int x, int low, int high)
{
// Repeat until the pointers low and high meet each other
while (low <= high)
{
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main()
{
int array[] = {3, 4, 5, 6, 7, 8, 9};
int x = 4;
int n = sizeof(array) / sizeof(array[0]);
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
}
//Stack Implementation
#include<stdio.h>
#define max 5
int stack[10];
int top=0;
void PUSH(int item)
{
if(top==max)
{
printf("Stack OverFlow\n");
}
else
{
top++;
stack[top]=item;
}
}
void POP()
{
if(top>0)
{
printf("%d is Popped\n",stack[top]);
top--;
}
else
{
printf("Stack UnderFlow\n");
}
}
void TOP()
{
if(top==0)
{
printf("Stack is empty\n");
}
else
{
printf("%d\n",stack[top]);
}
}
void SHOW()
{
if(top==0)
{
printf("Stack is empty\n");
}
else
{
for(int i=1; i<=top; i++)
{
printf("%d ",stack[i]);
}
}
}
int main()
{
int choice,item;
do
{
printf("\t\t....INDEX......\n");
printf("\t\t 1-> Push\n");
printf("\t\t 2-> Pop\n");
printf("\t\t 3-> Top\n");
printf("\t\t 4-> Show\n");
printf("\t\t 5-> Exit\n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter an item: ");
scanf("%d",&item);
PUSH(item);
break;
case 2:
POP();
break;
case 3:
TOP();
printf("\n");
break;
case 4:
SHOW();
printf("\n");
break;
case 5:
printf("Thank You!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
}
while(choice!=5);
}
//Postfix Evaluation
#include<bits/stdc++.h>
using namespace std;
int evaluatePostfixExpression(string expression)
{
stack<int> st;
for (int i = 0; i < expression.length(); i++)
{
char c = expression[i];
// If 'c' is a digit (operand)
if (c >= '0' && c <= '9')
{
// Convert 'c' in integer and
// push it into the stack.
int temp = (int)(c - '0');
st.push(temp);
}
// Otherwise it is an operator.
else
{
// Pop element from the stack.
int op1 = st.top();
st.pop();
// Pop another element from the stack.
int op2 = st.top();
st.pop();
// Use the switch case to deal with
// the operand accordingly.
switch(c)
{
case '+':
st.push(op2 + op1);
break;
case '-':
st.push(op2 - op1);
break;
case '*':
st.push(op2 * op1);
break;
case '/':
st.push(op2 / op1);
break;
}
}
}
return st.top();
}
int main()
{
string expression = "23*45+*";
cout << evaluatePostfixExpression(expression) << endl;
}
//CIRCULARQUEUE
#include<stdio.h>
#define max 4
int queue[100];
int front=0,rear=0;
void Insert(int item)
{
if(( front==1 && rear == max) || rear == front - 1)
{
printf("Queue Overflow\n");
}
else if( front!= 1 && rear == max)
{
rear = 1;
queue[rear]=item;
printf("%d is added\n",item);
}
else if( front== 0 )
{
front = 1;
rear = 1;
queue[rear]=item;
printf("%d is added\n",item);
}
else
{
rear++;
queue[rear]=item;
printf("%d is added\n",item);
}
}
void Delete()
{
if(front == 0)
{
printf("Queue Underflow\n");
}
else
{
printf(" %d is deleted\n",queue[front]);
if(front == rear)
{
front =0;
rear = 0;
}
else if( front == max)
{
front =1;
}
else
{
front++;
}
}
}
void Show()
{
if (front == 0)
{
printf("Queue is Empty\n");
}
else
{
if(rear>=front)
{
for (int i=front; i<=rear; i++)
{
printf("%d ",queue[i]);
}
}
else
{
for (int i=front; i<=max; i++)
{
printf("%d ",queue[i]);
}
for (int i=1; i<=rear; i++)
{
printf("%d ",queue[i]);
}
}
}
}
int main()
{
int choice,item;
do
{
printf("\t\t\t.....INDEX......\n");
printf("\t\t\t 1-> Insert\n");
printf("\t\t\t 2-> Delete\n");
printf("\t\t\t 3-> Show\n");
printf("\t\t\t 4-> Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter an item: ");
scanf("%d",&item);
Insert(item);
break;
case 2:
Delete();
break;
case 3:
Show();
break;
case 4:
printf("Thank you!! See you Again.\n");
break;
default :
printf("Invalid Choice! Try Again.");
break;
}
}
while(choice!=4);
return 0;
}
//linkListedStore
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
int main()
{
node *start,*ptr;
int i,n;
start=(node*)malloc(sizeof(node));
ptr=start;
printf("How many numbers: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("Enter value: ");
scanf("%d",&ptr->data);
if (i!=n)
{
ptr->next=(node*)malloc(sizeof(node));
ptr=ptr->next;
}
}
ptr->next=NULL;
printf("Printing list: ");
ptr=start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
return 0;
}
//LinkListedSearch
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
int main()
{
node *start,*ptr;
int i,n,x,f=0;
start=(node*)malloc(sizeof(node));
ptr=start;
printf("How many numbers: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
printf("Enter value: ");
scanf("%d",&ptr->data);
if (i!=n)
{
ptr->next=(node*)malloc(sizeof(node));
ptr=ptr->next;
}
}
ptr->next=NULL;
printf("Desired Value : ");
scanf("%d",&x);
ptr = start;
while (ptr!=NULL)
{
if(ptr->data==x)
{
f++;
break;
}
ptr=ptr->next;
}
if(f==0)
{
printf("Not Found");
}
else
{
printf("Found");
}
return 0;
}
//LinkListedInsertion
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
int main()
{
node *start,*insert,*ptr;
int i,n,position;
start=(node*)malloc(sizeof(node));
ptr=start;
printf("How many numbers: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("Enter value: ");
scanf("%d",&ptr->data);
if (i!=n)
{
ptr->next=(node*)malloc(sizeof(node));
ptr=ptr->next;
}
}
ptr->next=NULL;
printf("Printing list: ");
ptr=start;
while (ptr!=NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
printf("Enter a new value to insert: ");
insert = (node*)malloc(sizeof(node));
scanf("%d",&insert->data);
insert->next=NULL;
printf("Enter the position to insert: ");
scanf("%d",&position);
if (position<1 || position>n+1)
{
printf("Invalid position.\n");
}
else if (position==1)
{
insert->next=start;
start=insert;
}
else
{
ptr = start;
for (i=1; i<position-1; i++)
{
ptr=ptr->next;
}
insert->next=ptr->next;
ptr->next=insert;
}
printf("Printing list after insertion: ");
ptr = start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
return 0;
}
//LinkListedDeletion
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
int main()
{
node *start,*ptr,*prev;
int i,n,value;
start=(node*)malloc(sizeof(node));
ptr=start;
printf("How many numbers: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("Enter value: ");
scanf("%d",&ptr->data);
if (i!=n)
{
ptr->next=(node*)malloc(sizeof(node));
ptr=ptr->next;
}
}
ptr->next=NULL;
printf("Printing list: ");
ptr = start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
printf("Enter the value to delete: ");
scanf("%d",&value);
ptr=start;
prev=NULL;
while (ptr!=NULL)
{
if (ptr->data==value)
{
if (prev==NULL)
{
start=ptr->next;
}
else
{
prev->next=ptr->next;
}
printf("Value %d has been deleted.\n", value);
break;
}
prev=ptr;
ptr=ptr->next;
}
printf("Printing list after deletion: ");
ptr=start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
return 0;
}
//LinkListedSort
#include <stdio.h>
#include <stdlib.h>
struct linked_list
{
int data;
struct linked_list *next;
};
typedef struct linked_list node;
int main()
{
node *start,*ptr;
int i,n;
start=(node*)malloc(sizeof(node));
ptr=start;
printf("How many numbers: ");
scanf("%d",&n);
for (i=1; i<=n; i++)
{
printf("Enter value: ");
scanf("%d",&ptr->data);
if (i!=n)
{
ptr->next=(node*) malloc(sizeof(node));
ptr=ptr->next;
}
}
ptr->next=NULL;
printf("Original list: ");
ptr=start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
int temp;
for(i=1; i<=n; i++)
{
ptr=start;
while(ptr->next!=NULL)
{
if(ptr->data > ptr->next->data)
{
temp=ptr->data;
ptr->data=ptr->next->data;
ptr->next->data=temp;
}
ptr=ptr->next;
}
}
printf("Sorted list: ");
ptr = start;
while (ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
return 0;
}
//selection sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,temp,min;
cout<<"How many input: ";
cin>>n;
int data[n];
for(i=0; i<n; i++)
{
cout<<"Enter the value: ";
cin>>data[i];
}
cout<<endl;
for(i=0; i<n-1; i++)
{
min=i;
for(j=i+1; j<n; j++)
{
if(data[j]<data[min])
{
min=j;
}
}
temp=data[min];
data[min]=data[i];
data[i]=temp;
}
cout<<"Sorted Array by Selection Sort: ";
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
return 0;
}
//insertion sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,temp,min;
cout<<"How many input: ";
cin>>n;
int data[n];
for(i=0; i<n; i++)
{
cout<<"Enter the value: ";
cin>>data[i];
}
cout<<endl;
for(i=0; i<n; i++)
{
temp=data[i];
j=i-1;
while (j>=0 && temp<data[j])
{
data[j+1]=data[j];
j--;
}
data[j+1]=temp;
}
cout<<"Sorted Array by Insertion Sort: ";
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
return 0;
}
Editor is loading...
Leave a Comment