Random class and arrays

B

BlueOysterCult

Hello
I am having dificulty with this homework question ( I know its
homework but I have been trying for a while with this)
"generate 50 random numbers between 0 and 6 and count the number of
occurences of each value. IT should employ the DAyOfWeek enumeration,
where 0 represents Sunday....."

public class DayOfWeek {
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

int[] DayOfWeek = new int[7];
Random r = new Random();
for(int i = 0; i < 50; i++)
{
++DayOfWeek[r.Next(0,7)];
}
string output = null;
for(int ans=0; ans < 7; ans++)
output += DayOfWeek[ans] + "\n";
Console.WriteLine(output);


When I compile I am getting a lot of errors with the "++" , "<"
ect
Can Anyone help?
Thank
Rob
 
J

Jon Skeet [C# MVP]

BlueOysterCult said:
Hello
I am having dificulty with this homework question ( I know its
homework but I have been trying for a while with this)
"generate 50 random numbers between 0 and 6 and count the number of
occurences of each value. IT should employ the DAyOfWeek enumeration,
where 0 represents Sunday....."

public class DayOfWeek {
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

int[] DayOfWeek = new int[7];
Random r = new Random();
for(int i = 0; i < 50; i++)
{
++DayOfWeek[r.Next(0,7)];
}
string output = null;
for(int ans=0; ans < 7; ans++)
output += DayOfWeek[ans] + "\n";
Console.WriteLine(output);


When I compile I am getting a lot of errors with the "++" , "<"

Well, firstly I'm not sure why you've got your own Days enumeration
when you've been asked to use the DayOfWeek enumeration. You're then
calling one of your *variables* DayOfWeek, which doesn't seem like a
good idea.

However, most of the actual code is okay - it's just that you don't
have a method to put it in, and your braces don't match up.

(Personally I found the ++DayOfWeek[r.Next(0,7)]; syntax hard to
understand at first, but never mind.)
 
B

BlueOysterCult

I didn't notice the Days vs DayOfWeek ---
Thanks

I guess I could use any variable name?

And oh, I do have a Method I just didn't include it in the post

Thanks for your help!!
R
 
J

Jon Skeet [C# MVP]

BlueOysterCult said:
I didn't notice the Days vs DayOfWeek ---

I'm not suggesting that you change the enum name to DayOfWeek - I'm
suggesting that you use the one that .NET already provides.
I guess I could use any variable name?

Yes, but not using the name of another type would help to make the code
readable.
And oh, I do have a Method I just didn't include it in the post

That doesn't help much, then - in order to reproduce your compilation
error, we need to have the same code that you're trying to compile.
Please post a *complete* program (preferrably a short one that still
demonstrates the problem).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top