Im trying to make an auto solve for this math:
__ + __ + __ = 30, Fill the spaces using (1, 3, 5, 7, 9, 11, 13, 15), you can also repeat the numbers.
I made this code:
int[] nums = { 1, 3, 5, 7, 9, 11, 13, 15 };
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < nums.Length; i++)
{
for (int j = 0; j < nums.Length; j++)
{
for (int k = 0; j < nums.Length; k++)
{
if ((nums[i] + nums[j] + nums[k]) == 30)
{
result.Text += nums[i] + nums[j] + nums[k] + "\r\n";
}
}
}
}
}
But i get this error when click the button to calculate: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in app.exe
P.S.: Please do not spoil the answer for me.
Change the code
for (int k = 0; j < nums.Length; k++)
to
for (int k = 0; k < nums.Length; k++)
int[] nums = { 1, 3, 5, 7, 9, 11, 13, 15 };
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < nums.Length; i++)
{
for (int j = 0; j < nums.Length; j++)
{
/* check conditional statement value of k exceeds length of the num element array*/
for (int k = 0; j< nums1.Length; k++)
{
/* could have been >> for(int k=0; k < nums.Length; k++) */
if ((nums[i] + nums[j] + nums[k]) == 30)
{
result.Text += nums[i] + nums[j] + nums[k] + "\r\n";
}
}
}
}
}
:)
Related
int[] x = { 1, 2, 3, 4,5 };
int[] y = { 5, 4, 3, 2, 1 };
int[,] s=new int[(x.Length)*(x.Length),2];
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)
{
s[i, j] = x[i] * y[j];
Console.WriteLine(x[i] + " * " + y[j] + " = " + s[i, j]);
}
}
IndexOutOfRangeException: Index was outside the bounds of the array.
It gives a memory error, but it says it hangs the memory of the variable, but I transfer more than the field of the series. I couldn't figure out what the problem was. Can you help?
IndexOutOfRangeException: Index was outside the bounds of the array.
Change:
for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)
To:
for (int i = 0; i <= x.Length-1; i++)
{
for (int j = 0; j <= y.Length-1; j++)
Also this line:
int[,] s=new int[(x.Length)*(x.Length),2];
Although no big problems but if x[] and y[] are different lengths might cause issues with storing i,j values. Did you mean to declare like :=
int[,] s=new int[(x.Length)*(y.Length),2];
I have the 2d array below:
long[,] arr = new long[4, 4] {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }
};
I'm attempting to loop through each item in the array and finding items surrounding that point.
for (var row = 0; row < arr.GetLength(0); row++)
{
for (var col = 0; col < arr.GetLength(1); col++)
{
var itemElement = arr[row, col];
for (var i = 0; i < 2; i++)
{
for (var j = 0; j < 2; j++)
{
Item around = arr[row + i, col + j];
}
}
}
}
Once I attempt to look at [5,0] it's out of the index, how can I stay within my matrix?
Please try below code:
for (var row = 0; row < arr.GetLength(0); row++)
{
for (var col = 0; col < arr.GetLength(1); col++)
{
var itemElement = arr[row, col];
for (var i = 0; i < 2; i++)
{
for (var j = 0; j < 2; j++)
{
if((row + i) < arr.GetLength(0) && (col + j) < arr.GetLength(1))
{
Item around = arr[row + i, col + j];
}
}
}
}
}
If matrix A of size (3x3), then should i use the method of finding determinants, like grabbing the rows and column of first element and removing it from the array 2D array to get the remaining elements and then moving to the next element and repeating the same steps ?
[{1,2,3},
{4,5,6},
{7,8,9}]
I finally was able to do it, here's what I did :
enter image description here
class program
{
public static void Main()
{
int[,] arr = new int[3, 3];
Console.WriteLine("Enter elements of " + (arr.GetUpperBound(0) + 1) + "x" + (arr.GetUpperBound(1) + 1) + " matrix:");
for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
{
for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
{
arr[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("Matrix entered: ");
for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
{
for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
{
Console.Write("\t" + arr[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("Possible sub-matrices: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j< 3; j++)
{
TrimArray(i,j,arr);
}
}
}
public static int[,] TrimArray(int row, int column, int[,] original)
{
int[,] resultant = new int[original.GetLength(0) - 1, original.GetLength(1) - 1];
for (int i = 0, j = 0; i < original.GetLength(0); i++)
{
if (i == row)
continue;
for (int k = 0, u = 0; k < original.GetLength(1); k++)
{
if (k == column)
continue;
resultant[j, u] = original[i, k];
u++;
}
j++;
}
Console.WriteLine();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j< 2; j++)
{
Console.Write("\t"+resultant[i,j]);
}
Console.WriteLine();
}
return resultant;
}
}
I did this for you yesterday, I created a method that will return a square matrix, given a parent matrix and the length.
static void Main(string[] args)
{
int[][] parentMatrix = new int[][]
{
new int [] { 1, 2, 3 },
new int [] { 4, 5, 6 },
new int [] { 7, 8, 9 }
};
var chunks = GetSubMatrices(parentMatrix, 2);
Console.WriteLine(chunks);
}
static List<int[][]> GetSubMatrices(int[][] parentMatrix, int m)
{
int n = parentMatrix.Length > m ? parentMatrix.Length : throw new InvalidOperationException("You can't use a matrix smaller than the chunk size");
var chunks = new List<int[][]>();
int movLimit = n - m + 1;
var allCount = Math.Pow(movLimit, 2);
for (int selRow = 0; selRow < movLimit; selRow ++)
{
for (int selCol = 0; selCol < movLimit; selCol ++)
{
// this is start position of the chunk
var chunk = new int[m][];
for (int row = 0; row < m; row++)
{
chunk[row] = new int[m];
for (int col = 0; col < m; col++)
{
chunk[row][col] = parentMatrix[selRow + row][selCol + col];
}
}
chunks.Add(chunk);
}
}
return chunks;
}
If you have any problems using it, you can simply comment below.
I needed to solve a problem like and came up with this answer. Hope it adds to your library of answers. If the submatrix specified is not greater than 1, do nothing.
public static void GetSubMatrixes(int[,] arr, int size)
{
int parentMatrixRowLength = arr.GetLength(0);
int parentMatrixColLength = arr.GetLength(1);
var overall = new List<object>();
if(size > 1)
{
for (int i = 0; i < parentMatrixRowLength; i++)
{
//get the columns
for (int j = 0; j < parentMatrixColLength; j++)
{
var subMatrix = new int[size, size];
/*if the new matrix starts from second to the last value in either the row(horizontal or column)
* do not proceed, go to the row or column in the parent matrix
* */
if (j < parentMatrixColLength - (size - 1) && i < parentMatrixRowLength - (size - 1))
{
//add
for (int m = 0; m < subMatrix.GetLength(0); m++)
{
for (int n = 0; n < subMatrix.GetLength(1); n++)
{
/*check the sum of current column value and the sum of the current row value
* of the parent column length and row length if it goes out of bounds
*/
var row = i + m; var col = j + n;
//actual check here
if (row < parentMatrixRowLength && col < parentMatrixColLength)
{
subMatrix[m, n] = arr[i + m, j + n];
}
}
}
overall.Add(subMatrix);
}
}
}
//display the sub matrixes here
for (int i = 0; i < overall.Count; i++)
{
var matrix = overall[i] as int[,];
for (int y = 0; y < matrix.GetLength(0); y++)
{
for (int x = 0; x < matrix.GetLength(1); x++)
{
Console.Write(string.Format("{0} ", matrix[y, x]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
Console.WriteLine();
}
}
}
I am trying to make a matrix addition program through multithreading, in which i have to divide the rows of both matrices and run them in a separate thread, after that combine all the threads to get the final addition matrix.
i have made the program through single thread but unable to get that done with multiple threads.
this code is for addition with single thread
class Program
{
static void matrixAdd()
{
int[,] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[,] b = { { 4, 8, 7 }, { 6, 5, 4 }, { 3, 2, 1 } };
int[,] c = new int[3, 3];
int f = c.Length;
int i, m = 0;
int j = 0;
int n = 0;
for (i = 0; i < 3; i++)
{
Console.WriteLine(" ");
for (j = 0; j < 3; j++)
{
Console.Write(" " + a[i, j]);
}
}
Console.Write("\n");
for (m = 0; m < 3; m++)
{
Console.WriteLine(" ");
for (n = 0; n < 3; n++)
{
Console.Write(" " + b[m, n]);
}
}
Console.Write("\n");
for (int k = 0; k < 3; k++)
{
Console.WriteLine("");
for (int l = 0; l < 3; l++)
{
Console.Write(a[k, l] + b[k, l] + "\t");
}
}
}
static void Main(string[] args)
{
Thread t1 = new Thread(matrixAdd);
t1.Start();
Console.ReadLine();
}
I am just sorting an array and my code seem to be fine but I am unable to resolve the Array Out of Bound Exception. Any ideas.
static void Main(string[] args)
{
int temp = 0; // For temporary holding array values for swapping
int [] num = new int[] {1, 4, 7, 6, 9, 3, 0, 8, 5, 2 };
for (int i = 0; i <= 9; i++) //Outer loop
{
for (int j = 0; j < 9; j++) // Inner loop
{
if (num[j] > num[j + 1])
{
temp = num[j + 1];
num[j + 1] = num[j];
num[j] = temp;
}
}
}
//Displaying the array
for (int i = 0; i < 9; i++)
Console.WriteLine(num[i]);
Console.ReadKey();
}
Modifying the second loop to "j < loopSize - i" (instead of j < loopSize) will double the efficiency of your bubble sort. :)
int[] num = new int[] { 1, 4, 7, 6, 9, 3, 0, 8, 5, 2 };
//int comparisons = 0;
int loopSize = num.Length - 1;
for (int i = 0; i <= loopSize; i++) //Outer loop
{
for (int j = 0; j < loopSize - i; j++) // Inner loop
{
//comparisons++;
if (num[j] > num[j + 1])
{
temp = num[j + 1];
num[j + 1] = num[j];
num[j] = temp;
}
}
}
//Displaying the array
for (int i = 0; i < num.Length; i++)
Console.WriteLine(num[i]);
//Console.WriteLine("comp: {0}", comparisons);
//Console.ReadKey();
}