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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>

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 = "<<a<< " and d = "<<d<<" is "<< gcd(a,d)<<endl;

    int N;
    cout<<"Calculate N terms of the progression";
    cout<<"\nEnter a value for N:" ; cin>>N;

    vector <int> 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) <<endl;
    }
return 0;
 }

int gcd (int a, int d)
{
    int min = 1;
    if(a<=d)
    min = a;
    else
    min = d;

    int k = 1;
    for ( int t=2; t<=min ; t++ )
    if ( ( a%t==0) && ( d%t==0) )
    k = t;
    return k;
}
int a_N (int a, int d, int n)
{
    int an = a+d*n;
    if( gcd(a,d)==1)
    return an;
    if( gcd(a,d)>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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 for(int n=1; n<=N; n++)
    vector <int> 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) <<endl;
    }


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.

1
2
3
4
vector<int> 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.