Euclid gcd don't working

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int v[]
int gcd(v[1],v[2])
{int r;
{while(v[2]!=0)
r=v[1]%v[2];
v[1]=v[2];
v[2]=r;
}
return v[1];
}

int main()
{
int i;
for(i=1;i<3;i++)
{v[i]=rand()%20000+1;
cout<<v[i]<<" ";
}
cout<<gcd(v[1],v[2]);
return 0;
}

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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// Use a constant for the size of the array.
const int SIZE = 3;
int v[SIZE];

// Note that the names of the function parameters in the definition are
// not the same as the arguments when you actually call it.  In the definition
// the parameters must be individual variable names, not array elements.
int
gcd(int a, int b)
{
    int r;
    // Note that you need braces for multiple statements in the while loop
    // also note that the logic here is slightly different from your code.
    while (b != 0) {
        r = b;
        b = a % b;
        a = r;
    }
    return a;
}

int
main()
{
    int i;
    // seed the random number generator. Otherwise you get the same values
    // each time.
    srand(time(0));

    // arrays start at index 0
    for (i = 0; i < SIZE; i++) {
        v[i] = rand() % 20000 + 1;
        cout << v[i] << " ";
    }
    cout << '\n';  // print a newline so you can tell where the gcd() is
    cout << gcd(v[1], v[2]) << '\n';
    return 0;
}

Thank you very much!
Topic archived. No new replies allowed.