0% found this document useful (0 votes)
38 views

C++ DSA Codes (Questions)

This document contains C++ code examples for common array operations: 1) Finding the largest/first and second largest elements in an array. 2) Linear search to find an element in an array. 3) Inserting and deleting elements from an array. 4) Traversing and printing an array. 5) Sorting an array using bubble sort. 6) Performing binary search in a sorted array. 7) Multiplying two matrices by iterating through their elements.

Uploaded by

farman7757854
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

C++ DSA Codes (Questions)

This document contains C++ code examples for common array operations: 1) Finding the largest/first and second largest elements in an array. 2) Linear search to find an element in an array. 3) Inserting and deleting elements from an array. 4) Traversing and printing an array. 5) Sorting an array using bubble sort. 6) Performing binary search in a sorted array. 7) Multiplying two matrices by iterating through their elements.

Uploaded by

farman7757854
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Largest element in an array

#include <iostream.h>
#include <conio.h>
int findLargestElementIndex(int DATA[], int N) {
int LOC = 0;
int MAX = DATA[0];
for (int K = 1; K < N; K++) {
if (MAX < DATA[K]) {
LOC = K;
MAX = DATA[K];
}
}
return LOC;
}
int main() {
int DATA[] = {5, 10, 3, 8, 15};
int N = 5;
int LOC = findLargestElementIndex(DATA, N);
int MAX = DATA[LOC];
cout << "Largest element is " << MAX << " at index " << LOC << endl;
return 0;
}

// Find First and Second Largest


#include <iostream.h>
#include <conio.h>
void findFirstAndSecondLargest(int DATA[], int N, int& FIRST, int& SECOND, int&
LOC1, int& LOC2) {
FIRST = DATA[0];
SECOND = DATA[1];
LOC1 = 0;
LOC2 = 1;
if (FIRST < SECOND) {
int temp = FIRST;
FIRST = SECOND;
SECOND = temp;
temp = LOC1;
LOC1 = LOC2;
LOC2 = temp;
}
for (int K = 2; K < N; K++) {
if (FIRST < DATA[K]) {
SECOND = FIRST;
FIRST = DATA[K];
LOC2 = LOC1;
LOC1 = K;
}
else if (SECOND < DATA[K]) {
SECOND = DATA[K];
LOC2 = K;
}
}
}
int main() {
int DATA[] = {15, 10, 3, 8, 5};
int N = 5;
int FIRST, SECOND, LOC1, LOC2;
findFirstAndSecondLargest(DATA, N, FIRST, SECOND, LOC1, LOC2);
cout << "First largest element is " << FIRST << " at index " << LOC1 << endl;
cout << "Second largest element is " << SECOND << " at index " << LOC2 << endl;
return 0;
}

// Linear search
#include <iostream.h>
#include <conio.h>
int linearSearch(int DATA[], int N, int ITEM) {
int LOC = 0;
int K = 1;
while (LOC == 0 && K <= N) {
if (ITEM == DATA[K - 1]) {
LOC = K;
}
K++;
}
return LOC;
}
int main() {
int DATA[] = {5, 10, 3, 8, 15};
int N = 5;
int ITEM = 8;
int LOC = linearSearch(DATA, N, ITEM);
if (LOC == 0) {
cout << "ITEM is not in the array DATA." << endl;
} else {
cout << "LOC is the location of ITEM: " << LOC << endl;
}
return 0;
}

// Inserting an Element into an Array


#include <iostream.h>
#include <conio.h>
void insertElement(int LA[], int& N, int K, int ITEM) {
int J = N;
while (J >= K) {
LA[J + 1] = LA[J];
J--;
}
LA[K] = ITEM;
N++;
}
int main() {
int LA[10] = {2, 4, 6, 8, 10};
int N = 5;
int K = 3;
int ITEM = 7;
insertElement(LA, N, K, ITEM);
cout << "Updated array: ";
for (int i = 0; i < N; ++i) {
cout << LA[i] << " ";
}
cout << std::endl;
return 0;
}

// Deleting An Element From an Array


#include <iostream.h>
#include <conio.h>
void deleteElement(int LA[], int& N, int K) {
for (int J = K; J < N - 1; J++) {
LA[J] = LA[J + 1];
}
N--;
}
int main() {
int LA[10] = {2, 4, 6, 8, 10};
int N = 5;
int K = 2;
deleteElement(LA, N, K);
cout << "Updated array: ";
for (int i = 0; i < N; i++) {
cout << LA[i] << " ";
}
cout << endl;

return 0;
}

// Traversing an array
#include <iostream.h>
#include <conio.h>
void printArray(int LA[], int LB, int UB) {
int K = LB;
while (K < UB) {
cout << LA[K] << " ";
K++;
}
}
int main() {
int LA[] = {3, 6, 2, 8, 4};
int LB = 0;
int UB = 4;
printArray(LA, LB, UB);
return 0;
}

// Bubble Sort
#include <iostream.h>
#include <conio.h>
void bubbleSort(int DATA[], int N) {
for (int K = 0; K < N; K++) {
int PTR = 0;
while (PTR < N - K - 1) {
if (DATA[PTR] > DATA[PTR + 1]) {
int temp = DATA[PTR];
DATA[PTR] = DATA[PTR + 1];
DATA[PTR + 1] = temp;
}
PTR++;
}
}
}
int main() {
int DATA[] = {5, 3, 8, 2, 7};
int N = 5;
bubbleSort(DATA, N);
cout << "Sorted array: ";
for (int i = 0; i < N; i++) {
cout << DATA[i] << " ";
}
cout << endl;
return 0;
}

// Binnay Searcch of an array


#include <iostream.h>
#include <conio.h>
int binarySearch(int DATA[], int LB, int UB, int ITEM) {
int BEG = LB;
int END = UB;
int MID = (BEG + END) / 2;
while (BEG <= END && DATA[MID] != ITEM) {
if (ITEM < DATA[MID]) {
END = MID - 1;
} else {
BEG = MID + 1;
}
MID = (BEG + END) / 2;
}
if (DATA[MID] == ITEM) {
return MID;
} else {
return -1;
}
}
int main() {
int DATA[] = {2, 3, 5, 7, 8, 10};
int LB = 0;
int UB = 5;
int ITEM = 7;
int LOC = binarySearch(DATA, LB, UB, ITEM);
if (LOC != -1) {
cout << "Element found at index " << LOC << endl;
} else {
cout << "Element not found." << endl;
}
return 0;
}

// Multiplying Two Arrays


#include <iostream.h>
#include <conio.h>
const int M = 2;
const int N = 3;
const int P = 2;
void multiplyMatrices(int A[][P], int B[][N], int C[][N]) {
for (int I = 0; I < M; I++) {
for (int J = 0; J < N; J++) {
C[I][J] = 0;
for (int K = 0; K < P; K++) {
C[I][J] += A[I][K] * B[K][J];
}
}
}
}
void printMatrix(int C[][N]) {
cout << "Result matrix C:" << endl;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
}
int main() {
int A[M][P] = {{1, 2}, {3, 4}};
int B[P][N] = {{5, 6, 7}, {8, 9, 10}};
int C[M][N];
multiplyMatrices(A, B, C);
printMatrix(C);
return 0;
}

You might also like