Question

c++ questions Write a function that returns a pointer to the maximum value of an array...

c++ questions

Write a function that returns a pointer to the maximum value of an array of ints: int* ptrToMaximum(int* array, int* end); You don’t have to worry about what happens if end points beyond end(array). Maybe it is nice to allow end to point to before end(array). In the case that (array == end), you should return nullptr. Running the following main function should result in 8 7 being printed to the console. int main() { int a[] = {1,2,3,4,5,6,7,8,7,6,5,4,5,6,5,4,3,4,5,4,3,2,1}; int* maximum = ptrToMaximum(a,end(a)); cout << *maximum << " " << *(maximum+1) << endl; return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int *ptrToMaximum(int *array, int *end) {
    if (array == end)
        return nullptr;
    int *max = array;
    while (array < end) {
        if (*array > *max)
            max = array;
        array++;
    }
    return max;
}

int main() {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 1};
    int *maximum = ptrToMaximum(a, end(a));
    cout << *maximum << " " << *(maximum + 1) << endl;
    return 0;
}
Add a comment
Answer #2
#include <iostream>

using namespace std;

int *ptrToMaximum(int *array, int *end) {
    if (array == end)
        return nullptr;
    int *max = array;
    while (array < end) {
        if (*array > *max)
            max = array;
        array++;
    }
    return max;
}

int main() {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6, 5, 4, 3, 4, 5, 4, 3, 2, 1};
    int *maximum = ptrToMaximum(a, end(a));
    cout << *maximum << " " << *(maximum + 1) << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
c++ questions Write a function that returns a pointer to the maximum value of an array...
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
  • Exercise P7.6. Write a function that returns a pointer to the maximum value of an array...

    Exercise P7.6. Write a function that returns a pointer to the maximum value of an array of floating-point data: double* maximum(double al], int a size) If a size is 0, return NULL.

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • C++ Programme Write a function that receives, as arguments, a pointer to a double array, as...

    C++ Programme Write a function that receives, as arguments, a pointer to a double array, as well as the number of elements: in the array(int). The function must use pointer operations and calculate the standard deviation of the elements in the array. The function returns the standard deviation to the calling statementſ Use the function in main( to display its operation. | Example: array = (11.2, 2.4, 3.13, 16.4, 5.8, 9.22, 4.9, 10.5, 6.5, 2.99) std Dev(array) = 4.249367 ZWI...

  • a. Write a function named findmax() that finds and displays the maximum values in a two...

    a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100. b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value. I have this so far, and but it's only finding the...

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