c ++ help please
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.
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"
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"
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.
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.
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!}
Thanks
--{You fight like a dairy farmer!}
I can give you a small hint, think about recursion, and then your problem will solve itself.
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.
Which gives
(1/6)*n + (1/2)*n^2 + (1/3)*n^3 = n*(n+1)*(2n+1)/6
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement