Really really basic vectors.

I've writtena code for the gcd of two integers a and d and these functions apply to an arithmetic progression
a_n = a + n*d n a natural number including 0 and (a,d) = 1

If (a,d)>1 I need to output the empty vector
If (a,d) = 1 I need to output the progression in vector form.

The vectors are N-dimensional and n is a loop

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include using namespace std; int gcd (int, int); int a_N(int, int, int); int main() { int a, d; cout<<"Enter a value for a: "; cin>>a; cout<<"Enter a value for d: "; cin>>d; cout<<"The gcd of a = "<>N; vector v(N); if(gcd(a,d)>1) { } if(gcd(a,d)==1) { for(int n=1; n<=N; n++) cout<<"a_N = "<< a_N(a,d,n) <1) return 0; }``````

Obviously my output for (a,d) = 1 isn't a vector but I was just desperate to output something.

Bacically I'd like help with what the push.back and pop.back commands actually do and what exactly am I looking for when I print a vector out? And how do you cout a vector?
cout<<v<<endl; is an arror as is cout<<v(N)<<endl; and a lot of other things I've tried.

I've also tried things like
 ``123456789101112131415`` `````` for(int n=1; n<=N; n++) vector v(N);= a_N(a,d,n); if(gcd(a,d)>1) { } if(gcd(a,d)==1) { cout<<"a_N = "<< a_N(a,d,n) <

but they just return errors and I'm typing things without much direction because I just don't understand the vectors at all adn my lectur notes are really bad.

I know I've asked a lot but if anyone can help me in the smallest bit I'd realy appreciate it. :)
Last edited on
If it helps at all basically all I know about vectors is they're of the form
vector<digit type> name(dimension);
if they're empty is it
vector,digit type> name;?
In order to print a vector, use [ ]'s instead of ( )'s, just like arrays:
cout << v[n];

If a vector is empty, it has no elements and a size of zero.
You can initialize an empty vector by: vector<int> v;

push_back adds an element to the back of a vector.
pop_back deletes last element of a vector.

 ``1234`` ``````vector v; // declare an empty vector v.push_back(n); // add int n (to the back) cout << v[0]; // print out n v.pop_back(); // delete the last element ``````

If you are unfamiliar with vectors (I'm very new with them as well), it helps to visualize them as dynamic arrays.

http://www.cplusplus.com/reference/vector/vector/
After it's created, you can use them just like arrays. [] for random access, push_back() to add elements to the front. The only caveat is that pointers to a vector can become invalidated after a resize, since chances are the resize operation had to move the vector to a new place in memory.
Topic archived. No new replies allowed.