-8

i have two Arrays in type of int , int[] x = {1,2,3,4,5}; int[] y = {1,3,12};

and i want to multiply the two arrays in a randomly order ( i mean that any integer in first array can multiply of any integer of the second array) ,and the output must be equal to the length of the first array .

How do you think that what should i do to reach to the solution .

A.james
  • 27
  • 4
  • 7
    Not only is the problem poorly defined, but Stack Overflow is not a "please code this for me" site. You need to ask a *specific* question about how to do that, not just ask for the solution. Picking a language to work in is a good thing to do also – BradleyDotNET Aug 26 '16 at 23:06
  • ok thanks , i will edit it to define the problem – A.james Aug 26 '16 at 23:10
  • 1
    Well, you loop through the first array with a "for" loop and you multiply each item with a y[random(0,3)]. – Stef Geysels Aug 26 '16 at 23:13
  • And as a hint about that: if you're tagging both Java and C#, your question is probably too broad. – yshavit Aug 26 '16 at 23:13
  • I do this because the solution might be closer to one of these languages. – A.james Aug 26 '16 at 23:18
  • 1
    @A.james the solution is trivial in either language. Your question is still effectively asking for the solution also; break your problem down into its components (how to iterate an array, how to randomly select an element, etc.) and determine which you don't know how to do, then ask about *that*. Actually, **don't** ask about that; google it since I'm sure those questions have been asked before ;) – BradleyDotNET Aug 26 '16 at 23:23
  • 1
    @A.james Note that this isn't just how to solve your current problem, its how to solve *every* programming problem, and a skill you **have** to develop. – BradleyDotNET Aug 26 '16 at 23:24
  • @A.james with just a quick Google, I found a solution that you can adapt to your needs: http://stackoverflow.com/questions/20132884/how-to-multiply-all-values-in-an-array – Stef Geysels Aug 26 '16 at 23:26
  • @A.james and for the random part: http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c – Stef Geysels Aug 26 '16 at 23:27
  • Since I assume you are summing the result, the order doesn't matter. – Peter Lawrey Aug 27 '16 at 00:12

2 Answers2

3

You first want to loop the length of your first array. Then. you want to generate a random number between 0 and the the length of your second array minus one.

Since an array starts with an index of 0, your second array's last index is 2.

Then, you want to multiply the value of every iteration with the random number. It should look like something like this:

Assuming your third array which is the same size of the first one is called "z"

z[i] = x[i] * y[randomNumber];

Winter
  • 3,894
  • 7
  • 24
  • 56
Cedric Martens
  • 1,139
  • 10
  • 23
0

First I would index through the first array using a for loop and for each of the elements in the first loop, you can multiply it by a random element of the 2nd element. Afterwards, set the multiplication back to the original x array.

The code would be:

//using the Random Class from Java (http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt())
import java.util.Random;

public class randomArrayMultiplication {

  public static void main(String [] args) {

    int[] x = {1,2,3,4,5};
    int[] y = {1,3,12};

    //declare a new random class
    Random rn = new Random();

    //iterate through the first array
    for (int i = 0; i < x.length; i ++) {

        //for every element of the x array multiply it by a random   
        //element in the y array (using the randomInt method from the 
        //random class in which " Returns a pseudorandom, uniformly 
        //distributed int value between 0 (inclusive) and the specified 
        //value (exclusive), drawn from this random number generator's 
        //sequence."). Therefore, we will choose a random element in 
        //the y array and multiple it by the x array. At the same time, 
        //we will end up setting that back to the x array so that every 
        //array in x is multiplied by a random element in y.
        x[i] = x[i] * y[rn.nextInt(y.length)];
    }
  }
}
  • great ,it works well ... but i think the multiplication should be in this form x[i] = x[i] * y[rn.nextInt(y.length)]; – A.james Aug 27 '16 at 13:35