d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Merging Two Arrays
Prev12
Add Reply New Topic New Poll
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Sep 27 2013 12:30am
Quote (falco37 @ Sep 26 2013 07:26pm)
no need to be a dick about it. he's trying to learn.


Probably not the greatest class to start off with if you don't know fundamental programming concepts, or how to think in code.

To the OP

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

c[k] = a[i]; so c(0) is a(undefined)
k++; add to k okay
c[k] = b[j]; c(1) is b(undefined)
k++; end of a for loop so by putting this you are adding two

}


Imo do a for loop for each array turning it into a string then concatenate the two together
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Sep 30 2013 03:29am
Quote (carteblanche @ Sep 26 2013 07: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]


That is Java, not C. And really Pseudocode is as useful as a lit candle up your ass. I never understood the point of writing extra stuff of "this is how I am going to do something" instead of just fucking doing it.
Arrays in C are declared as type name[size]; ex. int somearray[20];


Only thing you need to do here is to declare a new array c that's the size of a+b so in this case it will be 15 and use a for loop to feed the elements into it, like:
int c[15]; //or c[a.size()+b.size()]; //(if this gives you compiler errors then add #include <array> to the top of the program, between #include <iostream> and using namespace std;
for(int i=0;i<15;i++){
if(i<10) c[i] = a[i];
else c[i]=b[i-10];
}

or do something like this if you want a more general loop:

c[a.size()+b.size()];
for(int i=0;i<a.size()+b.size();i++){
if(i<a.size()) c[i] = a[i];
else c[i]=b[i-a.size()];
}


This post was edited by NeX_1337 on Sep 30 2013 03:34am
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll