Question

1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take three arguments, int array, int as the length of the array, and int *max. (b) Find the maximum value and the minimum value in the array. (c) The pointer max points to the maximum value. (d) Return the pointer that points to the minimum value max)( findMinAndMax(int al l, int length, int int maxValue a[0], minValue al0 int int maxldx 0, minldx 0; for (int idx -1; idx< length; idx+)( if (aidx]> maxValue) maxvalue-alidxj maxldx idx; if (a[idx< minValue) minValue alidx] minldx - idx; 2. (60 pts) Consider the above function. Declare an integer array called myArray of length 5 Initialize myArray to 4, 1,-5, 3, 5, in sequence. Declare two pointers ptrMin and ptrMax, both pointing to integer. Call the above function findMinAndMax so that the pointer ptrMin points the the minimum value in the array and the pointer ptrMax points to the maximum value in the array
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

The min and max is printed with the help of driver function, please have a look.

code:

#include<iostream> //i/o library

using namespace std;

int *findMinAndMax(int a[], int length, int *max)

{

int maxValue= a[0], minValue= a[0];

int maxIdx=0, minIdx=0;

for (int idx = 1; idx<= length; idx++)

{

if (a[idx] > maxValue) {

maxValue= a[idx];  

maxIdx = idx;

}

if (a[idx]<minValue) {

minValue = a[idx];

minIdx= idx;

}

}

*max= a[maxIdx];

return &minValue;

}

int main(){

int myArray[]= {4, 1, -5, 3, 5};

int max, *min;

min= findMinAndMax(myArray, 5, &max);

cout<<*min<<" "<<max;

return 0; //This is to make sure that the program terminates

}

Output:

FStudy\ Chegg 2k181codes\C-C++findMinMax.exe Process exited after 0.06464 seconds with return value Press any key to continue

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major...

    (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major differences of * and & in the above example. (35 pts) Do the following in a program segment step by step. The next step uses the results of the previous step and earlier, using a single statement only and none for manual work. Declare an array called a of float of size 10 and initialize it to 2.5, -3.3, 5.9, 1.0, 3.5, 4.3, -7.3,...

  • Create a C program that: Within main, declares a linear array consisting of 10 double values....

    Create a C program that: Within main, declares a linear array consisting of 10 double values. You can assign values to the array when it is declared or fill them items manually using scanf () - your choice. Also, declare three doubles: min, max, and average. Then from within main, a function is called. The function should take a pointer to the array declared above and then finds the maximum value, the minimum value, and calculates the average of the...

  • please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI...

    please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI SuuenLD, JUN, UML function find MultiMax that finds both the value ane handle ties, the find MultiMax function she are and assumed to be initialized alues should be as the returned values show 4. (30 pts) Use the space provided to write the function find Multima location of the maximum element of an array. To handle ties, the store the index locations in an...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT