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

Array

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

Array

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

#include <iostream>

using namespace std;

void initArray(int arr[], int size);


void displayArray(int arr[], int size);
void minimum(int arr[], int size);
void find(int arr[], int size);
int maximum(int arr[], int size);
void sorting(int arr[], int size);
void bubbleSort(int arr[], int size);
void binarysearch(int arr[], int size, int element);
void insertionsort(int arr[], int size);

int main() {
int arr[5];
int opt;
int val;

do {
cout << "Press 1 for Initialization\n";
cout << "Press 2 for Display\n";
cout << "Press 3 for Minimum value\n";
cout << "Press 4 for Find a value\n";
cout << "Press 5 for Find Maximum value\n";
cout << "Press 6 for Sorting the Array\n";
cout << "Press 7 for Binary Search in Array\n";
cout << "Press 8 for Bubble Sort in Array\n";
cout << "Press 9 for Insertion Sort in Array\n";
cout << "Press 0 for Exit\n";

cin >> opt;

switch (opt) {
case 1:
initArray(arr, 5);
break;
case 2:
displayArray(arr, 5);
break;
case 3:
minimum(arr, 5);
break;
case 4:
find(arr, 5);
break;
case 5: {
int ind = maximum(arr, 5);
cout << "Index number is: " << ind << endl;
break;
}
case 6:
sorting(arr, 5);
break;
case 7:
cout << "Value to be searched: ";
cin >> val;
binarysearch(arr, 5, val);
break;
case 8:
bubbleSort(arr, 5);
displayArray(arr, 5); // Show sorted array
break;
case 9:
insertionsort(arr, 5);
displayArray(arr, 5); // Show sorted array
break;
case 0:
return 0;
default:
cout << "Invalid Input!" << endl;
break;
}
} while (true);

return 0;
}

void initArray(int arr[], int size) {


cout << "Enter " << size << " values of Array:\n";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
}

void displayArray(int arr[], int size) {


cout << "Displaying the values of Array:\n";
for (int i = 0; i < size; i++) {
cout << "index: " << i << " = " << arr[i] << endl;
}
}

void minimum(int arr[], int size) {


int min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
cout << "Minimum value is: " << min << endl;
}

void find(int arr[], int size) {


int n;
cout << "Enter the number to find: ";
cin >> n;

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


if (n == arr[i]) {
cout << n << " is available at index " << i << endl;
return;
}
}
cout << n << " is not available!\n";
}

int maximum(int arr[], int size) {


int max = arr[0];
int max_ind = 0;
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
max_ind = i;
}
}
cout << "Maximum is: " << max << endl;
return max_ind;
}

void sorting(int arr[], int size) {


for (int e = size - 1; e > 0; e--) {
int ind = maximum(arr, e + 1);
int temp = arr[e];
arr[e] = arr[ind];
arr[ind] = temp;
}
}

void binarysearch(int arr[], int size, int element) {


int b = 0, e = size - 1, mid;

while (b <= e) {
mid = (b + e) / 2;
if (element == arr[mid]) {
cout << "Found at index: " << mid << endl;
return;
} else if (element > arr[mid]) {
b = mid + 1;
} else {
e = mid - 1;
}
}
cout << "Not Found!\n";
}

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


for (int e = size - 1; e > 0; e--) {
for (int i = 0; i < e; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
cout << "Array sorted using Bubble Sort:\n";
}

void insertionsort(int arr[], int size) {


for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1; // Correct initialization of j
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
cout << "Array sorted using Insertion Sort:\n";
}

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