# Need help

• April 7th, 2011, 03:24 PM
Zerro900
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.

Quote:

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.
• April 7th, 2011, 06:22 PM
Junky
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.
Set array variable to refer to new array.
• April 12th, 2011, 03:50 PM
dumb_terminal
Re: Need help
an easy solution wud be ArrayList
Code java:

```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.
• April 12th, 2011, 10:50 PM
Junky
Re: Need help
Quote:

Originally Posted by dumb_terminal
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
• April 13th, 2011, 08:01 AM
KevinWorkman
Re: Need help
Quote:

Originally Posted by dumb_terminal
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.
• April 13th, 2011, 12:16 PM
Zerro900
Re: Need help
Quote:

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")
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
• April 13th, 2011, 12:45 PM
Zerro900
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.
• April 13th, 2011, 01:00 PM
KevinWorkman
Re: Need help
Quote:

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.
• April 13th, 2011, 01:07 PM
Zerro900
Re: Need help
Quote:

Originally Posted by KevinWorkman
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
• April 13th, 2011, 01:17 PM
KevinWorkman
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.