Public Group

#### Archived

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

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

## 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 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 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 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 on other sites
I can give you a small hint, think about recursion, and then your problem will solve itself.

##### 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     01 1 1  1 *  b =   11 2 4  8    c     51 3 9 27    d    14

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

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

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009311
• ### Who's Online (See full list)

There are no registered users currently online

×