mardi 26 mars 2019

How do I capture the minimum and maximum for 1000 random integers?

enter image description hereThe problem asks for 1000 iteration of the code.It must allow for whole numbers from 0-100000 and show how many odd numbers were generated during the iterations then show the highest number and lowest numbers generated. The first part of my code works and shows how many odd numbers were generated however I can not figure out how to capture/edit the smallest and largest numbers that were generated while the code was running.

I have tried many different methods including while loops and my if, else if, conditions. I have placed them through out my program however I am stuck. I know the problem is with the randNum going into the variables and staying there through each iterations without going back to zero.(when I run my code it displays zero for the smallNum and LargeNum.)

here is my work so far

using System;
using System.Windows.Forms;

namespace BissonnetteMessageBox

{
  class Program
    {
      static void Main(string[] args)

      {

        int oddNumCount = 0;
        int smallNum = 0;
        int largeNum = 0;
            Random randNum = new Random();


            for (int i = 0; i < 1000; i++)
            {

                int num = randNum.Next(100000);
                int remain = num % 2;

                if (remain != 0)
                {
                    oddNumCount++;


                }
                if (num < smallNum)
                {
                    num = smallNum;
                }
                else if (num > largeNum)
                {
                    num = largeNum;
                }

            }

            MessageBox.Show("the Number of odd numbers generated: " + oddNumCount +
                "\nSmallest number was: " + smallNum +
                "\nLargerst number was: "+ largeNum , "random number generation results");
        }
    }
}

Here is what the result is when I run my program.




Aucun commentaire:

Enregistrer un commentaire