Answer to Question #267535 in C++ for malika

Question #267535

The computeCube function is correct, but the main function has a problem. Explain why it may not work and show a way to fix it. Your fix must be to the main function only; you must not change computeCube in any way.

    void computeCube(int n, int* ncubed)
    {
        *ncubed = n * n * n;
    }

    int main()
    {
        int* ptr;
        computeCube(5, ptr);
        cout << "Five cubed is " << *ptr << endl;
    }
1
Expert's answer
2021-11-17T06:28:36-0500

ptr has not been initialized


#include <iostream>
using namespace std;


void computeCube(int n, int* ncubed)
{
    *ncubed = n * n * n;
}


int main()
{
    int* ptr; //The pointer has been declared but not initialized
    ptr = new int();
    computeCube(5, ptr);
    cout << "Five cubed is " << *ptr << endl;
    delete ptr;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS