473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Concatenation


From: "Mario Contreras" <ma***@txpos.com>
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[i] = a[i];
}

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

Jul 19 '05 #1
1 5090
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[i] = a[i];
}
// 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

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Oliver Crow | last post by:
As a realtive python newb, but an old hack in general, I've been interested in the impact of having string objects (and other primitives) be immutable. It seems to me that string concatenation is...
5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
by: Fahd Khan | last post by:
Hi team! While troubleshooting a crash I had while using BitTorrent where the torrent's target file names didn't fall into the ascii range I was playing around in the interpreter and noticed this...
1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.