This is just a throwaway, nothing too important. Just to please my appetiate.
So, in school we got a task, where we had to create an array with 20 slots, and fill it with random numbers anywhere between 1 and 100, with a random number generator, something like this like this:
int arr[] = new int[20];
for (i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100) + 1
}
Then we had to find the largest in the line-up that this random number generator made. Like:
int maximum = arr[0];
/*a random integer that's initialized after the line-up is made,
*since initializing ahead of that would be problematic,
*and then starts with the first value in the array
*/
for (i = 0; i < arr.length; i++) {
if (maximum < arr[i]) {
maximum = arr[i]
}
}
Then we just print the contents of the array, and the newly found maximum number from the array. I also got creative a little bit here; purely for asthetic reasons, you'll see:
System.out.println("The line-up of random numbers generated into the array:");
for (i = 0; i < arr.lenghth; i++) {
if (i == arr.length - 1) {
System.out.println(arr[i]);
} else {
System.out.print(arr[i] + ", ");
}
}
(If you didn't catch it; it prints all elements of the array in the same line, seperated with a comma and space. Then, when it reaches the end, it prints it in the same line, but anything beyond will be in the next line, and it also doesn't print a comma and/or space. Purely for asthetics.)
This is where I got even more creative. So, I wanted to put all the integers into growing order. It was fairly trivial with Insertion Sorting - after some research. (If you'll excuse me, I'll skip over typing the code out for that, because 1) it worked, 2) it has some non-english phrases, as I'm not natively english, therefore my variable naming is also not in english, especially if this was meant to be sent back to the teacher, 3) frankly the built in code sampler in stackoverflow isn't great - coming from NetBeans -, but whatever. The point is, no truble here.)
My trouble started, when I saw, that whole bunch of the numbers were the same. I figured "How about we change that." Aaaaand total disaster. Not only did it not change the numbers, it made EVEN MORE. That's karma, if you ask me.
Anyways, I went "Okay, let's do some research." And my research machine, Le Google, didn't have the correct, or otherwise had the EXACT same solution, as I was trying. That is:
for (i = 0; i < arr.length; i++) {
for (j = i+1; j < arr.length; j++)
if (arr[i] == arr[j]) {
arr[i] = (int) (Math.random() * 100) + 1;
}
}
}
Now, before you go "Just change >>arr[i]<< to >>arr[j]<<", well, no dice there either.
As I was thinking about it, I thought "Why can't I just go through all of the values one-by-one, then go through ALL of the values again, and compare them to the value I initially picked?" But then realized, that this algorythm does just that, and the problem lies elsewhere. But for the love of my life, I can't figure it out.
Also, if anyone's interested, here's the entire code; but without the broken code:
package searchformax;
public class SearchForMax {
public static void main(String[] args) {
//initialization
int arr[] = new int[20]; //array, and it's set length
//random number generation
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100) + 1;
}
int maximum = arr[0];
/*a random integer that's initialized after the line-up is made,
*since initializing ahead of that would be problematic,
*and then starts with the first value in the array
*/
//the search for the actual maximum number
for (int i = 0; i < arr.length; i++) {
if (arr[i] > maximum) {
maximum = arr[i];
}
}
//userfriendliness
System.out.println("The line-up of random numbers generated into the array:");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.println(arr[i]);
} else {
System.out.print(arr[i] + ", ");
}
}
//presenting the largest nummber
System.out.println("The largest number from these: " + maximum);
}
}
Now, again, this is not strictly necessary, just to please my appetiate for more knowledge - seriously, we are being so slow, especially since, in my country, every high-school student has been sent home for home-school; yes, you could say "Be careful with self-education.", but, don't worry about it. And, when it came time to send I wasn't gonna send it with the extra code, and stripped it out when I did. Still, I'm curious, as to where my problem lies, or if it's something that is unfixable due to the nature of Java, or something.
Aucun commentaire:
Enregistrer un commentaire