Archived

This topic is now archived and is closed to further replies.

c ++ help please

This topic is 5147 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

i''m new at c++, the only female in the class and trying to "keep up with the guys". i am wanting to create a program that inputs an integer larger than 1 and calculates the sum of the squaes from 1 to that integer. The program would repeat this process for several input values and a negative input value signals the end of the data. Can someone help me in designing this the best way possible? Pretty please.

Share this post


Link to post
Share on other sites
At first I wasnt going to give a response to this because it is obviously a hw question but there are so few women in programming that I felt I had to do something to let this woman know that some of us are helpful. So instead of giving the answer I will provide some clues.

I''m not sure I understand the problem but I would use a for loop for computing the squares from 1 to whatever number was input and a while loop to repeat the program until a negative number is entered.

Hopefully I havent said too much already

::looks around shiftly::

Clippy:"OMG, A NUMBAR! Let me format it for you"
Unsuspecting Student: "Ahhhhh! Damn you paperclip you ruined my paper. A thousand deaths upon you!"

"Game Programming: Without programming you''ve just got game"

Share this post


Link to post
Share on other sites
While most of the posters here are against doing homework problems, they have a near pathological urge to point out when people are doing something wrong. So if you post something that you tried, I''m sure people will come out of the woodwork to tell you ways to do it better.

And because I''m one of those pathological cases, I have to point out that the sum of squares has a simple mathematical formula ( n*(n+1)*(2n+1)/6 ), so a for loop is overkill.

Share this post


Link to post
Share on other sites
After looking at the problem, I was trying to derivate that formula for the sum of squares. I was able to figure out the basic formula for sum of a list of consecutive n*(n+1)/2 but couldn''t quite do it for the sum of squares. Can you go over briefly how it could be derived?

Thanks





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
The fast way is to look at it as a simultaneous linear equation problem. Because it''s the sum of squares 1 to n^2, it''s a pretty good guess that it''s polynomial (specifically cubic). So assume that it''s (a + b * n + c * n^2 + d * n ^ 3). Then use known values for the first four entries, 0, 1, 2, and 3. You can solve that using simple matrix math.


1 0 0 0 a 0
1 1 1 1 * b = 1
1 2 4 8 c 5
1 3 9 27 d 14

Which gives

a 0
b = 1/6
c 1/2
d 1/3

(1/6)*n + (1/2)*n^2 + (1/3)*n^3 = n*(n+1)*(2n+1)/6

Share this post


Link to post
Share on other sites