dsa
dsa
dsa
1.Addition of matrix
#include<bits/stdc++.h>
int main()
scanf("%d", &first[c][d]);
scanf("%d", &second[c][d]);
printf("%d\t", add[c][d]);
printf("\n");
}
return 0;
#include<iostream>
int main()
cin>>matOne[i][j];
cin>>matTwo[i][j];
cout<<matSub[i][j]<<" ";
cout<<endl;
cout<<endl;
return 0;
#include <iostream>
int main()
cout << "Enter rows and columns for first matrix: ";
cout << "Enter rows and columns for second matrix: ";
while (c1!=r2)
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cout << "Enter rows and columns for second matrix: ";
cout << endl << "Enter elements of matrix 1:" << endl;
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cout << endl << "Enter elements of matrix 2:" << endl;
cout << "Enter element b" << i + 1 << j + 1 << " : ";
mult[i][j]=0;
if(j == c2-1)
return 0;
#include <iostream>
int main() {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
if (j == column - 1)
transpose[j][i] = a[i][j];
}
if (j == row - 1)
return 0;
1.Bubble Sort
#include<iostream>
int temp;
temp = a;
a = b;
b = temp;
int swaps = 0;
swapping(array[j], array[j+1]);
swaps = 1;
if(!swaps)
break;
int main() {
int n;
int arr[n];
display(arr, n);
bubbleSort(arr, n);
display(arr, n);
#include<iostream>
int temp;
temp = a;
a = b;
b = temp;
int i, j, imin;
imin = i;
imin = j;
swap(array[i], array[imin]);
int main() {
int n;
cin >> n;
int arr[n];
display(arr, n);
selectionSort(arr, n);
display(arr, n);
#include<iostream>
int key, j;
key = array[i];
j = i;
array[j] = array[j-1];
j--;
array[j] = key;
int main() {
int n;
cin >> n;
int arr[n];
display(arr, n);
insertionSort(arr, n);
display(arr, n);
1. Linear Search
#include<iostream>
using namespace std;
int main()
{
int arr[10], i, num, index;
cout<<"Enter 10 Numbers: ";
for(i=0; i<10; i++)
cin>>arr[i];
cout<<"\nEnter a Number to Search: ";
cin>>num;
for(i=0; i<10; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"\nFound at Index No."<<index;
cout<<endl;
return 0;
}
int main(void)
{
int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int num;
cout << "Enter the number to search: \n";
cin >> num;
int index = binarySearch (arr, 0, n-1, num);
if(index == -1){
cout<< num <<" is not present in the array";
}else{
cout<< num <<" is present at index "<< index <<" in the array";
}
return 0;
}
Output – BINARY SEARCH
Write the programs in C/C++ to Creating a linked list, Inserting and Deletion an element in a
linked list
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int size = 0;
Node* getNode(int data)
{
newNode->data = data;
newNode->next = NULL;
return newNode;
}
while (pos--) {
if (pos == 0) {
*current = temp;
}
else
current = &(*current)->next;
}
size++;
}
}
int main()
{
Node* head = NULL;
head = getNode(3);
head->next = getNode(5);
head->next->next = getNode(8);
head->next->next->next = getNode(10);
size = 4;
data = 1, pos = 1;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 1 at position 1: ";
printList(head);
return 0;
}
Output Insertion in linked list
2. Deletion an node in linked list
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
};
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;
}
}
// linked list starting from the
// given node
void printList(Node* node)
{
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
}
// Driver code
int main()
{
deleteNode(&head, 1);
puts("\nLinked List after Deletion of 1: ");
printList(head);
return 0;
}
Output -Deleting an node in linked list