For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2005 > help please









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help please
CPSCmajor

2005-10-27, 3:57 am

I am trying to write a program that asks the user for how many random
numbers to generate and then return the number of times each occurs.
The size of the array I want to create must be based on user input. I
have the heart of the code written, I just cannot get it to display
what I want. Any help is appreciated, my code is below:

import java.util.*;
public class Array
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random r = new Random();
int numbers = 0;
int values = 0;
int i = 0;
int size = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numbers = s.nextInt();
System.out.print("What is the number of values? ");
values = s.nextInt();
int[] data = new int[size];
for (i = 0; i < data.length; i++)
{
data[i] = r.nextInt();
count++;
System.out.println(i + " " + count);
}
}
}

fsa71@mail.ru

2005-10-27, 3:57 am

You asks user for 'numbers' and 'values' but creates data = new int
[size] where size==0. So your 'for' is newer executed. :)

CPSCmajor

2005-10-27, 3:57 am


fsa71@mail.ru wrote:
> You asks user for 'numbers' and 'values' but creates data = new int
> [size] where size==0. So your 'for' is newer executed. :)


ok, i changed size to values, and my program will execute, but it
displays the following:

How many numbers do you want to generate? 1000
What is the number of values? 10
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

The correct answer should be:

How many numbers do you want to generate? 1000
What is the number of values? 10
0 78
1 101
2 118
3 97
4 103
5 102
6 112
7 91
8 94
9 104

Maybe I am just on the directions:

Make an array with one element for each possible outcome. Set each
element to 0. Then keep calling the random number generator. If it
returns a value v, then increment the counter belonging to v.
After all numbers have been generated, print the counters.

Bjorn Abelli

2005-10-27, 7:02 pm

"CPSCmajor" wrote...

> ok, i changed size to values,


[snip]

> Maybe I am just on the directions:


I would guess so...

When you're doing a homework, or other assignment, it's crucial that you
understand the task at hand.

The first thing to do, is to strip it down to smaller tasks.

> Make an array with one element for each possible outcome.


That must be to create an array with the length of "values".

> Set each element to 0.


(Which is done by default...)

> Then keep calling the random number generator.


And how many times would you do that?

That's the first loop that will iterate "number" of times... ;-)

> If it returns a value v, then increment the
> counter belonging to v.


I hope you know how to increment a specified element in an array...
(within the loop)

> After all numbers have been generated,
> print the counters.


And here comes a *second* loop, when the first one has finished...

It simply iterates through the array and prints out the result.

What you've done is to try to get it all into *one* iteration, which will
fail, as the two iterations in the assignment has two different purposes.
The first loop "fills" the array, the second loop prints out the results...

I hope this hasn't helped you too much, but just enough for you to make the
assignment on your own.

// Bjorn A


CPSCmajor

2005-10-28, 3:57 am


I hope you know how to increment a specified element in an array...
(within the loop)

Well I thought I knew how to do it, but my program still isn't working,
so maybe I don't?

Here is my code again:

/**
* This is a program that asks the user for how many random numbers to
generate and then return the number of times each occurs.
*
* @author Brandon Merritt
* @version 10-17-05
*/
import java.util.*;
public class Array
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random r = new Random();
int numbers = 0;
int values = 0;
int i = 0;
int size = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numbers = s.nextInt();
System.out.print("What is the number of values? ");
values = s.nextInt();
int[] data = new int[values];
for (i = 0; i < values; i++)
{
data[i] = r.nextInt();
count++;
System.out.println(i + " " + count);
}
}
}

Also I don't understand how to make it do two iterations or where is
should put the int numbers into the code (I put it in the for loop ( i
< numbers ) and when I run it an exception is throw. Do I need to make
another loop or something I am so ??

jonck@vanderkogel.net

2005-10-28, 7:02 pm

> Here is my code again:

I don't think you actually understand your assignment. You have to run
the random generator a certain number of times and count how many times
each possible value comes up. So for example, let's say the outcome
range is set to be between 0 and 1 (so only 0 and 1 are possible
outcomes) and you have to generate 5 numbers. Let's say the random
number generator outputs: 0, 0, 0, 1, 1. Your instructor now wants to
see an array with values {3, 2}.

What you are doing in your loop is:
generate a random number
each time a random number is generated increase the variable "count" by
1
output the variable "i" (which keeps track of how many times to loop)
and the variable "count"

So you're not doing anything with the random number generated!

In other words, you want to count how many times a certain number was
generated, you're counting how many random numbers you have generated.

HTH, Jonck

CPSCmajor

2005-10-28, 7:02 pm

right, I understand the assignment...I just don't know how to do it. I
realize that I need to count the number of times each number shows up
in the random number generation. However, I don't know how to make the
count do this. That is why I am so .

jonck@vanderkogel.net

2005-10-28, 7:02 pm

> right, I understand the assignment...I just don't know how to do it. I
> realize that I need to count the number of times each number shows up
> in the random number generation. However, I don't know how to make the
> count do this. That is why I am so .


Ok. I don't think I'll be doing you a favor by doing it for you, so
I'll give you a few (hopefully) helpful hints to get you on track.

- you ask the user how many numbers he wants generated, so you're going
to at least have the loop iterate that many times (in your snippet the
variable "numbers")
- Another large mistake lies here: data[i] = r.nextInt(); If you look
closely you'll see that you're actually storing the generated number
itself, while you don't want to do that. You want to evaluate the
number that's generated and increase the counter for that particular
value...

See if, with these hints, you can now write some code that's closer to
a proper solution. If you run into more problems come back here.

CPSCmajor

2005-10-28, 7:02 pm

Hey,
What you are saying seems to make sense, but then when I try to write
the code I am clueless. Here is what I have so far.

import java.util.*;
public class Array
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random r = new Random();
int numbers = 0;
int values = 0;
int nums = 0;
int i = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numbers = s.nextInt();
System.out.print("What is the number of values? ");
values = s.nextInt();
int[] data = new int[values];
for (i = 0; i < values; i++)
{
nums = r.nextInt() * numbers;
data[i] = count;
count++;
System.out.println(i + " " + count);
}
}
}

Am I on the right track at all? I realize now that I was storing the
generated number itself, but if I'm not supposed to do that then did
how do I use it? Also I don't know how to evaluate the number that's
generated (I know to increase it by saying count++;). The evaluation is
really where I am lost though. Do I need an if statement or something?
I don't think this is supposed to be this hard haha.

Oliver Wong

2005-10-28, 7:02 pm


"CPSCmajor" <bmerritt1987@gmail.com> wrote in message
news:1130525106.213708.5770@g44g2000cwa.googlegroups.com...
> Hey,
> What you are saying seems to make sense, but then when I try to write
> the code I am clueless. Here is what I have so far.
>
> import java.util.*;
> public class Array
> {
> public static void main(String[] args)
> {
> Scanner s = new Scanner(System.in);
> Random r = new Random();
> int numbers = 0;
> int values = 0;
> int nums = 0;
> int i = 0;
> int count = 0;
> System.out.print("How many numbers do you want to generate? ");
> numbers = s.nextInt();
> System.out.print("What is the number of values? ");
> values = s.nextInt();
> int[] data = new int[values];
> for (i = 0; i < values; i++)
> {
> nums = r.nextInt() * numbers;
> data[i] = count;
> count++;
> System.out.println(i + " " + count);
> }
> }
> }
>
> Am I on the right track at all? I realize now that I was storing the
> generated number itself, but if I'm not supposed to do that then did
> how do I use it? Also I don't know how to evaluate the number that's
> generated (I know to increase it by saying count++;). The evaluation is
> really where I am lost though. Do I need an if statement or something?
> I don't think this is supposed to be this hard haha.


If I were you, I'd give the variables really long, but descriptive
names. You're miss-using a variable somewhere, and perhaps if the variable
names really explicitly said what data they contained, the mistake would
become clear.

For example, change the name of the variable "values" to
"numberOfValues" or "rangeOfValues" or
" maximalValueThatRandomNumberGeneratorWil
lGenerate". Similarly for
"numbers".

- Oliver


Roedy Green

2005-10-28, 7:02 pm

On 28 Oct 2005 08:18:14 -0700, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>right, I understand the assignment...I just don't know how to do it. I
>realize that I need to count the number of times each number shows up
>in the random number generation.


You mean "integer" not "number". I think some reading may have
thought you were dealing with random doubles over some range.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
CPSCmajor

2005-10-28, 7:02 pm

Well I renamed numbers to numbersToGenerate and values to
numberOfValues...the mistake still isn't clear. I don't know how to
store the count in the array or evaluate the generated number.

Oliver Wong

2005-10-28, 7:02 pm


"CPSCmajor" <bmerritt1987@gmail.com> wrote in message
news:1130531746.191786.142810@g47g2000cwa.googlegroups.com...
> Well I renamed numbers to numbersToGenerate and values to
> numberOfValues...the mistake still isn't clear. I don't know how to
> store the count in the array or evaluate the generated number.
>


Okay, I've taken your code and renamed various things around. I didn't
rename "i" because it's a standard idiom to use i as a name for a for-loop
variable. I didn't rename "data" and "count" because I'm not sure yet what
purpose they serve in your program. Here's the code I ended up with:

<code>
import java.util.*;

public class RandomNumberDistributionChecker {
public static void main(String[] args) {
Scanner userInputSource = new Scanner(System.in);
Random randomNumberGenerator = new Random();
int numberOfNumbersToGenerate = 0;
int upperBoundOnRandomlyGeneratedValues = 0;
int randomlyGeneratedNumber = 0;
int i = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numberOfNumbersToGenerate = userInputSource.nextInt();
System.out.print("What is the number of values? ");
upperBoundOnRandomlyGeneratedValues = userInputSource.nextInt();
int[] data = new int[upperBoundOnRandomlyGeneratedValues]
;
for (i = 0; i < upperBoundOnRandomlyGeneratedValues; i++) {
randomlyGeneratedNumber = randomNumberGenerator.nextInt()
* numberOfNumbersToGenerate;
data[i] = count;
count++;
System.out.println(i + " " + count);
}
}
}
</code>

Now tell me if you see anything strange with this code. My understanding
is you're asking the user how many random numbers to generate, and then
you'll eventually generate that many numbers. Is that what the code is
doing?

- Oliver


CPSCmajor

2005-10-28, 7:02 pm


Oliver Wong wrote:
> "CPSCmajor" <bmerritt1987@gmail.com> wrote in message
> news:1130531746.191786.142810@g47g2000cwa.googlegroups.com...
>
> Okay, I've taken your code and renamed various things around. I didn't
> rename "i" because it's a standard idiom to use i as a name for a for-loop
> variable. I didn't rename "data" and "count" because I'm not sure yet what
> purpose they serve in your program. Here's the code I ended up with:
>
> <code>
> import java.util.*;
>
> public class RandomNumberDistributionChecker {
> public static void main(String[] args) {
> Scanner userInputSource = new Scanner(System.in);
> Random randomNumberGenerator = new Random();
> int numberOfNumbersToGenerate = 0;
> int upperBoundOnRandomlyGeneratedValues = 0;
> int randomlyGeneratedNumber = 0;
> int i = 0;
> int count = 0;
> System.out.print("How many numbers do you want to generate? ");
> numberOfNumbersToGenerate = userInputSource.nextInt();
> System.out.print("What is the number of values? ");
> upperBoundOnRandomlyGeneratedValues = userInputSource.nextInt();
> int[] data = new int[upperBoundOnRandomlyGeneratedValues]
;
> for (i = 0; i < upperBoundOnRandomlyGeneratedValues; i++) {
> randomlyGeneratedNumber = randomNumberGenerator.nextInt()
> * numberOfNumbersToGenerate;
> data[i] = count;
> count++;
> System.out.println(i + " " + count);
> }
> }
> }
> </code>
>
> Now tell me if you see anything strange with this code. My understanding
> is you're asking the user how many random numbers to generate, and then
> you'll eventually generate that many numbers. Is that what the code is
> doing?
>
> - Oliver


Yes, I thought it was what I was generating? r.nextInt() *
1000-Wouldn't that generate 1000 random numbers? Maybe I don't need to
assign this value to a variable? I seem to be making less progress...

Oliver Wong

2005-10-28, 7:02 pm


"CPSCmajor" <bmerritt1987@gmail.com> wrote in message
news:1130533878.362316.309600@g14g2000cwa.googlegroups.com...
>
> r.nextInt() *
> 1000-Wouldn't that generate 1000 random numbers?


No, here's the documentation for the nextInt() method:

<quote>
Returns the next pseudorandom, uniformly distributed int value from this
random number generator's sequence. The general contract of nextInt is that
one int value is pseudorandomly generated and returned. All 2^32 possible
int values are produced with (approximately) equal probability.
</quote>

So your first "r.nextInt()" is evaluated, and that returns a random
number between -2147483648 and 2147483647, and then you take whatever that
value is and multiply it by 1000. So let's say the random number generator
returned "3", you'd get the value 3000 back.

BTW, just in case you didn't know, you can read the full documentation
for the Random class at
http://java.sun.com/j2se/1.5.0/docs...til/Random.html though it gets
sort of technical at times.

> I seem to be making less progress...


You were making a mistake but you didn't know what the mistake was. Now
you're starting to understand the mistake. Soon you'll be on your way to
fixing the mistake. I think that still counts as progress.

- Oliver


jonck@vanderkogel.net

2005-10-28, 7:02 pm

> BTW, just in case you didn't know, you can read the full documentation
> for the Random class at
> http://java.sun.com/j2se/1.5.0/docs...til/Random.html though it gets
> sort of technical at times.


In fact, I think that you should take a look at the API documentation
of Random as soon as you can, before you do anything else. The
nextInt(int n) section contains an important clue...

CPSCmajor

2005-10-28, 7:02 pm


Oliver Wong wrote:
> "CPSCmajor" <bmerritt1987@gmail.com> wrote in message
> news:1130533878.362316.309600@g14g2000cwa.googlegroups.com...
>
> No, here's the documentation for the nextInt() method:
>
> <quote>
> Returns the next pseudorandom, uniformly distributed int value from this
> random number generator's sequence. The general contract of nextInt is that
> one int value is pseudorandomly generated and returned. All 2^32 possible
> int values are produced with (approximately) equal probability.
> </quote>
>
> So your first "r.nextInt()" is evaluated, and that returns a random
> number between -2147483648 and 2147483647, and then you take whatever that
> value is and multiply it by 1000. So let's say the random number generator
> returned "3", you'd get the value 3000 back.
>
> BTW, just in case you didn't know, you can read the full documentation
> for the Random class at
> http://java.sun.com/j2se/1.5.0/docs...til/Random.html though it gets
> sort of technical at times.
>
>
> You were making a mistake but you didn't know what the mistake was. Now
> you're starting to understand the mistake. Soon you'll be on your way to
> fixing the mistake. I think that still counts as progress.
>
> - Oliver


Ok thanks, I realize now that I do not need to multiply by 1000. I know
that when I do r.nextInt() that it will call a random number, and the
loop will execute i < numberOfValues times (is this right?). I seem to
still be about the part of the question that says "If it
returns a value v, then increment the counter belonging to v. After all
numbers have been generated, print the counters." I don't know what I
need to set data[i] equal to. I had it equal to generatedNumbers, and I
put that part in System.out.println just to see what it would do, and
it displayed 10 random numbers. I guess I just don't know how to count
if it returns a value v.

CPSCmajor

2005-10-28, 7:02 pm

?? I'm not using the nextInt(int n) form, I am using the nextInt()
form.

jonck@vanderkogel.net

2005-10-28, 9:56 pm

> ?? I'm not using the nextInt(int n) form, I am using the nextInt()
> form.


You are setting an upper bound on the random numbers right? Hence, take
a look at nextInt(int n)...

CPSCmajor

2005-10-28, 9:56 pm

Ok...I looked at it and if I put nextInt(int numberOfValues) it returns
a .class error? I don't see what this has to do w/ the problem.

Roedy Green

2005-10-28, 9:56 pm

On 28 Oct 2005 13:35:46 -0700, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

> I don't know how to
>store the count in the array or evaluate the generated number.


Surprise, you have already proved you do.! You showed us code where
you stored a number in an array. So you proved you know the basic bits
needed.

Counting is a matter of adding one to what in already in a bin. You
can do that with + or ++.

Select which bin. Fish the value out. Add 1. Put it back into the same
bin.. Once you have that working use the ++ shortcut.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2005-10-28, 9:56 pm

On 28 Oct 2005 14:53:02 -0700, jonck@vanderkogel.net wrote, quoted or
indirectly quoted someone who said :

>In fact, I think that you should take a look at the API documentation
>of Random as soon as you can, before you do anything else. The
>nextInt(int n) section contains an important clue...


or see http://mindprod.com/jgloss/gotchas.html#RANDOM
for a less formal introduction.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2005-10-28, 9:56 pm

On 28 Oct 2005 16:22:47 -0700, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>?? I'm not using the nextInt(int n) form, I am using the nextInt()
>form.


And which one gives you numbers over a tractable range?

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green

2005-10-28, 9:56 pm

On 28 Oct 2005 17:25:22 -0700, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Ok...I looked at it and if I put nextInt(int numberOfValues) it returns
>a .class error? I don't see what this has to do w/ the problem.

because you are invoking a method, not defining it. Int only goes in
declarations.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
CPSCmajor

2005-10-28, 9:56 pm

"Select which bin. Fish the value out. Add 1. Put it back into the same

bin.. Once you have that working use the ++ shortcut."

I don't know how to do this?? I am so I have been working on
this for so long.

jonck@vanderkogel.net

2005-10-29, 7:57 am

> I don't know how to do this?? I am so I have been working on
> this for so long.


If you don't understand how to handle a problem, simplify it to the
point that you do understand (a technique that you will be using the
rest of your life).

In this case, it would appear that your first step should be: how do I
generate a thousand random numbers. Once you have solved that, you want
to get to the evaluation part.
So how do you tackle that. Well, let's look at your requirements. You
have to set an upper bounds on the random number generated. So take the
most elementary upper bound you can think of: a random generator that
only generates zeros; put this into your loop and count how many zeros
are being produced.
Then, when you have solved that, allow your random generator to produce
zeros and ones and count how many of each of those are produced. Then
let your random generator produce zeros, ones and twos, etc... Repeat
this process untill you understand precisely what you are doing. Once
you have reached that point you will be able to extrapolate so your
program will be capable of handling any given upper bounds.

Michael Powe

2005-10-29, 7:02 pm

>>>>> "CPSCmajor" == CPSCmajor <bmerritt1987@gmail.com> writes:

CPSCmajor> I am trying to write a program that asks the user for
CPSCmajor> how many random numbers to generate and then return the
CPSCmajor> number of times each occurs. The size of the array I
CPSCmajor> want to create must be based on user input. I have the
CPSCmajor> heart of the code written, I just cannot get it to
CPSCmajor> display what I want. Any help is appreciated, my code
CPSCmajor> is below:

Here is the core of the issue:

You are writing code without having first solved the problem. Your
code "doesn't work" because it's solving the wrong problem.

The best way to solve a problem like this is with a whiteboard, on
which you can diagram your problem -- by diagramming and
rediagramming, you will get a clear understanding of what needs to be
done and eventually, from your diagram you will learn what you need
to write in your code. You don't have to know UML to diagram a
problem, you just have to be able to draw circles and squares and
connect them with arrows. Diagramming will force you to VISUALIZE the
process and ABSTRACT those portions of the process that need to be
coded. It will force you to ASK the QUESTIONS that need to be asked
about the problem, in order to arrive at the proper abstractions.

If you don't have or can't afford a $20 whiteboard, buy a $1 tablet of
paper and scribble that up.

I know what I'm talking about because I've been through this myself,
many times. ;-) Always, always, always ask yourself "what is the
question?", followed by "Am I answering the question that was asked?"
I have reams of functional code than answered the wrong questions, and
therefore was useless.

Thanks.

mp

--
'cat' is not recognized as an internal or external command,
operable program or batch file.
CPSCmajor

2005-10-29, 7:02 pm

How do you make a random number generator that generates only zeros? I
thought that was the whole point of the random number generator, so
that the number would be random? Also I don't think I know how to count
how many of each are produced. Can you give me an example of code or
something, I am not making any sense out of just telling me to do
stuff.

Roedy Green

2005-10-30, 3:57 am

On 29 Oct 2005 12:38:58 -0700, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Also I don't think I know how to count
>how many of each are produced. Can you give me an example of code or
>something, I am not making any sense out of just telling me to do
>stuff.


NO! You totally misunderstand the purpose of this exercise. It is not
to get he answer to hand in. It is to modify YOUR brain cell wiring so
that you can handle problems like this ON YOUR OWN in future.

It is like asking your gym coach to jump for you to help you train.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli

2005-10-30, 7:56 am


"CPSCmajor" wrote...
>
> I hope you know how to increment a specified element in an array...
> (within the loop)
>
> Well I thought I knew how to do it, but my program still
> isn't working, so maybe I don't?


Obviously not.

Let's try with a simple one...

If you've got an array of "values" ranging from 0 to 3, you can use an array
like this.

int[] data = {0,0,0,0};

Let's say that you got the random generator to generate the value "2", then
it would be the "bucket" corresponding to that value you'd need to
increment, e.g.:

data[2] = data[2] + 1; // Incrementation simply means "add one"

There's a shorthand for incrementation as well:

data[2]++;

Now, let's look at your code...

> import java.util.*;
> public class Array
> {
> public static void main(String[] args)
> {
> Scanner s = new Scanner(System.in);
> Random r = new Random();
> int numbers = 0;
> int values = 0;
> int i = 0;
> int size = 0;
> int count = 0;
> System.out.print("How many numbers do you want to generate? ");
> numbers = s.nextInt();
> System.out.print("What is the number of values? ");
> values = s.nextInt();
> int[] data = new int[values];


So far, so good...

But what happens then?

How many random numbers did you want to generate?

You should exchange "values" for something else...

> for (i = 0; i < values; i++)
> {


And the next one is horrifying!

Wasn't the assignment to *increment* the "bucket", depending on what value
you've got generated?

What you did was to *replace* the current value of the bucket with the
random value...

> data[i] = r.nextInt();


The "count" variable is completely useless here...

> count++;
> System.out.println(i + " " + count);


....as it just tells you how many times you've iterated. You already have a
variable for that (i).

> }
> }
> }
>
> Also I don't understand how to make it do two iterations
> or where is should put the int numbers into the code
> (I put it in the for loop ( i < numbers ) and when I run
> it an exception is throw.


I guess it throws an Exception when you exceed "the number of buckets".

That's because you're trying to use the loop for two different things, which
are entirely unrelated.

> Do I need to make
> another loop or something I am so ??


As I said before, you need two loops...

In the first loop, you increment the values in the array, depending on what
values you generate.

The second loop is for the printout of the results...

// Bjorn A


CPSCmajor

2005-10-30, 7:00 pm


Bjorn Abelli wrote:
> "CPSCmajor" wrote...
>
> Obviously not.
>
> Let's try with a simple one...
>
> If you've got an array of "values" ranging from 0 to 3, you can use an array
> like this.
>
> int[] data = {0,0,0,0};
>
> Let's say that you got the random generator to generate the value "2", then
> it would be the "bucket" corresponding to that value you'd need to
> increment, e.g.:
>
> data[2] = data[2] + 1; // Incrementation simply means "add one"
>
> There's a shorthand for incrementation as well:
>
> data[2]++;
>
> Now, let's look at your code...
>
>
> So far, so good...
>
> But what happens then?
>
> How many random numbers did you want to generate?
>
> You should exchange "values" for something else...
>
>
> And the next one is horrifying!
>
> Wasn't the assignment to *increment* the "bucket", depending on what value
> you've got generated?
>
> What you did was to *replace* the current value of the bucket with the
> random value...
>
>
> The "count" variable is completely useless here...
>
>
> ...as it just tells you how many times you've iterated. You already have a
> variable for that (i).
>
>
> I guess it throws an Exception when you exceed "the number of buckets".
>
> That's because you're trying to use the loop for two different things, which
> are entirely unrelated.
>
>
> As I said before, you need two loops...
>
> In the first loop, you increment the values in the array, depending on what
> values you generate.
>
> The second loop is for the printout of the results...
>
> // Bjorn A


Well I tried it again, and here is my code:

import java.util.*;
public class Array
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random r = new Random();
int numbersToGenerate = 0;
int numberOfValues = 0;
int generatedNumbers = 0;
int i = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numbersToGenerate = s.nextInt();
System.out.print("What is the number of values? ");
numberOfValues = s.nextInt();
int[] data = new int[numberOfValues];
for (i = 0; i < data.length; i++)
{
generatedNumbers = r.nextInt();
data[i]++;
System.out.println(i + " " + data[i]);
}
}
}

Still not right...

Bjorn Abelli

2005-10-30, 7:00 pm


"CPSCmajor" wrote...
> Bjorn Abelli wrote:
>
> Well I tried it again, and here is my code:


You don't seem to understand the pretty direct hints I gave you, if you read
them at all...


I'll repeat them, in context...

> import java.util.*;
> public class Array
> {
> public static void main(String[] args)
> {
> Scanner s = new Scanner(System.in);
> Random r = new Random();
> int numbersToGenerate = 0;
> int numberOfValues = 0;
> int generatedNumbers = 0;
> int i = 0;
> int count = 0;
> System.out.print("How many numbers do you want to generate? ");
> numbersToGenerate = s.nextInt();
> System.out.print("What is the number of values? ");
> numberOfValues = s.nextInt();
> int[] data = new int[numberOfValues];


So far, so good...

But here it comes...

You *still* use the same number of iterations as before, instead of the
number of iterations your user asks for...

What do you think data.length equals to (Hint: How did you initialize it?)

> for (i = 0; i < data.length; i++)
> {


The next line is okey, but how will you use it?
In your code, nowhere at all...

> generatedNumbers = r.nextInt();


In the next line, you've finally got an incrementation, but you incremented
the wrong bucket...

*Which* bucket do you need to increment, to afterwards be able to see how
many hits you had on each number?

> data[i]++;


And you *didn't* listen to what I said about separating the printout to a
*second* loop, to do after you're done with the randomly generated
numbers...

> System.out.println(i + " " + data[i]);
> }
> }
> }
>
> Still not right...


I see that...

Another thing to do when you're learning to program is to *NOT*
try-and-error. You need a distinct plan for each step you take. Each change
in the code must be thought of before, with a clear understanding on what
that change will do.

If you now still don't get my hints, try to draw a simple graph over the
algorithm, in a flowchart, structured graph, pseudo code or whatever tools
you've got at school.

When that's done, use that graph to test some values on paper, *before* your
next attempt to code it.

// Bjorn A


CPSCmajor

2005-10-30, 7:00 pm

The next line is okey, but how will you use it?
In your code, nowhere at all...


> generatedNumbers = r.nextInt();



In the next line, you've finally got an incrementation, but you
incremented
the wrong bucket...

*Which* bucket do you need to increment, to afterwards be able to see
how
many hits you had on each number?



> data[i]++;



And you *didn't* listen to what I said about separating the printout to
a
*second* loop, to do after you're done with the randomly generated
numbers...


> System.out.println(i + " " + data[i]);
> }


I tried looking at the problem like you suggested, but I just don't
know how to do these things. I don't know where to use the
generatedNumbers again, which bucket to increment, or what the second
loop should be (as far as the conditions) to make it print the
counters. Everytime I think I finally understand the problem, I change
the code to what I think will work and it either gets worse or still
doesn't work. I don't know how to avoid trial and error now because I
have already tried to think about the problem and what I should do to
solve it. I think the problem is that I don't understand exactly what
I'm supposed to do.

CPSCmajor

2005-10-30, 7:00 pm

Now nothing is displayed on the printout:

import java.util.*;
public class Array
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random r = new Random();
int numbersToGenerate = 0;
int numberOfValues = 0;
int generatedNumbers = 0;
int i = 0;
int count = 0;
System.out.print("How many numbers do you want to generate? ");
numbersToGenerate = s.nextInt();
System.out.print("What is the number of values? ");
numberOfValues = s.nextInt();
int[] data = new int[numberOfValues];
for (i = 0; i < numberOfValues; i++)
{
generatedNumbers = r.nextInt();
if (generatedNumbers == i)
{
data[i]++;
System.out.println(i + " " + data[i]);
}
}
}
}

Roedy Green

2005-10-31, 7:57 am

On 30 Oct 2005 16:44:12 -0800, "CPSCmajor" <bmerritt1987@gmail.com>
wrote, quoted or indirectly quoted someone who said :

> if (generatedNumbers == i)


what did you intend for that to do?

If you are trying to limit printout, you are doing too much.

Play computer and go through this one step and a time on paper.You
will see why that line makes no sense.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli

2005-10-31, 7:02 pm

"CPSCmajor" wrote...

> Now nothing is displayed on the printout:


Exactly! That's why you need to *separate* the tasks, in order to grasp
which part does what...

- The first iteration should increment the "buckets".
- The second iteration *just* does the printout.

I'll help you with a marker in the code below, where this second loop should
be inserted...

You have also managed to insert another error with the "if-selection"...

> import java.util.*;
> public class Array
> {
> public static void main(String[] args)
> {
> Scanner s = new Scanner(System.in);
> Random r = new Random();
> int numbersToGenerate = 0;
> int numberOfValues = 0;
> int generatedNumbers = 0;
> int i = 0;
> int count = 0;
> System.out.print("How many numbers do you want to generate? ");
> numbersToGenerate = s.nextInt();
> System.out.print("What is the number of values? ");
> numberOfValues = s.nextInt();
> int[] data = new int[numberOfValues];


So far so good, but you seem to mix what the variables should be used for...

You asked the user for how many numbers to keep track of (numberOfValues),
and how many numbers to be generated (numbersToGenerate).

So, which of those variables do you need to use for the iteration in which
you generate numbers?

> for (i = 0; i < numberOfValues; i++)
> {
> generatedNumbers = r.nextInt();


In the line above you got one generated number. However, there's one crucial
error with that line, as you'll get numbers outside the interval you asked
for...

Several other posters have pointed out to you that you need to understand
what you're doing, and some have alreday pointed to the documentation on the
Random class.

http://java.sun.com/j2se/1.5.0/docs...til/Random.html

Look especially into the methods "nextInt()" and compare it to
"nextInt(int)".

So what bucket do you need to increment? There's absolutely no need for this
"if-selection", as you by the generated number *knows* what bucket to
increment, and it's *not* "data[i]" ...

> if (generatedNumbers == i)
> {
> data[i]++;


And you *still* didn't listen to what I said about separating the printout
to a *second* loop, to do after you're done with the randomly generated
numbers...

> System.out.println(i + " " + data[i]);
> }
> }


////////////////////////////////////////////////
// And HERE you should place the second loop, //
// iterating through the "buckets"... //
////////////////////////////////////////////////

> }
> }


// Bjorn A


Oliver Wong

2005-10-31, 7:02 pm

"CPSCmajor" <bmerritt1987@gmail.com> wrote in message
news:1130541698.398037.240200@z14g2000cwz.googlegroups.com...
>
> Ok thanks, I realize now that I do not need to multiply by 1000. I know
> that when I do r.nextInt() that it will call a random number, and the
> loop will execute i < numberOfValues times (is this right?).


At one point you ask the user how many values she wants generated. The
user enters in a value and that value is stored somewhere. That value is how
many times you want loop to execute. If you've put that value in
"numberOfValues", then that's right, but if you've put that value somewhere
else, you need to make sure you're checking the correct variables.

> I seem to
> still be about the part of the question that says "If it
> returns a value v, then increment the counter belonging to v. After all
> numbers have been generated, print the counters." I don't know what I
> need to set data[i] equal to.


"Increment" essentially means "add 1", so the value you need to set
data[i] to is the same as it's old value plus one. Since "setting a value to
be the same as it's old value" is done so often in programming, there's a
shortcut for writing this. Let's say I want to increase the value of
variable "foo" by 5; instead of writing "foo = foo + 5;", I can write "foo
+= 5;" which means the same thing, but is shorter. Now it turns out
increasing the value by 1 is done even more often, so there's an even
shorter short cut for that special case: instead of writing "foo += 1;", you
can write "foo++;".

Do you have a solid understand of how arrays work, by the way? You need
to know them pretty well for this assignment.

- Oliver


Oliver Wong

2005-10-31, 7:02 pm


"CPSCmajor" <bmerritt1987@gmail.com> wrote in message
news:1130614738.679205.318020@g49g2000cwa.googlegroups.com...
> How do you make a random number generator that generates only zeros?


See the nextInt(int n) method of the class java.util.Random:

http://java.sun.com/j2se/1.5.0/docs...om.html#nextInt(int)

- Oliver


CPSCmajor

2005-11-02, 7:00 pm

I just needed help understanding how to write the code, but you just
kept telling me to do things that I didn't know how to do...when I get
help I want to know what I am doing wrong and why it's not working and
what I need to do do fix it. I was told not to do trial and error, but
if I don't know what to do then how can I write the code for it. I told
you I didn't know what bucket to increment or how to do the counters or
print them. Telling me to do that is pointless haha.

CPSCmajor

2005-11-02, 7:00 pm

Thanks for the help though, this was due on Monday so I just turned in
what I had so I wouldn't get a zero.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com