Combining arrays

Combine two arrays into a single array

Page 1 of 1

5 Replies - 14647 Views - Last Post: 08 April 2008 - 03:02 PM Rate Topic: -----

#1 trixieIam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-February 08

Combining arrays

Post icon  Posted 08 April 2008 - 01:25 PM


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 25

using std::cout;
using std::cin;

void loadArray(int array[]);
void arrayEven(int array[], int even[]);
void arrayOdd(int array[], int odd[]);
void combinedArray(int even[],int odd[]);

int main() {
		int array[SIZE];
		int even[SIZE];
		int odd[SIZE];

		loadArray(array);
		arrayEven(array,even);
		arrayOdd(array,odd);

		return 0;
}

void loadArray(int array[]) {
		int index;

		srand(time(NULL));
		for(index=0;index<SIZE;index++) {
		  array[index] = (int) (rand()%10)+1;
		}
		return;
}


void arrayEven(int array[], int even[]) {
		int index;
		int element;
		int temp;

		for(index=0;index<SIZE;index++) {
		  if(array[index]%2 ==0) {
			even[index] = array[index];
		 }
		}
		for(index=0;index<SIZE-1;index++) {
		  for(element=0;element<SIZE-1;element++) {
			if(even[element] > even[element+1]) {
			 temp = even[element];
			 even[element] = even[element+1];
			 even[element+1] = temp;
			}
		  }
		}
		return;
}

void arrayOdd(int array[], int odd[]) {
		int index;
		int temp;
		int element;

		for(index=0;index<SIZE;index++) {
		  if(array[index] %2 != 0) {
			odd[index] = array[index];
		 }
		}
		for(index=0;index<SIZE-1;index++) {
		  for(element=0;index<SIZE-1;element++) {
			if(odd[element] > odd[element+1]) {
			 temp = odd[element];
			 odd[element] = odd[element+1];
			 odd[element+1] = temp;
			}
		  }
		}
		return;
}

void combinedArray(int even[], int odd[]) {
		int index;
		int temp;
		int element;
	
	return;
}



The program is suppose to randomly get an array of 25 numbers, split and sort it into two arrays, then recombine the arrays into one ordered array. How do you combine the two arrays back into a single array?

Is This A Good Question/Topic? 0
  • +

Replies To: Combining arrays

#2 bstdnator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 05-November 07

Re: Combining arrays

Posted 08 April 2008 - 01:38 PM

you have one glaring problem that i can see in your code, and thats that from the master array your taking items and putting them into even[index] and odd[index] where index is the index into the master array (array). so for example your array contained the values {1 4 3 5 2}, your odd would contain {1 null 3 5 null} and your even would contain {null 4 null null 2}. what you should be doing is using another counter to determine the correct index into even and odd that you should be placing items into. this counter can then be used to tell you the number of elements in even and the number of elements in odd.
if i was doing this i would go through the array elements once putting items into odd or even rather than doing that twice. to recombine the two just make a new array which is the length of both the smaller ones combined, ie oddLength = 12, evenLength = 13, then array = new int[oddLength+evenLength]; then for each element in even add it to array, do the same for odd, and then sort the array to get the elements in order.
Was This Post Helpful? 0
  • +
  • -

#3 trixieIam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-February 08

Re: Combining arrays

Posted 08 April 2008 - 01:59 PM

How would I go about figuring how long the even and odd arrays are? I mean I know I would need a while loop, but what would I use to stop it once I found all the numbers for each array?
Was This Post Helpful? 0
  • +
  • -

#4 bstdnator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 05-November 07

Re: Combining arrays

Posted 08 April 2008 - 02:11 PM

personally i would go through the main array once and do your odd/even test, if its even add it to the even array and incriment the evenLength value by one each time, conversely for odd add it to odd array and incriment oddLength.

but for what you have, to do it for the even values ...
void arrayEven(int array[], int even[]) {
		int index;
		int element;
		int temp;
		int evenLength=0;

		for(index=0;index<SIZE;index++) {
		  if(array[index]%2 ==0) {
		  even[evenLength] = array[index];
		  evenLength++;
		 }
		}
		//cout<<"there are "<<evenLength<<" items in the array even"<<endl;

		for(index=0;index<SIZE-1;index++) {
		  for(element=0;element<SIZE-1;element++) {
			if(even[element] > even[element+1]) {
			 temp = even[element];
			 even[element] = even[element+1];
			 even[element+1] = temp;
			}
		  }
		}
		return;
}


Was This Post Helpful? 0
  • +
  • -

#5 trixieIam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-February 08

Re: Combining arrays

Posted 08 April 2008 - 02:51 PM

So, what you're saying is sort the array into order first, then split into even and odd arrays that way it should already it in order because it'll go for example: 1 odd, 2 even, 5 odd, 12 even, etc?
Was This Post Helpful? 0
  • +
  • -

#6 bstdnator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 05-November 07

Re: Combining arrays

Posted 08 April 2008 - 03:02 PM

no, the ordering of elements has nothing to do with seperating them into odd and even arrays. it has nothing to do with recombining them either. you only want to order the arrays after doing all of that as per your original question

Quote

then recombine the arrays into one ordered array.

have a look through the code i posted, its got 3 different lines to your original code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1