-3

computeCube function is correct, but the main function has a problem. Explain why it doesn't work and show how to fix it. Your fix must be to the main function only; you must not change computeCube.

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

    int main()
    {
        int* ptr;
        computeCube(5, ptr);
        cout << "Five cubed is " << *ptr << endl;
    }

Thanks

Edited by Nick Evan: Added code-tags

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by icygalz
0

And what was your answer? We're not here to do your homework for you.

p.s. Use code tags for code. It's not like Daniweb doesn't slap you in the face with instructions each time you post.
p.p.s. At least this question is solvable, unlike your other one.

Edited by Narue: n/a

0

i know that I need to change the main function to:

int main()
    {
        int ptr;
        computeCube(5, &ptr);
        cout << "Five cubed is " << ptr << endl;
    }

but I don't understand why i need to do that.. Is that because i cannot pass a pointer without initialized it first?

Edited by Nick Evan: Added code-tags (again)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.