Thread: would anybody be kind enough to help?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    would anybody be kind enough to help?

    I am stuck on a homework problem. What I am suppose to do is write a program that calculates the square of all the intergers from 1 till the number a user enters. So my program should calculate something like 1^2 + 2^2 + ...... n^2. I know I am suppose to use the for loop or the while loop, but I am lost. This is what I came up with so far:

    using while loop

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int x;
    	cout << "Please enter a number: ";
    	cin >> x;
    	int i = 1;
    	while (i < x)
    	{
    		x++;
    		int sum=i+x;
    		cout << "Result: " << pow(sum,2) << endl;
    	}
    	return 0;
    }
    using for loop
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int x;
    	cout << "Please enter a number: ";
    	cin >> x;
    	for (int i = 1; i< x+1; x++)
    		cout << "The sum of the square of all intergers from 
    		1 to the number you have entered is: " 
    		<< pow(x=i+x,2) << endl;
    	return 0;
    }

    Any help and guidance would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    13
    try:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
    	
    	int x=0, sum = 0;
    	cout << "Please enter a number: ";
    	cin >> x;
    	
    
    	while (x){
    		sum+=pow(x,2);
    		--x;
    	}
    
    	
    	cout << "Result: " << sum <<endl;
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I commented your old code so you could see the changes. This prints the calculation each time thru the loop. Once debugged, you may want to move the cout after the loop.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int x;
    	cout << "Please enter a number: ";
    	cin >> x;
    	int sum = 0;
    	int i = 1;
    	//while (i < x)
    	while (i <= x)
    	{
    		//x++;
    		//int sum=i+x;
    		sum += i * i;
    		cout << "Result: " << sum << endl;
    		//cout << "Result: " << pow(sum,2) << endl;
    		++i;
    	}
    	return 0;
    }
    You could also use pow() instead of i*i.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    here's a really short soultion
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int x;
      cout<<"Enter Number"<<endl;
      cin>>x;
      int i=1;
      while (x >= i)
    	{
    		cout << "Result: " << i*i << endl;
    		i++;
    	}
    	return 0;
    }
    Woop?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >here's a really short soultion

    Well it is short for sure.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    13
    yeah, short code is nice!

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Whoops i'm dumb I totally read the question wrong
    Woop?

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    wow, thank you all for your help! i am glad i learned something from this board.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Your welcome even though im dumb and come back anytime you have some questions i'll try not to miss read your questions agian
    Woop?

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    8
    I rewrote your for loop for a few reasons. First I would use the FOR loop for this problem over the while loop, since you need to declare, test and count. Second if this is for school, most teachers will know if you go beyond your ability and while what everyone else posted is correct most new programmers don't think that way. Ex: while(x).

    Next, you made some bad logical errors in this one. Namely, having the cout in the loop and taking x^2 instead of i^2 the wrong number. I know for loops are the toughest of the 3 main loops but if you take a little time to learn them it's hard to not use them.

    Remember the standard for FOR loops is i is the counter variable and increases to a number in this case x. You have to use a sum variable to hold the ongoing result of the for loop unless you want each i square to be output. The FOR loop is only one statement here so the braces are optional. The pow() statement is of the format pow(number, power); No equals or the like are needed. I think that pretty much covers it.

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    int x;
    int sum = 0;
    cout << "Please enter a number: ";
    cin >> x;
    for (int i = 1; i <= x; i++)
    sum += pow(i,2);

    cout << "The sum of the square of all integers from
    1 to the number you have entered is: "
    << sum << endl;
    return 0;
    }
    Last edited by raum; 07-07-2004 at 05:33 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, in this case there is an even simpler solution:
    Given an integer n, n > 0
    1^2 + 2^2 + 3^2 + ... + n^2 = n(n+1)(2n+1)/6

    By implementing this formula, you would avoid having to use a loop.

    On the other hand, this is probably what you would use in a 'real' program, not a school assignment where the teacher wants you would learn how to use loops better by writing a program.

    EDIT:
    in this case, do not use pow() as it would be overkill.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What kind of job...
    By Discolemonade in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-15-2005, 08:23 PM
  2. what kind compiler should I use
    By ilove in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2005, 05:07 PM
  3. Getting three of a kind
    By Kons in forum C Programming
    Replies: 8
    Last Post: 08-19-2003, 12:44 PM
  4. What kind of code do u want a front-end designer generating?
    By MovingFulcrum in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-15-2001, 06:45 PM
  5. Physics: Kind of like a cannonball...
    By Cheeze-It in forum Game Programming
    Replies: 11
    Last Post: 09-12-2001, 01:53 PM