Generate random numbers in array and sort without predefined sort function

Help! I’m trying to generate random numbers in two arrays and then merge them without the predefined function. Any suggestions would be great. Having issues converting between int [] and int…This is what i have so far:

void setup ()
{
  int n = 5;
  int m = 8;
  int [] one  = new int [n];
  int [] two = new int [m];
  foo(one,two); 
  one = sort(one);
  two = sort(two);
  printArray(one);
  printArray(two);
}
int foo( int [] one, int [] two)
{
  for(int i = 0; i < one.length; i ++)
  {
    for( int j = 0; j < two.length; j ++)
    {
     int r = (int(random(50)));
     int [] random = {r};
     one = random;
     two = random;
     return r;
    }
  }
}
1 Like

merge means you need to generate a third array the size of the 2 first arrays together. Then fill it with data from 1 and 2.

Then you for loop over each one separately and pick the smallest number. Then replace the number by a marker -1000 and repeat to fill #3

Homework?

Yes, thank you for the help. I’m confused involving generating random numbers in an array. I’m always getting convert errors. The goal is to merge two arrays that have random numbers and sort them without the predefined sort function. I should of been more clear.

Well first set up the arrays with random.

The sorting is coming later. It is connected with merging.

Setting up the random arrays

When you want the function to return a random array

int[] getRandomArray() { 
   int[] randomArray=new int[10];
   for(....) {
       .....
   }//for

   return randomArray; // !!!!!!!!!!!!!!!!!
}//function

and in setup():

int[] one=getRandomArray();

int[] two=getRandomArray();

You can give the length of the array as a parameter

1 Like

that’s good.

put that in the array

see