How to input a two-dimensional array in one loop? [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 9 years ago.
Improve this question
My teacher gave me today this mission, to built a code block in c# that input data to a two-dimensional array, in only one loop.
What is the easiest way to do so? I tried some thing with While and they didn't work at all.

You can create a counter that is the length of the first array, a counter for the length of the second array. Then increment each as you seed the arrays appropriately. I'm not sure you want actual code since this is an assignment.
You can use a while loop that checks for the counters to be a certain length to know when the arrays are finished being loaded with data.

I suspect your teacher wants to learn about modulo division operator (%). If your two dimensions are of sizes X & Y respectively, then you have a total of X*Y items in your 2D array. So you can always translate item's count into it's position in 2D array. E.g. (in pseudo code):
for(int i = 0; i < X*Y; ++i)
{
myArray[i%x, i/x] = i;
}

Related

Why does foreach allocate int arrays 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 1 year ago.
Improve this question
Why does Rider DPA say that this code allocates int arrays?
foreach doesn't itself allocate anything (except space for some locals, which aren't heap allocations in this case). It is roughly equivalent to:
int result = 0;
using (IEnumerator<int> iter = numbers.GetEnumerator()) {
while (iter.MoveNext()) {
var number = iter.Current;
result |= 1 << (number - #base);
}
}
return result;
As you can see: it has literally no direct allocations, but whatever you pass in as numbers has 4 opportunities to do anything it chooses: GetEnumerator(), MoveNext(), Current, and Dispose(). We can't tell you what the input numbers is, but there's a good chance that the tool is measuring these invisible allocations and attributing them to the foreach.
Look at whatever numbers is.

Arrays, Jagged arrays, multidimensional arrays Performance [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
I'll make it short:
int[,,,] Stats = new int [6, 4, 30, 10];
This will have 6 * 4 * 30 * 4 = 7200 elements, right?
And performance should be as good as it gets right?
If I loop through that and look for specific numbers by
if (x = y)
this should be faster than doing that with List or other things, right, because internally all is handled like the basic array, if I understand that all correctly?
Arrays are constant, vs List that is dynamic, which means when you make a new array, c# allocate memory for all those elements (in this case 7200x(int size)).
In a list, whenever you make a new entry, c# has to update the list, allocate new memory for your new int and "connect" it to the list, therefore will be slower always.
SO, if you don't care about "wasting" memory, array will be your best choice for performance.

How do i initialize an array only once #c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
char array[4] = {'1','1','1','1'}
.....
array[0] = 3
array = {'3','1','1','1'}
However, when it loops again, values reset back to 1,1,1,1
How do i keep the 3 there?
As the question states, how do initialize an array only once?
As i plan to use the array to keep track of some values in a do while loop, every time it loops again, the values get reset to 1,1,1,1. How do i only make sure that it is set to 1,1,1,1 ONCE and when it enters the loop again, whatever value that was changed STAYS and not get reset to 1,1,1,1 again.
Thanks!
At the end of a loop iteration, array goes out of scope. When you initialize array in the next iteration, it creates a completely new char[] in memory.
If you wish to keep the value of array, you need to do two things:
Only initialize it once (i.e. set it to {'1','1','1','1'} once)
Keep it in scope across iterations - i.e. declare it outside of the loop (above it, of course).

Can I add numbers to int value instead of changing it? [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 7 years ago.
Improve this question
Is it possible to add numbers to the total value of an int cell instead of set its value? Correctly I'm using a list and I and things are getting weird.. the list is <int> list and its value is jumping into random numbers without any reason. I think that changing the count type (from list into int) will solve it. (c#. visual studio 2013).
Doesn't look so hard to me...?
int variable = 10; // start with 10 points
variable++; // add 1
variable += 3 // add 3

Adding values from textbox to 2-d array or list? [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 9 years ago.
Improve this question
i have a form where values from textbox need to be stored in an 2-d array
the values are cloth length, quantity and cut value, which are entered into a listbox, so while displaying these values into labels while selecting from listbox, i use the array.
the problem is there any other data structure than 2-d array like list or the collection classes which can be useful. removing values from 2 d array is too much code every time.
You could use one of these:
List<Cloth>
Dictionary<string, Cloth>
There are no problems to remove items from array (by index I suppose):
array = array.ToList().RemoveAt(...).ToArray();
But if you plan to have many of such, then you should, as RononDex suggested, use List-based storage from start.

Categories

Resources