Public Group

Trying to compute a real number cubed

This topic is 4082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi everyone I'm doing an exercise to write a program that asks the user to input a real number n, compute n cubed and output the result. I'm using the following source code:

// Prompt user to enter a real number, compute the cube of it and output the result

#include <iostream>

using namespace std;

int main ()
{
float n = 0.0f;

cout << "Enter a real number n: ";
cin >> n;

float Cubed = n ^ 3.0f;

// Perform n cubed

cout << n << " ^ " << 3 << "=" << Cubed;
cout << endl;
}


I get the following in th build output box:

------ Build started: Project: Gi project 18, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\gi project 18\gi project 18\main.cpp(14) : error C2296: '^' : illegal, left operand has type 'float'
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\gi project 18\gi project 18\main.cpp(14) : error C2297: '^' : illegal, right operand has type 'float'
Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\Gi project 18\Gi project 18\Debug\BuildLog.htm"
Gi project 18 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


How do I compute cubed? I know I could do n * n * n but obviously this would problematic for very high powers. Thanks, Chris.

Share on other sites
^ is bitwise xor. It has nothing to do with exponentiation in C++. To do powers you should use the pow() function (in the <cmath> header).

Share on other sites

My source code is now the following:

// Prompt user to enter a real number, compute the cube of it and output the result#include <iostream>#include <math.h>using namespace std;int main (){	float n = 0.0f;	cout << "Enter a real number n: ";	cin >> n;	float Cubed = pow(n,3);	// Perform n cubed	cout << n << " pow(n,3) " << "=" << Cubed;	cout << endl;}

I now get the output:

Enter a real number n: 7.127.12 pow(n,3) =360.944Press any key to continue . . .

I know this is trivial but the 2nd line of the given output in my exercise reads 7.12^3= 360.944.
Is there another way to put this in c++ to give the output as ammended above?

Thanks,
Chris.

Share on other sites
Quote:
 Original post by chockydavid1983I know this is trivial but the 2nd line of the given output in my exercise reads 7.12^3= 360.944.Is there another way to put this in c++ to give the output as ammended above?Thanks,Chris.

You want it to output 7.12^3 = 360.944 instead of 7.12 pow(n,3) =360.944?

You have:
cout << n << " pow(n,3) " << "=" << Cubed;

You want to have:
cout << n << "^3" << " = " << Cubed;

Share on other sites
Quote:
 Original post by chockydavid1983Hi, thank you for your reply.My source code is now the following:*** Source Snippet Removed ***I now get the output:*** Source Snippet Removed ***I know this is trivial but the 2nd line of the given output in my exercise reads 7.12^3= 360.944.Is there another way to put this in c++ to give the output as ammended above?Thanks,Chris.

Hint: the process of calculating the value and the process of displaying it have nothing to do with each other (as it should be). You currently output raw text with values mixed in; think about what raw text you want to output, keeping in mind that it doesn't have to bear any resemblance to the code that calculates the values.

Share on other sites
Quote:
 Original post by Omid Ghavamicout << n << "^3" << " = " << Cubed;

Or
cout << n << "^3 = " << Cubed;

Is that same thing a little shorter [smile]. But thats nitpicky.

At chockydavid1983,
In general dont use pow for such a trivial operation, the reason being is that pow is intended to calculate the power for arbitrary or large indices
So like pow(n, 313) for example, or if the user had to enter the power then you wouldnt know what its going to be before-hand.

If you just want to cube a number its more efficient to just multiply it by itself twice:

float Cubed = n * n * n;

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 10
• 9
• 14
• 10
• 25
• Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• Who's Online (See full list)

There are no registered users currently online

×