RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1863 | Replies: 13
Reply
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help C++ question on alternating sums

  #1  
Jul 23rd, 2005
Question:
I have to do a program on alternating sums using vectors. I am not sure how to start this program. I thought about using an array and every even index add the value and every odd index subtract the value. Does that sound right? Any ideas?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,330
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 44
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: C++ question on alternating sums

  #2  
Jul 23rd, 2005
The only good way to do this is to figure it out yourself. You've thought about using an "array," but didn't you just mention "vectors"? You might have correctly described your algorithm; now you just need to implement it.
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: C++ question on alternating sums

  #3  
Jul 23rd, 2005
I have what sounds like the same assignment and yes that will work. If this is the same assignment, use a vector because the assignment specifies this type. Then, test for evens and odds. The first index is zero which is even so add evens and subtract odds.
Reply With Quote  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: C++ question on alternating sums

  #4  
Jul 23rd, 2005
wat exactly do u mean by alternating sums using vectors??
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,330
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 44
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: C++ question on alternating sums

  #5  
Jul 23rd, 2005
An alternating series is one in which the sign flip-flops. For example,

4 + 4/3 + 4/5 + 4/7 + 4/9 + 4/11 + 4/13

is not an alternating series. But the following is:

4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13

I think the original poster wants to take a vector containing

[ a b c d e f g h ... ]

and compute

a - b + c - d + e - f + g - h + ...
Reply With Quote  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: C++ question on alternating sums

  #6  
Jul 23rd, 2005
then wat he is doing sounds correct
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help Re: C++ question on alternating sums

  #7  
Jul 23rd, 2005
Below is my source code but I am getting errors stating invalid conversions. Any ideas?





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

double alternating_sum(vector<double>);

int main()
{


double num_values = 0;

while (true)
{
cout << " Enter a real number (CTRL-D to quit): ";
cin >> num_values;
}
vector<double> num_sum(num_values);
double altsum_of_vector = 0;
altsum_of_vector = alternating_sum(num_sum);

cout << " The alternating sum of the vector is " << altsum_of_vector << endl;

return 0;
}


double alternating_sum(vector<double> v)
{
double sum = 0;
if (v.size() == 0)
{
return 0;
}

else
{
for (int i = 0; i < v.size(); i++)
{
if ( (i % 2) == 0)
{
sum += v[i];
}
else
{
sum -= v[i];
}
}
}
return sum;
}
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ question on alternating sums

  #8  
Jul 23rd, 2005
>Below is my source code but I am getting errors stating invalid conversions.
Post the errors.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help Re: C++ question on alternating sums

  #9  
Jul 23rd, 2005
Below is the error message I am receiving.


p9-2.cpp: In function `int main()':
p9-2.cpp:31: warning: passing `double' for argument 1 of `std::vector<_Tp,
_Alloc>::vector(unsigned int) [with _Tp = double, _Alloc =
std::allocator<double>]'
[hs001@cs hs001]$
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: C++ question on alternating sums

  #10  
Jul 23rd, 2005
That's a warning, not an error. It's telling you that the constructor expects an unsigned int, but you're passing a double, and there may be a loss of data in the conversion.
I'm here to prove you wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:58 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC