Sums not being calculated right - c#

Hi i am trying to calculate the sums of the submatrix of order K from a matrix of order M, but I am getting wrong result from the sums matrix. In my head the logic makes sense, I don't know what the mistake is.
static void Main(string[] args)
{
Console.WriteLine("Shkruani te madhesine e matrices dhe madhesine e submatrices: ");
int M;
int K;
int sum = 0;
M = int.Parse(Console.ReadLine());
K = int.Parse(Console.ReadLine());
int[] sums = new int[M - K + 1];
int[] matrix = new int[M];
Console.WriteLine("Shkruani vlerat e matrices: ");
foreach (int i in matrix)
{
matrix[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i <= M - K; i++)
{
for (int j = 0; j < K; j++)
{
sum += matrix[i + j];
}
sums[i] = sum;
sum = 0;
}
for (int i = 0; i < (M - K + 1); i++)
{
Console.WriteLine(sums[i]);
}
Console.ReadKey();
}

Instead of using
foreach (int i in matrix)
{
matrix[i] = int.Parse(Console.ReadLine());
}
Use this:
for (int i = 0; i < matrix.Length; i++)
{
matrix[i] = int.Parse(Console.ReadLine());
}
Explanation:
Because For-Each loop works with value (iterate through value) instead of index (May be what you want) while for loop works with index (iterate through index).
Hope this helps.
Thank you.

Related

The Snake in the 2D array

I have to write a program that creates a two dimensional array and fills it like a snake. Here is the task itself:
Blockquote write a program that creates a two-dimensional numeric array and this array is filled with a "snake": first the first row (from left to right), then the last column (from top to bottom), the last row (from right to left), the first column (from bottom to top), the second row ( left to right), etc.
I tried to write something, but bugs constantly appear. Please help to shorten this code and make it correct.
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int len = rand.Next(5, 20);
int wid = rand.Next(5, 20);
int[,] arr = new int[len, wid];
int num = 10;
int line = 0;
int line2 = len;
int col = wid - 1;
int col2 = 0;
for (int i = 0; i <= (len > wid ? len : wid); i++)
{
for (int k = 0; k < wid; k++, num++)
{
arr[line, k] = num;
}
line++;
for (int k = 0; k < len; k++, num++)
{
arr[k, col] = num;
}
col++;
for (int k = (wid - 1); k >= 0; k--, num++)
{
arr[line2, k] = num;
}
line2++;
for (int k = 0; k < (len - 1); k++, num++)
{
arr[k, col2] = num;
}
col2++;
}
for (int i = 0; i <= len; i++)
for (int q = 0; q <= wid; q++)
{
Console.WriteLine(arr[i, q] + " ");
}
}
}

how to remake 2d array random unique numbers as 3d?

static void Main(string[] args)
{
Random r = new Random();
int[,] x = new int[10,8];
int[] temp = new int[x.Length];
// two dimensional array and i want for three dimensional array
for(int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 100);
for(int j = 0; j < i; j++)
{
if(temp[i] == temp[j])
{
i--;
break;
}
}
}
for(int i = 0, index = 0; i < x.GetLength(0); i++)
{
for(int j = 0; j < x.GetLength(1); j++)
{
x[i, j] = temp[index++]; //two dimensional array unique numbers
Console.Write(x[i, j] + " ");
}
}
// i want do it as for 3D and 4 D array unique numbers like that method what can i change or add?
It's pretty straight forward to do what you're doing for higher dimensions.
Here's my code for 3D:
var r = new Random();
int [,,] x = new int[10, 8, 8];
var count =
Enumerable
.Range(0, x.Rank)
.Select(y => x.GetLength(y))
.Aggregate((y, z) => y * z);
var values =
Enumerable
.Range(10, count)
.OrderBy(y => r.Next())
.ToArray();
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
x[i, j, k] = values[v++];
To change it to 4D these lines change:
int [,,,] x = new int[10, 8, 8, 12];
// ...
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
for (var l = x.GetLowerBound(3); l <= x.GetUpperBound(3); l++)
x[i, j, k, l] = values[v++];
Now, in this code I have explicitly called GetLowerBound as well as GetUpperBound as it is possible in .NET code to have a non-zero based array.
Also, rather than repeatedly re-try getting random numbers until you have unique numbers I simply generated a sequence of unique numbers and then randomly sorted them. That's a little different from your original code. You needed 80 (10 x 8) random values and you were choosing from values ranging from 10 to 99 inclusive. So you had some holes in your numbers.
Random r = new Random();
int[,,] x = new int[10, 8, 8];
int[] temp = new int[x.Length];
#region one dimensional array unique numbers.
for (int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 650);
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
i--;
break;
}
}
}
#endregion
for (int i = 0, index = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
for (int k = 0; k < x.GetLength(2); k++)
{
x[i, j, k] = temp[index++];
Console.Write(x[i, j, k] + " ");
}
Console.WriteLine();
}
}// i think it's correct code i've changed it

C#: Build 3D-Array and fill it with data

The code I tried so far below:
private static List<List<List<int>>> threeDArrayToThreeDList(int [,,] letters) {
// 3d-array to 3d-list
List<List<List<int>>> letterslist = new List<List<List<int>>>();
List<List<int>> sublist = new List<List<int>> ();
List<int> subsublist = new List<int> ();
for (int i = 0; i < 2; i++) {
letterslist.Add (sublist);
for (int j = 0; j < 2; j++) {
letterslist[i].Add (subsublist);
for (int k = 0; k < 2; k++) {
Console.WriteLine (letterslist [i][j][k]); // Element not found
Console.WriteLine (letters [i,j,k]);
letterslist [i] [j] [k] = letters [i,j,k];
}
}
}
return letterslist;
}
Why letterslist [i][j][k] isn't found?
Your code is wrong. You need to create a list for each "index". You're code only creates 3 lists altogether.
Here's how it should work:
private static List<List<List<int>>> threeDArrayToThreeDList(int [,,] letters) {
// 3d-array to 3d-list
List<List<List<int>>> letterslist = new List<List<List<int>>>();
for (int i = 0; i < 2; i++) {
letterslist.Add (new List<List<int>> ());
for (int j = 0; j < 2; j++) {
letterslist[i].Add (new List<int> ());
for (int k = 0; k < 2; k++) {
Console.WriteLine (letters [i,j,k]);
letterslist [i] [j].Add(letters [i,j,k]);
}
}
}
return letterslist;
}
This because the letterslist [i][j] list has no elements in it.
Add an element to it, and it will get you through the line that causes the exception.
Change the code in the innermost loop as follows:
for (int k = 0; k < 2; k++) {
letterslist[i][j].Add (letters[i, j, k]);
Console.WriteLine(letterslist[i][j][k]); // Should work fine
Console.WriteLine(letters[i, j, k]);
}

Merge Sort code is not working and showing exception

public static void Merge(int[] arr,int p,int q,int r )
{
int n1 = q-p;
int n2 = r-q;
int[] L=new int[n1];
int[] R = new int[r-n2];
for (int i = 0; i < n1; i++)
L[i] = arr[i];
foreach (int x in L)
Console.WriteLine(x);
for (int i = 0; i < n2; i++)
R[i] = arr[q+i];
Console.WriteLine("New part");
foreach (int x in R)
Console.WriteLine(x);
int k=0, d=0;
for (int i = p; i < r; i++)
{
if (L[k] <= R[k])
{
arr[i] = L[k];
k++;
}
else
{
arr[i] = R[d];
d++;
}
}
}
The above code shows exception(index out of bound when I call from main() method using Merge(arr,0,0,12). Where arr is an int array of length 12.
You get an Index out of bounds exception in this part:
for (int i = 0; i < n2; i++)
R[i] = arr[q+i];
Your R-array is of size 0, while n2 is defined as 12 with the given arguments.
You array L is declared of size q-p, which are both 0
Both you L and R array are defined as too small
Initialize them like this instead:
int[] L = new int[arr.Length];
int[] R = new int[arr.Length];

Creating a 3x3 matrix with user input numbers C#

im trying to create a 3x3 matrix in c# language, i know how to create the matrix but i need help for user input numbers. I hope someone can help me thank you for that.
I will add a while loop and use double.TryParse to validate user's input. Usin BWHazel's code:
const int MATRIX_ROWS = 3;
const int MATRIX_COLUMNS = 3;
double[,] matrix = new double[MATRIX_ROWS, MATRIX_COLUMNS];
for (int i = 0; i < MATRIX_ROWS; i++)
{
for (int j = 0; j < MATRIX_COLUMNS; j++)
{
double input;
Console.Write("Enter value for ({0},{1}): ", i, j);
while (!double.TryParse(Console.ReadLine(), out input)
{
Console.Write("Enter correct value for ({0},{1}): ", i, j);
}
matrix[i,j] = input
}
}
To get the totals for all rows you can use following snippet:
for (int i = 0; i < MATRIX_ROWS; i++)
{
// The some for each row
double sum = 0.0;
for (int j = 0; j < MATRIX_COLUMNS; j++)
{
sum += matrix[i,j];
}
Console.WriteLine(string.format("The sum for row {0} is: {1}", i, sum));
}
If you are using the command-line, something like this should work:
const int MATRIX_ROWS = 3;
const int MATRIX_COLUMNS = 3;
double[,] matrix = new double[MATRIX_ROWS, MATRIX_COLUMNS];
for (int i = 0; i < MATRIX_ROWS; i++)
{
for (int j = 0; j < MATRIX_COLUMNS; j++)
{
Console.Write("Enter value for ({0},{1}): ", i, j);
matrix[i,j] = double.Parse(Console.ReadLine());
}
}
This assumes you are using double for the values. The .Parse() method is available for all .NET numeric types including int.
private void button1_Click(object sender, EventArgs e)
{
txtResult.Text=GenerateMatrix(Int32.Parse(txtRow.Text), Int32.Parse(txtColumn.Text));
}
private string GenerateMatrix(int Row,int Column)
{
string matrix = string.Empty;
string Result = string.Empty;
int nxtline=0;
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Column; j++)
{
if (nxtline==Column)
{
matrix = matrix + Environment.NewLine;
nxtline = 0;
}
matrix = matrix+"*";
nxtline = nxtline + 1;
}
}
Result = matrix;
return Result;
}

Categories

Resources