Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Array exercise in Java

1. ## Array exercise in Java

Currently I am learning Java as well as JavaScript.

Last we had learnt arrays at uni and in order to "digest" arrays I found some questions/tasks about arrays, sadly there was no solution for it, anyway I still tried to do it, it would be good if some of you guys could help me out with it:

Question 01: Given an integer array data, write a code fragment that finds and prints the product of all the entries in data. For example, if data={1,2,3,4}, the printed answer should be 24.
Solution:
PHP Code:
``` int [] data = new int [n]; //Creating an array called data int product; // I have declared a product variable once the items in the array will be multiplied with each other the value will be stored in this variable. //and now I am creating a for loop in order to multiply all the array elements with each other: for (int i=0; i<n.length;i++) { product = data[i]*data[i]; } system.out.println(product);  ```
Question 02: Write a method that takes in an array of salary values and a double rise rate, to calculate and return the array of salaries after they are raised. If, for example, the rise rate is 1.07 and one has the salary of £20000, then after raising the salary is 20000*1.07=21400.

Solution:

So I guess I should declare 1 array for salaries and 1 variable for double rise rate, then simply do the calculation again using for loop and simply print out.

PHP Code:
``` int [] salaries = new int [n]; float doubleRiseRate = a; int newSalary; for (int i=0; i<n.length;i++) { product = salaries[i]* doubleRiseRate; } system.out.println(newSalary);  ```

To be fair both questions seem to be so easy but I am pretty sure I did mistakes, so what do you think the right solution would be for these exercises?

• ^By the way it shouldn't be a PHP Code, it should be for Java.

• The php code tags are more for simple markup. They work with pretty much any C based language including Java.
1. To find the product you must multiply this element and the current product value. To do so, you simply use:
PHP Code:
``` if (product == 0){    product = data[i];}else{    product *= data[i];}  ```
Thats it. So the first iteration the product is 1, then 2, than 6, and finally 24.
2. This it the correct idea, but you'll likely need to put the result into another array. Create a double array the same size as the integer array, and then loop the integer array and stuff the calculated result into the same element of the double array.

• Originally Posted by Fou-Lu
The php code tags are more for simple markup. They work with pretty much any C based language including Java.
[list=1][*]To find the product you must multiply this element and the current product value. To do so, you simply use:
PHP Code:
``` if (product == 0) {     product = data[i]; } else {     product *= data[i]; }  ```
Thats it. So the first iteration the product is 1, then 2, than 6, and finally 24.[*]
But why do you need an if loop for the first one?

• Do you mean an if branch? You require that for the first non-zero item within the array. Without it, every result of product would be 0 since the default value of an integer is 0. Anything multiplied by 0 is always 0, so without it your product would result in 0 regardless of the items within the array.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•