If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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.

The php code tags are more for simple markup. They work with pretty much any C based language including Java.

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.

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.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

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.[*]

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.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)