Here is my program
#include <bits/stdc++.h>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of input2ay of
// numbers
int findGCD(int input2[], int n)
{
int result = input2[0];
for (int i = 1; i < n; i++)
{
result = gcd(input2[i], result);
if(result == 1)
{
return 1;
}
}
return result;
}
// Driver code
int main(int input1,int input2[40])
{
int n = sizeof(input2) / sizeof(input2[0]);
cout << findGCD(input2, n) << endl;
return 0;
}
The input has to go in following format
3 2 4 8
where 3 is size of array and 2 4 8 are elements of array.
Now I am getting following errors
main.cpp:40:5: warning: second argument of ‘int main(int, int*)’ should be ‘char **’ [-Wmain]
int main(int input1,int input2[40])
^~~~
main.cpp: In function ‘int main(int, int*)’:
main.cpp:43:23: warning: ‘sizeof’ on array function parameter ‘input2’ will return size of ‘int*’ [-Wsizeof-array-argument]
int n = sizeof(input2) / sizeof(input2[0]);
^
main.cpp:40:34: note: declared here
int main(int input1,int input2[40])
^
What is the problem here?
my question is mostly code adding more details.
edit
based on suggestions I came up with using atoi.
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y){
unsigned wk;
if(x<y){ wk=x;x=y;y=wk; }
while(y){
wk = x%y;
x=y;
y=wk;
}
return x;
}
int gcd_a(int n, int a[n]){
if(n==1) return a[0];
if(n==2) return gcd(a[0], a[1]);
int h = n / 2;
return gcd(gcd_a(h, &a[0]), gcd_a(n - h, &a[h]));
}
int main(int argc ,char *argv[]){
// argc is number of arguments given including a.out in command line
// argv is a list of string containing command line arguments
int total = 0;
int i,input1;
int *value;
for(i = 1; i < argc; i++)
{
// The integers given is read as (char *)
value[i] = atoi(argv[i]);
total++;
}
input1 = total;
int gcd = gcd_a(input1, value);
printf("%d\n", gcd);
return 0;
}
But still this is not giving me desired result. I compiled it online but it did not give any error but it did not took any arguments also.
The post can not be submitted because it looks mostly code java script programming error in SO.