Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Help with padding arrays

    i'm trying to write a program that gets inputs from a user and creates an array, and outputs the top 6 values. but if less than 6 values are entered, i need the remaining space in array to be filled with the lowest value previously entered.

    any help would be greatly appreciated.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help with padding arrays

    What code do you have so far?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    Quote Originally Posted by sdee13 View Post
    but if less than 6 values are entered, i need the remaining space in array to be filled with the lowest value previously entered.
    Why would you need that? When you create an array it is automagically filled with a default value. For int that value is zero. I imagine this is sufficient. Alternatively you could use Arrays.fill() and fill it with Integer.MIN_VALUE. That way you know all entered values must be greater.

  4. #4
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    Quote Originally Posted by Wnt2bsleepin View Post
    What code do you have so far?
    import java.util.Scanner;
    public class array{
    public static void main(String args[])
    {

    final int totalGrades=9;
    int[] grades=new int[9];

    int num = 0;
    Scanner input=new Scanner(System.in);
    System.out.println("Please enter grades, if less than six grades are entered\n "+
    "the top 6 grades will be output, if less than six grades are entred\n"
    +"the lowest grade will fill in until 6 total grades are shown\n");

    for(int j=0;j<grades.length;j++){
    grades[j]=j+1;
    int sum = 0;
    for (int i = 0; i < grades.length; i++){
    sum += grades[i];
    }



    System.out.println(sum);

    }
    sort(grades);

    System.out.println("The grades are : ");
    for (int i=0;i<6;i++)
    System.out.println(grades[i]);
    }

    public static void sort(int[]list){
    for(int i= 0; i<list.length; i++){
    int min =list[i];
    int minIndex =i;

    for( int j = i+1; j<list.length; j++){
    if(min< list[j]){
    min=list[j];
    minIndex = j;
    }
    }

    if (minIndex !=i&&minIndex>6){
    list[minIndex] = list[i];
    list[i]= (int) min;
    min++;


    }


    }

    }


    }

  5. #5
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    its for a project we have to pad the array with the lowest value. in order to find the sum

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    That still makes no sense. If you have an array with a length of 10 but user enters only 4 values. Then you only add the first 4 values in the array. You never need to touch the last 6 elements. Filling them with the lowest value is just extra work that makes no sense.

  7. #7
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    i know but thats the assignment:

    B. Quizzes Mrs. Marshall's Computer Design class has a series of pop quizzes to access the student's knowledge of the material. For a single student, a series of the quiz scores is entered followed by a trailer record of -1. The following rules apply in determining the total points allotted for quizzes:

    1) Six quiz scores are added together to form the total quiz portion
    of the final grade.

    2) If the student has taken over six quizzes, the lowest scores are
    not used.

    3) If the student has taken under six quizzes, the lowest quiz score
    is 'padded in' as many times as it takes to make up a total of six
    quiz scores. (35)

  8. #8
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    not sure how to approach this problem, i know i have to fill array with user input, then sort array in decending order, than if array is below 6 user inputs, pad array with remaining values to get a total sum

    if you guy could kinda help me out..i got the insert, sort, can't figure out how to pad

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    You may want to talk to your teacher as I think you are misunderstanding. Imagine only four scores are entered: 5 7 6 7. Then what you need to do is total those four scores (25) and then "pad" the score by adding the lowest score (5) 2 more times giving a total of 35. This can be achieved without unnecessary padding of the array.

    How?

    Keep track of the "number of scores" entered
    Keep track of the "lowest score" so far. This requires you checking each new score against the previous "lowest score" and if it is even lower replace "lowest score" with the new score.
    Sum "number of scores" elements in the array
    If less than 6 score entered add "lowest score" * (6 - "number of scores")

    You can even do away with an array altogether.

  10. #10
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    Well yes but the values(grades) are randomly input...so how can keep track and fill the remaining spaces...see I know I have to sort a
    Because the grade amounts aren't in order...

  11. #11
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    Can you guys provide me an example....going nuts over this..thanks

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    You still do not need to pad the array.

    Get user too enter the scores
    Keep track of how many are entered (needed if less than six entered).
    Sort the array highest score first and lowest last. NOTE: if you don't pad the array with the lowest score then the last x elements will all be zero of Integer.MIN_VALUE like I said above.
    If 6 or more scores enter sum first six scores.
    If less than six scores enter then sum up the nth scores. Then the lowest score will be at position n which needs to be added to the sum as per above.

    ex
    0,0,0,0,0,0,0,0,0,0
    enter 7
    7,0,0,0,0,0,0,0,0,0
    enter 9
    7,9,0,0,0,0,0,0,0,0
    enter 4
    7,9,4,0,0,0,0,0,0,0
    enter 8
    7,9,4,8,0,0,0,0,0,0
    finished
    sort
    9,8,7,4,0,0,0,0,0,0
    Now you know that only 4 scores were entered so add up the first 4 elements. The lowest score is the 4th element so add that to the sum 2 more times.
    Done.
    No need to pad the array. It is just wasted effort.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Help with padding arrays

    I believe you need an array of size 6.

    Here are the first six scores.

    1,4,2,6,9,7
    lowest score = 1.
    Next comes 8.

    How do you set the lowest score to 2 without an array to remember the scores? Once the array is full, just sort the array in descending order and replace the last score if it is less than the current score.


    Regards,
    Jim
    Last edited by jim829; 04-04-2013 at 03:28 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    How do I find out how many entries there were? If(counter>6) then? I'm not sure how to code it...
    I understand what you're saying perfectly though

  15. #15
    sdee13 is offline Member
    Join Date
    Apr 2013
    Posts
    9
    Rep Power
    0

    Default Re: Help with padding arrays

    I meant if(counter<6)

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    In an earlier post I mentioned that you need to keep track. That means using a variable and incrementing each time user enters a valid score (greater than -1).

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    Quote Originally Posted by jim829 View Post
    I believe you need an array of size 6.
    User can enter more than 6 so an array size of 6 is a bad idea. The actual number of scores to be entered is unknown which means using an array is a bad idea completely.

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Help with padding arrays

    I don't think so. After you enter six scores you simply stop adding new ones to the array. But you sort the scores in descending order. Then you continue to read in any number of new scores and compare the latest score to the last one in the array. If it is greater than that score, you simply replace it and resort. Otherwise you read in the next score. So an array of size 6 is all you need.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help with padding arrays

    That could work but has the potential of doing a lot of sorting instead of once at the end.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: Help with padding arrays

    Another and more efficient way is to iterate over the array for every input value and record the index of the smallest value. Then just replace that if the new value is larger.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. Vertical padding problem
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 01-30-2011, 09:13 PM
  2. BAd Padding exception
    By Bverly in forum Advanced Java
    Replies: 1
    Last Post: 09-26-2010, 09:48 PM
  3. Padding Problem
    By Srikar in forum Advanced Java
    Replies: 1
    Last Post: 08-20-2010, 08:39 PM
  4. Single Component Padding
    By subb in forum AWT / Swing
    Replies: 2
    Last Post: 03-28-2009, 07:09 PM
  5. padding informaiton for PKCS#5
    By nanaji in forum Advanced Java
    Replies: 3
    Last Post: 07-31-2008, 05:36 AM

Posting Permissions

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