void swap (int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void findMinAndMax(int arr[], int size, int *min, int *max) {
*min = arr[0];
*max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < *min) {
*min = arr[i];
}
if (arr[i] > *max) {
*max = arr[i];
}
}
}
int main() {
int array[100];
int i,n;
>>n;
cinint min = 0, max = 0;
for(i=0; i<n; i++) {
>> array[i];
cin }
(array, n, &min, &max);
findMinAndMax
<< "Minimum value: " << min << endl;
cout << "Maximum value: " << max << endl;
cout return 0;
}
Write functions for sorting array by using the following method for sorting: Bubble sort.
Write functions for reading and printing elements of an array, and write main program to test the sort function.
#include <iostream>
using namespace std;
//Reference vs. without reference
void swap(int& a, int& b) {
int temp = a;
= b;
a = temp;
b }
void inputArray(int array[], int size)
{
for (int i = 0; i < size; i++) {
<< "Enter element " << i + 1 << ": ";
cout >> array[i];
cin }
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; i++) {
<< array[i] << " ";
cout }
<< endl;
cout }
void bubbleSort(int array[], int size)
{
bool swapped = true;
while (swapped) {
= false;
swapped for (int i = 0; i < size - 1; i++) {
if (array[i] > array[i + 1]) {
(array[i], array[i + 1]);
swap= true;
swapped }
}
}
}
int main()
{
int size;
>> size;
cin
int array[size];
(array, size);
inputArray//Original array
(array, size);
printArray
(array, size);
bubbleSort//Bubble sort
(array, size);
printArray
return 0;
}
Write the following functions for searching an array:
-Linear search
-Binary search
Then write a program that fill an array with numbers from 1 to 1000000, and than generates random number in this range and finds its index by calling the two functions for searching.
For the functions count and compare the number of needed iterations to find the number.
#include <iostream>
#include <cstdlib> // For rand()
#include <ctime> // For time()
using namespace std;
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int binarySearchIterative(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
= m + 1;
l
// If x is smaller, ignore right half
else
= m - 1;
r }
// If we reach here, then element was not present
return -1;
}
int binarySearch(int arr[], int l, int r, int key)
{
if (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == key) {
return mid;
} else
if (arr[mid] < key) {
return binarySearch(arr, mid + 1, r, key);
} else {
return binarySearch(arr, l, mid - 1, key);
}
}
return
-1;
}
int main()
{
// Initialize the array with numbers from 1 to 1,000,000
int arr[1000001];
for (int i = 0; i < 1000001; i++) {
[i] = i + 1;
arr}
// Generate a random number within the range
(time(NULL)); // Seed the random number generator
srandint randomNum = 1 + rand() % 1000000; // Generate a random number between 1 and 1,000,000
// Perform linear search
int linearSearchIndex = linearSearch(arr, 1000001, randomNum);
<< "Linear search: " << linearSearchIndex << endl;
cout
// Perform binary search
int binarySearchIndex = binarySearch(arr, 0, 1000000, randomNum);
<< "Binary search: " << binarySearchIndex << endl;
cout
return 0;
}
Write a program that will transofrm the input array a into output array b in the following way:
bi = ai + an − 1 − i
The input array: 1 2 3 5 7
should be transformed into: 8 7 6 7 8
Explanation: Since the input array a is of size n, that b will be the same size. The transformation is done by pairwise addition and replacement of the elements. For each pair of elements a[i] and a[n-1-i], where i is from [0 to n/2-1], the neighborhood sum is stored in b[i], while a[i] is replaced with [n-1-i]. If n is odd, the middle element a[n/2] is multiplied by 2.
#include <iostream>
using namespace std;
#define MAX 100
void transform(int arr[], int n) {
for (int i = 0, j = n - 1; i < j; i++, j--) {
[i] += arr[j];
arr[j] = arr[i];
arr}
if (n % 2) {
[n / 2] *= 2;
arr}
}
void transformRecursive(int *arr, int start, int n) {
if (start >= n/2) {
if (n % 2 == 1) {
*(arr + n/2) *= 2;
}
return;
}
int end = n - start - 1;
int tmp = *(arr + start) + *(arr + end);
*(arr + start) = tmp;
*(arr + end) = tmp;
(arr, start + 1, n);
transform}
int main() {
int n;
int arr[MAX];
<< "Enter the size of the array: ";
cout >> n;
cin
<< "Enter the elements of the array: ";
cout for (int i = 0; i < n; i++) {
>> arr[i];
cin }
//transform(arr, n);
(arr, 0, n);
transformRecursive
<< "Transformed array: ";
cout for (int i = 0; i < n; i++) {
<< arr[i] << " ";
cout }
<< endl;
cout
return 0;
}