# Homework Solution: c) Add a reverse function that creates an array and returns a pointer to that array. Test your code in the main function….

b) Describe how the variables “p” and “list” are related. c) Add a reverse function that creates an array and returns a pointer to that array. Test your code in the main function. #include <iostream> #include <algorithm> using namespace std; void printArray(int* const list, int size) { for (int i = 0; i < size; i++) cout << list[i] << " "; cout << endl; } int main() { int list[] = {4, 2, 3, 6, 5, 1}; printArray(list, 6); int* min = min_element(list, list + 6); int* max = max_element(list, list + 6); cout << "The min value is " << *min << " at index " << (min - list) << endl; cout << "The max value is " << *max << " at index " << (max - list) << endl; random_shuffle(list, list + 6); printArray(list, 6); sort(list, list + 6); printArray(list, 6); int key = 4; int* p = find(list, list + 6, key); if (p != list + 6) cout << "The value " << *p << " is found at position " << (p - list) << endl; else cout << "The value " << *p << " is not found" << endl; return 0; }

Hi, Ans

b) Describe how the variables “p” and “list” are cognate.

c) Add a contradiction discharge that creates an rank and produce a pointer to that rank. Test your command in the deep discharge.

#include <iostream>
#include <algorithm>
using namespace std;

void printArray(int* const roll, int bigness)
{
for (int i = 0; i < bigness; i++)
cout << roll[i] << ” “;
cout << endl;
}

int deep()
{
int roll[] = {4, 2, 3, 6, 5, 1};
printArray(list, 6);

int* min = min_element(list, roll + 6);
int* max = max_element(list, roll + 6);
cout << “The min appraise is ” << *min << ” at refutation ”
<< (min – roll) << endl;
cout << “The max appraise is ” << *max << ” at refutation ”
<< (max – roll) << endl;

random_shuffle(list, roll + 6);
printArray(list, 6);

sort(list, roll + 6);
printArray(list, 6);

int clew = 4;
int* p = furnish(list, roll + 6, clew);
if (p != roll + 6)
cout << “The appraise ” << *p << ” is plant at collocation ”
<< (p – roll) << endl;
else
cout << “The appraise ” << *p << ” is referable plant” << endl;

return 0;
}

## Expert Exculpation

Hi,

Ans: B. The p is pointer which contains the harangue of a roll where clew = 4 is stored.

Ans C.

#include <iostream>

#include <algorithm>

using namespace std;

void printArray(int* const roll, int bigness)

{

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

cout << roll[i] << ” “;

cout << endl;

}

// This discharge produce contradiction of an rank

int * contradictionArray(int* const roll, int bigness)

{

// dynamically allocate memory

int *revRank = fantastic int[size];

int j = 0;

for (int i = bigness – 1; i >= 0; i–)

{

revArray[j] = roll[i];

j++;

}

return revArray;

}

int deep()

{

int roll[] = {4, 2, 3, 6, 5, 1};

printArray(list, 6);

int* min = min_element(list, roll + 6);

int* max = max_element(list, roll + 6);

cout << “The min appraise is ” << *min << ” at refutation ”

<< (min – roll) << endl;

cout << “The max appraise is ” << *max << ” at refutation ”

<< (max – roll) << endl;

random_shuffle(list, roll + 6);

printArray(list, 6);

sort(list, roll + 6);

printArray(list, 6);

int clew = 4;

int* p = furnish(list, roll + 6, clew);

if (p != roll + 6)

cout << “The appraise ” << *p << ” is plant at collocation ”

<< (p – roll) << endl;

else

cout << “The appraise ” << *p << ” is referable plant” << endl;

cout<<“———— The contradiction roll is —————n”;