1

This piece of code was written to generates 100 random numbers between 0 and 1000 and display the number of even values generated as well as the smallest, the largest, and the range of values.

When I compile I'm getting an error about using unassigned local varibale of minInt and maxInt. It is supposed to get assigned variable from the generator, what did I do wrong?

using System;
using System.Windows.Forms;

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)
        {
            int evenNumbers = 0;
            int minInt;
            int maxInt;
            int range;
            string result;

            range = maxInt - minInt;

            result = "Even numbers:\t" + evenNumbers;
            result += "\nMin number:\t" + minInt;
            result += "\nMax number:\t" + maxInt;
            result += "\nRange of number:\t" + maxInt + " - " + minInt + " = " + range;

            DisplayResults(result);
        }

        static void GenerateNumbers(int evenNumbers,  int minInt,  int maxInt,  int range)
        {
            //creating 100 element array
            //and using the random function to fill it
            int[] randomNumbers = new int[100];
            Random number = new Random();
            int randy;
            for (int i = 0; i < randomNumbers.Length; i++)
            {
                randy = number.Next(1000);
                randomNumbers[i] = randy;
                if (randy < minInt)
                {
                    minInt = randy;
                }
                else
                    if (randy > maxInt)
                    {
                        maxInt = randy;
                    }
                if (randomNumbers[i] % 2 == 0)
                {
                    evenNumbers++;
                }
            }
        }
        static void DisplayResults(string outcome)
        {
            MessageBox.Show(
            outcome, "results!",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
8
  • 10
    what is the problem? Does it not compile? Does it blow up when it runs? Print the works of Shakespeare? Print mostly what you expected but not quite? People can't help if they don't know what help you need. Oct 15, 2012 at 20:24
  • You're not calling GenerateNumbers. Is this just a cut and paste error?
    – ChrisF
    Oct 15, 2012 at 20:25
  • Apart from not calling the GenerateNumbers method, what's your problem exactly? You need to supply some more information about what is wrong.
    – DeanOC
    Oct 15, 2012 at 20:25
  • Aside from not calling Generate and whatever other compiler issues you are having. Your Generate function will not update the values of evenNumbers, minInt, maxInt that are defined in your main function. You will need to make them ref parameters if you want their values updated in the method.
    – pstrjds
    Oct 15, 2012 at 20:31
  • 1
    Is this homework by any chance?
    – pstrjds
    Oct 15, 2012 at 20:37

1 Answer 1

1

I suspect your main problem is you never assign the random number to randy:

int randy;
for (int i = 0; i < randomNumbers.Length; i++)
{
    randy = number.Next(1000);
    randomNumbers[i] = randy

EDIT: I'm surprised this even compiles. Shouldn't it have complained about attempting to use an non-initialized variable?

EDIT: In addition, you need to assign some value to minInt and maxInt in your Main method. After these fixes, it should compile (but won't give you the result you need as you do not invoke GenerateNumbers or gather the results out of it. For that you should use ref:

int evenNumbers = 0;
int minInt = Int32.MaxValue;
int maxInt = Int32.MinValue;
int range;
string result;

GenerateNumbers(ref evenNumbers, ref minInt, ref maxInt);

range = maxInt - minInt;



result = "Even numbers:\t" + evenNumbers;
result += "\nMin number:\t" + minInt;
result += "\nMax number:\t" + maxInt;
result += "\nRange of number:\t" + maxInt + " - " + minInt + " = " + range;

DisplayResults(result);

Then your GenerateNumbers method:

static void GenerateNumbers(ref int evenNumbers,  ref int minInt,  ref int maxInt)
{
    //creating 100 element array
    //and using the random function to fill it
    int[] randomNumbers = new int[100];
    Random number = new Random();
    int randy;
    for (int i = 0; i < randomNumbers.Length; i++)
    {
        randy = number.Next(1000);
        randomNumbers[i] = randy;
        if (randy < minInt)
        {
            minInt = randy;
        }
        else
            if (randy > maxInt)
            {
                maxInt = randy;
            }
        if (randomNumbers[i] % 2 == 0)
        {
            evenNumbers++;
        }
    }
}

(I took out the range parameter as it wasn't used)

Also note that I set your minInt and maxInt variables to the maximum/minimum values initially. This means that they'll they'll take the first random value, then subsequently keep it updated.

EDIT: Cleaned the generator code a bit too. Watch out for the if < elseif > check as there are corner case conditions where if there are too few random numbers and too small a range, or each of the random numbers generated are descending, it's plausible that your "else if (randomNumber > maxInt" check will never execute.

static void GenerateNumbers(ref int evenNumbers,  ref int minInt,  ref int maxInt)
{
    Random numberGenerator = new Random();
    for (int i = 0; i < 100; i++)
    {
        int randomNumber = numberGenerator.Next(1000);

        if (randomNumber < minInt)
            minInt = randomNumber;

        if (randomNumber > maxInt)
            maxInt = randomNumber;

        if (randomNumber % 2 == 0)
            evenNumbers++;
    }
}
2
  • The main problem I'm having right now is that it wont compile.
    – Nghia Le
    Oct 15, 2012 at 20:26
  • 3
    @user1748201 - you need to edit the question to include what errors you are getting when you try to compile the code.
    – ChrisF
    Oct 15, 2012 at 20:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.