-3

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.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • 5
    `main` needs to be defined as `int main(int argc, char *argv[])` you can't just decide to make it `main(int, int[])`. Your compiler is telling you exactly that – John Doe Dec 04 '19 at 11:11
  • 5
    If you can assume C++17 or later, there is `std::gcd()` in standard header ``. – Peter Dec 04 '19 at 11:12

1 Answers1

1

The main method declaration must be int main(int ,char**);
Here you will get array of c-strings. i.e (["10","20","40"....]).
So you need to convert char* to integer using below methods

  1. using atoi() method ==> read this
  2. sscanf() method ==> read this

see the below code

int findGCD(char* input2[], int n) 
{ 
    int result = atoi(input2[1]); /*Here first argument is fileName itself ,so we are taking from second.i.e input2[1]*/
    for (int i = 2; i < n; i++) 
    { 
        int a = atoi(input2[i]);
        result = gcd(a, result); 

        if(result == 1) 
        { 
            return 1; 
        } 
    } 
    return result; 
} 

// Driver code 
int main(int args,char** argv) 
{ 
    // for(int i = 0;i < args;i++)
    // {
    //     cout <<"argv["<<i<<"] = "<<argv[i]<<endl;
    // }
    cout << "GCD::"<<findGCD(argv, args) << endl; 
    return 0;
} 
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37