• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Recording and printing from a large test array

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that works, but I would like to know an easier way to record and print from a large array.
Here is what I have

 
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you mean by easier, your printRSFreqArray() method seems pretty simple to me and gets the job done.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something that could record the data for each (RR, RS IR, IS) and print for each one
Basically so I wouldn't have to do each individually.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pass the array as an argument.
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Likewise
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I was relieved when you posted a response so I tried it and got a result that only printed Iterative Sum and even that was wrong because the result was too long.
Here is what I changed



Here is some of my output
run:
StackOverflowError
StackOverflowError
0's 1
1's 2
2's 3
3's 4
4's 5
5's 6
6's 7
7's 8
8's 9
9's 10
10's 11
11's 12
12's 13
13's 14
14's 15
15's 16
16's 17
17's 18
18's 19
19's 20
20's 21
21's 22
22's 23
23's 24
24's 25
25's 26
26's 27
27's 28
28's 29
29's 30
30's 31
31's 32
32's 33
33's 34
34's 35
35's 36
36's 37
37's 38
38's 39
39's 40
40's 41
41's 42
42's 43
43's 44
44's 45
45's 46
46's 47
47's 48
48's 49
49's 50
50's 51
51's 52
52's 53
53's 54
54's 55
55's 56
56's 57
57's 58
58's 59
59's 60
60's 61
61's 62
62's 63
63's 64
64's 65
65's 66
66's 67
67's 68
68's 69
69's 70
70's 71
71's 72
72's 73
73's 74
74's 75
75's 76
76's 77
77's 78
78's 79
79's 80
80's 81
81's 82
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Stack Overflow usually means you have too many iterations in a recursive method or a loop that keeps creating new objects without releasing them.

On one hand your use of printArray() is correct but you are not changing the values in the array between subsequent calls to printArray().
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void testMethods()
{
testRS();
testIR();
testRR();
testIS();

}
Here each test is overwriting the values created by the previous test. You may want to step back at this point and consider creating a "Statistics" class that has: header, array, freq array, total time, min, max, iterations (or something like this). then you can create a Statics object for each one of your tests.

If you want to keep on with your current approach you'll have to execute each test just prior to printing each test results, i.e. test,print,print,test,print,print, etc.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I did do the test print for each and it yielded the same miles long output and output should be pretty short.
Here is how it is
StackOverflowError
StackOverflowError
0's 1
1's 2
2's 3
3's 4
4's 5
5's 6
6's 7
7's 8
8's 9
9's 10
10's 11
11's 12
12's 13
13's 14
14's 15
15's 16
16's 17
17's 18
18's 19
19's 20
20's 21
21's 22
22's 23

Here is what I am expecting
run:
StackOverflowError
StackOverflowError
0's 9081
1's 914
2's 1
5's 2
9's 1
The Recursive Sum
The Longest Time Was9
The Shortest Time Was 0
The Mean Time Was0
0's 9081
1's 914
2's 1
5's 2
9's 1
The Iterative Reverse
The Longest Time Was9
The Shortest Time Was 0
The Mean Time Was0
0's 9081
1's 914
2's 1
5's 2
9's 1
The Recursive Reverse
The Longest Time Was9
The Shortest Time Was 0
The Mean Time Was0
0's 9081
1's 914
2's 1
5's 2
9's 1
The Iterative Sum
The Longest Time Was9
The Shortest Time Was 0
The Mean Time Was0
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You realize that you're printing 'array' and not 'rsFreq' don't you?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not so tried changing it and it did the same thing lots of numbers like I posted before
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rsFreq is 200 elements long so your print out should be no longer than 200. If you expect the printArray() to be shorter that means that some of the rsFreq values are not zero when you expect them to be. Should you expect rsFreq values of zero? Do the values that you get show up in some sort of order which may lead to figuring out how you're not setting the values properly? At this point I would comment out all but one test and then put in lots of println()s to follow how the values are getting set.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I expect greater than 7000 0's
They show up like this
0's 1
1's 2
and basically are just an addition of one to the previously stated number if that makes sense
So if the number was 12 it would be 12's 13
And ok will do Thanks for the help thus far
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan O'Neill wrote:I expect greater than 7000 0's
They show up like this
0's 1
1's 2
and basically are just an addition of one to the previously stated number if that makes sense
So if the number was 12 it would be 12's 13
And ok will do Thanks for the help thus far


Well, if you're expecting 7000+ entries then it won't fit into an array of size 200.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One bit of feedback on this method


What is the purpose of the loop?
If you know the record you are after, then you could index to it directly.



It has already been pointed out that because you are using one array to store the results of ALL the tests, and you're not clearing it out between calls, then you get all the frequencies from all the tests in your results array mashed up together.
Doesn't really lend itself to a comparision.

I think conceptually it would be better if your code went something like:
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey What you're saying is that I should increase the size of rsFreq?

Stefan I am trying to record the number of times a certain number occurs in the array
I like the idea of breaking up the tests Thanks
How would I print the rsResult from each test?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just commented out all but one test and called printArray( int[] array ) with rsFreq and this is what I got:
StackOverflowError
0's 9950
10's 49
Recursive Sum
The Longest Time Was10
The Shortest Time Was 0
The Mean Time Was0


I think you need to hunt down your overflow problem.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My overflow occurs because I am trying to create an array too large for recursive sum and then I take the number that recursive sum can handle and pass it on to recursive reverse, iterative sum, and iterative reverse.
I just tried to do what you did Casey and I got the same output I keep getting. Now I am puzzled
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by hunt down my overflow problem?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I just commented out all but one test and called printArray( int[] array ) with rsFreq and this is what I got:
StackOverflowError
0's 9950
10's 49
Recursive Sum
The Longest Time Was10
The Shortest Time Was 0
The Mean Time Was0


I think you need to hunt down your overflow problem.






these are the lines I commented out.
Had to add:




 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is what I did and yet I get a completely different output than you did.

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm lost any help would be wonderful
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is what I ran. Note that I also changed currentTimeInMillis to nanoTime which I then divide by 1000 to get micro seconds. At least on my computer milli-seconds was to coarse a measurement.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carey How do I access and print the array after each test has been performed?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem is that I need to access and print the array after the completion of the recursiveSum, iterativeSum... not in the middle which is what I would be doing if I printed in the try catch
So where do I test and print from?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have this and when I try to test recursive reverse I get a long spew of numbers


And here is my output
run:
StackOverflowError
StackOverflowError
0's 9093
1's 905
2's 1
Recursive Sum
The Longest Time Was2
The Shortest Time Was 0
The Mean Time Was0
0's 9093
1's 905
2's 1
4's 1
Iterative Reverse
The Longest Time Was4
The Shortest Time Was 0
The Mean Time Was0
BUILD SUCCESSFUL (total time: 1 second)
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have lots of class member variables that you are reusing for each test, before each test they must be reset to their initial values. I suggest a reset() method to do this.
You do need to have your tests inside the try block.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should the reset method contain?
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, replace every occurrence of "Long" with "long", the built in type will work just fine and has less overhead.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For my class I am supposed to use Long
I have never used a reset method so I don't know what they should contain
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan O'Neill wrote:What should the reset method contain?


I think you need to show some effort here. As an example rsTotalTime will need to be set to zero for the results of the next test to have any meaning.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh ok now I understand what you want Thanks for the help
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan O'Neill wrote:For my class I am supposed to use Long
I have never used a reset method so I don't know what they should contain


"reset" is just a name I picked out of a hat, call it anything you want.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I have so far. Am I missing anything?

 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan O'Neill wrote:Here is what I have so far. Am I missing anything?


The whole rsFreq array.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I just create a new one?

new code:
 
Carey Brown
Bartender
Posts: 10980
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note in your output that you're getting a 'shortest time' of zero. This, of course, is impossible and is due to the granularity of your measurements. This is why I swapped in micro-seconds in place of milli-seconds; as a result I also had to increase the size of rsFreq from 200 to 2000. Maybe that for your assignment zero is OK but it also affects your Mean Time.

0's 9093
1's 905
2's 1
Recursive Sum
The Longest Time Was2
The Shortest Time Was 0
The Mean Time Was0
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that milliseconds is not small enough but since that is what the prof wants that is what I have to use unfortunately.

Is this what you mean by reset the rsFreq?

 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic