Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a function to find the smallest value in an array. The prototype declar

ID: 3625211 • Letter: 1

Question

1) Write a function to find the smallest value in an array. The prototype declaration for the function is:

int * findMin (int * arr, int size);

See that this function takes an integer pointer, pointing to the array, and the size of the array. It returns a pointer to the smallest number in the array.

int main() {

const int size = 10;
int arr[size];

cout << "Enter the array numbers :" << endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}

int * res = findMin (arr, size);
cout << "The value returned by findMin = " << res << " and the value pointed to is " << *res << endl;

system("pause");
return 0;
}

2) We will write a function that re-arranges values in 3 variables. Suppose variables
are x, y, z; after a call to the function, x will have the smallest value of the three
variables, y will have the middle value, and z will have the largest value. Note: The
re-arrangement must happen within the function.

Hint: You may want to define a function with prototype:
void arrange(int * p1, int * p2, int * p3);

---------------------
Sample Output:

Enter 3 numbers : 7 3 5
The 3 numbers after arranging are :
3
5
7

Explanation / Answer

#include<iostream>
using namespace std;
int * findMin (int * arr, int size)
{
    int *min = arr;
    for(int i=1; i<size; i++)
    if(*(arr+i)<*min)
    min = (arr+i);
   
    return min;
   
}
int main() {

const int size = 10;
int arr[size];

cout << "Enter the array numbers :" << endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}

int * res = findMin (arr, size);
cout << "The value returned by findMin = " << res << " and the value pointed to is " << *res << endl;

system("pause");
return 0;
}