dsa 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

1- Write a program to show traversal in 1-D array

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];

cout << "\nPlease Enter the Matrix Items\n";


for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> upperTriMatrix[rows][columns];
}
}

cout << "\nThe Result of the Upper Triangle Matrix is :\n";


for(rows = 0; rows < i; rows++)
{
cout << "\n";
for(columns = 0; columns < j; columns++)
{
if(columns >= rows)
{
cout << upperTriMatrix[rows][columns] << " ";
}
else
{
cout << "0 ";
}
}
}

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>

#include <set> // for performing set operations

using namespace std;

int main()

//set initialisation

set<int> sst;

// iterators declaration

set<int>::iterator ite = sst.begin();

set<int>::iterator ite1, ite2;

//declaring a pair for the set's return value

// Initialize iterator and bool

pair<set<int>::iterator, bool> pt;

// insert() function for inserting element


pt = sst.insert(30);

// checking if the element was already present or


newly

// inserted

if (pt.second)

cout << "Newly inserted element";

else

cout << "The element was inserted already";

// display of set elements after insertion

cout << "\nThe elements of the set after the first


insertion are ";

for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)

cout << *ite1 << " ";

//inserting the elements using hint

sst.insert(ite, 34);

// display of set elements after insertion


cout << "\nThe elements of the set after the second
insertion are: ";

for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)

cout << *ite1 << " ";

//elements inserting

// the element 34 is not inserted

int array[3] = { 25, 34, 26 };

sst.insert(array, array + 3);

// display of set elements after insertion

cout << "\nThe elements of the set after the third


insertion are: ";

for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)

cout << *ite1 << " ";

}
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;

// declaration of the iterators


set<int>::iterator ite;
set<int>::iterator ite1;
set<int>::iterator ite2;

// the return value declaration of the set


pair<set<int>::iterator, bool> pt;

// inserting values in the set


for (int i = 1; i < 10; i++)
sst.insert(i * 2);

// display of initial elements in the set


cout << "The elements of the set after the insertion are: ";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";

ite = sst.begin();

cout << endl;

// deleting element with an iterator


// removes the second element, i.e., 4
++ite;
sst.erase(ite);

//display of elements after the deletion


cout << "The elements of the set after the first deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";

// erasing by value
sst.erase(40);

//display of elements after the deletion


cout << "\nThe elements of the set after the second deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";

++ite;
++ite;
++ite;
++ite;

// by using the range


sst.erase(ite, sst.end());

// display of elements after the 3rd deletion


cout << "\nThe elements of the set after the third deletion are :";
for (ite1 = sst.begin(); ite1 != sst.end(); ++ite1)
cout << *ite1 << " ";

cout << endl;


}
10- Write a program of linear search using C++
#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int target) {


for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Element found, return its index
}
}
return -1; // Element not found
}

int main() {
int data[] = {12, 45, 78, 23, 56, 89, 67, 34, 90};
int n = sizeof(data) / sizeof(data[0]);
int target = 67;

int result = linearSearch(data, n, target);

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;

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 (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;

// Merge two subarrays L and M into arr


void merge(int arr[], int p, int q, int r) {

// Create L ← A[p..q] and M ← A[q+1..r]


int n1 = q - p + 1;
int n2 = r - q;

// Use std::vector to dynamically allocate arrays


vector<int> L(n1);
vector<int> M(n2);

for (int i = 0; i < n1; i++)


L[i] = arr[p + i];

for (int j = 0; j < n2; j++)


M[j] = arr[q + 1 + j];

// Maintain current index of sub-arrays and main array


int i = 0, j = 0, k = p;
// Until we reach either end of either L or M, pick larger among
// elements L and M and place them in the correct position at
A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}

// When we run out of elements in either L or M,


// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = M[j];
j++;
k++;
}
}

// 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);
}
}

// Print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

// 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>

void bubbleSort(int arr[], int n) {


int i, j;
bool flag;
for(i = 0; i < n; i++) {
flag = false;
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
std::swap(arr[j], arr[j+1]);
flag = true;
}
}
if(!flag) {
break;
}
}
}

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);

std::cout << "Sorted Array: ";


for(int i = 0; i < n; i++)
std::cout << arr[i] << " ";

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;
}

void insertionSort(int array[], int size) {


for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
while (j >=0 && key < array[j]) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
int main() {
int data[] = {9, 5, 1, 4, 3};
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
cout << "Sorted array in ascending order:\n";
printArray(data, size);
}
15- Write a program of selection sort using c++
#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void printArray(int array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}

void selectionSort(int array[], int size) {


for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx])
min_idx = i;
}
swap(&array[min_idx], &array[step]);
}
}

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);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy