Jump to content
  • Advertisement
Sign in to follow this  
chockydavid1983

Trying to compute a real number cubed

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
^ 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 this post


Link to post
Share on other sites
Hi, thank you for your reply.

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.12
7.12 pow(n,3) =360.944
Press 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 this post


Link to post
Share on other sites
Quote:
Original post by chockydavid1983
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by chockydavid1983
Hi, 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 this post


Link to post
Share on other sites
Quote:
Original post by Omid Ghavami

cout << 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;


Edit: Ah...just the read the bottom of your top post [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!