how add to list of structure in c# - c#

I Write this code in c# , i use list of structure , but how can i add to list with structure,i use add to add xx,yy,size but not correct ? i need only method for add data to list of structure
public struct blocks
{
public Int32 xx;
public Int32 yy;
public Int32 size;
};
namespace test2
{
class Program
{
static List<blocks> blocks1 = new List<blocks>();
static void Main(string[] args)
{
Int32 index=0;
for (int y = 1; y < 5; y++)
for (int x = 1; x < 5; x++)
{
blocks1[index].xx +=( x * 2); // store start point (x) of block
blocks1[index].yy += (y *2); // store start point (y) of block
blocks1[index].size +=( 2); // store block size
index++;
}
}
}
}

I would use a List<blocks> blocks1 = new List<blocks>() since that is a data structure that you can just add to as needed.
You can add to it like so:
blocks newBlock = new blocks();
blocks1.Add(newBlock) ;

This is probably what you are trying to achieve but not very sure. If this is not what you need then please update your question with the information as per my comment
public struct blocks
{
public Int32 xb;
public Int32 yb;
public Int32 size;
};
namespace test
{
class Program
{
static List<blocks> blocks1;
static void Main(string[] args)
{
blocks1 = new List<blocks>();
for (int y = 1; y < 5; y++)
{
for (int x = 1; x < 5; x++)
{
blocks newBlock = new blocks();
newBlock.xb = x * 2;
newBlock.yb = y * 2;
newBlock.size = 2;
blocks1.Add(newBlock);
}
}
}
}
}
Hope this helps.

Related

NxN matrix with n from user input and default values if no n provided

I need to make a Jacobi Gauss program but I'm struggling with the start of it. We need to create a class which encapsulates a nxn matrix and stores it in a 2D array, with the default value being 3x3 with all values 0. We then have to add features to this and the rest of the Jacobi Gauss model. I've tried a few ways to make a matrix but I can't get one that relies on user input for size or defaults to a set size/values.
There are many ways to represent 2d matrix but the most simple is just as a native 2d array. By default a new array is filled with default values which for ints is zero. You can initialize yourself. Or, of course you can simply set the values after you create it.
Note in the first case I've created the array based off a integer. In this case for simplicity 1 value, obviously it does not have to be square.
static void Main(string[] _)
{
int dimensions = 3;
int[,] matrix = new int[dimensions, dimensions];
foreach (var v in matrix)
Console.WriteLine(v);
int[,] matrix2 = new int[,] { { 1,1,1},{ 2,2,2},{ 3,3,3} };
for (int x = 0; x < dimensions; ++x)
{
for (int y = 0; y < dimensions; ++y)
{
// matrix2[x,y] = whatever...
Console.WriteLine(matrix2[x, y]);
}
}
}
One option to wrap this in a class is to use a indexer method. For a simple square matrix with absolutely no error handling that might look like this:
public class MyMatrix
{
int[,] data = null;
public MyMatrix(int dims = 3)
{
this.data = new int[dims, dims];
}
public void SetDimensions(int dims)
{
if (this.data.GetLowerBound(0) != dims)
{
this.data = new int[dims, dims];
for (int x = 0; x < dims; ++x)
for (int y = 0; y < dims; ++y)
this.data[x, y] = 0;
}
}
public int this[int x, int y]
{
get { return this.data[x, y]; }
set { this.data[x, y] = value; }
}
public int Size => this.data.GetLowerBound(0);
}
static void Main(string[] _)
{
MyMatrix m = new MyMatrix(3);
for (int x = 0; x < m.Size; ++x)
{
for (int y = 0; y < m.Size; ++y)
{
// m[x,y] = whatever...
Console.WriteLine(m[x, y]);
}
}
}

Incrementing t2 variables

I have 2 variables x and y ,and they have to increment by 1, the tow variables start in 1, first x has to increment by 1,, until it reaches 25 and y stay at 1. Once it reaches 25, x has to go back to 1 and y increment to y=2, and the repeat (once x has reached 25 again y will increment by 1). here my implementation but isn't working...
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int x;
int y = 1;
for (x = 1; x < 26; x++)
{
if (x == 25)
{
x = 1;
for (y = 1; y < 30; y++)
{
Console.WriteLine("X = " + x + ", Y = " + y);
}
}
}
}
}
}
It's not standard practice to manipulate x in a for loop. I assume you only want both variables going to 25 (edit: y now goes to 30 like in OP). So the trick is to put y on the outside and x on the inside.
In this case it's easiest to still use for loops instead of while loops.
Try something like this:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
for(int y = 0; y < 30; y++) //y goes up 1 every time x goes up 25
{
for(int x = 0; x < 25; x++) //counts x to 25
{
Console.WriteLine("X=" + x + " Y=" + y);
}
}
}
}
}
You can use a little bit of recursivity for that too (just adding this for more infos ).
public static void run25(ref int x, ref int y)
{
if(x <= 25)
{
Console.WriteLine($"x = {x}");
x++;
run25(ref x, ref y);
}
if (y <= 25)
{
Console.WriteLine($" >> y = {y}");
y++;
x = 1;
run25(ref x, ref y);
}
else return ;
}
you can use that as follow :
public static void Main (string[] args)
{
int _x = 1;
int _y = 1;
run25(ref _x, ref _y); // once it's done , _x and _y values are 26 .
}
using System;
public class Program
{
public static void Main()
{
for(var y = 1; y <=30 ;y++)
{
for(var x = 1; x<=25;x++)
{
Console.WriteLine("x = {0}, y = {1}", x, y);
}
}
}
}

How to iterate numbers to a limit by addition

Here is the code I have right now:
for(int i=0; i< 35; ++i)
{
int a = 1;
a+=1;
Console.WriteLine(a);
}
The output is:
2
2
2
...
and so on, until the loop terminates.
What I want to do is add the previous two numbers to make the nest number then so on all the way to 34.
This is how you can achieve what you want:
//Initializing the elements
int a = 0;
int b = 1;
int c = 1;
Console.WriteLine(a); //Typing the 0'th element
Console.WriteLine(b); //Typing the first element
for(;c <= 34; c = a + b) {
Console.WriteLine(c); //Typing the actual element
a = b;
b = c;
}
You mean count to some number by adding other and using this as what? Escape condition? Just as a constant in addition?
as escape condition:
using System;
public class Program
{
static private int a = 0;
static private int i = 0;
public static void Main()
{
for (i = 0; i < 100; i++){
a += i;
if (a == 595) break;
}
Console.WriteLine(i);
}
}
as a constant in addition:
using System;
public class Program
{
static private int a = 0;
static private int i = 0;
static private int d = 1;
public static void Main()
{
for (i = 0; i < 34; i++){
a += d;
}
Console.WriteLine(a);
}
}
Both seem highly illogical to use tho :/
EDIT: Your question is now different from what you told before :D
You are wondering why is a not getting larger than 2? Because you initialize it each step of for loop. You need to move initialization out of the scope of for. Try this:
using System;
public class Program
{
static private int a = 0;
static private int i = 0;
static private int d = 1;
public static void Main()
{
int a = 1;
for(int i=0; i< 34; ++i)
{
Console.WriteLine(a);
a+=1;
}
}
}
I understand you need numbers from 1 to 34. So you can't start with 1, immediately increment by 1 and then write it cause it will be 2. Write first, then increment.
Or you can start with 0 then you can increment first. But adjust end condition as well.
You are declaring a new variable a with each iteration and then add 1 to it. Since the initial value of a is 1, you calculate 1+1 with each iteration. Step through the code in a debugger to see what I mean.
Move the declaration out of the loop and you should see your desired output:
int a = 1;
for(int i=0; i< 35; ++i)
{
a+=1;
Console.WriteLine(a);
}
There are tons of tutorials how you can print out the fibonacci sequence, for example with two nested for-loops:
for (int i = 0; i < 10; ++i)
{
int a = 0;
int b = 1;
for (int j = 0; j < i; ++j)
{
var temp = a;
a = b;
b = temp + b;
}
Console.WriteLine(a);
}
Example taken from dotnetperls (albeit slightly edited by me).

Getting from a dictionary with int[] arrays as value [duplicate]

This question already has answers here:
An integer array as a key for Dictionary
(3 answers)
Closed 7 years ago.
I have the following piece of code
public Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x=0; x <= 10; x++) {
for (int y=0; y <= 10; y++) {
int[] cords = new int[]{x,y};
worldMap.Add(cords, "empty");
}
}
How do I get values from this dictionary?
Create an IEqualityComparer and define your dictionary using that. you can find sample code in below SO question:
An integer array as a key for Dictionary
You can iterate over the entire dictionary.
foreach (var map in worldMap)
{
Console.WriteLine(String.Join(", ", map.Key) + " - " + map.Value);
}
Or look it up by reference
Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x = 0; x <= 10; x++)
{
for (int y = 0; y <= 10; y++)
{
int[] cords = new int[] { x, y };
worldMap.Add(cords, "empty");
Console.WriteLine(worldMap[cords]);
}
}
When you use an array or class, the dictionary uses the reference to the object in the background. This makes some things impossible. For instance if you new another list with the same values, the dictionary will throw an exception saying the key is not found.
Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x = 0; x <= 10; x++)
{
for (int y = 0; y <= 10; y++)
{
int[] cords = new int[] { x, y };
worldMap.Add(cords, "empty");
//This will cause an exception
Console.WriteLine(worldMap[new int[] { x, y }]);
}
}
Consider using System.Drawing.Point instead of an int array. In general, Dictionaries will only match if it's the exact same object that went into it. Even another object with the same value won't work.
But some data types such as Point are different, and implement all the hashing and equality check features necessary to be used a keys in a dictionary.
After you populate the worldMap you can get your keys like this (You have to add using System.Linq):
List<int[]> coords = worldMap.Keys.ToList();
Then you can get any value from the coords List like this (Make sure you use a value within the bounds of your List):
worldMap[coords[0]]
Code Sample:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x = 0; x <= 10; x++)
{
for (int y = 0; y <= 10; y++)
{
int[] cords = new int[] { x, y };
worldMap.Add(cords, String.Format("empty{0}_{1}", x, y));
}
}
// This should have 121 coordinates from (0, 0) to (10, 10)
List<int[]> coords = worldMap.Keys.ToList();
Console.WriteLine(worldMap[coords[21]]);
}
}
Demo
The use of an int[] for your word map coordinates is a poor choice for a number of reasons, not least of which I could send through zero, one, three, or more values when it seems like you specifically are using only two.
You would be far better off creating you own read-only class to hold your coordinates instead.
Try this class:
public sealed class WorldMapCoord : IEquatable<WorldMapCoord>
{
private readonly int _X;
private readonly int _Y;
public int X { get { return _X; } }
public int Y { get { return _Y; } }
public WorldMapCoord(int X, int Y)
{
_X = X;
_Y = Y;
}
public override bool Equals(object obj)
{
if (obj is WorldMapCoord)
return Equals((WorldMapCoord)obj);
return false;
}
public bool Equals(WorldMapCoord obj)
{
if (obj == null) return false;
if (!EqualityComparer<int>.Default.Equals(_X, obj._X)) return false;
if (!EqualityComparer<int>.Default.Equals(_Y, obj._Y)) return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
hash ^= EqualityComparer<int>.Default.GetHashCode(_X);
hash ^= EqualityComparer<int>.Default.GetHashCode(_Y);
return hash;
}
public override string ToString()
{
return String.Format("{{ X = {0}, Y = {1} }}", _X, _Y);
}
}
Now your code would look like this:
public Dictionary<WorldMapCoord, string> worldMap = new Dictionary<WorldMapCoord, string>();
for (int x=0; x <= 10; x++)
{
for (int y=0; y <= 10; y++)
{
worldMap.Add(new WorldMapCoord(x, y), "empty");
}
}
Since the class is read-only, and it implements GetHashCode & Equals you can use this code to retrieve values from the dictionary:
for (int x=0; x <= 10; x++)
{
for (int y=0; y <= 10; y++)
{
string value = worldMap[new WorldMapCoord(x, y)];
}
}

C# read only multidimensional array and tilemaps

I made a tile map class, that only contains the data and methods to get or set tiles.
Now I have the problem, that I want to make a tile map renderer.
But I thing its not very fast to call the methods GetTile(int layerIndex, int x, int y); to iterate threw the tiles and draw them.
So I want to give the possibility to get the full array with all data.
In this case tiledMapData.
But the problem is, that I want that someone cant change the values of the array, because I want make events for changing tiles for physics.
How I can do this.
public class TiledMap
{
public int LayerCount
{
get;
private set;
}
public int TileCountX
{
get;
private set;
}
public int TileCountY
{
get;
private set;
}
public int TotalTileCount
{
get;
private set;
}
private int[,] tiledMapData;
public TiledMap(int layerCount, int tileCountX, int tileCountY)
{
LayerCount = layerCount;
TileCountX = tileCountX;
TileCountY = tileCountY;
TotalTileCount = TileCountX * TileCountY;
tiledMapData = new int[LayerCount, TotalTileCount];
for (int layerIndex = 0; layerIndex < TotalTileCount; layerIndex++)
{
for (int tileIndex = 0; tileIndex < TotalTileCount; tileIndex++)
{
tiledMapData[layerIndex, tileIndex] = 0;
}
}
}
public int GetTile(int layerIndex, int x, int y)
{
return tiledMapData[layerIndex, y * TileCountX + x];
}
public void SetTile(int layerIndex, int x, int y, int tileType)
{
tiledMapData[layerIndex, y * TileCountX + x] = tileType;
}
}
And generally, how you would design a tilemap 100 x 100 tiles 16 bit graphics and destroy able/build able tiles, how handle physics player interaction and so on....?
Maybe someone know good tutorials(The language is not important)
Thanks for helping me :)
Okay I have to reject this, it makes a difference event when not using Length directly.
const int countX = 100;
const int countY = 100;
const int countLayer = 5;
TiledMap tm = new TiledMap(countLayer, countX, countY);
const int testRuns = 10000;
Console.WriteLine("Run 1, start");
DateTime start1 = DateTime.Now;
for (int test = 0; test < testRuns; test++)
{
for (int i = 0; i < countLayer; i++)
{
for (int j = 0; j < countX; j++)
{
for (int k = 0; k < countY; k++)
{
int data = tm.GetTile(i, j, k);
data++;
}
}
}
}
DateTime end1 = DateTime.Now;
Console.WriteLine(String.Format("Run 1, time: {0}", end1-start1));
Console.WriteLine("Run 2, start");
DateTime start2 = DateTime.Now;
for (int test = 0; test < testRuns; test++)
{
for (int i = 0; i < countLayer; i++)
{
for (int j = 0; j < countX; j++)
{
for (int k = 0; k < countY; k++)
{
int data = tm.DirectArray[i, k*countX + j];
data++;
}
}
}
}
DateTime end2 = DateTime.Now;
Console.WriteLine(String.Format("Run 1, time: {0}", end2 - start2));
Console.ReadLine();
The array access variant performed much better. Still not sure if it would when you use some kind of read only array. So make sure you check it out.

Categories