Thread: alternating sum

  1. #16
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well at least with (i*i) you wont get function overheads, and its less work to type.

  2. #17
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Hi all,
    Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
    Here's the code and the output though.. I still got warnings but it compiled anyway..

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	double i = 0;
    	int sum = 0, n = 0;
    	int elements = 0;
    	cout << "Enter a value for n where n >= 1: ";
    	cin >> n;
    
    	for (i = 1; i < n; ++i)
    	{
    		elements = (pow(-1.0,i-1))*(i * i);
    		sum += elements;
    		cout << elements << " + ";
    	}
    	elements = (pow(-1.0,i-1))*(i * i);
    	sum += elements;
    	cout << elements << " = " << sum << endl;
    }

    Code:
    Enter a value for n where n >= 1: 4
    1 + -4 + 9 + -16 = -10
    Press any key to continue...

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by alyeska View Post
    Hi all,
    Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
    Well, that's pretty much it. The (-1)^i-1 goes back and forth between 1 and -1. So you start it off as 1; then after you use it, you set sign=-sign; and now it's -1. Then after you use it, you set sign=-sign; and now it's 1 again.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    I would say that depends.
    In Microsoft's implementation, at least, when you do power to an integer, it does a simple multiplication loop. Though if you do power against a double, it will enter complicated assembly code with lots of instructions to get the result.
    Oh, I hadn't noticed that until you said today.
    I guess it's only approx 10-20 times slower on recent versions of MSVC then. It's also O(log n) instead of O(1), as it takes longer the bigger the power is. Still no reason to use it when there are simpler ways.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by alyeska View Post
    Hi all,
    Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
    Well in your case you change sign to elements, but you basically write the line of code exactly as given.

    The warnings you are getting are about conversion from double to int. The above would eliminate that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Here is what I came up with. There is definitely a better way to write this but I will go for it this time. Thank you everyone, especially tabstop and iMalc.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	double i = 0;
    	int sum = 0, n = 0;
    	int elements = 0;
    	cout << "Enter a value for n where n >= 1: ";
    	cin >> n;
    
    	for (i = 1; i < n; ++i)
    	{
    		elements = (pow(-1.0,i-1))*(i * i);
    		sum += elements;
    		if (elements % 2 != 0)
    			cout << " + " << elements << " ";
    		else
    			cout << elements;
    	}
    	elements = (pow(-1.0,i-1))*(i * i);
    	sum += elements;
    	if (elements % 2 != 0)
    		cout << " + " << elements << " = " << sum << endl;
    	else
    		cout << elements << " = " << sum << endl;
    }
    Code:
    Enter a value for n where n >= 1: 4
     + 1 -4 + 9 -16 = -10
    Press any key to continue...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM