470,591 Members | 2,155 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

vectors and arithmetic progression

Hi, I have been attempting a question for hours now and don't seem to be getting anywhere. I have to return a vector of <long> containing the arithmetic progression up to N terms, with a and d long arguments, so
a_n = a+n*d

I have created the function vector<long>arith_prog(long, long, int) like follows:

vector<long> arith_prog(long a, long d, int N)
long x = 0;
for( int i = 0; i < N; i++)
x = a + i*d;
x = arith_vec[i];
return arith_vec;

Can anyone tell me where I am going wrong?
Quick reply would be great!
Nov 19 '08 #1
5 3423
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. arith_vec.push_back;
  2. x = arith_vec[i];
You have to pass an argument to push_back. Maybe you meant to push x into the vector?
Expand|Select|Wrap|Line Numbers
  1. arith_vec.push_back(x);
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks.
Hope this helps.
Nov 19 '08 #2
Thanks, but no unfortunately that still doesn't make it work. I realised this mistake just after I had posted my original message. So can anyone help me as to whats wrong. I get loads of error messages when I try to compile it, which I have no idea what they mean.
Expand|Select|Wrap|Line Numbers
  1. vector<long> arith_prog(long a, long d, int N)
  2.     {
  3.     vector<long>arith_vec;
  4.     long x = 0;
  5.     for( int i = 0; i < N; i++)
  6.         {
  7.         x = a + i*d;
  8.         arith_vec.push_back(x);
  9.         x = arith_vec[i];
  10.         }
  11.     return arith_vec;
  12.     }
Nov 20 '08 #3
469 Expert 256MB
Please post the error messages here so it's easier for people to help you.
I have put your function in a program that calls it, and it works fine. The errors must be from something else. Maybe you forgot to include vector.
This line of code
Expand|Select|Wrap|Line Numbers
  1. x = arith_vec[i];
is unnecessary. x is already equal to arith_vec[i], and at that point in your loop it doesn't matter what x equals anyway.
Nov 20 '08 #4
9,208 Expert Mod 8TB
Did you #include <vector> ??
Nov 20 '08 #5
and are you using namespace std?
Nov 20 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Michael Aramini | last post: by
3 posts views Thread by Amit | last post: by
3 posts views Thread by Trent Buck | last post: by
335 posts views Thread by extrudedaluminiu | last post: by
5 posts views Thread by madhu | last post: by
1 post views Thread by Rob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.