How to initialize an array by default in a single row C#? - c#
I have this line of code and I want under certain conditions to initialize the whole array to true in one line,
How can I do that?
public bool[,] OptionalHours { get; set; } = new bool[6, 24];
Technically, this would do it:
for(int i = 0, x = _optionalHours.GetLength(0), y = _optionalHours.GetLength(1); i < x * y; _optionalHours[i/y, i%y] = true, i++);
It gets shorter if you're happy to fix the 6 and 24 and jiggle some ops around:
for(int i = 6*24; --i >= 0; _optionalHours[i/24, i%24] = true);
Would you use it though? Well; could you understand it immediately and are happy to explain it to anyone else who asks? If yes, then use it. If no then go for something more expected:
for(int x = _optionalHours.GetUpperBound(0); x >= 0; x--)
for(int y = _optionalHours.GetUpperBound(1); y >= 0; y--)
_optionalHours[x, y] = true;
There aren't any prizes at code review for the person who wrote the most WTF..
It's also quite a small array (hours in a week?) so you could also init it with this one-liner that's easier to figure:
var _optionalHours = new[,]{
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true}
};
As an idle curiosity, you could also do it "mathematically", wrap the array in a class that will invert the bools if some condition is set:
public class SixByTwentyFour
{
private bool[,] _optionalHours { get; set; } = new bool[6, 24];
public bool CertainCondition;
public bool this[int x, int y]{
get => _optionalHours[x,y] ^ CertainCondition;
set { _optionalHours[x,y] = value ^ CertainCondition; }
}
}
You can access an instance of this class as though it were a 2D array:
var sbtf = new SixByTwentyFour();
sbtf.CertainCondition = true;
Console.WriteLine(sbtf[1,1]); //prints true
This one doesn't give you a "Properties should not return arrays" warning if you have an analayzer enabled that looks for such :D
Related
How can I add objects Cbook to my class CBooks without using lists
Cbooks has an atribute "CTeam[] Teams" and it is of fixed size (8). If I want to add objects to it using this in the Main: CBook A1 = new CBook("Title1", "Author1"); CBook A2 = new CBook("Title1", "Author2"); CBooks ArrayOfBooks = new CBooks(8); ArrayOfBooks.Add(A1); ArrayOfBooks.Add(A2); then position 0 and 1 are ocuppied, and the positions from 2 to 7 are null. What I want to do is, using a variable "int aux=0", count the ocupied positions like this: for (int k = 0; k < NumberOfTeams; k++) { if (Teams[k].Name=="") Aux += 1; } So, Aux in this case would be 2, then I want to do "Teams[Aux] = A" so that A would be in the position 2 and now I should have three objects in my array. But I'm getting "Index out of bound"
Your implementation then should look similar to this: public class Program { public static void Main(string[] args) { Element a = new Element("A"); Element b = new Element("B"); MyArray array = new MyArray(8); array.Add(a); array.Add(b); Console.WriteLine(array.Count()); //2 Elements are in the array } } //Sample element class. public class Element{ public readonly String MyString; public Element(String myString){ MyString = myString; } } //Sample array class. public class MyArray{ private readonly Element[] myArray; private int count; //Use a property here public MyArray(int size){ //Be careful -> check if size is >= 0. myArray = new Element[size]; } public bool Add(Element element){ if(myArray.Length == count) // return false if no more elements fit. return false; myArray[count] = element; count++; return true; } public int Count(){ return count; } } So there is no need for creating an extra count loop. Your "count" variable in "MyArray" class holds always the correct value. Anyway the implementation or use case of this code is a little bit clunky. Why are you cant use directly a more safe list or something. That would be a better solution.
What do you need CBooks for? From what I understand, it's just an array of 8 CBook objects so why not use CBook[]? CBook A1 = new CBook("Title1", "Author1"); CBook A2 = new CBook("Title1", "Author2"); CBooks[] ArrayOfBooks = new CBook[8]; ArrayOfBooks[0] = A1; ArrayOfBooks[1] = A2; int aux = 0; for (int k = 0; k < ArrayOfBooks.Length; k++) { //break the loop because we know there are no more books if (ArrayOfBooks[k] == null) break; aux++; } The question doesn't cover what the variables NumberOfTeams and Teams are for but could those be added to the implementation of CBook?
Nested Loop not iterating
In my code I have a nested loop which does not iterate with the exception of an if statement that always occurs no matter what the condition. Without the if statement the portion of the for loop's code which iterates the loop becomes unreachable. No matter what I have tried I have not been able to get the inside loop to iterate. class Map { public int Width { get; set; } public int Height { get; set; } public Vector2[] positions = new Vector2[500*500]; private GroundVoxel[,] map = new GroundVoxel[500, 500]; private Vector2 voxelPosition = new Vector2(0,0); private static int sizeX = 499, sizeY = 499, airLevel = 425; private int positionX = 0, positionY = 0, vectorNumber = 0; public Map() { } public Vector2[] Initialize() { for (int i = 0; i <= sizeY; i++) { for (int j = 0; j <= sizeX; j++) <-- This does not iterate. { map[positionX, positionY] = new GroundVoxel(voxelPosition); voxelPosition.X += 80; positions[vectorNumber] = voxelPosition; vectorNumber += 1; if (j == sizeX) <-- This always executes even though j != sizeX. { break; } } voxelPosition.Y += 80; voxelPosition.X = 0; } return positions; } } }
You have to use the fully qualified name to refer to a static class member variable like your sizeX and sizeY. Here is an article on the subject. Hope this helps!
I think we'll need more code. I've copied your code into a basic winforms test application and both of my loops iterates as expected. I'm not familiar with XNA or what a "VoxelPosition" is, but I think you have a lurking bug here: voxelPosition.X += 80; positions[vectorNumber] = voxelPosition; You are simply storing the same pointer in a very large array -- all of the entries will be pointing to the same object. You will need to declare another object every time through the loop to store individal vector entries. Hope this helps?
Custom iteration for multi-dimensional arrays?
Something like: forelement (element G_Element, Grid) { Grid[G_Element.dim1, G_Element.dim2] = new clsGridElement(G_Element.dim1, G_Element.dim2); } instead of for (int X = 0; X < GridWidth; X++) for (int Y = 0; Y < GridHeight; Y++) Grid[X, Y] = new clsGridElement(X, Y); If something doesn't innately exist, is that something that could be created? Thanks, Tim
You could do this - just make a custom type that exposes these, and use a regular foreach: public class TwoDimensionalIterator { public TwoDimensionalIterator(int i, int j) { this.Dim1 = i; this.Dim2 = j; } public int Dim1 { get; private set; } public int Dim2 { get; private set; } } Then make an extension method somewhere to return an enumerable of this: public static IEnumerable<TwoDimensionalIterator> IterateTwoDim<T>(this T[,] array) { for (int i=0;i<array.GetLength(0);++i) for (int j=0;i<array.GetLength(1);++j) yield return new TwoDimensionalIterator(i,j); } With this, you could then do: foreach(var index in Grid.IterateTwoDim()) { Grid[index.Dim1, index.Dim2] = new clsGridElement(index.Dim1, index.Dim2); }
Not sure exactly what you are trying to do here, or why, or what you expect to get from it, but if you implement your own iterator than implements the IEnumerator interface then you could create something that would hit every cell in your 2D (or more) collection. Of course, you won't actually gain anything performance-wise from doing this versus just using nested loops, but I guess it'd be syntactic sugar.
Such creation of indexes can be obtained by using "Cartesian product" of all indexes. Here is sample based on Is there a good LINQ way to do a cartesian product? (courtesy of the Eric Lippert): var allIndexes = from dim1 in Enumerable.Range(0, Grid.GetLength(0)) from dim2 in Enumerable.Range(0, Grid.GetLength(1)) select new {dim1, dim2}; foreach (var G_Element in allIndexes) { Grid[G_Element.dim1, G_Element.dim2] = new clsGridElement(G_Element.dim1, G_Element.dim2); }
Problems with double array?
I am required to create a program which reads in data from a .cvs file, and use these (x, y and z) values for a series of calculations. I read in the file as a string, and then split this into 3 smaller strings for x, y and z. The x, y and z coordinates represents the x and y coordinates of the contours of a lake, and the depth (z). One of the calculations which I have to do, is to calculate the surface area of the lake, using the formula (x[i]*y[i+1])-(x[i+1]*y[i]), where z(depth) = 0. I can get my code to run perfectly, up until the x[i+1] and y[i+1], where it keeps giving me a value of 0. Can someone please tell me how to fix this? Here is my code; { string[] ss = File.ReadAllLines(#"C:File.csv"); for (int i = 1; i < ss.Length; i++) { string[] valuesAsString = ss[i].Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); double[] X = new double[valuesAsString.Length]; double[] Y = new double[valuesAsString.Length]; double[] Z = new double[valuesAsString.Length]; for (int n = 0; n < 1; n++) { X[n] = double.Parse(valuesAsString[0]); Y[n] = double.Parse(valuesAsString[1]); } do { double SurfaceArea = (X[n] * Y[n + 1]) - (X[n + 1] * Y[n]); Console.WriteLine(SurfaceArea); } while (Z[n] == 0); } }
Ok, im not sure if i got it right, so you if you would take a look to what i did and tell me if its of any help. After reviewng it a little i came up with the following: A class for the values public class ValueXyz { public double X { get; set; } public double Y { get; set; } public int Z { get; set; } } A class to manange the calculation: public class SurfaceCalculator { private ValueXyz[] _valuesXyz; private double _surface; private readonly string _textWithValues; public SurfaceCalculator(string textWithValues) { _textWithValues = textWithValues; SetValuesToCalculate(); } public double Surface { get { return _surface; } } public void CalculateSurface() { for (var i = 0; i < _valuesXyz.Length; i++) { if (_valuesXyz[i].Z == 0) _surface = (_valuesXyz[i].X*_valuesXyz[i + 1].Y) - (_valuesXyz[i + 1].X*_valuesXyz[i].Y); } } private void SetValuesToCalculate() { var valuesXyz = _textWithValues.Split(' '); _valuesXyz = valuesXyz.Select(item => new ValueXyz { X = Convert.ToDouble(item.Split(',')[0]), Y = Convert.ToDouble(item.Split(',')[1]), Z = Convert.ToInt32(item.Split(',')[2]) }).ToArray(); } } So now your client code could do somethin like: [TestMethod] public void TestSurfaceCalculatorGetsAValue() { //var textWithValues = File.ReadAllText(#"C:File.csv"); var textWithValues = "424.26,424.26,0 589.43,231.46,0 720.81,14.22,1"; var calculator = new SurfaceCalculator(textWithValues); calculator.CalculateSurface(); Assert.IsNotNull(calculator.Surface); } I'm not very sure i got the idea correct of how to implement the formula, but i just wanted to expose an alternative you can use, you can never have to many ways of doing one thing :). Cheers. By the way part of the intent i had, was not tying up your funcionality to the csv in case your source for the text in the future would change.
Step through your code in the debugger. Pay special attention to tbe behavior of the line for (int n = 0; n < 1; n++) This loop will execute how many times? What will the value of n be during each iteration through the loop?
Well, one thing i noticed is when you're setting your X, Y, Z vars, you're setting it to the Length of the array object instead of it's value - is that intentional? Put a debug break on the line with: double SurfaceArea = (X[n] * Y[n + 1]) - (X[n + 1] * Y[n]); and check the datatype of "X", "Y" and "Z" I've had problems in the past where it tries to calculate them as strings (because it took it out of the data source as strings). I ended up fixing it by adding CInt() to each of the variables (or Convert.ToInt32();). Hope this helps.
As this looks like it might be a homework problem, I am trying not to give a direct solution in my answer, but I see a number of questionable parts of your code that you should examine. Why are X, Y, Z arrays? You are creating a new array each time through the outer loop, setting the length of the array to the number of elements in the line, then only assigning a value to one element of X and Y, and never assigning Z to anything. As phoog suggests in his answer, what is the purpose of: for (int n = 0; n < 1; n++)? What are you trying to accomplish with the do-while loop? As it has been mentioned in the comments by Mr Skeet, X[n], Y[n], Z[n] don't exist because n does not exist outside of the loop it is declared for. Even if it did exist Z[n] will always be zero because you never assign anything to the Z array after it is initialized, so the do-while loop will run forever.
comparing strings, one from an array other from an entered value
Basically comparing a string that is entered, and trying to get that position from the array. If I initialize position to 0 then it returns the position zero of the array, if I initialize to 1 then it gives me the item in slot 1, so it's skipping the compare statement. I also tried using (custStatus == cardStatus[i]) public static int discount(string []cardStatus, int []pDiscount, string custStatus) { int position= 0; int discount; for(int i = 0; i < 2; i++) { if (string.Equals(custStatus, cardStatus[i])) position = i; } discount = pDiscount[position]; return discount; }
With your code, there's no way to tell if position = 0 means custStatus was found in your cardStatus array or if no match was made at all and the default value is being used. I'd recommend either using a boolean matchFound variable or setting position = -1 and adding an extra if statement at the end either way. Either: boolean matchFound = false; ... if(matchFound) { discount = pDiscount[position]; } or else int position = -1; ... if(position >= 0) { discount = pDiscount[position]; }
Give this a try: public static int discount(string[] cardStatus, int[] pDiscount, string custStatus) { var position = Array.IndexOf(cardStatus, custStatus); return (position == -1) ? -1 : pDiscount[position]; }
public static int discount(string []cardStatus, int []pDiscount, string custStatus) { for(int i = 0; i < Math.Min(cardStatus.Length, pDiscount.Length); i++) { if (string.Equals(custStatus, cardStatus[i])) { return pDiscount[i]; } } return -1; } Don't be afraid to return directly from FOR-loop, it is old-school that teaches to have only one return point from method. You can have as many returns as it helps you to keep your code clean and easy to read. And perhaps it would be better to use the following expression in for-loop as it will guard you from possible different lengths of arrays: for (int i = 0; i < Math.Min(cardStatus.Length, pDiscount.Length; i++)
This looks ok, even though this is somewhat more straightforward: for(int i = 0; i < cardStatus.Length; i++) { if (custStatus == cardStatus[i]) { position = i; break; } } Given your question it appears to be the case that all cardStatus[i] match custStatus - did you check the input? Also given your code what happens if there is no match? Currently you would return pDiscount[0] - that doesn't seem to be correct.