-2

I am new to programming. I came across this question:

"Write a program which will fill three arrays a,b and c, all lengths is 8,with pseudo random integers between 2 and 10,including 2 but not including 10, and set the array elements of a fourth array, d, of length 8,to the sum of corresponding elements of a,b, and c, and output all four arrays as indicated below.get the three arrays (a,b,and c) generated in the main and pass to an non-main method to add the corresponding elements to determine the sum array(d) and return the sum array (d) to the main.with all four arrays outputting from the main."

So far I have no problem creating the arrays a,b and c with random elements. But I do not know how to create d outside the main (how to send multiple parameters, how to return values to main). Please help!

I tried something like this:

What's wrong with this?:

public static void main(String args[]){
//generated arrays a, b, c
        d[] = assemble (a, b, c);
    }
    public assemble (int a, int b, int c){
            int d = a + b + c;
            return;
    }
}
Jin
  • 3
  • 3

2 Answers2

0

Some pseudocode:

function main{
    // stuff going on
    array result;
    result = assemble (a, b c);
}

function array assemble (array a, array b, array c){
    // do something with the arrays
    // assing result to d
    return array d
}

Call assemble within main and make it return the resulting array. Assign the returned array to a variable in order to store the result.

After this you will have all 4 arrays available in main (with array d stored in the variable you created) and you can print all of them out or whatever else you need to do with them.

Java example: Passing arguments and returning result

Modified your code:

public static void main(String args[]){
    //generated arrays a, b, c
        int[] d = assemble(a, b, c);
}
    public int[] assemble(int[] a, int[] b, int[] c){
            int[] result;
            // do something with a b c and assign it result to result[]
            return result;
    }
}
Code Whisperer
  • 1,041
  • 8
  • 16
  • Can I do the stuffs with the arrays a, b, c in main, and then pass the values to the method assemble? – Jin Oct 16 '14 at 15:48
  • You can but from what I understood from your question you have to do it within another function. If you have to just assemble it in another function and do stuff in main it will work as well. Just be sure to pass the modified a, b, c to the other function and not the originals. You can do this by overwriting the originals or making new variables for the modified a, b, c. – Code Whisperer Oct 16 '14 at 15:51
  • Sorry, I still don't get it lol. Let me figure out how to paste my codes here – Jin Oct 16 '14 at 15:56
  • Yes paste the code in your question so we can see it. – Code Whisperer Oct 16 '14 at 15:58
0

First problem (pseudorandom number generation): Math.random() explained

Second part of the problem: Iterate (loop) through the arrays to add each pseudorandom integer values and store the result in the corresponding index location: d[i] = a[i] + b[i] + c[i].

Since your requirement is to make all the arrays of the same length, I would probably create a constant integer MAX_SIZE and set it to 8. When you declare your four arrays, I would use this constant to specify the size (i.e. int[] a = new int[8]).

The instruction "and output all four arrays as indicated below" is not clear to me. I do not know that 'below' means in this context. But, I assume that as you make the assignment of each value, you could use System.out to output each value to the screen.

"I do not know how to create d outside the main." Your array 'd' is the sum of the values stored in a, b, and c. Your code is wrong because you are passing integer values (not array of integers) to the method and returning an integer and trying to assign it to an array (which are incompatible data types). Your assemble method should take array of integers as arguments (a, b, c) and return an array of integers (d). Inside this method, you must iterate through the arrays being passed, get the values in each index location and assign the sum to the corresponding index on 'd'. Once all values have been added, you will return the array. This is very simple. I am not sure what the difficulty is. If you can do it inside 'main', you can do it outside of it as well.

Community
  • 1
  • 1
hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • For beginners it is not necessarily simple especially when they only start to learn how to create functions outside of main and pass it arguments. – Code Whisperer Oct 16 '14 at 16:43
  • Thanks for the answer. Yea, like code Whisperer said, it is hard for a beginner like me to pass arguments around. But I am slowly getting it. Thanks everyone. – Jin Oct 16 '14 at 16:52
  • I did not mean it from the arguments perspective, I meant it from the action the method must perform. After all, main is just another method. But, I understand the point and it well taken. – hfontanez Oct 16 '14 at 17:24