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

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).

Related

Copying an Array from a line, into an Array Object - "_numbersArray is never assigned to, and will always have its default value null" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am creating an application that will read in a CSV file, with 5 x lotto numbers on each line.
For each line, I take the array of lotto numbers (named csvArray in the screenshot), and create a "LottoLine" object.
(The LottoLine object is basically just another array)
As you can see I have created my _numbersArray array, then in the constructor I have looped through each element on the csvfile array, to update the values of _numbersArray.
However my _numbersArray for some reason is not allowing the numbers of the csvfile array to copy across and I am receiving the warning "arrayName is never assigned to, and will always have its default value null"
I'm very new to Object-Oriented Programming so I have tried winging it with:
creating a get set property for the _numbersArray
changed the field from public to protected
adding static to the _numbersArray field
using the array.copy method
All failures obviously.
Really appreciate any advice on this issue as well as my question format, please let me know if I should elaborate more or provide better context etc.
You don't have created an Array in you example. In C# you need to call the constructor of the array with a fixed size. So your first line inside of the constructor should be something like this:
_numbersArray = new int[42]
Then you have an error inside your loop. Iterating and accessing elements of an array, without knowing the bounds is dangerous.
In order to make this better you should change your constructor to something like this:
_numbersArray = new int[6];
for(int i = 0; i < vcsArray.length && i < _numbersArray.length; i++)
{...

C#/Unity - How to get only Distinct values (with threshold) and a specific count? [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 the following Problem:
I have a list Vector3 positions of an object I tracked in Unity (e.g. an animated sphere flying some track). I am adding the position of it every frame to the List. Which leads to a sum of e.g. 500 values. When it's animation stopped i want to "clean" the recorded track and only keep the most distinct values (in the right order). But the List.Count has to be EXACTLY 100. So it has to check for "equal" values with a threshold.
What i've done so far: I am recording the position of "trackableObject" as Vector3 in every frame and cleaning it directly (only keep values that are further away than 'cleaningDiffTolerance' and/or have a greater angle difference then 'cleaningAngleMaxDiff'). This way i am getting only values for significant changes in direction/distance and get more points i curves/corners.
What i want: Do the cleaning not every frame but do it after i stopped recording. Then I want to only keep the most distinct values in correct order and exactly 100 values.
It depends how precise your result needs to be (and how you define 'precise').
The first question would be:
Must the 100 values be exact position values from the first list, or is it ok if it is near.
Position values won't change much every frame. An easy way tou solve the problem would be to average every step:
Compute how many values must be grouped together: n = totalValues/100
Take the first n values and store the average in your final list
Do the same for the next n values, and so on
Alternatively, if you need to have exact values, replace step 2 by "take first value of the groupe" for example.
This approche will be precise enough if the move is smooth.
The problem with this is that if you have a sudden position change (like an angle instead of a smooth turn), you will likely not get the exact position at which the angle occure. The only solution to identify these is to do some more advance analysis. You can search on google for "High Pass Filter" for instance.
I would recommend trying the simple approach first and see if it is fine for your needs

Loop not going inside when the value is same [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
you can see that value is 74 but its not going inside the if why?
Because you have a space in TEMP. " 74". The two values are not equal because the second one TEMP2 is "74" without the space.
Try using Trim on your values to remove whitespace:
string temp = ids[i].Trim();
string temp2 = raditem.Value.ToString().Trim();
Also, always use the .Equals overload for the String class, as you can control the culture for the string comparison. In this case it doesn't matter because they're numerals but it could matter if you want to compare to alphabetical strings where you want "A" to equal "a" and so forth.
Furthermore, if you are sure your array contains only numbers, I could recommend converting the values to numbers before comparing them.
// one way to validate is wrap this in a try-catch and handle input format error.
int temp = Convert.ToInt32(ids[i].Trim());
That way if you have a case where there aren't numbers, you could validate and complain to the user or whatever you want.

Shortest path that goes through different points [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 5 years ago.
Improve this question
Let's say I have few points : -5,-4,-3,-2,-1,0,1,2,3,4,5
I'm at point 0, I need to create a line that goes all through the points of 1,2,3,4,5,-1,-2... etc.
The line would start at 0 and end at whatever point that ends as the shortest.
The answer for this example would be that it'd go like this 0->1->2->3->4->5->-1->-2->-3->-4->-5 or that it'd go first to -1 and go all through the minus to the plus, same result (5*4=20 length).
If for example we'd go 0->1->-1->2->-2... it'd end as the longest line that goes straight from point to point (1+2+3+4+5+6+7+8+9+10=10*11/2=55 length)
The question is how to write this in code?
The points might also consist of 2 or 3 dimensional points, where the start would be (0,0,0,0) or whatever, eventually the line can go through all of these points, but which way will achieve the shortest line?
How to make it as a code, as we see it in the eye?
I think this is basically the Travelling Salesman problem. You've got N destinations, and each pair of destinations has a concrete length between them, and you're trying to find out the shortest travel time to visit all destinations.
You've got two different directions to pursue this, that I can see. First, is to read up on the Travelling Salesman problem and the various algorithms that have been proposed for it (it's a very famous algorithm problem) and then try to implement one in C# - though just to warn you, you should be very proficient in math, because it's not an easy problem. Or, alternatively, you can look for someone else's existing implementation for it and just use it without understanding the theoretical underpinnings.

How to input a two-dimensional array in one loop? [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
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;
}

Categories

Resources