Create two arrays from command line and work on them
Hello,
I'm trying to make it possible such that when I run from the commadn line the following :
./program 1 2 3 | 3 4 5
it will create two arrays, A and A1 and process them as per the match function (which I did test to work). So I only need help with the Array extractor methods and the main. Awaiting your kind suggestions.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void match(int A1[], int A2[]){
int n1 = sizeof(A1);
int n2 = sizeof(A2);
int i,m,a1k,a2i,l,r,a1m;//global variables
int a10=A1[0];
for (i=0; i<n2; i++){//scan all elements of A2
a2i = A2[i];//for efficiency
if (a10 == a2i || A1[i]==a2i) printf("%d\t",a10);//special cases: print element
else{ //when special case not true
r = n1-1;
l = 0;
if (a2i >= a1k){
if (a1k == a2i)
printf("%d\t",a1k); //print the element
else{
while(r>=l){//since A1 is sorted
m = (l+r)/2;
if (a2i == (a1m = A1[m])){ //if element is found
printf("%d\t",a1m); //print the element
break;}
else{
if (a1m > a2i) r = m-1;
else l = m+1;
}
}
}
}
}
}
}
int * arrayExtractor(const char * argv[], char sep[]){
int n,i=1;
while (strcmp(argv[i], sep) != 0) {n++;}
static int * A;
for (i=1; i<n; i++){A[i] = atoi(argv[i]);}
return A;
}
int * arrayExtractor2(const char * argv[], char sep[]){
int i=2;
while (argv[i] != sep) i++;
static int * A1;
for (i=2; argv[i] != '\0'; i++){A1[i] = atoi(argv[i]);}
return A1;
}
int main(int argc, const char * argv[]){
match (arrayExtractor(argv, '|'), arrayExtractor2(argv,'|'));
return 0;
}