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

using namespace std

The document contains multiple C++ code snippets demonstrating different algorithms. The first snippet converts a sparse matrix into a compact representation, while the second and third snippets implement linear and binary search algorithms, respectively. Each code segment includes the main function and outputs the result of the search operation.

Uploaded by

grishkhanna13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

using namespace std

The document contains multiple C++ code snippets demonstrating different algorithms. The first snippet converts a sparse matrix into a compact representation, while the second and third snippets implement linear and binary search algorithms, respectively. Each code segment includes the main function and outputs the result of the search operation.

Uploaded by

grishkhanna13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

2nd

using namespace std;

int main() {

int sparseMatrix[4][5] = {

{0 , 0 , 3 , 0 , 4 },

{0 , 0 , 5 , 7 , 0 },

{0 , 0 , 0 , 0 , 0 },

{0 , 2 , 6 , 0 , 0 }

};

int size = 0;

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

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

if (sparseMatrix[i][j] != 0)

size++;

int compactMatrix[3][size];

int k = 0;

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

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

if (sparseMatrix[i][j] != 0)

compactMatrix[0][k] = i;

compactMatrix[1][k] = j;

compactMatrix[2][k] = sparseMatrix[i][j];

k++;

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

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

cout <<" "<< compactMatrix[i][j];

cout <<"\n";

return 0;
2nd

#include<iostream>

using namespace std;

int search(int arr[], int N, int x)

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

if (arr[i] == x)

return i;

return -1;

int main()

int arr[] = { 2, 3, 4, 10, 40 };

int x = 10;

int N = sizeof(arr) / sizeof(int);

int result = search(arr, N, x);

(result == -1)

? cout << "Element is not present in array"

: cout << "Element is present at index " << result;

return 0;

}
2nd

#include<iostream>

using namespace std;

int binarySearch(int arr[], int low, int high, int x)

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == x)

return mid;

if (arr[mid] < x)

low = mid + 1;

else

high = mid - 1;

return -1;

int main()

int arr[] = { 2, 3, 4, 10, 40 };

int x = 10;

int n = sizeof(arr) / sizeof(arr[0]);

int result = binarySearch(arr, 0, n - 1, x);

if(result == -1) cout << "Element is not present in array";

else cout << "Element is present at index " << result;

return 0;

}
2nd
2nd
2nd
2nd
2nd
2nd

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