Skip to main content
gamedev.net
🔒 Locked

Newbie java ready help

Started by ryguyrocks 4 replies 1.2k views
Original Post
ryguyrocks
ryguyrocks
Hey, I just started my gr 11 java course, and I need some help with something. And no moderators, im not asking for answers. I need to know how to sort number arrays. Im generating 200 numbers from 1-100 and they need to be sorted. I know you have to use 2 for Loops, im just not sure what to put in them. This is what I have so far; P.S- Look at the code with the ***, when I run it it prints the numbers, but says It has gone beyond boundaries or something like that, in essence, it doesnt work. // The "RandomStats" class. import java.awt.*; import hsa.Console; public class RandomStats { static Console c; // The output console public static void main (String [] args) { c = new Console (); int random [] = new int [200]; int first = 0; int second = 0; int third = 0; int fourth = 0; int fifth = 0; for (int n = 1 ; n <= 200 ; n++) { random [n] = (int) (Math.random () * 100) + 1;************ c.print (random [n] + " "); if (random [n] == 1 && random [n] <= 20) { first += 1; } else if (random [n] == 21 && random [n] <= 40) { second += 1; } else if (random [n] == 41 && random [n] <= 60) { third += 1; } else if (random [n] == 61 && random [n] <= 80) { fourth += 1; } else { fifth += 1; } } c.print (first); c.print (second); c.print (third); c.print (fourth); c.print (fifth); // Place your program here. 'c' is the output console } // main method
rip-off
rip-off
Arrays in Java are zero indexed, which means that the first index is 0. A 10 element array has the following valid indices:
Quote:

0 1 2 3 4 5 6 7 8 9

Consider a for loop in the following form:
int [] array = new int[10];for(int i = 1 ; i <= 10 ; i++){    // do something with array}

Can you see that the indices that this loop generates will not match the valid indices in I showed above?

Now see if you can apply that knowledge to your code. [smile]
OrangyTang
OrangyTang
Quote:
Original post by ryguyrocks
I need to know how to sort number arrays. Im generating 200 numbers from 1-100 and they need to be sorted.

If that's all you want, then you can use Arrays.sort().

Alternativly if your task is to write the sorting yourself then you can always make a copy of the array and use Arrays.sort to check if your own sorting is working correctly.
Zahlman
Zahlman
General stuff:

What are the counts 'first, second, third, fourth, fifth' for? Have you considered using an array of count values instead? Have you considered using a simpler mathematical relationship to determine which should be incremented? (If you use the array, then you just write math to determine which index to use. Hint: integer division in Java discards the remainder.) Also, instead of putting the number into an array and repeatedly "pulling it out" to do more math, consider setting up a local variable, doing the math and then putting the variable into the array (to avoid repeatedly writing the indexing logic).

Oh, and you might want to delete that comment at the end :) Actually, right now none of the comments there are useful, nor serving the purpose that comments are *supposed* to. A lot of teachers really don't know how to explain commenting properly, though :/
anemian
anemian
Do it by hand and take no short cuts.

You're going to literally start at index 0 go through the whole list and find the smallest/largest value.

then you're going to be at index 1 go through the whole list from the index 1 and find the smallest/largest value.


Take like 20 pieces of paper and do that by hand. then write out exactly how you did that on a piece of paper assuming that the next person to do it has to follow your directions exactly.



To be perfectly honest the last people on earth that are going to use real books, paper, and pencils will be programmers.
The only valid measurement of code quality: WTFs/minute
cignox1
cignox1
I din't understand one thing: are you supposed to write the code WITHOUT any reference (i.e. the dercription of the algorithm in a book)?. Because otherwise you can find plenty of examples on internet, are you allowed to look at them? The simplest form is just a matter of a few lines of code...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.