Index was outside the bounds of the array confusion - c#

I am trying to learn two dimensional array and I wrote some basic code, but I am getting this exception. Could you tell me what am I doing wrong?
static void Main(string[] args)
{
Random rnd = new Random();
int[,] array = new int[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; i++)
{
array[i, j] = rnd.Next(0, 100);
}
}
for (int i = 0; i < array.GetLength(0); i++)
{
Console.WriteLine(array[i, 0] + "---" + array[i, 1]);
}
Console.ReadLine();
}

The problem is in your inner for-loop. In the iterator section, you're incrementing the i variable, but it should be j. Try this:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i, j] = rnd.Next(0, 100);
}
}

Related

How to set random numbers in a matrix between two numbers using loops

I want to randomly generate two matrix arrays so I can later add them up and store them into a third matrix, how would I go about doing this? Nearly totally lost, here's what I have so far.
using System;
namespace question2_addingrandommatrice
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int[,] newarray = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int ran2 = random.Next(-10, 10);
int ran1 = random.Next(-10, 10);
newarray[i, j] = ran1, ran2;
}
}
Console.ReadKey();
}
}
}
You are nearly there, you just needed one random.Next
Here is a method that does it for you
private static int[,] GenerateRandomMatrix(int x, int y)
{
var array = new int[x, y];
for (int i = 0; i < array.GetLength(0); i++)
for (int j = 0; j < array.GetLength(1); j++)
array[i, j] = random.Next(-10, 10);
return array;
}
Add pepper and salt to taste
Usage
// 3*3 random matrix
var matrix = GenerateRandomMatrix(3,3);
Additional Resources
Multidimensional Arrays (C# Programming Guide)
You can simply do this.
Random random = new Random();
int[,] newarray = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
newarray[i, j] = random.Next(-10, 10); ;
}

Change 2d array bubble sort to counting sort

The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array

I am making a sudoku on c#

enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testing_random
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int[,] a = new int[n,n];//declaring the matrix
Random o = new Random();
a[0,0] = o.Next(n);
for (int i = 1; i < n; i++)//filling the first line
{
int d = 1;
while (d != 0)
{
a[i,0] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i,0] == a[j,0])
d++;
}
}
for (int i = 1; i < n; i++)//filing the first column
{
int d = 1;
while (d != 0)
{
a[0, i] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[0, i] == a[0, j])
d++;
}
}
for (int k = 1; k < n; k++)//filling the rest of the matrix
for (int i = 1; i < n; i++)
{
int d = 1;
while (d != 0)
{
a[i, k] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i, k] == a[j, k])
d++;
for (int j = 0; j < k; j++)
if (a[i, k] == a[i, j])
d++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0} ", a[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
The output should be a matrix of 4*4 where each column and each line contains each number once.
The problem is when i run the code, not every time i get an output, i think the problem is not every set of first line and column can give a matrix as required the i get into an unending loop.
what i want to do is limit the running time of the application to 100 ms per example,so if the matrix is not filled,the program restarts
What piece of code am i missing?
replace the while( d != 0 ) with a loop which counts up to a certain very large maximum number of iterations. (Try 1000, 100000, whatever.)
Are you trying to randomly insert numbers between 1-4 in the first line of the array? If so there is a much easier way to do it.
You can generate the 4 numbers to be inserted into the array and then just look through the first line of the array and set each value.
Random rnd = new Random();
var randomNumbers = Enumerable.Range(1, 4).OrderBy(i => rnd.Next()).ToArray();
for (int i = 0; i < n; i++)
{
a[i, 0] = randomNumbers[i];
}

How to improve nested loop in C#?

I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.

Bubblesort listbox Am i missing something?

I want to sort an array[10] with bubblesort and place the unsorted/sorted items in an listbox.
But the array isn't sorted and gives me always the same array of numbers back.
private void button1_Click(object sender, EventArgs e)
{
lblB.Show();
lblQ.Hide();
lbS.Items.Clear();
lbU.Items.Clear();
Random r = new Random();
int n = 10;
int[] arr = new int[n];
//listbox fill random
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(0, 20);
lbU.Items.Add(arr[i]);
}
int temp = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j;
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
lbS.Items.Add(arr[i]);
}
}
Am i missing something here,
Any help would be greatly appreciated.
You have a mistake in your code that's sorting the array:
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j; // <<-- Mistake here - should be arr[i] = arr[j];
arr[j] = temp;
}
}
}

Categories