Click here to Skip to main content
16,015,274 members
Home / Discussions / C#
   

C#

 
AnswerRe: Extracting Cell Data From A DataGridView Pin
nelsonpaixao24-Nov-08 12:27
nelsonpaixao24-Nov-08 12:27 
QuestionIssue with array and program taking so long to run Pin
SilentCodingOne24-Nov-08 9:38
SilentCodingOne24-Nov-08 9:38 
AnswerRe: Issue with array and program taking so long to run Pin
Jimmanuel24-Nov-08 9:59
Jimmanuel24-Nov-08 9:59 
QuestionRe: Issue with array and program taking so long to run Pin
SilentCodingOne24-Nov-08 10:27
SilentCodingOne24-Nov-08 10:27 
AnswerRe: Issue with array and program taking so long to run Pin
Ben Fair24-Nov-08 10:59
Ben Fair24-Nov-08 10:59 
AnswerRe: Issue with array and program taking so long to run Pin
Jimmanuel24-Nov-08 11:00
Jimmanuel24-Nov-08 11:00 
GeneralRe: Issue with array and program taking so long to run Pin
SilentCodingOne24-Nov-08 16:54
SilentCodingOne24-Nov-08 16:54 
AnswerRe: Issue with array and program taking so long to run Pin
Ben Fair24-Nov-08 10:49
Ben Fair24-Nov-08 10:49 
The error around line 50 is because your outer loop is going until i = 10,000; whereas the array is only indexed to 1,000
elements. So, when i = 1,000 you'll get the error as you've gone beyond the bounds of the first dimension of the array.

Just my personal opinion, but I always look for places to make small performance and memory savings. Since your values
are 3-digit numbers you are wasting a lot of memory using int as it is a 32-bit value covering numbers that are
way larger than you'll need. You can use ushort (UInt16) that will cover the range of numbers you're
working with and use half the memory.

Also, I believe that the maximum value parameter to Random.Next(int) is exclusive for the
upper bound, meaning the maximum value will never be the number you supply; if you want 999 to be a possible value, you
should supply 1,000 as the maximum and then 999 will be the largest value that it will return.

The performance is bad, currently, because you are looping through all elements of the array with each random number you
produce. If you changed your logic to index into the array on the random number instead of using a loop to get to the
element it would greatly improve the performance. In other words, replace this:

currentNum = randomNumbers.Next(999);
for (int j = 0; j < 1000; ++j)
{
    if (lotteryNumbers[j, 0] == currentNum)
    {
        lotteryNumbers[i, 1]++;
    }
}


with this:

currentNum = randomNumbers.Next(1000);
lotteryNumbers[currentNum]++; <--- assuming the array's redefined to just one dimension as suggested below


I noticed that the array is 2 dimensional; I didn't really see any need for having 2 dimensions; I'd just create it
as ushort[] lotteryNumbers = new ushort[1000];. The index position will simulate the values 0 - 999 and
the value of each element would be its number of occurrences. Also, with this change there is no need to initialize any
values in the array as all the elements will default to 0 when it is created.

Last, you'll need a way to sort the results to get the 10 with the least and 10 with the most occurrences. I'd recommand
sorting the results into a single collection and then pulling the first 10 and last 10 elements; I'll leave that as an
exercise to the reader Smile | :) .

Keep It Simple Stupid! (KISS)

GeneralRe: Issue with array and program taking so long to run Pin
SilentCodingOne24-Nov-08 16:53
SilentCodingOne24-Nov-08 16:53 
QuestionEditing Database using ADO.NET Pin
Ain't me babe24-Nov-08 9:11
Ain't me babe24-Nov-08 9:11 
AnswerRe: Editing Database using ADO.NET Pin
Christian Graus24-Nov-08 9:24
protectorChristian Graus24-Nov-08 9:24 
QuestionSo your question is Pin
led mike24-Nov-08 9:26
led mike24-Nov-08 9:26 
AnswerRe: So your question is Pin
Ain't me babe24-Nov-08 9:55
Ain't me babe24-Nov-08 9:55 
GeneralRe: So your question is Pin
led mike24-Nov-08 10:01
led mike24-Nov-08 10:01 
Questionhow to import excel sheet to postgresql database Pin
Ranjt kumar24-Nov-08 8:53
Ranjt kumar24-Nov-08 8:53 
AnswerRe: how to import excel sheet to postgresql database Pin
Wendelius24-Nov-08 9:30
mentorWendelius24-Nov-08 9:30 
QuestionbindingNavigator button images Pin
pcjd6324-Nov-08 7:18
pcjd6324-Nov-08 7:18 
AnswerRe: bindingNavigator button images Pin
Giorgi Dalakishvili24-Nov-08 7:22
mentorGiorgi Dalakishvili24-Nov-08 7:22 
GeneralRe: bindingNavigator button images Pin
pcjd6325-Nov-08 2:05
pcjd6325-Nov-08 2:05 
QuestionHow can an AxWebBrowser notify it's host about changes on its controls Pin
kozu24-Nov-08 7:14
kozu24-Nov-08 7:14 
Questionsetup customer background color Pin
Pr@teek B@h!24-Nov-08 5:50
Pr@teek B@h!24-Nov-08 5:50 
AnswerRe: setup customer background color Pin
Giorgi Dalakishvili24-Nov-08 6:03
mentorGiorgi Dalakishvili24-Nov-08 6:03 
GeneralRe: setup customer background color Pin
Pr@teek B@h!24-Nov-08 7:12
Pr@teek B@h!24-Nov-08 7:12 
GeneralRe: setup customer background color Pin
Giorgi Dalakishvili24-Nov-08 7:21
mentorGiorgi Dalakishvili24-Nov-08 7:21 
QuestionHow to generate the application line when using XMLSerializer to generate an Excel 2003 XML file Pin
TxPomeroy24-Nov-08 5:47
TxPomeroy24-Nov-08 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.