2
Answers

Problem with array and program taking so long to run

Bill Cappoli

Bill Cappoli

15y
2.7k
1
I have a program that I'm developing for a class. I have to generate 10,000 random 3 digit numbers and keep track of how often each one comes up. Once complete the program should display 10 most common and the 10 least common numbers. The problem I'm facing is that when I run the program it seems to just run for a long time and than after a few minutes throws an out of index error at line 50 which reads as such lotteryNumbers[i, 1]++;. I have included my code below. Anyone have any ideas of what I'm doing wrong with the array and how I might speed up the application.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace William_Cappoli_IT466_Unit_3_Project
{

    public partial class frmMain : Form
    {
        // Array to hold numbers 000 - 999 and count occurance
        int[,] lotteryNumbers = new int[1000, 2];
        // Array to hold 10 most frequent numbers
        int[,] maxNumbers = new int[10, 2];
        // Array to hold l0 least frequent numbers
        int[,] minNumbers = new int[10, 2];

        // stores most recently generated number
        int currentNum;

        // random number generator
        Random randomNumbers = new Random();

        public frmMain()
        {
            InitializeComponent();
            fillLotteryArray();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            fillMinNumbersArray();

            // clears out labels displaying minimum and maximum number lists
            lblDisplayMostCommon.Text = "";
            lblDisplayLeastCommon.Text = "";

            for (int i = 0; i < 10000; ++i)
            {
                currentNum = randomNumbers.Next(999);

                for (int j = 0; j < 1000; ++j)
                {
                    if (lotteryNumbers[j, 0] == currentNum)
                    {
                        lotteryNumbers[i, 1]++;
                    }
                }
                maxNumber();
                minNumber();

            }
        }  // btnGenerate_Click method

        private void fillLotteryArray() //Fill lottery number array with values 000 - 999
        {
            for (int i = 0; i < 1000; ++i)
            {
                lotteryNumbers[i, 0] = i;
            }
        }  // end fillLotteryArray()

        private void fillMinNumbersArray() //Fill minimum numbers array with value 5000
        {
            for (int i = 0; i < 10; ++i)
            {
                minNumbers[i, 1] = 5000;
            }
        }  // fillMinNumbersArray()

        public void maxNumber() // Find 10 most frequent numbers
        {
            for (int j = 0; j < 1000; ++j) // Loop for all 1000 lottery numbers
            {
                for (int i = 0; i < 10; ++i) // Loop for the 10 maximum numbers
                {
                    if (lotteryNumbers[j, 1] > maxNumbers[i, 1]) // Check if there is a more fequent number than the one stored at each index
                    {
                        for (int h = 9; h > i; --h) // Starting with index 9, store previous index value until you reach the index that will store the new value
                        {
                            maxNumbers[h, 1] = maxNumbers[h - 1, 1];
                            maxNumbers[h, 0] = maxNumbers[h - 1, 0];
                        }
                        maxNumbers[i, 1] = lotteryNumbers[j, 1]; // Store new values at slated index
                        maxNumbers[i, 0] = lotteryNumbers[j, 0];
                        i = 10;
                    }
                }
            }

            for (int i = 0; i < 10; ++i) // Display each number and its frequency
            {
                lblDisplayMostCommon.Text += maxNumbers[i, 0].ToString("000") + "     " + Convert.ToString(maxNumbers[i, 1]) + "\n";
            }
        }  // end maxNumber method

        public void minNumber() // Find 10 least frequent numbers
        {
            for (int j = 0; j < 1000; ++j) // Loop for all 1000 lottery numbers
            {
                for (int i = 0; i < 10; ++i) // Loop for the 10 minimum numbers
                {
                    if (lotteryNumbers[j, 1] > 0 & lotteryNumbers[j, 1] < minNumbers[i, 1]) // Check if there is a less frequent number than the one stored at each index disregarding numbers that have not been drawn
                    {
                        for (int h = 9; h > i; --h) // Starting with index 9, store previous index value until you reach the index that will store the new value
                        {
                            minNumbers[h, 1] = minNumbers[h - 1, 1];
                            minNumbers[h, 0] = minNumbers[h - 1, 0];
                        }
                        minNumbers[i, 1] = lotteryNumbers[j, 1]; // Store new values at slated index
                        minNumbers[i, 0] = lotteryNumbers[j, 0];
                        i = 10;
                    }
                }
            }

            for (int i = 0; i < 10; ++i) // Display each number and its frequency
            {
                lblDisplayLeastCommon.Text += minNumbers[i, 0].ToString("000") + "     " + Convert.ToString(minNumbers[i, 1]) + "\n";
            }
        }  // end minNumbers method
    }  // form1 partial class
}  // end namespace


Thanks for your help
Answers (2)