C# How to: 2d array of jagged arrays? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make a multidemesional array of jagged arrays. Is this possible? How?
For Instance I see lokts of examples like the following:
int[][,] jaggedArray4 = new int[3][,]
I want to create the following:
int[,,][] myFixedJagged = new int[2,2,3][]
where the last [] is Jagged. How can I declare that?
Thanks!

This just works:
int[,,][] myFixedJagged = new int[2, 2, 3][];
myFixedJagged[0, 0, 0] = new int[10];
myFixedJagged[0, 0, 0][9] = 1;
I wouldn't want to use it though.

Is this what you want?
static void Main(string[] args)
{
// This is the silliest thing I have seen in my life
int[, ,][][] jgarray=new int[2, 2, 3][][];
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<3; k++)
{
var array =new int[10][];
for (int z=0; z<10; z++)
{
array[z]=new int[20];
for (int v=0; v<20; v++)
{
array[z][v]=v+20*(z+10*(k+3*(j+2*i)));
}
}
jgarray[i, j, k]=array;
}
}
}
}

Related

How to take 'snapshot' of variable value before a for loop? C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
static void Main(string[] args)
{
Random rnd = new Random();
float myfloat = rnd.Next(1, 50);
for (int i = 0; i <= 2; i++)
{
myfloat = 3;
}
Console.WriteLine(myfloat);
Console.ReadLine();
}
Say I want to change the value of a variable for the duration of a for loop, but then I want it to go back to whatever it was just befoe the loop (if i don't know what that value was). How would I do something like that?
With this code 'myfloat' is stuck at 3 after the loop ends.
I recommend using a "temporary" variable.
static void Main(string[] args)
{
Random rnd = new Random();
float myfloat = rnd.Next(1, 50);
float tempFloat = myfloat;
for (int i = 0; i <= 2; i++)
{
myfloat = 3;
}
myfloat = tempFloat;
Console.WriteLine(myfloat);
Console.ReadLine();
}
It could look something like this.

How to separate and move nonnegative and positive values between arrays? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How to write a method which moves nonnegative values (found in two given arrays) into the first array, then moves negative values (found in two given arrays) into the second array? For example: for {1, -2, 5} and {-2, 4, -9} we should receive {1, 5, 4} and {-2, -2, -9}. The even split between nonnegative/negative numbers is not guaranteed. The sizes of arrays can be changed.
static void Arrays(ref int[] tab1, ref int[] tab2){
int[] tempNegative = new int[3];
int[] tempNonNegative = new int[3];
for (int i = 0; i < tab1.Length; i++)
{
for (int j = 0; j < tab2.Length; j++)
{
if (tab1[j] < 0)
{
tempNegative[j] = tab1[j];
}
else
{
tempNonNegative[i] = tab1[j];
}
}
}
tab1 = tempNonNegative;
tab2 = tempNegative;
}
After using this method in a main program I received {5, 5, 5} and {0, -2, 0}. I know that there should be another loop for checking values in the second array. The question is how to put a value in a proper position in those arrays?
There's 2 pieces of evidence in your question which leads me to the following way to solve it:
You state that "The sizes of arrays can be changed"
You use ref when declaring the parameters to the method
I've already commented why the first one is actually a fallacy, but these two taken together means that there's two ways of writing the solution to your question.
Using LINQ
Doing it manually
I'll do it manually first.
The easiest way is to declare two lists, put each number into one if it is a negative and the other if it isn't, and then reassign the parameters to a new array at the end, like this:
static void Arrays(ref int[] tab1, ref int[] tab2)
{
var negatives = new List<int>();
var positives = new List<int>();
foreach (var item in tab1)
if (item < 0)
negatives.Add(item);
else
positives.Add(item);
foreach (var item in tab2)
if (item < 0)
negatives.Add(item);
else
positives.Add(item);
tab1 = positives.ToArray();
tab2 = negatives.ToArray();
}
Now, if we allow a slight bit of LINQ we can combine the two arrays into one collection, and thus go down to one foreach:
static void Arrays(ref int[] tab1, ref int[] tab2)
{
var negatives = new List<int>();
var positives = new List<int>();
foreach (var item in tab1.Concat(tab2))
if (item < 0)
negatives.Add(item);
else
positives.Add(item);
tab1 = positives.ToArray();
tab2 = negatives.ToArray();
}
The second approach is to use LINQ altogether:
static void Arrays(ref int[] tab1, ref int[] tab2)
{
var negatives = tab1.Concat(tab2).Where(i => i < 0).ToArray();
tab1 = tab1.Concat(tab2).Where(i => i >= 0).ToArray();
tab2 = negatives;
}
It has to use a temp variable, negatives, otherwise we'd destroy the contents of tab2 before we got to working out what to put in tab1.
If we rewrite the whole method using newer C# syntax, such as Expression-bodied members (C# 6+) and Tuples (C# 7+), we get:
static void Arrays(ref int[] tab1, ref int[] tab2)
=> (tab1, tab2) = (
tab1.Concat(tab2).Where(i => i >= 0).ToArray(),
tab1.Concat(tab2).Where(i => i < 0).ToArray()
);
It's an easy task with 2 indexes, I'll write the pseudocode:
int pos = 0, neg = 0;
for(int i = 0; i < tab1.Length(); i++){
if(tab1[i] < 0) tempNegative[neg++] = tab1[i];
else tempNonNegative[pos++] = tab1[i];
}
for(int i = 0; i < tab2.Length(); i++){
if(tab2[i] < 0) tempNegative[neg++] = tab2[i];
else tempNonNegative[pos++] = tab2[i];
}
This should do the trick. The algorithm loops through both arrays and put their values in the right arrays.

Combination of two arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a integer array that shows the nodes of a path : {1,2,3,4,5}
1 --> 2 --> 3 --> 4 --> 5
But some nodes of this path can be optional.
For example node 4 is optional.
So, I can use this path 1 --> 2 --> 3 --> 4 --> 5
or this path 1 --> 2 --> 3 --> 5 to reach my destination.
I want to produce all path combinations.
//ProduceCombinations(int[] path,int[] possibleNodes)
{1*,2,3,4*,5*}
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
Here you go:
static void Main(string[] args)
{
int[] pathNodes = new int[] {1,2,3,4,5};
int[] optionalNodes = new int[] { 1, 4, 5 };
List<int[]> combies = ProduceCombinations(pathNodes, optionalNodes);
}
public static List<int[]> ProduceCombinations(int[] PathNodes, int[] OptionalNodes)
{
List<int[]> results = new List<int[]>();
results.Add((int[])PathNodes.Clone());
int index = 0;
for (int j = 0; j < OptionalNodes.Length; j++)
{
while (PathNodes[index] < OptionalNodes[j]) index++;
int lenght = results.Count;
for(int i = 0; i < lenght; i++)
{
var newSol = (int[])results[i].Clone();
newSol[index] = 0;
results.Add(newSol);
}
}
return results;
}

How to Zip two numbers using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
string A = "1234"
string B = "567890"
I want to zip the numbers. Out put should display as "1526374890"
what is the best way to achieve this using C# code.
You can do that with the following code. It does not make any assumptions about which string is longer.
string A = "1234";
string B = "567890";
char[] chars = new char[A.Length + B.Length];
int charsIndex = 0;
for (int i = 0; i < A.Length || i < B.Length; i++)
{
if(i < A.Length)
chars[charsIndex++] = A[i];
if(i < B.Length)
chars[charsIndex++] = B[i];
}
string result = new string(chars);
Console.WriteLine(result);

Having trouble counting in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

Categories