How to handle this ArgumentOutOfRangexception? - c#

I'm trying to make a program that reads a file with 50 random words, it stores the words with 3 letters or less in a list called SmallWords and the words with 4 letters or more in a list called LargeWords. I'm using Windows Forms and I have a ListView control with 2 columns... 'Small Words' and 'Large Words'. It seems obvious that what I want to do is just put the words in its corresponding column. The thing is that the file doesn't have like 25 small and 25 large words, you know? Maybe it has 30 small words and 20 large words, so when I do this loop to add items to the ListView, it throws this exception:
ArgumentOutOfRangeException.
This is my code
var MaxNum = Math.Max(SmallWords.Count, LargeWords.Count);
for (var index = 0; index < MaxNum; index++)
{
ListViewItem item = new ListViewItem(SmallWords[index]);
item.SubItems.Add(LargeWords[index]);
listView1.Items.Add(item);
}
The exception is thrown at this line item.SubItems.Add(LargeWords[index]);
Is there a way to handle this? Or like another way to add the items to the columns? I know it works because if change Math.Max(...); to Math.Min(...) it adds the words but just the 20 large words and 20 small words, I'm going to be missing 10 small words in the SmallWords column.
Thanks

You have two arrays with different lengths and try to loop through and access up the the max of both arrays. That's never going to work. Once you hit the limit of the smaller array, you'll get the out of range exception.
Two options to fix depend on what you want to do. You can loop through to the max of the smaller array, or you can skip the part that deals with the smaller array once you hit its max.
var MaxNum = Math.Min(SmallWords.Count, LargeWords.Count);
// ^--- Changed Max to Min
for (var index = 0; index < MaxNum; index++)
{
ListViewItem item = new ListViewItem(SmallWords[index]);
item.SubItems.Add(LargeWords[index]);
listView1.Items.Add(item);
}

I think you should look at this problem in a different way. Basically you are adding a large word at i index to a small word at the same index. Which means that you have to have a SmallWord to add a LargeWord as a subitem. Forget finding the min or max, just loop through the count of SmallWords and only add the LargeWords if the index is in range:
for (var index = 0; index < SmallWords.Length; index++)
{
ListViewItem item = new ListViewItem(SmallWords[index]);
if(index < LargeWords.Length)
{
item.SubItems.Add(LargeWords[index]);
}
listView1.Items.Add(item);
}

Related

Switching around elements in an array based on calculations (in a nested for loop?) [duplicate]

This question already has answers here:
Sorting an array related to another array
(4 answers)
Sorting two arrays (values,keys), then sorting the keys
(5 answers)
Closed 5 years ago.
This is my first post on stackoverflow, so forgive any formatting mistakes.
I have a project named BOGOTotal - basically, its job is to take a decimal array of prices, sort them in ascending order, and then determine which prices will be bought and which prices will be free. Anyways, that's just a bit of backstory, but I need something more specific.
I've got two arrays - one of the prices, and one of the quantities of those prices. For example, in my Items array (should have been named "prices"), Items[0] is set to 2.20m, and my Quantities array, Quantites[0] is set to 5. Meaning I have five of the same item that are priced at $2.20.
//create items array
decimal[] Items = new decimal[5];
Items[0] = 2.20m;
Items[1] = 1.50m;
Items[2] = 8.40m;
Items[3] = 4.60m;
Items[4] = 3.75m;
//create quantity array
int[] Quantity = new int[Items.Length];
Quantity[0] = 5;
Quantity[1] = 2;
Quantity[2] = 1;
Quantity[3] = 3;
Quantity[4] = 6;
I then had to sort the Items array in ascending order.
Array.Sort(Items);
However, when I sorted my Items array, the relation of each item to its quantity is now lost, because Items[0] and Quantity[0] no longer are related. Now, instead of Items[0] = 2.20m, it has become Items[0] = 1.50m. Now, I have 5 items that are $1.50 instead of $2.20.
Here's where I had my idea - I would go ahead and calculate the prices of the old arrays by creating a firstPrices array, putting them in a for loop, and saying
decimal[] firstPrices = new decimal[Items.Length];
//calculate prices before sorting - will match back up afterward
for (int i = 0; i < Items.Length; i++)
{
firstPrices[i] = Items[i] * Convert.ToDecimal(Quantity[i]);
}
Here comes the hard part: I'm trying to re-align (for lack of a better word) each quantity to its Item - meaning I'm trying to make the item that is $2.20 match back up with its correct quantity (being 5). However, I'm having a hard time doing this. I tried a nested for loop within another for loop that tested each quantity by multiplying it by the current Item and then comparing it to that spot in firstPrices:
//i would be newItems
//j would be quantity
for (int i = 0; i < Items.Length; i++)
{
for (int j = 0; j < Items.Length; j++)
{
if (Items[i] * Quantity[j] == firstPrices[i])
{
Quantity[i] = Quantity[j];
break;
}
}
}
When it found a match, I set it to break out of the nested loop and increment "i", which goes to the next item in the Items array. Is there something I'm doing wrong here?
You got two seperate arrays that are related, wich is not a good thing. Make a Struct, Class or Tupel containing both values. Optionally also a custom comparer. Make a array of that type.
Then it would be as simple as calling Array.Sort().
In caess where there is no single order (sometimes you want to sort by prices times quantity. Sometimes single item price), Array.Sort() has overrides that allow you to specify the comparer to be used.
You could also go for Linq, if you have experience with it. But getting both values into one type is a requirement. And the perfect time to learn that.

C# Add int numbers to a list while running a loop

I have tried to get the point across in the title as best as I can but basically what I want to do is add certain items to List while running a loop so I don't have to manually put them into an if statement. Let me please show an example so that I can explain properly.
Example :-
What I need is :- the first number would be 500 and that would be in index 0, then i want a loop to add 150 to the last number generated so that the int list would look like this,
index 0 = 500
index 1 = 650
index 2 = 800
index 3 = 950
Do this repeatedly until say the last number will read 2,000,000
Now I believe that this would be simple to run a loop and base it on conditions but I can only seem to figure out to run a loop that will increment the value in 1.
Hope I have explained well enough
Regards,
M
Now I believe that this would be simple to run a loop and base it on
conditions but I can only seem to figure out to run a loop that will
increment the value in 1.
This is not true, you can adjust the increment of the iterator as you wish.
var numbers = new List<int>();
for(int i=500; i<=2000000; i+=150)
{
numbers.Add(i);
}
For further information on this, please have a look here.
Just another implementation:
var result = new List<int>();
var number = 500;
do
{
result.Add(number);
number+= 150;
} while (number <= 2000000);

String to Array / Var Set .NET Bingo

I have a set of thirty numbers in a DB which are all in one cell, separated by spaces. I am evaluating them as such:
<%# Eval("winning_numbers").ToString().Split(' ')[0]%>
<%# Eval("winning_numbers").ToString().Split(' ')[1]%>
<%# Eval("winning_numbers").ToString().Split(' ')[2]%>
And so forth, up to thirty (29). This displays them all in a row. The numbers are random up to 75, like in a bingo game. What I would like to be able to do is to display them in a table, where if the number is between 01 and 15, it displays in the first row, 16-30 = second row, and so forth. Where I'm stuck mentally is that there aren't a finite amount of numbers in each tier - it is totally random. How can I do what I'm looking for?
You are looking for two-dimensional arrays, check this out:
Multidimensional Arrays (C# Programming Guide) # MSDN
If you have all of them items inside of a list then you can iterate through the list using a for loop
for(int I = 0; I < numlist.Count; I++)
{
//Inside the for loop we can check the number, and see what column it will fit into.
if(1 <= I <= 15)
{
//The code to put the item into the respective list.
}
if(16 <= I <= 30)
{
//The code to put the item into the respective list.
}
etc...
}

Randomly select a specific quantity of indices from an array?

I have an array of boolean values and need to randomly select a specific quantity of indices for values which are true.
What is the most efficient way to generate the array of indices?
For instance,
BitArray mask = GenerateSomeMask(length: 100000);
int[] randomIndices = RandomIndicesForTrue(mask, quantity: 10);
In this case the length of randomIndices would be 10.
There's a faster way to do this that requires only a single scan of the list.
Consider picking a line at random from a text file when you don't know how many lines are in the file, and the file is too large to fit in memory. The obvious solution is to read the file once to count the lines, pick a random number in the range of 0 to Count-1, and then read the file again up to the chosen line number. That works, but requires you to read the file twice.
A faster solution is to read the first line and save it as the selected line. You replace the selected line with the next line with probability 1/2. When you read the third line, you replace with probability 1/3, etc. When you've read the entire file, you have selected a line at random, and every line had equal probability of being selected. The code looks something like this:
string selectedLine = null;
int numLines = 0;
Random rnd = new Random();
foreach (var line in File.ReadLines(filename))
{
++numLines;
double prob = 1.0/numLines;
if (rnd.Next() >= prob)
selectedLine = line;
}
Now, what if you want to select 2 lines? You select the first two. Then, as each line is read the probability that it will replace one of the two lines is 2/n, where n is the number of lines already read. If you determine that you need to replace a line, you randomly select the line to be replaced. You can follow that same basic idea to select any number of lines at random. For example:
string[] selectedLines = new int[M];
int numLines = 0;
Random rnd = new Random();
foreach (var line in File.ReadLines(filename))
{
++numLines;
if (numLines <= M)
{
selectedLines[numLines-1] = line;
}
else
{
double prob = (double)M/numLines;
if (rnd.Next() >= prob)
{
int ix = rnd.Next(M);
selectedLines[ix] = line;
}
}
}
You can apply that to your BitArray quite easily:
int[] selected = new int[quantity];
int num = 0; // number of True items seen
Random rnd = new Random();
for (int i = 0; i < items.Length; ++i)
{
if (items[i])
{
++num;
if (num <= quantity)
{
selected[num-1] = i;
}
else
{
double prob = (double)quantity/num;
if (rnd.Next() > prob)
{
int ix = rnd.Next(quantity);
selected[ix] = i;
}
}
}
}
You'll need some special case code at the end to handle the case where there aren't quantity set bits in the array, but you'll need that with any solution.
This makes a single pass over the BitArray, and the only extra memory it uses is for the list of selected indexes. I'd be surprised if it wasn't significantly faster than the LINQ version.
Note that I used the probability calculation to illustrate the math. You can change the inner loop code in the first example to:
if (rnd.Next(numLines+1) == numLines)
{
selectedLine = line;
}
++numLines;
You can make a similar change to the other examples. That does the same thing as the probability calculation, and should execute a little faster because it eliminates a floating point divide for each item.
There are two families of approaches you can use: deterministic and non-deterministic. The first one involves finding all the eligible elements in the collection and then picking N at random; the second involves randomly reaching into the collection until you have found N eligible items.
Since the size of your collection is not negligible at 100K and you only want to pick a few out of those, at first sight non-deterministic sounds like it should be considered because it can give very good results in practice. However, since there is no guarantee that N true values even exist in the collection, going non-deterministic could put your program into an infinite loop (less catastrophically, it could just take a very long time to produce results).
Therefore I am going to suggest going for a deterministic approach, even though you are going to pay for the guarantees you need through the nose with resource usage. In particular, the operation will involve in-place sorting of an auxiliary collection; this will practically undo the nice space savings you got by using BitArray.
Theory aside, let's get to work. The standard way to handle this is:
Filter all eligible indices into an auxiliary collection.
Randomly shuffle the collection with Fisher-Yates (there's a convenient implementation on StackOverflow).
Pick the N first items of the shuffled collection. If there are less than N then your input cannot satisfy your requirements.
Translated into LINQ:
var results = mask
.Select((i, f) => Tuple.Create) // project into index/bool pairs
.Where(t => t.Item2) // keep only those where bool == true
.Select(t => t.Item1) // extract indices
.ToList() // prerequisite for next step
.Shuffle() // Fisher-Yates
.Take(quantity) // pick N
.ToArray(); // into an int[]
if (results.Length < quantity)
{
// not enough true values in input
}
If you have 10 indices to choose from, you could generate a random number from 0 to 2^10 - 1, and use that as you mask.

Non repeating random number in c# for Mine sweeper Game

i am developing a mine sweeper game in c# of dimension (8 x 8).The difficulty levels increase/decrease the number of mines on the grid.
I use a random class (with min,max set;) to generate a random cell number.The problem i am facing is ,the random object keeps repeating the same number.I tried to resolve this issue by maintaining a local list where i store the generated unique random numbers.The next time i call Next(), i would check it against the local list ,to see if its already present.If the number is already present i would keep calling Next() until i get a new number which is unique and not present in the list.But this doesnt look in itself a good solution as sometimes it takes painful amount of time to generate a new list.
Any suggestions on this please
Even if you use the same random number generator, repeating values are possible.
One way to avoid this would be to generate a list of possible values and using the random number generated to access a value in this list (using as indice) and reducing this list, as you find places to put mines to.
For 8 X 8 example, you have 64 possible places
List<int> possibleValues = new List<int>();
for (int i = 0; i < 64; i++)
{
possibleValues[i] = i;
}
List<int> result = new List<int>();
Random r = new Random();
int numberOfMines = 50; //say you want to put 50 mines there
for (int i = 0; i < numberOfMines; i++)
{
int indice = r.Next(possibleValues.Count);
int value = possibleValues[indice];
possibleValues.Remove(value);
result.Add(value);
}
It looks like you want a shuffle based on a fixed number of cells (8,8), e.g. a Fisher-Yates shuffle. This would guarantee that any coordinate only appears exactly once (as opposed to repeatedly using Random.Next() where you can draw the same number many times), and the order of appearance of coordinates is randomized.
Initialize an array that contains all the coordinates of your cells, shuffle the array and maintain an index of the next "random" cell, return the cell at the offset of the index and increase the index.
First calculate the number of mines, and empty fields.
Random rand=new Random();
int mines=GetMinesFromDifficulty(...);
int empty=TotalFields-mines;
Then for each field:
for(int y=0;y<height;y++)
for(int x=0;y<height;y++)
{
if(random.Next(mines+empty) < mines))
{
field[x,y]=Mine;
mines--;
}
else
{
field[x,y]=Empty;
empty--;
}
}
Instead of picking slots where the mines should be, loop through the slots and calculate the probability that there should be a mine there. The implementation for this becomes very simple, as you just need a single loop:
bool[] mines = new bool[64];
int cnt = 12;
Random rnd = new Random();
for (int i = 0; i < mines.Length; i++) {
if (rnd.Next(mines.Length - i) < cnt) {
mines[i] = true;
cnt--;
}
}
(Room for improvement: You can exit out of the loop when cnt reaches zero, if you don't need to initialise all slots.)
If your grid is 8x8, and you want to randomly choose an unused cell instead of pulling random numbers until you hit an unused one, then keep track of the number of unused cells. Say 8 have been used, leaving 55 unused. Then generate a random number between 0 and 54. You would then have to count through, and find the nth empty cell.
It would probably be easier to think of the problem in a more linear way. Instead of say a 2D array... Squares[8][8] think of it as a single dimension array Squares[64].
At this point you generate a number between 0-63 for your random mine placement. If say the value is 10 you could store for later to offset subsequent numbers. You can reduce your range now from 0-62, if you pulled out the value 16 you would want to add +1 for each value you'd already pulled out underneath it (so actually use 17 in this case, but square 10 has been excluded from our set).
Without seeing any code it's kind of hard to get the gist of things, but from what I can tell you have the following:
A multi-dimensional array [8][8] for the grid layout of the game, you're now trying to randomly place mines?
You'll need to keep one instance of Random for generating the numbers, else you will get the same number over and over again. Something like this
private readonly Random randomNumber = new Random();
for(int i = 0; i < 10; i++)
{
Console.WriteLine(this.randomNumber.Next(1, 10));
}
This will then generate 10 random numbers, each one different.

Categories

Resources