Hi there fellas..
I am learning this powerful language, but came accross a concatenation
problem that I can't resolve.
I have 2 arrays
A1[] = {1,2,3,4};
A2[] = {1,3,5,7};
I need to put them together in an int array that looks like this.
A3[32] = {1,2,3,4,5,7};
Sum them both, but skip duplicates..
Can some1 Help Please...
Thanks... 9 8873
"PerritoPer ron" <ma******@ev1.n et> wrote in message
news:2c******** *************** ***@posting.goo gle.com... Hi there fellas.. I am learning this powerful language, but came accross a concatenation problem that I can't resolve.
I have 2 arrays
A1[] = {1,2,3,4}; A2[] = {1,3,5,7};
I need to put them together in an int array that looks like this. A3[32] = {1,2,3,4,5,7};
Sum them both, but skip duplicates..
Use vectors instead of C-style arrays. Here's some untested snippet:
vector <int> a1, a2, a3;
// fill vectors with your values
a3 = a1; // copy first into a3
a3.insert (a3.end (), a2.begin (), a2.end ()); // append second
sort (a3.begin (), a3.end (), less <int> ()); // sort a3
unique (a3.begin () , a3.end ()); // remove dups
Again, I just typed this into my newsreader without trying to compile
this. But it will give you an idea of how it would work.
hth
--
jb
(replace y with x if you want to reply by e-mail)
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);
}
"Mario Contreras" <ma***@txpos.co m> wrote in message
news:nX******** ************@tw ister.austin.rr .com... 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
Looks like you are trying to write a Set class but you don't really know how
to design it.
You need to put one of the arrays inside the Set class, otherwise it's a
waste of time. Then you need to add some useful member functions to the Set
class.
Start like this
// set, maximum size 32
class Set
{
public:
Set()
{
// make empty set
}
Set(int a[], int size)
{
// make set from array (remove duplicates)
}
void add_int(int val)
{
// add one int to array (if not duplicate)
}
void union(const Set& x)
{
// add another Set to this Set (remove duplicates)
}
void print()
{
for (int i = 0; i < size; ++i)
cout << elem[i] << ' ';
cout << '\n';
}
private:
int elem[32]; // can't have more than 32 integers
int size; // how many integers we have got
};
Then do this
int main()
{
int a[] = {1,2,3,4};
int b[] = {1,3,5,7};
Set a_set(a, 4);
Set b_set(b, 4);
Set c_set = a_set; // copy a_set to c_set
c_set.union(b_s et); // add b_set to c_set
c_set.print();
}
All you have to do is write the member functions above.
john
> Start like this // set, maximum size 32 class Set { public: Set() { // make empty set } Set(int a[], int size) { // make set from array (remove duplicates) } void add_int(int val) { // add one int to array (if not duplicate) } void union(const Set& x) { // add another Set to this Set (remove duplicates) }
Of course you can't have a member function called union, change union to
make_union, or something.
john
"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:be******** *****@news.t-online.com...
| "PerritoPer ron" <ma******@ev1.n et> wrote in message
| news:2c******** *************** ***@posting.goo gle.com...
| > Hi there fellas..
| > I am learning this powerful language, but came accross a concatenation
| > problem that I can't resolve.
| >
| > I have 2 arrays
| >
| > A1[] = {1,2,3,4};
| > A2[] = {1,3,5,7};
| >
| > I need to put them together in an int array that looks like this.
| > A3[32] = {1,2,3,4,5,7};
| >
| > Sum them both, but skip duplicates..
|
|
| Use vectors instead of C-style arrays. Here's some untested snippet:
|
| vector <int> a1, a2, a3;
| // fill vectors with your values
|
| a3 = a1; // copy first into a3
| a3.insert (a3.end (), a2.begin (), a2.end ()); // append second
| sort (a3.begin (), a3.end (), less <int> ()); // sort a3
| unique (a3.begin () , a3.end ()); // remove dups
[snip]
Its not that big a deal in this case I suppose, but
'std::unique()' will *not remove all duplicates*.
It will only remove *consecutive* duplicates.
Cheers.
Chris Val
"Chris ( Val )" <ch******@bigpo nd.com.au> wrote "Jakob Bieling" <ne*****@gmy.ne t> wrote in message
| sort (a3.begin (), a3.end (), less <int> ()); // sort a3 | unique (a3.begin () , a3.end ()); // remove dups
Its not that big a deal in this case I suppose, but 'std::unique()' will *not remove all duplicates*.
It will only remove *consecutive* duplicates.
Which is why I used std::sort before removing the dups.
regards
--
jb
(replace y with x if you want to reply by e-mail)
"PerritoPer ron" <ma******@ev1.n et> wrote in message
news:2c******** *************** ***@posting.goo gle.com... Hi there fellas.. I am learning this powerful language, but came accross a concatenation problem that I can't resolve.
I have 2 arrays
A1[] = {1,2,3,4}; A2[] = {1,3,5,7};
I need to put them together in an int array that looks like this. A3[32] = {1,2,3,4,5,7};
Sum them both, but skip duplicates..
Can some1 Help Please... Thanks...
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int > vec1(4), vec2(4), vec3;
vec1[0] = 1;
vec1[1] = 2;
vec1[2] = 3;
vec1[3] = 4;
vec2[0] = 1;
vec2[1] = 3;
vec2[2] = 5;
vec2[3] = 7;
vec3.reserve(8) ; // reserve space for the maximum
possible number of elements necessary
std::set_union( vec1.begin(), vec1.end(), vec2.begin(), vec2.end(),
std::back_inser ter<std::vector <int> >(vec3));
vec3.swap(std:: vector<int>(vec 3)); // shrink-to-fit (if desired)
std::copy(vec3. begin(), vec3.end(),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << '\n';
return 0;
}
There are a number of things to explain about this:
1) std::vector<int > vec1(4) creates a std::vector (a sort of self-managing
array) with current size 4.
2) vec3.reserve(8) ; does *not* resize the vector, it ensures that the
vector's capacity (distinct from its size) is at least 8. This means that
the memory has been allocated for at least 8 elements, even though the
current number of elements (the size of the vector) is still 0.
3) std::set_union( <blah>) is the bit which does what you want, since what
you want is in fact just a set union operation.
4) std::back_inser ter<std::vector <int> >(vec3) is an output iterator which
pushes things onto the end of vec3.
5) vec3.swap(std:: vector<int>(vec 3)); trims excess capacity from the vector
(a so-called "shrink-to-fit" operation). Although the size of vec3 will be 6
after the set_union, the capacity will still be at least 8. Shrink-to-fit
attempts to make it as small as possible (whilst still greater than 6),
though the exact capacity afterwards depends on the implementation.
6) std::copy(<blah >) outputs the elements of the vector to std::cout, using
an ostream_iterato r which separates the elements with a space (" ").
While we're using lots of stuff from the Standard Library, here's a book
recommendation:
The C++ Standard Library (Josuttis)
HTH,
Stuart.
In comp.lang.c++ ma******@ev1.ne t (PerritoPerron) wrote: I am learning this powerful language, but came accross a concatenation problem that I can't resolve.
I have 2 arrays
A1[] = {1,2,3,4}; A2[] = {1,3,5,7};
I need to put them together in an int array that looks like this. A3[32] = {1,2,3,4,5,7};
Sum them both, but skip duplicates..
You have presented the problem but none of your work at solving it. How do
YOU think you should do it? Perhaps: sort both, add, compare, add,
compare....
In comp.lang.c++
"Chris \( Val \)" <ch******@bigpo nd.com.au> wrote: | sort (a3.begin (), a3.end (), less <int> ()); // sort a3 | unique (a3.begin () , a3.end ()); // remove dups
[snip]
Its not that big a deal in this case I suppose, but 'std::unique() ' will *not remove all duplicates*.
It will only remove *consecutive* duplicates.
Hmmm, you sure about that? If it is sorted first, as it is in his code, it
should remove all dupes.
"Every time a consecutive group of duplicate elements appears in the range
[first, last), the algorithm unique removes all but the first element." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nick Heppleston |
last post by:
I have a concatenation problem and I was wondering if somebody might be
able to offer some help :-)
I have the following table structure holding product long descriptions:
Part No (pn) Sequence (seq) Long Description (long_desc)
--------------- --------------- ----------------------------------------
HL1450 10 This is part of a long description and
HL1450 20 it overlaps onto several lines. I'm
HL1450 30 having difficulty writing...
|
by: priya |
last post by:
Hi
How to concatenate two integer Values.
Example Program :
#include "Port.h"
#include "BinaryConversion.h"
# include "iostream.h"
|
by: Dixie |
last post by:
I have the results of a query to send to a mailmerge with Word 2000. The
query produces say 6 to 8 records, where only 1 of the fields is different
from record to record. I can only have one record for my mail merge, so I
would like to concatenate the 6 to 8 records into 1 with the text field that
is different in each record being concatenated into a single text field with
a carriage return/line feed at the end of each record so it comes...
|
by: Sheldon |
last post by:
Hi,
I am trying to build a large array using concatenate function in
python.
So as I loop over the number of arrays, of which there are 12 (4 down
and 3 across), I create 3 long arrays by concatenating them at the
bottom and then concatenating them side by side:
for ind in range(num_arrays):
if ind == 0:
bias_down1 = array(bias)
|
by: metamorphiq |
last post by:
Hello,
I'm a Java programmer, so I'm probably asking a very simple question
here, but I have trouble solving it :)
I'd like to know how you concatenate multiple (4, in my case) char* in
C++, and have the result as a char*.
I first tried using strcat, but you need a const char* as the second
parameter to work, but I don't know all the issues regarding const
chars, so maybe I overlooked something.
| |
by: exapplerep |
last post by:
I've seen how to use VBA code to concatenate two fields into a third by
using an expression in the "After Update" property in fields 1 & 2.
field3 = field1 + field2
The above code would go into both fields 1 and 2.
Here's the problem: It is possible that field1 may be blank (or may be
made empty by the user). In this case, tabbing out of field1 (after
emptying it, for example) also empties field3. Now tabbing out of
|
by: Dan |
last post by:
Hi all, I am creating a search table where the keywords field is made
up of several text fields and this is causing me some problems. I can
concatentate the text ok but i can't seem to concatenate matching
records here is the cursor loop. I'm not a fan of cursors but also
didn't see another way of achieving this.
declare @ptr1 varbinary(16)
declare @Ptr2 varbinary(16)
declare @profileid int
declare @x int
|
by: parth |
last post by:
Hi
I want to achieve the following transformation of data using a stored
procedure.
Source
col1 col2(varchar)
-------------------------
|
by: sinbad |
last post by:
hi,
how to concatenate a "hash defined" constant value to another "hash
defined" constant string. For example
#define ABC 100
#define MYSTR "The value of ABC is"
Now i need a string that will concatenate the value of ABC to MYSTR .
I need this at compile time.
|
by: Aaron Hoffman |
last post by:
Hello,
I'm hoping someone might be able to offer some guidance to my problem.
I have one query in MS Access which consists of 2 tables joined by a
SEQUENCE_ID. By joining the two tables I am able to produce a query
that gives me two fields, Part_Number and Product_Type. Below you can
see some sample data:
Part_Number Product_Type
10MC35231 XYZ1A
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |