Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help

    Okay In my class we just learned arrays and boy am i lost anyways i got stuck in my lab I don't understand it.

    Write program Lab11.java which reads integer grades until the user enters the sentinel -1. As you read the grades, store them in an integer array. You can assume each grade is on the range 0..100. Afterwards, your program:
    ● should sort the grades read in increasing order. Use the Bubble Sort algorithm that we covered in class to sort the grades in increasing order;
    ● should compute the mode, median, average, and the standard deviation σ of the grades;
    ● should determine how many grades fall within one standard deviation σ from the average, i.e., how many grades are in the range (average σ)(average + σ);
    ● should print the sequence of grades in increasing order.
    Remember that:
    ● The mode is the grade that appears the most number of times.
    ● The median is the grade that appears right in the middle of the sequence of increasingly sorted grades.
    ● The average is the sum of all the grades divided by the number of grades.
    ●The formula for the standard deviation σ is the following:
    n-1
    Standard deviation σ = 1 Σ (gradei average)2
    √ n-1 i=0
    where n is the number of grades.
    Ok so how do I take user input for the array for a unknown size??

    i no it's first a while loop because its a sentinel control loop, and have to import scanner from there I draw a blank. Also I haven't done mode before.

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Need help

    Create an array of some reasonable size, not too big and not too small.
    Keep track of how many values have been added to the array.
    When it is full create a new array of a larger size. Once again a reasonable size. If you increase by one you will be doing a lot of overhead if there are another 100 values to be inserted.
    Copy all values from old array into new array.
    Add new value.
    Set array variable to refer to new array.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Need help

    an easy solution wud be ArrayList
    import java.util.Scanner ;
    import java.util.ArrayList ;
    class Whatever{
        public static void main(String [] args){
             Scanner s = new Scanner(System.in) ;
             ArrayList<Integer> al = new ArrayList<Integer>() ;
             while(s.hasNext()){
                int ii = s.nextInt() ;
                if (ii == -1) break ;
                al.add(ii) ;
             }
            int [] arr = al.toArray(new int[al.size()]);
       }
    }

    Hope this helps...
    But this is usually a overhead when u frequntly need to convert arraylist to array. B.O.L.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Need help

    Quote Originally Posted by dumb_terminal View Post
    an easy solution wud be ArrayList
    Hope this helps...
    You mean "would". Please spell words out in full, especially if English is not your first language. No it doesn't help as the instructions explicitly said to use an array not a List

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,967
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Need help

    Quote Originally Posted by dumb_terminal View Post
    an easy solution wud be ArrayList
    Chances are, using an ArrayList is probably not allowed, since the point of the assignment is to learn about arrays.

    This is not the first time I've seen you attempt to spoonfeed a solution but actually post incorrect information. Please stop doing that. Even if your spoonfeeding was correct, it robs the OP of the learning process. The process of working through a problem to reach that "a-ha!" moment is an incredibly important skill for a programmer to learn, and it's really hard to teach. You offering full solutions actually hurts the OP, because it takes away the ability to learn about the process of working through a problem independently.

    Not only that, but most of the time you're flat out wrong. I really urge you to stop spoonfeeding, and I would hope you would wait to "help" until you have a better grasp of the fundamentals yourself.

    Consider this a warning.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    import.util.Scanner;

    public class Lab11
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in)

    int grade = 0, total = 0, sum = 0;

    while (grade != -1);
    {
    System.out.println("Enter grade. When done enter -1")
    grade = scan.nextInt();
    total = total + grade
    sum = sum + 1
    }

    // bubble sort
    int i, j;

    for(i = 1; i <= sum; i = i + 1)
    for(j = 0; j <= sum -1 - i; j = j + 1)
    if(array[j] > array[j + 1]
    {
    temp = array[j];
    array[j] = array[j + 1];
    array[j + 1] = temp;
    }

    // average
    int index;
    double avg;

    for(index = 0; index < total; index = index + 1)
    if sum > 0 sum = sum + array[index]
    {
    avg = (double)total/sum
    return avg;
    }
    else
    return does not compute;





    }
    }
    this is what i have so far sorry I have been busy with other stuff so i haven't complied because my computer will not let me. Anyways can you spot any errors i need to fix

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    Thanks, but an array list doesnt help if i dont understand arrays still. My professor is not very good at teaching so I am trying to learn on my own and I'm not doing such a great job.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,967
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Need help

    this is what i have so far sorry I have been busy with other stuff so i haven't complied because my computer will not let me. Anyways can you spot any errors i need to fix
    Your first step is to get a compiler working then.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    Quote Originally Posted by KevinWorkman View Post
    Your first step is to get a compiler working then.
    im going to my school to do so but before then trying to get as little errors as possible before I ask my old professor for some help and tips

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,967
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Need help

    A compiler is going to be able to tell you what's wrong with your code much better than we can. You've got a ton of syntax errors. Check your semicolons, parenthesis, equality operators, everything.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!