Jacob Sousie

Ranch Hand
+ Follow
since Jan 29, 2017
Jacob likes ...
Eclipse IDE Python
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
9
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jacob Sousie

Carey Brown wrote:
On line 16 you are generating random numbers from 0 through 9 inclusive. This was not part of your requirements. Additionally if you have a million of these and sort them your sorted list will probably come out as: 0 0 0 0 0 0 0 0 0 0.

I think you want random integers. Using Math.random() is now considered obsolete and has been replaced with the Random class. Then you can use its nextInt() to get a random integer.




When I changed it to that the output was this:
(P.S I didn't wait the 10 minutes it takes for the 1,000,000 to print lol)


----------------------
The original order is:
-1879149745
1757240302
1055153058
1596833882
-523450158
622502470
1349540553
-625047098
1121336350
-718157567
The sorted values are:
-1879149745 -718157567 -625047098 -523450158 622502470 1055153058 1121336350 1349540553 1596833882 1757240302
----------------------
The original order is:
1487329618
-1693883286
-823401678
2141062909
-1758188659
-1475810748
1820909735
625575232
742380899
-586603047
The sorted values are:
-1758188659 -1693883286 -1475810748 -823401678 -586603047 0 0 0 0 0
----------------------
The original order is:
-749229472
-133866982
-1136321893
497656738
496711691
1924788732
-882747006
1804858718
1480091910
2104589220
7 years ago

Carey Brown wrote:

Jacob Sousie wrote:Which looks right so far.  But I don't know why 1,000,000 wont print it's 10 sorted values!


You may not be waiting long enough for the sort to finish. Depending on the speed of your machine it may take anywhere from 5 to 15 minutes.

I don't know how to use that run method you showed me. :/  I don't know what would go inside of it for the random numbers.  Maybe if I use that it will help xD


So far so good. You've eliminated a bunch of redundancy, these 4 lines are the only redundant ones left. Put them inside run().




I see!  using run() made it so much easier!


Here's my updated code:





It's so much shorter than before hahaha.   Thank you so much!  I understand it now!  lets just hope my laptop will print the 1000000 soon lol.
7 years ago

Carey Brown wrote:
You don't want to be printing the whole array here. Remove lines 34-37. Every method should only do one thing.

You don't want to repeat your code. Move your functionality into reusable methods. For instance, your main() might look something like this:



Okay, I got rid of some of my methods and got this:



And here's the output:


The original order is:
1
2
2
3
7
3
1
1
3
3
The sorted values are:
1 1 1 2 2 3 3 3 3 7
The original order is:
9
7
4
8
6
3
0
2
2
8
The sorted values are:
0 0 0 0 0 0 0 0 0 0
The original order is:
9
8
1
1
6
3
8
2
7
0




Which looks right so far.  But I don't know why 1,000,000 wont print it's 10 sorted values!



I don't know how to use that run method you showed me. :/  I don't know what would go inside of it for the random numbers.  Maybe if I use that it will help xD
7 years ago

Carey Brown wrote:
You don't want to be printing the whole array here. Remove lines 34-37. Every method should only do one thing.

You don't want to repeat your code. Move your functionality into reusable methods. For instance, your main() might look something like this:



When I get rid of the other methods, it prints 1000 numbers.  that's why I made different methods for each case.  Because when I use the one that was meant for the number 10 it doesn't count them right.  And when I try using the run(10) methods I get an error.
7 years ago

Norm Radder wrote:

I was trying to work on the logic before I made it look pretty)  Is my logic still valid  


I don't see any of the logic expressed in comments in the code so its hard to comment on the logic without seeing what it is.



All I meant were my formulas.
7 years ago

Carey Brown wrote:

Jacob Sousie wrote:Create an array of ten random integers, and array of 1,000 random integers, and an array of a million (1,000,000) random integers.
For each array:
Display the first ten elements of the array.
Call selection sort for the array.
Display the first ten elements of the sorted array.


You don't need different sort methods for each array length, one will do the job.
You need a method: displayFirstTen( int[] array )
You can reuse that display method.



I just cut them out  now I just have one selectionSort(int[] array)     (required to use that method name)

why does it work with 10 and 1000, but not 1,000,000?  I tried debugging but it just skips over the code.
7 years ago

Carey Brown wrote:

Carey Brown wrote:

Jacob Sousie wrote:So, what I was supposed to do was create 3 randomly generated arrays.  one with 10 values, one with 1000, and one with 1,000,000. and then list ONLY the first ten elements of each, and then sort ONLY the first 10 digits of the whole array.


I see attempts to sort and print the entire array, your instructions say to do this for ONLY the first 10 [numbers] of the whole array.


Doesn't make sense to fill an array with a million numbers and then only deal with the first 10. Are you sure about those instructions? Are you paraphrasing?




I cleaned up the code a bit:


7 years ago

Liutauras Vilda wrote:Can you show us those instructions?



3. Selection sort, based on Gaddis in-chapter example. (15 points)
Create a new Java project and class. Add the following method, based on the Gaddis in-chapter example. Please remember to type in this code rather than copy-and-pasting; Word loves to mangle source code.
public static void selectionSort(int[] array) {
int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for (index = startScan + 1; index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
Create an array of ten random integers, and array of 1,000 random integers, and an array of a million (1,000,000) random integers.
For each array:
Display the first ten elements of the array.
Call selection sort for the array.
Display the first ten elements of the sorted array.







These are the instructions
7 years ago

Liutauras Vilda wrote:I'm looking to the code, well, not looking for particular mistake now, but seeing lots of same methods. Why have you created each method multiple times? Probably you know that purpose of methods ar partially that, not to repeat yourself, but rather re-use methods for the same kind of task.

Feels like you can throw away about 1/3 of the code and have same functionality.

Now there are other insteresting things. I'm not trying to look for potential mistakes, just looking for weird things now. countM() method, why you have loop starting like that? "for (int j = 999990..." Can't wrap my head.

Let's do that way - try to put some print statements on quite a few places by writing i.e.: "i'm in a for loop, my i is: + i"... values after swap are ... and you'll likely see unexpected things.



Why I used 999,990 is because I only need the first 10 out of the 1,000,000(that's what my teacher told me to do)  so I start the value at 999,990 because 1,000,000 - 999,990 = 10
it worked for my other methods(10 and 1000),  but for some reason when I try it with 1,000,000 it won't work.
7 years ago
Hey everyone! I'm back with another Java question.  So, what I was supposed to do was create 3 randomly generated arrays.  one with 10 values, one with 1000, and one with 1,000,000. and then list ONLY the first ten elements of each, and then sort ONLY the first 10 digits of the whole array.

So everything was going well.  I made a few methods for each of my cases, my RandomArray for 10 and 1000 worked just fine.  But when I got to the 1,000,000 RandomArray it wouldn't print the sorted values, and never stops running the program.  Is this because the number is so big?  

(sorry its a bit of a mess, but I was trying to work on the logic before I made it look pretty)  Is my logic still valid for the RandomArray 1,000,000   would it work in theory, but just not on my laptop?

Here's my code so far:





Thanks in advance!
7 years ago

Junilu Lacar wrote:Congratulations on finding out how to make it work. Hopefully, you'll try to come back to this again in the future and try to make it a little less complicated. There are still a number of things you can do to make this code simpler and cleaner.



How should I make it simpler and cleaner?
I should learn how to make clean code from the beginning haha.  I don't want to keep doing things a more messy and difficult way than I need to.
Thanks in advance!
7 years ago

Liutauras Vilda wrote:

Jacob Sousie wrote:here's my finished code

I'd argue on that.

Now there are lots of things to fix:
1. Class name. EvenArray? Maybe EvenNumbersArray? Former I don't think is even correct English formulation. Sounds like if you were have lots of arrays and the problem is about arrays which are even   Problem is not about the arrays, but rather about the content, which is even numbers.

2. Variable names. int1, int2. What is that? Maybe startingNumber1 and startingNumber2 at least, so it would be closer the actual intent you're trying to express in your program? [1] "Please enter the staring number: " - typo in prompt. [2] "please enter the ending number: " - typo again, sentences start with an upper case.

3. Variable name 'size'. Not looking to code have no clue what that might be. But trying right away to imagine size.length - odd on its own right.

4. Here is a lot confusion with initialisations. 'count' initialised outside loop, why? What is index? Why is the count2 starts being used and only then count, why not other way round? Do you see how many questions pops up only from few lines of code?



How about this?   this code works:






NOTE:  I mean for the evens to add the last number.  That's what my teacher wants.

Example output:



Please enter the staring number:
1
Please enter the ending number:
10
The array size is:
5
The contents of the array are:
2 4 6 8 10

That last 10 is supposed to be there intentionally.
This follows example I was given.




Thank you all so much for helping me solve this problem!
7 years ago

Fred Kleinschmidt wrote:Better, but you still have not taken into account what will happen if one or both of the input range ends might be odd.
For example, if you enter (1,7) then your current code will say n=(7-1)/2+1 which is 4, but there are only 3 evens in that range.



Oh no!  I didn't realize that!  How do I fix it without breaking the old one?
7 years ago

Fred Kleinschmidt wrote:Why are you creating an array of size zero? later on, you write which will always print zero.

You need to pay attention to what Junilu wrote - figure out an equation to calculate the number of even values.
Here's a hint: if both ends are even,
ONly after you determine that number should you create the int array.




Yeah!  That's crazy how you have to do that. lol  here's my finished code:





7 years ago

Fred Kleinschmidt wrote:You have coded this:
Why do you think you need three arrays: You only need one array, whose size is equal to the number of evens in the specified interval. So this is what you need (everything inside <...> needs to be coded ):


And the loop can be done even better, eliminating the if-test altogether, if you start at int1 if it is even, and int1+1 if it is odd, and incrementing by 2.






Here's what I've come up with.  Everything else works, but I don't know how to count the even elements of the array.  which should be 8 using 15 and 30


7 years ago