Nothing Special   »   [go: up one dir, main page]

dsa

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

Write the programs in C/C++ to find Matrix Addition, Subtraction, Transpose, Multiplication.

1.Addition of matrix

#include<bits/stdc++.h>

using namespace std;

int main()

int m, n, c, d, first[10][10], second[10][10], add[10][10];

printf("Enter the number of rows and columns of matrix\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)

for (d = 0 ; d < n; d++)

scanf("%d", &second[c][d]);

printf("add of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0 ; d < n; d++) {

add[c][d] = first[c][d] + second[c][d];

printf("%d\t", add[c][d]);

printf("\n");
}

return 0;

Output -Addition of matrix


2.Subtraction of matrix

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

Output- Subtraction of matrices


3. Multiplication of matrices

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

// Storing elements of first matrix.

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;

Output -multiplication of matrtix


4.Transpose of matrix

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

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

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;

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

for (int j = 0; j < column; ++j) {

transpose[j][i] = a[i][j];
}

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;

Output – Transpose of matrix


Write the programs in C/C++ to Implement Bubble, Selection, Insertion Sort.

1.Bubble Sort

#include<iostream>

using namespace std;

void swapping(int &a, int &b) {

int temp;

temp = a;

a = b;

b = temp;

void display(int *array, int size) {

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

cout << array[i] << " ";

cout << endl;

void bubbleSort(int *array, int size) {

for(int i = 0; i<size; i++) {

int swaps = 0;

for(int j = 0; j<size-i-1; j++) {

if(array[j] > array[j+1]) {

swapping(array[j], array[j+1]);

swaps = 1;

if(!swaps)

break;

int main() {

int n;

cout << "Enter the number of elements: ";


cin >> n;

int arr[n];

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

bubbleSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

Output -BUBBLE SORT


2. Selection Sort

#include<iostream>

using namespace std;

void swapping(int &a, int &b) {

int temp;

temp = a;

a = b;

b = temp;

void display(int *array, int size) {

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

cout << array[i] << " ";

cout << endl;

void selectionSort(int *array, int size) {

int i, j, imin;

for(i = 0; i<size-1; i++) {

imin = i;

for(j = i+1; j<size; j++)

if(array[j] < array[imin])

imin = j;

swap(array[i], array[imin]);

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n];

cout << "Enter elements:" << endl;


for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);

selectionSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

Output- SELECTION SORT


3.Insertion Sort

#include<iostream>

using namespace std;

void display(int *array, int size) {

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

cout << array[i] << " ";

cout << endl;

void insertionSort(int *array, int size) {

int key, j;

for(int i = 1; i<size; i++) {

key = array[i];

j = i;

while(j > 0 && array[j-1]>key) {

array[j] = array[j-1];

j--;

array[j] = key;

int main() {

int n;

cout << "Enter the number of elements: ";

cin >> n;

int arr[n];

cout << "Enter elements:" << endl;

for(int i = 0; i<n; i++) {

cin >> arr[i];

cout << "Array before Sorting: ";

display(arr, n);
insertionSort(arr, n);

cout << "Array after Sorting: ";

display(arr, n);

Output- INSERTION SORT


Write the programs in C/C++ to Implement Linear and Binary Search.

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

Output -LINEAR SEARCH


2.Binary Search
#include<bits/stdc++.h>
using namespace std;

int binarySearch(int arr[], int p, int r, int num) {


int mid=(p+r)/2;
while(r>=p && arr[mid]!=num)
{
if(num<arr[mid])
r=mid-1;
else
p=mid+1;
mid=(p+r)/2;
}
if(arr[mid]==num)
return mid;
else
return -1;
}

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

1.Inserting in linked list

#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
struct Node* next;
};

int size = 0;
Node* getNode(int data)
{

Node* newNode = new Node();

newNode->data = data;
newNode->next = NULL;
return newNode;
}

void insertPos(Node** current, int pos, int data)


{

if (pos < 1 || pos > size + 1)


cout << "Invalid position!" << endl;
else {

while (pos--) {

if (pos == 0) {

Node* temp = getNode(data);


temp->next = *current;

*current = temp;
}
else

current = &(*current)->next;
}
size++;
}
}

void printList(struct Node* head)


{
while (head != NULL) {
cout << " " << head->data;
head = head->next;
}
cout << endl;
}

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;

cout << "Linked list before insertion: ";


printList(head);

int data = 12, pos = 3;


insertPos(&head, pos, data);
cout << "Linked list after insertion of 12 at position 3: ";
printList(head);

data = 1, pos = 1;
insertPos(&head, pos, data);
cout << "Linked list after insertion of 1 at position 1: ";
printList(head);

data = 15, pos = 7;


insertPos(&head, pos, data);
cout << "Linked list after insertion of 15 at position 7: ";
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;
};

void push(Node** head_ref, int new_data)


{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void deleteNode(Node** head_ref, int key)


{
Node* temp = *head_ref;
Node* prev = NULL;

if (temp != NULL && temp->data == key)


{
*head_ref = temp->next;
delete temp;
return;
}
else
{
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->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()
{

// Start with the empty list


Node* head = NULL;

// Add elements in linked list


push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);

puts("Created Linked List: ");


printList(head);

deleteNode(&head, 1);
puts("\nLinked List after Deletion of 1: ");

printList(head);

return 0;
}
Output -Deleting an node in linked list

You might also like