dsa 1
dsa 1
dsa 1
using c++
#include <iostream>
using namespace std;
int main()
{
int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
//traversing array
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
}
2- Write a program subtraction of two matrices
using C++
#include<iostream>
using namespace std;
int main()
{
int matOne[3][3], matTwo[3][3], matSub[3][3], i, j;
cout<<"Enter 9 Elements for First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matOne[i][j];
}
cout<<"Enter 9 Elements for Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matTwo[i][j];
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = matOne[i][j] - matTwo[i][j];
}
cout<<"\nThe New Matrix (Subtraction Result) is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matSub[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0;
}
3- Write a program to multiplication of two matrices
using C++
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
4- Write a program to store element in 1-D array
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
// Printing array elements
// using range based for loop
for (int n : numbers) {
cout << n << " ";
}
cout << "\nThe numbers are: ";
// Printing array elements
// using traditional for loop
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
5- Write a program of lower triangular matrix using
C++
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter Matrix rows and Columns to find
Lower Triangle = ";
cin >> i >> j;
int lowerTriMatrix[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> lowerTriMatrix[rows][columns];
}
}
cout << "\nThe Result of the Lower Triangle Matrix is :\n";
for(rows = 0; rows < i; rows++)
{
cout << "\n";
for(columns = 0; columns < j; columns++)
{
if(rows >= columns)
{
cout << lowerTriMatrix[rows][columns] << " ";
}
else
{
cout << "0 ";
}
}
}
return 0;
}
6- Write a program of upper triangular matrices
using C++
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "\nPlease Enter Matrix rows and Columns to find Upper
Triangle = ";
cin >> i >> j;
int upperTriMatrix[i][j];
return 0;
}
7- Write a program of transpose matrices using C++
#include <iostream>
using namespace std;
int main() {
int a[10][10], transpose[10][10], row, column, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> row >> column;
cout << "\nEnter elements of matrix: " << endl;
// Storing matrix elements
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}
// Printing the a matrix
cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}
// Computing transpose of the matrix
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}
// Printing the transpose
cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}
return 0;
}
8- Write a program of insertion in linear array using
c++
#include <iostream>
int main()
//set initialisation
set<int> sst;
// iterators declaration
// inserted
if (pt.second)
else
sst.insert(ite, 34);
//elements inserting
}
9- Write a program of deletion in linear array using
C++
#include <iostream>
#include <set> // for set operations
using namespace std;
int main()
{
// set declaration
set<int> sst;
ite = sst.begin();
// erasing by value
sst.erase(40);
++ite;
++ite;
++ite;
++ite;
int main() {
int data[] = {12, 45, 78, 23, 56, 89, 67, 34, 90};
int n = sizeof(data) / sizeof(data[0]);
int target = 67;
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}
11- Write a program of Binary search using C++
#include <iostream>
using namespace std;
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (x == array[mid])
return mid;
if (x > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
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);
}
12- Write a program of merge sort using c++
#include <iostream>
#include <vector>
using namespace std;
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted subarrays
merge(arr, l, m, r);
}
}
// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
cout << "Sorted array: \n";
printArray(arr, size);
return 0;
}
13- Write a program of bubble sort using c++
#include <iostream>
int main() {
int arr[] = {10, 5, 15, 0, 12};
int n = sizeof(arr) / sizeof(arr[0]);
std::cout << "Unsorted Array: ";
for(int i = 0; i < n; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
bubbleSort(arr, n);
return 0;
}
14- Write a program of insertion sort using c++
#include <iostream>
using namespace std;
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
cout << "Sorted array in Acsending Order:\n";
printArray(data, size);
}