473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 3710
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.