Saturday, May 22, 2010

I am trying to write in program in C++, summation notation, i have n on top, i=1 in the bottom and 1^3 on the?

side. this is how i meet so far, i think my main problem is i done know the symbol to the cube.





#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


using namespace std;


int main ()


{


int i, n, sum1 = 0;





for (i = 1; i %26lt;= n; i++) {


sum1 += i*i*i;


cout %26lt;%26lt; sum1 %26lt;%26lt; endl;


}


system ("PAUSE");





return 0;


}





can anyone please help me, i dont want u to do it for me, u can tell me if i am oing right or wrong. i have to print the sum of the n cubes in four different ways and this is the first and i am stuck. when i removed the i*i*i, i am getting 1, 3, and so forth. please help me.

I am trying to write in program in C++, summation notation, i have n on top, i=1 in the bottom and 1^3 on the?
There is no symbol for powers in C++. Writing i*i*i is correct. The symbol ^ in C++means Exclusive Or. Very different than math notation.





The biggest problem is you don't give any value for n, either from yourself or the user. From what you want the program to do, you should ask the user for 'n'.





Before the for loop, put something like this:


cout %26lt;%26lt; "Please enter value for n.\n";


cin %26gt;%26gt; n;





AS the other poster mentioned, you should move the line


cout %26lt;%26lt; sum1 %26lt;%26lt; endl;


after the loop, otherwise it will output for each iteration of the loop,
Reply:Do you need to print the cube of a number?


Like :





1 = 1*1*1 = 1


2 = 2*2*2 = 8


3 = 3*3*3 = 27


etc.





Then you shouldn't increment the sum1 variable using +=, you should simply assign it. Because your sum1 += i*i*i adds all the cube.





Also, I don't think there are a symbol for cube...





You should send the output this program makes when you run it if it is possible.
Reply:That should work, but why is your cout in the loop?





Look at the pow() function.


No comments:

Post a Comment