d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Merging Two Arrays
12Next
Add Reply New Topic New Poll
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 26 2013 03:44pm
I have two arrays, and I need to merge them into one list. They don't have to be sorted or anything, but I don't know how to code it.

Here's what I have written:

Code
#include <iostream>
using namespace std;


int main()
{

int a[10]={3,12,45,59,2,38,99,24,81,17};
int b[5]={6,81,72,28,59};

int maxa = a[0];
int mina = a[0];

int maxb = b[0];
int minb = b[0];

double suma = 0;
double sumb = 0;

double averagea = 0;
double averageb = 0;


for (int i=0; i < 10; i++){

if( maxa <= a[i] )
maxa = a[i];

if ( mina >= a[i] )
mina = a[i];

suma+=a[i];
averagea = suma/10;

cout << "Array 1: Average: " << averagea << endl;

cout << "Array 1: Smallest Array Element: " << mina << endl;

cout << "Array 1: Largest Array Element: " << maxa << endl;

}

for (int j=0; j < 5; j++){

if( maxb <= b[j] )
maxb = b[j];

if ( minb >= b[j] )
minb = b[j];

sumb+=b[j];
averageb = sumb/5;

cout << "Array 2: Average: " << averageb << endl;

cout << "Array 2: Smallest Array Element: " << minb << endl;

cout << "Array 2: Largest Array Element: " << maxb << endl;


}

system("pause");
return 0;
}


This post was edited by King3r on Sep 26 2013 03:45pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 26 2013 05:29pm
if you dont even need to sort it, should be simple. in pseudo code

Code


mergedarray = new array[array1.size + array2.size];
for i = 0 to array1.size
mergedarray[i] = array1[];

for i = 0 to array2.size
mergedarray[i+array1.size] = array2[i]
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 26 2013 05:51pm
I dont understand what youre trying to say :/ im so new at this
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Sep 26 2013 05:55pm
Quote (King3r @ Sep 26 2013 06:51pm)
I dont understand what youre trying to say :/ im so new at this


Maybe you should drop the class
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 26 2013 06:06pm
Quote (King3r @ Sep 26 2013 07:51pm)
I dont understand what youre trying to say :/ im so new at this


you have two lists. to combine them, you have to create one nice big list that's big enough to hold them both. then put the first array in there, then the second.

array1 = a a a a
array2 = b b
merged array = a a a a b b

Quote (0n35 @ Sep 26 2013 07:55pm)
Maybe you should drop the class


pretty good advice imo

This post was edited by carteblanche on Sep 26 2013 06:07pm
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 26 2013 06:19pm
this is what ive tried and it doesnt work


Code
for (int k=0; k < 15; k++){

c[k] = a[i];
k++;
c[k] = b[j];
k++;

}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 26 2013 06:23pm
Quote (King3r @ Sep 26 2013 08:19pm)
this is what ive tried and it doesnt work


Code
for (int k=0; k < 15; k++){
           
              c[k] = a[i];
              k++;
              c[k] = b[j];
              k++;
         
          }


i recommend adding print statements after every line. print out k, c[k], i, a[i], j, b[j]. then you'll understand what you're doing.
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 26 2013 06:26pm
Quote (0n35 @ Sep 26 2013 06:55pm)
Maybe you should drop the class


no need to be a dick about it. he's trying to learn.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 26 2013 06:37pm
Quote (carteblanche @ Sep 26 2013 08:23pm)
i recommend adding print statements after every line. print out k, c[k], i, a[i], j, b[j]. then you'll understand what you're doing.


if this doesn't help you, you can try writing it without a loop, adding one entry at a time with hard-coded indexes. make sure you get this correct. then convert it to a loop based on the index pattern you see.
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 26 2013 07:08pm
Quote (King3r @ Sep 27 2013 02:19am)
this is what ive tried and it doesnt work


Code
for (int k=0; k < 15; k++){
           
              c[k] = a[i];
              k++;
              c[k] = b[j];
              k++;
         
          }


neither i nor j ever change. You're always pointing to the same index within your list. You need to assign each index of both your list to a unique index in the merged list. So you should start with copying list a into c's first 10 slots, then copying b into c's last 5 slots.

Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll