Force method to accept different variables in signature - c#

is there a way to force a method in c# to accept arrays/variables of different types in the same signature slot or to make it ignore one part of the signature?
My code:
private void array_joiner(String[,] newArray, Int32[,] MatrixArray, String[,] RekursionArray, Char[] ArrayX, Char[] ArrayY)
{
for (Int16 i = 0; i < ArrayX.Length + 1; i++)
{
newArray[i, 0] = ArrayX[i].ToString();
}
for (Int16 i = 1; i < ArrayY.Length + 1; i++)
{
newArray[0, i] = ArrayY[i].ToString();
}
for (Int16 y = 1; y < ArrayY.Length + 1; y++)
{
for (Int16 x = 1; x < ArrayX.Length +1; x++)
{
newArray[y, x] = MatrixArray[y, x].ToString();
}
}
}
My problem is basically that I want to parse two different arrays in the slot of Int32[,]MatrixArray (Int32[,] and String[,]) to the method but I just don't know how. Anyone got a better idea than to write two different methods? Thx in advance.

You could just declare them as the Array class and then work out the type of the data in the arrays and how many ranks they have separately.

You basically have two ways:
Make it a generics method http://msdn.microsoft.com/it-it/library/512aeb7t.aspx (being careful to handle multidimensional collections which could be tricky)
Overload the method, declaring two signatures with the same name and different parameters http://msdn.microsoft.com/en-us/library/vstudio/ms229029(v=vs.100).aspx

Related

Parse 2d int array to List of BubbleDataPoints

I have an algorithm which makes use of a grid which is implemented via an int[,] in C#. I would like to graph the output in Blazor using the Bubble chart of blazor chart.Js. Preferably I would like to create separate datasets depending on the value of the int. 0 = empty/ignore, 1 = particular object type, 2 = particular object type.
I'm having an issue with picking out the data and converting it to the BubbleDataPoint(x,y,r) which the chart expects. I would like the x,y to correspond to the array index e.g. graph[1,1] = 1 would be bubbledatapoint(1,1,1) etc. I implemented an IEnumerable method in my class so that I could use foreach or .select on the int values but not sure how to get the array indices out.
public IEnumerable<int> GridValues()
{
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; x < grid.GetLength(1); y++)
{
yield return grid[x,y];
}
}
}
Thanks,
Ok, so for anyone interested I went with this method. I initially wanted to avoid looping over the array until I realised that 2D arrays don't implement IEnumerable and I was doing a half-way method but then just decided to go with this.
public IEnumerable<BubbleDataPoint> GridToDataPoints()
{
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
yield return new BubbleDataPoint(Convert.ToDouble(x), Convert.ToDouble(y), Convert.ToDouble(grid[x, y]));
}
}
}

Printing square with non repetitive character

I want to print a rectangle like this :
&#*#
#*#&
*#&#
#&#*
But problem is that i can't find the algorithm to print this.
I only know how to print a simple rectangle/square
public static void Main(string[] args)
{
Console.Out.Write("Saisir la taille : ");
int taille = int.Parse(Console.In.ReadLine());
int i;
int j;
for(i = 0; i < taille; i++){
for(j = 0; j < taille; j++){
Console.Write("*");
}
Console.WriteLine("");
}
}
Thank you !
First things first unless you need your iterators outside of your loop, just declare them in the for declaration
public static void Main(string[] args)
{
Console.Out.Write("Saisir la taille : ");
int taille = int.Parse(Console.In.ReadLine());
for(int i = 0; i < taille; i++){
for(int j = 0; j < taille; j++){
Console.Write("*");
}
Console.WriteLine("");
}
}
Second you'll need a list of the characters you want to use, given your example
char[] chars = { '&', `#`, `*`, '#' };
and we'll need a way to know which character we want to use at any given time, say an iterator we can call characterIndex for simplicity. We will increment it each iteration. If incrementing it puts it out of the range of our character array, if characterIndex == 4, we set it back to zero.
int characterIndex;
To get the scrolling effect you have, before each line we must select a characterIndex that is offset by the row
characterIndex = i % chars.Length;
Tying it all together
public static void Main(string[] args)
{
char[] chars = { '&', `#`, `*`, '#' };
int characterIndex;
Console.Out.Write("Saisir la taille : ");
int taille = int.Parse(Console.In.ReadLine());
for(int i = 0; i < taille; i++){
characterIndex = i % chars.Length;
for(int j = 0; j < taille; j++){
Console.Write(chars[characterIndex]);
characterIndex++;
if(characterIndex == chars.Length)
characterIndex = 0;
}
Console.WriteLine("");
}
}
Getting the permutations by nesting for loops will only work if you know exactly how many elements there will be. Basically you need to write a for-loop for every element after the 1st.
The proper way to deal with this is Recursion. While there are cases where Recursion and nested for-loops are interchangeable. And in cases where they are, for loops have a potential speed advantage. While normally the speed rant applies to such differences, with the sheer amount of data both Recursion and Loops might have to deal with, it often maters - so best to prefer loops where possible.
Permutations is AFAIK not a case where loops and recursion are interchangeable. Recurions seems to be mandatory. Some problem as simply inherently recursive. As the recursion version is fairly well known, I will not post any example code.
You should defiitely use Recursion. With your example code I basically asume you are:
In a learning environment
You just learned recursion
A input variant recurions can effortless solve (like a 6 or 20 size input), is the next assignment

Calling from a public class and from its public method, into 2d double array, my zeros before the fractional point change to number 9

I've been writing a program, to use it as a tool for quick calculations in an online game, and it also helps me a bit to revise C# for my final exam in IT.
Here's my code:
public class ConvertingToArrays
{
public static double[,] CountryVAT(double[,] vat)
{
//I have a table in a .txt file with 20 rows and 6 columns
//and I only need one of the cols.
vat = new double[20, 1];
string[,] convertTableToString = new string[20, 6];
//Here I'm just calling from ReadFromFile public class and its
//public static string[,] Input method and until this point
//everything works fine
convertTableToString = ReadFromFile.Input(convertTableToString);
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 1; j++)
{
vat[i, j] = double.Parse(convertTableToString[i, 1]);
}
}
return vat;
}
}
With the string to double converting I had no problem, I tested it and it should not be the cause.
class Program
{
public static void Main(string[] args)
{
double[,] vat = new double[20, 1];
vat = ConvertingToArrays.CountryVAT(vat);
Console.WriteLine("Testing ConvertVAT Method Call");
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 1; j++)
{
Console.Write(vat[i, j] + '\t');
}
Console.WriteLine('\n');
}
Console.ReadKey();
}
}
I'm reading from a .txt file a few numbers like: 0,03; 0,05; 0,4; 0
And for some reason the output for these numbers are: 9,03; 9,05; 9,4; 9
I've tried to look it up on Google but I found nothing. It might be just one subtle and easy thing that I overlooked accidentally (please keep in mind that I have started learning to code by myself just 6 months before and I've practised it only 10-12 hours a week).
Can anyone help with a solution?
Look at this line:
Console.Write(vat[i, j] + '\t');
What is going on here? You add a double with a char value. You think you do some string operation, but that is not what your code is actually doing. Note that no strings are involved in the operation above. Both the double variable and the char literal are numeric data types, thus your code is executing an addition of the two numeric values.
What is the numeric value of the tabulator char '\t'? It is 9. So basically your code is doing Console.Write(vat[i, j] + 9);
There are different ways to you can change the code. One is to make two Console.Write calls like this:
Console.Write(vat[i, j]);
Console.Write('\t');
Alternatively, you could also force a string concatenation by converting the double value or the tab char to a string before "adding" them:
Console.Write(vat[i, j] + "\t");
or, less elegantly:
Console.Write(vat[i, j].ToString() + '\t');
As a third option you could also use format strings:
Console.Write("{0}{1}", vat[i, j], '\t');
or, simplified:
Console.Write("{0}\t", vat[i, j]);

Cannot implicitly convert type 'System.Array' to 'decimal[]'. An explicit conversion exists (are you missing a cast?)

How to convert this in C# code?
//Cannot implicitly convert type 'System.Array' to 'decimal[]'.
//An explicit conversion exists
Array[] s = new Array[10];
for (int i = 0; i < 9; i++)
{
s[i] = average; //average is decimal []
}
for (int i = 0; i < 2; i++)
{
Mean = s[i]; //mean is decimal[] & s[i] system array
}
It looks like you're trying to setup a jagged array. In which case, I suspect the best option is to type s as decimal[][]:
decimal[][] s = new decimal[10][];
Then everything will just work.
You can also cast:
Mean = (decimal[])s[i];
but that is less satisfying, IMO.
I don't know what you are trying to do with this code, as it does not calculate an average or mean,
but you are using the typeless array class, this is primarily intended to serve as a common base class so that all arrays exhibit the same behaviour, it can also be used in some rare situations when you don't know what type the array is
the correct way to defined an array is TypeName[ArraySize] so if you want a decimal array of size 10 then the correct use would be decimal[10]
here is your "working" code:
decimal demo_average = 0;
decimal demo_mean = 0;
decimal[] s = new decimal[10];
for (int i = 0; i < 9; i++)
{
s[i] = demo_average;
}
for (int i = 0; i < 2; i++)
{
demo_mean = s[i];
}

Utilizing c++ from c#

I currently have some c++ code which processes 'char ***myArray' much faster than any other method for string comparison.
I'm also wrapping my c++ into a DLL and calling functions from a C# GUI which uses a 'DataTable'.
I'm curious how I go about passing my 'DataTable' data accross to my 'char ***myArray'.
Interface.cs:
DataTable table
cppFunctions.cpp:
int CheckColumn(char ***myArray)
{
int k = 0;
double weight = 0;
for (int i = 1; i < RowCount; i++)
{
for (int j = i + 1; j < RowCount; j++)
{
weight = nGram(myArray[i][colNum], myArray[j][colNum], 3);
k++;
}
}
return k;
}
If I pass int, double, string, or any simple value across it works just fine.
DataTable is part of the .NET FCL, so you cannot pass it. The reason int, string, etc work is that they are primitives. You could serialize / de-serialize the DataTable.
Alternatively, you could use marshalling:
http://msdn.microsoft.com/en-us/library/ms235266.aspx

Categories

Resources