473,397 Members | 1,950 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,397 software developers and data experts.

Merging of arrays

Hello, I have 2 arrays unsorted, and I need to merge them together and then sorted. But I really don´t know, how to do it. Could you please help me? Thanks.
Expand|Select|Wrap|Line Numbers
  1. //Array 1:
  2. int i=0; 
  3. int *array=new int[14];
  4. int y = 14;
  5. int x = 0;
  6. ifstream in("somefile.txt", ios::in |ios::binary); 
  7. while(in >> array[x] && x < y)
  8. {x++;}
  9. for(i = 0; i < 14; i++)
  10. { cout << array[i] << endl;}
  11.  
  12. //Array 2:
  13. int j=0; 
  14. int *arra=new int[14];
  15. int z = 14;
  16. int h = 0;
  17. ifstream in("somefile2.txt", ios::in |ios::binary); 
  18. while(in >> arra[h] && h < z)
  19. {h++;}
  20. for(j = 0; j < 14; j++)
  21. { cout << arra[j] << endl;}
Apr 27 '10 #1
4 2250
hype261
207 100+
The topic of sorting in computer science is considerable and there are many different methods on how to best sort an array in terms of speed and memory. The wikipedia article (below) provides generic details and psuedocode on many of the sorting algorithms that are in use today. I would begin by looking over the article and picking one that you think you can implement.


http://en.wikipedia.org/wiki/Sorting_algorithm
Apr 27 '10 #2
whodgson
542 512MB
I think that you would need to sort each container before merging
Apr 28 '10 #3
jkmyoung
2,057 Expert 2GB
Are you guaranteed to get the same length of elements in both arrays?

Either way, since you have two seperate arrays already, sort them seperately before merging them for better efficiency.
Apr 28 '10 #4
whodgson
542 512MB
Given that a string is an array:
Expand|Select|Wrap|Line Numbers
  1. string s="12487369";
  2. string s2="59214603";
  3. s3=s+s2="1248736959214603";
  4. sort(s3);
  5. s3="0112233445667899";
//now convert s3 to integers using atoi() inside a for loop with its condition specified in terms of s3.length().
May 7 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tmenke | last post by:
Can someone help, I have two arrays and want to merge them into a third array.What I want to do is, after I create this third array (combination of the first two) is to erase the original two...
4
by: Bush will disarm all workers next | last post by:
I'm new to JavaScript. So please excuse my mistakes. I need help on merging two arrays into a third one. There is an array a = {188, 180, 159, 67 } There is another array b={'Rio de Janeiro', 'Sao...
3
by: Patrick | last post by:
I have got 2 XML documents, both of which conform to the same XSD Schema, which define possible optional elements. The 2 XML documents contain 2 disjoint set of XML elements. What is the best,...
3
by: André Hänsel | last post by:
Hi! Is there a better way to do this? $command = '$array = array_merge($array,$array);'; eval($command); Regards, André
8
by: vidishasharma | last post by:
Can somebody suggest good URL which contains code for merging of 2 or more priority queues in c#.
11
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes...
5
by: John | last post by:
Hi Is there a way to merge two or more single dimension string arrays into a single, single dimension string array? Thanks Regards
5
by: Sarge | last post by:
Good Afternoon All, I am working with Visual Studio.Net 2003 If I have Two 1D Arrays: Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17} Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12,...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
1
by: kliopatraisis | last post by:
I think I've been working on this assignment for too long and my brain has stopped making connections! Basically, we are making a very simple version of AutoCAD, called HomeCAD. The problem I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

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.