• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create a large test array  RSS feed

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my programming class we need to create a large test array of Longs to iteratively sum/reverse the array and recursively sum/reverse the array. I need some help in creating the array and where to go from there. Any help would be appreciated.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to create an array of 10 longs containing the values from 1 to 10? (hint: use a loop)

If so, you are most of the way there. Then you can decide what data do you want in your test array. Random numbers in some range? Java has a class called Random that you can use to generate random numbers.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have Is this on the right track?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. That creates a million different arrays. You need to declare the array once outside the loop. Then inside the loop you just assign a1[i] to a value.

Also, you said you wanted an array of longs, but you didn't create that data type.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks How do I reverse and sum the array?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:Thanks How do I reverse and sum the array?


Well, would you like to take a shot at it first? Or better yet, would you like to create the test array first?

Henry
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: . . . Java has a class called Random that you can use to generate random numbers.
You can use its nextLong() method. You can also create a Stream with its longs() method. I haven't done that before myself.

But it seems to be very simple. Only works in Java8+.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code that I have at this point. I getting this error at line 71
incompatible types: possible lossy conversion from long to int
Any ideas?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about this: did you really want the sum of a number of longs to be an int?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh yes I changed it to longs and now it works thanks.



Now I have to Print the mean time, the shortest and the longest time.
I know how to compute the mean, but How do I compute shortest and longest time?
I also need to compute and display a simple frequency table of the frequencies of each time value that occurred within the trials.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a recursive method rather than a loop?
You will find maximum and minimum quite easy to find; you can do all three in a single loop. I suggest you try one or the other and get it to work. If you get maximum to work, then you can get minimum with only a few tiny changes (and vice versa). The hard part is that you are not allowed to return several values from the same method. Nil desperandum! You create a class which encapsulates the valuesThen you can have your method return an instance of the Tuple class. Note that the Tuple class as I wrote it is immutable so it is thread‑safe, too.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a frequency table, you can use a Map for counting, as shown in the Java® Tutorials.
Or you can subdivide the range and do some division to reduce the numbers. For example, i / 10 will give you ranges ten at a time. It may go wrong if you have negative numbers because −9…+9 will all come out as 0. Create an (int[]) array with the ranges in and increment the elements every time you encounter a value.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it say @author Feihong Hsu?

If you have copied the code from elsewhere, please be sure to tell us where from.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:Here is the code that I have at this point. I getting this error at line 71
incompatible types: possible lossy conversion from long to int
Any ideas?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"incompatible types: possible lossy conversion from long to int "
What do you think the error means?

I shall you refer you to what Darryl Burke said before: Think about this: did you really want the sum of a number of longs to be an int?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I was editing my code to comment where I got it at. I got my question answered Thanks
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the help so far. Now my program runs, but I need to clean it up and make it more easy to read. How would you go about fixing the print__FreqArray() & printISStatistics(int iterations) so that each method(Iterative Reverse, Iterative Sum, Recursive sum, and Recursive Reverse) is able to use them?

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions I'm at a loss
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!