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;
}

At the top to make things a little more smooth:

1
2
3
4
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;


There is an argument against the using namespace std; line, so look that up if you get the chance.... Someone will probably post the link.

If you have variable arrays, and are already using standard libraries, why not use vector?

Beyond that, you could make the third array the double the size of the other two arrays and keep track of what it used. Then you could just dump as you check, so you don't have to traverse the arrays twice.

If you allow the user to put the same name twice in an array, then you need to check that also.

In this part:
1
2
3
4
5
6
7
8
9
10
11
12
13
	//it checks the two first tables to see what
	//is not in common so as to put it to the last table
	for( e=0; e<n; e++) {
		for(int j=0; j<n; j++) {
			if (name2[e] == name1[j]){
				den=true;
			 }
			if(den==false){
				k=k+1;
				name3[k] = name2[e];
			}
		}
	}


Once you set den to true, then it'll remain true the entire time, so you won't get anything past the first repeat in loop2... well, if it hits in these loops.

When you hit where den=true; you can probably break from that inner loop there.

I think you mean to have the if ( den == false ) part outside of the first loop. It's hard to tell, since you don't really use brackets much.


Hope that helps for now... If you are allowed to use vector (or other container class), then I suggest it.

..Oh, for the future using [code][/code] tags around your code makes it easier to read.

Last edited on
thanks a lot for your advices. i am sorry for the brackets but i am new in this forum and i dont know what and how to do it .

you are right about your comments but i have to tell you that when i turn the den into false again before the first if ,using [if] brackets accordingly, when i run the program ti breaks the memory of the windows and turns the running window off.
Topic archived. No new replies allowed.