472,805 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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)
{
vector<long>arith_vec;
long x = 0;
for( int i = 0; i < N; i++)
{
x = a + i*d;
arith_vec.push_back;
x = arith_vec[i];
}
return arith_vec;
}

Can anyone tell me where I am going wrong?
Quick reply would be great!
Thanks
Nov 19 '08 #1
5 3665
boxfish
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.     }
  13.  
Nov 20 '08 #3
boxfish
469 Expert 256MB
Please post the error messages here so it's easier for people to help you.
Thanks.
Edit:
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
weaknessforcats
9,208 Expert Mod 8TB
Did you #include <vector> ??
Nov 20 '08 #5
vmpstr
63
and are you using namespace std?
Nov 20 '08 #6

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

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
3
by: Trent Buck | last post by:
(Note: C99 supports variadic macros, but C89 does not.) I'm pretty sure what I'm trying to do is impossible, but I'll ask here in case I'm missing something. I'm trying to define generic,...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
3
by: sivasujithsp | last post by:
Hi all, I am designing a Programmer for Microcontrollers in VB6.0 How to use a progression bar to show the process, FYI, to start the progression bar i recieve a start_character and at the...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.