PDA

View Full Version : C# Basic help.



Vullf
10-23-2008, 09:26 AM
I'm trying differnt things in CSharp, now i started "Rolling the dice". With the following code:

//*using System;
using System.Collections.Generic;
using System.Text;

namespace DiceTest
{
class Program
{
static void Main(string[] args)
{
Random randomObj = new Random();

for(int i = 1; i <= 1000000; i++)
Console.WriteLine("Dice Roll Nr. {0} = {1}", i, randomObj.Next(1,7));
}
}
}
*//

Is there any way to get the average out of it in the code?

P_1
10-24-2008, 07:54 AM
using System.Collections.Generic;
using System.Text;
using System;

namespace DiceTest
{
class Program
{
static void Main(string[] args)
{
const int NUMROLLS = 1000000;
Random randomObj = new Random();

int runningtotal = 0;
for (int i = 1; i <= NUMROLLS; i++)
{
int Roll = randomObj.Next(1, 7);
runningtotal += Roll;
Console.WriteLine("Dice Roll Nr. {0} = {1}", i, Roll);
}
Console.WriteLine("Avg Roll: {0}", (runningtotal / NUMROLLS));
}
}
}


The point here is to have a running sum of the results so that you can divide them later.

Also a coding style tip is to use something like "const int NUMROLLS" whenever possible, so that its not just another number laying around your code and so that when you decide to change the number of rolls to something else you don't have to go all over your code changing it, all you would have to do is change the initial assignment expression.

Hope this helps.

Vullf
10-24-2008, 12:28 PM
ehm, not sure i understand you that much.
the fact. is that im pretty new into C#.
And i have this code as one of my assignments.

I figured out how to make the 1.000.000 hits. But what code should i implent and where please?

And Thanks for the anwser!

P_1
10-24-2008, 01:24 PM
I gave you the code. My code prints out the average for you.
Console.WriteLine("Avg Roll: {0}", (runningtotal / NUMROLLS));

Vullf
10-24-2008, 01:46 PM
Yeah works like a charm!
I just didnt realize it was your code. Lol.


Well can you help me with this task?


1st Create a program to a Cola vending machine that can take against the Euro and provide proper back in the Euro. A Cola will cost 1.45 €.
Euro coins are: 100, 50, 20, 10, 5, 2 and 1 cents.
Input: The user's throw-in and Euro cents.
Output: Money Exchange.
Test the program with different throw.
The task can be expanded with a menu of different products which can be purchased.

Really, thanks for your help. When i see the codes i understand them a bit more.
Just cant get them out of my head atm.

thanks again man.

P_1
10-24-2008, 11:20 PM
Well from what I understand in your post, you want to simulate a vending machine. I don't know what your teacher wants as the return value, but lets assume that its just a WriteLine. So essentially what you have to do is store these different coin values in an array, then have a for loop go through them and keep using the % operator in descending order on the input value - the cola value. I am not going to go further than that as I will not do your hw for you. But this is how you would have to do it. The reason is because this isn't a matter of c# anymore but rather your understanding of the problem, same goes for the first problem you posted, had I realized that it was for your homework I wouldn't have written out the code for you.