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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Clueless on an Array project

    We were given this project today, which is due Friday, and I am completely clueless where to even start with this. Any help would be appreciated.

    Code, execute and debug the application below using a one-dimensional array.
    1. Write an application which inputs and stores six floating point values in an array, each between 1.0 and 1000.0, inclusive.
    2. Display in table form, the index and the value of each element in the array.
    3. Calculate and display the minimum, maximum and average of the array elements.
    4. Allow the user to specify the number of values to be entered.
    5. Calculate and display the average after eliminating any duplicates in the array. Count and display the number of duplicates removed."

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Can't do too much by way of code since this is an assignment, so you'll need to work through that and post back any specific issues.
    1. That is a matter of instantiating an array of new float[6]. See here for some additional information about arrays: http://docs.oracle.com/javase/tutori...ts/arrays.html. Also, to control input I'd suggest that a method could be added to provide the verification; this is only a requirement if you actually accept input. Please note that it will be somewhat tricky as you are using a float/double type which is primitive, so you cannot keep adding to an existing one without using member properties on objects. For that you would need to use Float/Double class wrapper types.
    2. This depends on exactly what the display is. If its a command line application, you simply use a for loop. Arrays are always sequential, so you can loop from for (int i = 0; i < yourarray.length; ++i). Each entry represents one line, so you need to make sure you add the linefeed (either manually with a print and \n, or using println). Since you only have one thing to display per record, using println is sufficient. The i in the loop represents the offset index.

      1. Average: For this I would use new methods. Ignoring other packages, to do this you need to take all the items and sum them up, then divide by the number in the collection. Simple as that, return a float or double.
      2. Min/Max: Without writing a custom sort and without using additional packages, you can use Arrays.sort(yourArray) to sort them ascending. The first item is the smallest, the last item is the largest. Also makes sense to write methods for these as you will likely want to work on a copy of the collection (and using a primitive type provided as an argument will be a copy).
    3. This tells me you should actually write a method to deal with forcing entries between 1.0 and 1000.0. Methods are good since it doesn't matter what you interface with. Input in command line will use the Scanner or Console class or swing text area or input dialogs. Scanner is easy to use; I can't find a decent one for cli entry on the tutorials for oracle, but here's a (lengthy) tutorial here: http://www.java-made-easy.com/java-scanner.html
    4. Start by sorting an array and creating the resulting array (of the same size). Use a a loop and check to see if it exists in the new array. I won't suggest a collection; I have suggested Arrays.sort already so I'd suggest that Arrays.binarySearch would also be useful.
      Code:
      int iAdd = 0;
      for (int i = 0; i < mysortedarray.length; ++i)
      {
          if (Arrays.binarySearch(myResultingArray, mysortedarray[i]) < 0)
          {
              myResultingArray[iAdd++] = mysortedarray[i];
          }
      }
      Something like that should work. If you need to track the actual duplicates, you can add to the else clause there and push them into an array of their own. If you just need the count, it will be the difference between the mysortedarray length, and the iAdd value.


    Hope that gives you some ideas to start with.


  •  

    Posting Permissions

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