So I have a number array like this
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
And know what I want to know is how to make it read the number 7 after the number 3, and number 8 after number 4. And so on, like this:
0 -> 1 -> 2 -> 3
|
\/
4 <- 5 <- 6 <- 7
|
\/
8 -> 9 -> 10 -> 11
|
\/
12 <- 13 <- 14 <- 15
However if I use nested incremental for it will read in normal sequential order.
I have no idea how to make it read from 3 to 7, 4 to 8 and so on...
How can I achieve that?
You can store a flag indicating if the next iteration is going to start on the left or right. So...
static void Main(string[] args)
{
// initializing values
int[,] arr = new int[4, 4];
int n = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arr[i, j] = n++;
}
}
n = 0;
// initialization end
// starts from the left
bool left = true;
// go line by line
for (int i = 0; i < 4; i++)
{
// if it starts from the left, set start point a index 0
// otherwise start from max index
for (int j = left ? 0 : 3; left ? j < 4 : j >= 0; )
{
arr[i, j] = n++;
// increment/decrements depending on the direction
j = left ? j + 1 : j - 1;
}
// if it started from the left, the next iteration will
// start from the right
left = !left;
}
}
Results:
Initialized:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
After navigating on it:
0 1 2 3
7 6 5 4
8 9 10 11
15 14 13 12
If you have:
_arr = new int[,] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } };
Then try:
public void PrintArr()
{
for (int i = 0; i < 4; i++)
{
if (i % 2 == 0)
{
for (int j = 0; j < 4; j++)
Console.Write(_arr[i, j] + " ");
Console.WriteLine();
}
else
{
for (int j = 3; j >= 0; j--)
Console.Write(_arr[i, j] + " ");
Console.WriteLine();
}
}
}
Here's a short, simple solution:
int h = array.GetLength(0), w = array.GetLength(1);
for(int i = 0, j = 0, c = 1; i < h; i++, c = -c, j += c)
{
for(int k = 0; k < w; k++, j += c)
{
Console.WriteLine(array[i, j]);
}
}
Related
I am trying to print out a pyramid/Pascal Triangle that will give output in series of 3. You can see it in the example below.
1
1 3 1
1 3 9 3 1
1 3 9 27 9 3 1
1 3 9 27 81 27 9 3 1
1 3 9 27 81 243 81 27 9 3 1
1 3 9 27 81 243 729 243 81 27 9 3 1
1 3 9 27 81 243 729 2187 729 243 81 27 9 3 1
Instead of getting the above output, I am getting this:
Here is my code:
using System;
namespace ConsoleApp
{
class PiscalTriangle
{
static void Main(string[] args)
{
Console.WriteLine("Enter length : ");
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < num; i++)
{
for (int j = num; j > i; j--)
{
Console.Write(" ");
}
int val = 1;
for (int j = 0; j <= i; j++)
{
Console.Write(val + " ");
val = val * (i - j) / (j + 1);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
There are three main aspects to be considered for generating the desired ouput:
The depth of the pascal tree which is defined with the help of num variable and an iterator i which ranges from 1 to num
The elements in each row of the pascal tree are in the sequence 1,3,5,7.. which when correlated with depth of tree is equivalent to 2i-1. Hence the row values iterator j ranges from 1 to 2i-1
And finally the value (For example 3rd row, 1 3 9 3 1), can be written in the form of 3 power x, where x increases from 0 to i and then decreases to 0
By summing up the 3 conditions, we can get our desired output.
using System;
namespace ConsoleApp
{
class PiscalTriangle
{
static void Main(string[] args)
{
Console.WriteLine("Enter length : ");
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= num; i++)
{
for (int j = num; j > i; j--)
{
Console.Write(" ");
}
var x = 0;
for (double j = 1; j <= 2*i-1; j++)
{
double val = Math.Pow(3, x);
Console.Write(val + " ");
if(j < i)
{
x++;
}
else
{
x--;
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
I wrote a program for addition sparse Matrix in C#.
Could you please check my program work correctly?
First element in this array is a row, the second is column and the third is value.
Matrix 1:
1 2 8
1 3 10
2 4 5
3 1 9
Matrix 2:
0 2 3
1 3 10
4 4 5
3 1 1
3 3 15
1 4 66
1 1 25
using System;
using System.Collections.Generic;
using System.Text;
namespace CSharp
{
class Program
{
public static void Main(String[] args)
{
List<int[]> Addition = new List<int[]>();
List<int[]> listArray1 = new List<int[]>();
listArray1.Add(new int[] { 1,2,8});
listArray1.Add(new int[] { 1, 3, 10 });
listArray1.Add(new int[] { 2, 4, 5 });
listArray1.Add(new int[] { 3, 1, 9 });
List<int[]> listArray2 = new List<int[]>();
listArray2.Add(new int[] { 0, 2, 3 });
listArray2.Add(new int[] { 1, 3, 10 });
listArray2.Add(new int[] { 4, 4, 5 });
listArray2.Add(new int[] { 3, 1, 1 });
listArray2.Add(new int[] { 3, 3, 15 });
listArray2.Add(new int[] { 1, 4, 66 });
listArray2.Add(new int[] { 1, 1, 25 });
for (int i = 0; i < listArray1.Count; i++)
{
for (int j = 0; j < listArray2.Count; j++)
{
if (listArray1[i][0] == listArray2[j][0])
{
if (listArray1[i][1] == listArray2[j][1])
{
Addition.Add(new int[] { listArray1[i][0], listArray1[i][1], listArray1[i][2]+ listArray2[j][2] });
listArray1.RemoveAt(i);
listArray2.RemoveAt(j);
break;
}
}
}
}
for (int i = 0; i < Addition.Count; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{Addition[i][j]} ");
}
Console.WriteLine();
}
for (int i = 0; i <listArray1.Count; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{listArray1[i][j]} ");
}
Console.WriteLine();
}
for (int i = 0; i < listArray2.Count; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{listArray2[i][j]} ");
}
Console.WriteLine();
}
}
}
}
Program out is:
1 3 20
3 1 10
1 2 8
2 4 5
0 2 3
4 4 5
3 3 15
1 4 66
1 1 25
I make this code to print array if the number of array is
{
1, 2, 3, 4, 5,
6, 7, 8, 9,
10,11,12,13,
14,15,16,17,
18,19,20,21,
22,23,24,25
}
the out put of this code is
1,2,6,3,7,11,4,6,12,16,5,9,13,17,21,10,14,18,21,15,19,23,20,24,25
I want to make change in this my code to be the out put begging from
21 16 22 11 17 23 6 12 18 24 1 7 13 19 25 2 8 14 203 9 15 4 10 5
and this my Code
int [,] p = new int [5,5];
int sum = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
p[i, j] = sum;
richTextBox1.Text += p[i, j].ToString();
sum++;
}
}
int C;
int R=1;
for (int i = 1; i <= 5; i++)
{
C = i;
for (int r = 1; r <= i; r++)
{
Output = Output + p[r, C];
C--;
}
}
richTextBox2.Text += Output.ToString();
for (int i = 2; i >= 5; i++)
{
R = i;
for (C = 5; C >= i; C--)
{
Output = Output + p[R, C];
R++;
}
}
richTextBox2.Text += Output.ToString();
This is more fiddly than you might think!
You want to traverse the diagonals of this matrix:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
starting at the bottom left and traversing each diagonal as depicted in this very badly-drawn diagram:
I've written a helper method called DiagonalIndices() that for a given maximum index (in the case of your 5x5 matrix, it will be 4) will produce a sequence of (row,col) indices in the correct order to traverse all the diagonals.
Note that the input array MUST be square - I'm assuming that is the case for your data! It clearly is for a 5x5 matrix.
The output of the following program is
21 16 22 11 17 23 6 12 18 24 1 7 13 19 25 2 8 14 20 3 9 15 4 10 5
Here's the code:
using System;
using System.Collections.Generic;
namespace ConsoleApplication4
{
public sealed class Index
{
public Index(int row, int col)
{
Row = row;
Col = col;
}
public readonly int Row;
public readonly int Col;
}
public static class Program
{
private static void Main()
{
int [,] p = new int[5, 5];
for (int i = 0, n = 1; i < 5; ++i)
for (int j = 0; j < 5; ++j, ++n)
p[i, j] = n;
// This is the bit you will use in your program.
// Replace the Console.WriteLine() with your custom code
// that uses p[index.Row, index.Col]
int maxIndex = p.GetUpperBound(1);
foreach (var index in DiagonalIndices(maxIndex))
Console.Write(p[index.Row, index.Col] + " ");
Console.WriteLine();
}
public static IEnumerable<Index> DiagonalIndices(int maxIndex)
{
for (int i = 0; i <= maxIndex; ++i)
for (int j = 0; j <= i; ++j)
yield return new Index(maxIndex-i+j, j);
for (int i = 0; i < maxIndex; ++i)
for (int j = 0; j < maxIndex-i; ++j)
yield return new Index(j, i+j+1);
}
}
}
This is a more efficient of printing only the diagonal values in a positive direction in a matrix.
int [,] p = new int [5,5]
{
{1, 2, 3, 4,5},
{6, 7, 8, 9},
{10,11,12,13},
{14,15,16,17},
{18,19,20,21},
{22,23,24,25}
};
for (int i = 4, j = 4; i >= 0 && j > = 0; i--, j--)
{
Console.Writeline(p[ i, j ]);
}
Basic C# question:
I need to have that result when entering some number (this case was entered 4):
4 3 2 1 0
3 2 1 0 4
2 1 0 4 3
1 0 4 3 2
I was trying that code, but cant figure out my mistake:
Console.WriteLine("Please write a Number: ");
Console.Write("Number: ");
int num = int.Parse(Console.ReadLine());
for (int i = 0; i <= num; i++)
{
for (int j = num - i; j >= 0; j--)
{
Console.Write(j);
}
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadLine();
This is the output I get:
4 3 2 1 0
3 2 1 0 1
2 1 0 1 2
1 0 1 2 3
0 1 2 3 4
Try this:
Console.WriteLine("Please write a Number: ");
Console.Write("Number: ");
int num = int.Parse(Console.ReadLine());
for (int i = 0; i <= num; i++)
{
for (int j = num - i; j >= 0; j--)
{
Console.Write(j);
}
for (int j = num; j > num - i; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadLine();
The problem is that your second inner loop is starting at one and counting up rather than starting from num and counting down.
Change that loop to:
for (int j = num; j > num -i; j--)
{
Console.Write(j);
}
Also I'm not clear if you want the last line of 04321 or not. If you don't (as in the original example) then just change your loop check to i<num.
Try something like this
get a number(x) from user.
create a list of integer containing x to 0.
run a loop for x times.
every time print the list and pop the first number and push it at the end
var ints = new List<int> { 4, 3, 2, 1, 0 };
for (int i = 0; i < 4; i++)
{
ints.ForEach(n => Console.Write(n + " "));
Console.WriteLine("");
var a = ints[0];
ints.RemoveAt(0);
ints.Add(a);
}
As a hint I give you the main loop as a pseudo code:
for i from 0 to number_input-1 {
for j from number_input to 0 {
print((j-i)%(number_input+1) + " ")
}
print("\n")
}
Just for fun:
const int NUM = 4; // num from user
for (int start = NUM; start > 0; start--)
{
for (int i = 0; i <= NUM; i++)
{
int current = (start - i) >= 0 ? start - i : NUM + (start - i) + 1;
Console.Write(current + " ");
}
Console.WriteLine();
}
Honestly this is a classic sorting task. it's just hidden beyond "user types and bla bla bla" but I remember at school it was..
There is an array [4,3,2,1,0].. so
we swap 1 and 2 and get [3,4,2,1,0].
we swap 2 and 3 and get [3,2,4,1,0].
we swap 3 and 4 and get [3,2,1,4,0].
we swap 4 and 5 and get [3,2,1,0,4].
so just simple code
int[] numbers let say you have this array [4,3,2,1,0]
for(int i = 0; i < numbers.length - 2; i++){
for(int y = 0; y < numbers.length - 1; y++){
int buf = numbers[y];
numbers[y] = numbers[y + 1];
numbers[y + 1] = buf;
}
}
how can we find out different combination of the elements of an array using c# code.
are there any inbuilt library function for this.?
for eg: suppose an array has elements {2,3,4,5,6,7}
then the possible combination would be 2,3,4,5,6,7,2 3,2 3 4,2 3 4 5, etc
so basically wat i need is a function which gives different combination based on its input for eg: comb(array,2) gives output 2 3,1 2,3 4 and comb(array,3) gives output 1 2 3,2 3 4,3 4 5 and so on
Eg: valid comnbination for array= {1, 2, 3} and length = 2 are 1 2,1 3,2 3 .....
static void Main()
{
var cnk = comb(new [] {1,2,3},2);
foreach ( var c in cnk)
{
}
}
public static IEnumerable<int[]> comb(int[] a, int k)
{
if (a == null || a.Length == 0 || k < 1 || k > a.Length)
yield break;
int n = a.Length;
// 1
if ( k == 1)
for ( int i = 0; i < n; i++)
{
yield return new int[] {a[i]};
}
else
{
// k
for ( int i = 0; i < n - k + 1; i++)
{
var res = new int[k];
for (int t = i, c = 0; t < i + k - 1; t++, c++)
res[c] = a[t];
for (int j = i + k - 1; j < n; j++)
{
res[k-1] = a[j];
yield return res;
}
}
}
}
You should take the algorithm from here, my answer doesn't solve your problem
Algorithm to return all combinations of k elements from n
Seemed logic is not absolutely correct as:
var cnk = comb(new[] { 1, 2, 3, 4 }, 3);
This gives 3 variants, but as a matter of fact it is 4:
1 2 3
1 2 4
1 3 4
2 3 4
I guess comb is better to be implemented in recursive way.