Two questions in this code

#include < iostream >
#include < iomanip >
#include < algorithm >
#include < cstring >

using namespace std;

struct integer{
int n;
int thg[500];
};

int binary_search(const int array[x], int first, int final, int point) {
int tag = -1;
int mid;
while (first <= final) {
mid = first + (final - first) / 2;
if (array[mid] < point)
first = mid + 1;
else if (array[mid] > point)
final = mid - 1;
else {
tag = 1;
break;
}
return tag;
}

void results(integer a[ ]){
for(int i=0 ; i<2 ; i++){
int smallest = 10000;
cout<<"The smallest number in {";
for(int j=0 ; j<a[i].n ; j++){
if(j==0)cout<<a[i].thg[j];
else cout<<", "<<a[i].thg[j];
if(a[i].thg[j]<smallest)smallest=a[i].thg[j];
}
cout<<"} is "<<smallest<<"."<<endl;
}
}

void union_sets(integer a[], int *c, int &n){
sort(a[0].thg, a[0].thg+a[0].n);
for(int i=0 ; i<a[0].n ; i++){
c[i] = a[0].thg[i];
}

n += a[0].n;
int *p;
p = new int[a[1].n];
int k=0;
for(int i=0 ; i<a[1].n ; i++){
	if(binary_search(a[0].thg, 0, a[0].n-1, a[1].thg[i]) == 1);
	else{
		rang[k] = a[1].thg[i];
		k++;	
	}
}
n += k;
for(int i=a[0].n, b=0 ; i<n ; i++, b++){
	c[i] = p[b];
}
delete [ ] p;

}

int En_data(integer a[ ], int i, char str[ ], char *token){

if(i==0) printf("Enter the first integer set: ");
else printf("Enter the second integer set: ");
fgets(str, 500, stdin);
int len = strlen(str);
str[len-1] = '\0';
token = strtok(str," ");
a[i].thg[0] = atoi(token);
int j = 1;
while(token != NULL){
	token = strtok(NULL," ");
	if(token != NULL){
		a[i].thg[j] = atoi(token);
		j++;
	}
}
a[i].n = j;

}

using namespace std;

int main(void){

char str[500]; 
integer a[2];
char *token;
for(int i=0;i<2;i++){
	En_data(a, i, str, token);
}

results(a);
int tol = a[0].n+a[1].n;
int c[tol], n=0;
union_sets(a, c, n);
sort(c, c+n);

cout<<"The union of two sets is {";
	for(int i=0 ; i<n ; i++){
		if(i==0)cout<<c[i];
		else cout<<", "<<c[i];
	}
	cout<<"}."<<endl;

}

[Error] a function-definition is not allowed here before '{' token
[Error] expected '}' at end of input

Is this code intended to run on an Arduino ?

If so, then where will the output be sent to ?

oops, actually it runs in c++

Well, the Arduino uses C++ so the code might compile if it were correct but has no cout device to send the output to

In any case the code is not correct. For instance, where is the closing brace of the binary_search() function ?

I have no doubt that there are other problems but I stopped looking

1 Like

oh I see. thank you !!!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.