Friday, July 31, 2009

C++ Programming Question. How do you calculate Squares and Cubes?!?

I am trying to write a program for my Intro to C++ Course to calculate Squares and Cubes but I am unsuccessful. The printout is supposed to be aligned in a chart, 3 columns, with rows 1-10. Below is part of the program, but even this will not compile properly:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;





using namespace std;





int main ()





{





cout %26lt;%26lt; setw(6) %26lt;%26lt; 1 %26lt;%26lt; setw(6) %26lt;%26lt; pow(1 , 2) %26lt;%26lt; setw(6) %26lt;%26lt; pow(1 , 3) %26lt;%26lt; endl;





return 0;





}








Any idea's where I am going wrong? Sorry if this is such a broad question. I have been working on this for hours and I am just having trouble getting the concepts of C++ down.





Thanks!!!

C++ Programming Question. How do you calculate Squares and Cubes?!?
logically u r goin right way
Reply:Here ya go, I'm assuming you are doing G++ compiler this compiles, and I think this is what you're trying to do.











Remember you can pass variables as parameters to functions.








#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;





using namespace std;





int main ()





{





int num = 1;





while(num %26lt;= 10){


cout %26lt;%26lt; setw(6) %26lt;%26lt; num %26lt;%26lt; setw(6) %26lt;%26lt; pow(num , 2) %26lt;%26lt; setw(6) %26lt;%26lt; pow(num , 3) %26lt;%26lt; endl;


++num;


};


return 0;





}


No comments:

Post a Comment