Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:53:01+00:00 2026-06-13T01:53:01+00:00

This piece of code was written to generates 100 random numbers between 0 and

  • 0

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:53:02+00:00Added an answer on June 13, 2026 at 1:53 am

    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++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written this piece of code where I've assigned an unsigned integer to two
I wrote this piece of code that is supposed to redirect something written on
I've written this short piece of php code that requires 2 variables name and
I saw a piece of code which was written like this: if (from n
This piece of code is from Adobe docs : package { import flash.display.Loader; import
I have written this piece of code to catch error launched by ppl try
I have written this piece of code that splits a string and stores it
I have written this piece of code public class Test{ public static void main(String[]
I have written this piece of code: bitmapData = calloc(1, bitmapByteCount ); context =
I'm looking at this piece of code written by someone else, and I'm wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.