2 string name tables to a 3rd table so as each name only once in the last table

hello! could anyone tell me how could i: create two string tables ,same size each,check them out to see if they have any same string element inside (in common) and after that to create dynamicly a new table whose size will be such as to fit all the elements of the two tables ,but only once each, for example: if the first table has size 3 and elements: [george, steve , jim] and the second table has also size 3 and the elements: [george, paul, dick], the final table will have the size 5 , so as to fit all the unique names of the two tables, and the elements:[george, steve, jim, paul,dick].due to the fact that the name jeorge is in both tables there is no need to be inserted to the last table twice ,but only once. i have already created an algorithm for the problem above but despite the fact that it has no compile errors when i run it the windows refer problem to the memory and the exetution window of dev c++ is turned off. the algorithm is this (please check it and help me or suggest me another non problematic algorithm for the problem above):

#include<iostream.h>
#include<conio.h>
#include<string.h>
using namespace::std;





main(){

std::string check;
std::string* name1;
std::string* name2;
int metr=0,metr2=0,n,e;
int k=0;
bool den= false;


cout<<"give size of tables(the 2 tables have same size): "<<endl;
cin>>n;
name1 = new string[n];
name2 = new string[n];
for(int i =0; i < n; i++){
cout<<"give name "<<i+1<<" of 1st grapgh: "<<endl;
cin>>name1[i];}

for(int i=0; i<n; i++){
cout<<"give name "<<i+1<<" of 2nd grapgh: "<<endl;
cin>>name2[i];
}
cout<<endl;

for(int i=0; i<n; i++)
cout<<name1[i]<<endl;
cout<<endl;

for(int i=0; i < n; i ++)
cout<<name2[i]<<endl;


cout<<endl;

for(int i=0; i<n; i++){
for(int j=0; j<n; j++)
if(name1[i]==name2[j])
metr++; } //counts how many same names exist in the 2 tabes

cout<<metr<<endl;
cout<<endl;


if(metr!=n){ //if the two tables does not contain exactly the same names
metr2 = (n + (n-metr));//counts the size of the new table //that will contain the names of the two tables only once each
cout<<metr2<<endl;
cout<<endl;
std::string* name3;
name3 = new string[metr2];

for( k=0; k<n; k++)
name3[k]=name1[k]; //inserts the n first names of the first table // to the last table


for( e=0; e<n; e++)//it checks the two first tables to see what //is not in common so as to put it to the last table
for(int j=0; j<n; j++){
if (name2[e] == name1[j]){
den=true;}
if(den==false){
k=k+1;
name3[k] = name2[e];}
}




for (int i=0; i<metr2; i++)
cout<<name3[i]<<endl;
}





getch();
return 0;
}
Last edited on
Topic archived. No new replies allowed.