Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Location
    London, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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=0i<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=0i<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?

  • #2
    New to the CF scene
    Join Date
    Feb 2014
    Location
    London, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ^By the way it shouldn't be a PHP Code, it should be for Java.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New to the CF scene
    Join Date
    Feb 2014
    Location
    London, UK
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 


  •  

    Posting Permissions

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