I would like a function to convert a single dimension array int[480000] into a 2d array of size int[800,600]. Can you please help me how this can be done?
int. Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For example, 0 , -5 , 10. We can use int for declaring an integer variable.
To convert an array of strings to an array of numbers, call the map() method on the array, and on each iteration, convert the string to a number. The map method will return a new array containing only numbers.
Description. The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.
You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.
public static T[,] Convert<T>(this T[] source, int rows, int columns)
{
int i = 0;
T[,] result = new T[rows, columns];
for (int row = 0; row < rows; row++)
for (int col = 0; col < columns; col++)
result[row, col] = source[i++];
return result;
}
Do you really want to physically move the data or would a 800x600 'View' be sufficient?
You could use a wrapper like this:
// error checking omitted
class MatrixWrapper<T>
{
private T[] _data;
private int _columns;
public MatrixWrapper(T[] data, int rows, int columns)
{
_data = data;
_columns = columns;
// validate rows * columns == length
}
public T this[int r, int c]
{
get { return _data[Index(r, c)]; }
set { _data[Index(r, c)] = value; }
}
private int Index(int r, int c)
{
return r * _columns + c;
}
}
And you use it like:
double[] data = new double[4800];
var wrapper = new MatrixWrapper<double>(data, 80, 60);
wrapper[2, 2] = 2.0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With