Concatenation

  • Thread starter Mario Contreras
  • Start date
M

Mario Contreras

From: "Mario Contreras" <[email protected]>
Subject: Re: Concatenate
Date: Friday, July 04, 2003 2:53 PM

This is what I have, but it needs a little modification
It gets all values from 2 arrays, but still not perfect..


// I need to put 2 arrays together in an
// single int array.
// no duplicates


#include <iostream>
using namespace std;
#define L2 << endl << endl
#define L3 << endl << endl << endl << endl

class Set{
public:
void Sum(int a[], int b[], int c[], int size);
}; // End of class


// i Corre a

void Set::Sum(int a[], int b[], int c[], int size){
int tmp = size;
for (int i = 0; i < size; i++){
c = a;
}

// Jota corre b

for (int j = 0; j < size; j++){
for (int k = 0; k < size; k++){
if(c[k] != b[j])
c[j+tmp] = b[j];

}

}

// Just to print all c array

for (int z = 0; c[z] != '\0'; z++)
cout << "c[" << z << "] = " << c[z] << endl;

}

void main(){
int size = 4;
int a[] = {1,2,3,4};
int b[] = {1,3,5,7};
int c[32] = {0};
Set s;
s.Sum(a, b, c, size);
}
 
S

Suzanne Vogel

In the future, it would help if you would state exactly what the problem
is, and comment your code.
// Jota corre b

for (int j = 0; j < size; j++){ // iterate through b
for (int k = 0; k < size; k++){ // iterate through c, to see if it already contains b[j]
if(c[k] != b[j])
c[j+tmp] = b[j];

}

I added a couple of comments to the code above.

Note: For abbreviation, I have ended c "short" below. i.e., If the end
contains zeros, I don't bother typing them. So, I'd write {2,1} instead
of {2,1,0,0,0,0}.

The if-test will work iff *all* items of c already contain b[j], e.g.,
c == {4,4,4,4,4}
b == {8,3,4,5,6}

If j==2, then we're looking at b[2], and the if-test will always fail,
so c will not receive another copy of b[2]==4, which is good. BUT suppose

c == {4,4,4,4,3}

Then, since there is at least one item (c[4]) of c that is not equal to
b[2]==4, then another value of b[2]==4 will be appended to c:

c == {4,4,4,4,3,4}

Not what you want. This works (I tried it):

void Set::Sum(int a[], int b[], int c[], int size){
int tmp = size;
for (int i = 0; i < size; i++){
c = a;
}
// Jota corre b
int csize = size; // number of items in c
for (int j = 0; j < size; j++) { // iterate through b
// 'duplicate' will be true iff (Ej: 0<=j<=size-1: Ek:
0<=k<=csize-1: b[j]==c[k])
bool duplicate = false;
for (int k = 0; k < csize; k++){ // iterate through c, search
for items already in b
if (c[k]==b[j]) {
duplicate = true; // found an item of b that is already
in c
break; // no sense continuing to search b
}
}
if (!duplicate) { // insert b[j] into c iff it has not already
been inserted
c[csize] = b[j];
++csize;
}
}
// Just to print all c array
for (int z = 0; c[z] != '\0'; z++)
cout << "c[" << z << "] = " << c[z] << endl;
}

Suzanne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top