How to swap elements in a matrix 2D array? - c#

Hello I have created a program that the user imputs the values of a 2D array 6x6 and show it now I have to make a new metod i guess to rotate the elements from rows 0,2,4 put them in colums 1,3,5 and elements from rows 1,3,5 putting them in colums 0,2,4 than tell how many "M" are in total at the two last rows". can someone tell me how to do that i have no idea. Thanks
This is a photo of my code and another photo how should i change the indexes

The Solution is written in java hope you get the idea.
public class ProgramMain {
public static void main(String[] args) {
int[][] arr = {
{0,0,0,0,0,0},
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5}
};
int[][] output = new int [6][6];
// elements from rows 0,2,4 put them in column 1,3,5 and elements from rows 1,3,5 putting them in column 0,2,4
for (int i = 0; i < output.length; i++) {
for (int j = 0; j < output.length; j++) {
if(i==0||(i%2)==0) {
output[j][i+1]=arr[i][j];
}else {
output[j][i-1]=arr[i][j];
}
}
}
//Ignore the below code this is only used for printing the matrix
System.out.println("Input Array");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n\nOuput Array");
for (int i = 0; i < output.length; i++) {
for (int j = 0; j < output.length; j++) {
System.out.print(output[i][j]+"\t");
}
System.out.println();
}
}
}

Related

How to populate 2D array with char array in C#

I'm trying to fill a 2D char array with characters form a char array
I have tried this code but not finding the problem
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
for (int k = 0; k < values.Length; k++)
{
array[i, j] = values[k];
}
}
}
}
Your first for loop is responsible for iteration over first dimension of array (i) It's okay.
Your second for loop is responsible for iteration over second dimension of array (j). It's okay.
Your third loop is responsible for iteration over char values array (k) Here's your bug.
For a given set of values of i and j which represents dimensions indexes of array, your function iterates through all positions of values array. So for each k value i and j values remain unchanged. Therefore you sequentially put all the values of values array (k+1)times into the same cell of two dimension array, ultimately leaving it with value of values[values.Length] as it is the highest possible value of k in the most nested loop.
I'd suggest solution similar to what #adv12 has proposed with slight modification as I am not sure if the k value would be 0 during first iteration of the nested for loop. It is also more readable IMO.
int k = 0;
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = values[k];
k++
if (k >= values.Length)
{
k = 0;
}
}
}
}
Here's my best guess at what you want based on your comments:
int k = 0;
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = values[k++];
if (k >= values.Length)
{
k = 0;
}
}
}
}

Index out of range for multi dimensional array

I'm getting the error that the index for my array is out of range.
I define a 3D array like this:
Button[, ,] posAr_ItemManager = new Button[maxRows, maxColumns, maxCategories];
Where maxRows, maxColumns and maxCategories are all constant integers.
I then want to loop through this whole array, i do it using nested loops as shown here:
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; i < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; i < posAr_ItemManager.GetLength(2); z++)
{
if (posAr_ItemManager[i,j,z] != null)
{
Button but = posAr_ItemManager[i, j, z];
but.Width = itemWidth;
but.Height = itemHeight;
but.SetValue(Canvas.LeftProperty, itemPanelX + (i + 1) * butSpacing + i * itemWidth);
but.SetValue(Canvas.TopProperty, itemPanelY + (i+1)*butSpacing + i* itemHeight);
}
}
}
}
When I run the program it gives the out of range error where I check if the item is not equal to null.
I have no idea why it does it as I thought my declaration of the 3D array is correct and the nested loop as well? thanks in advance!
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; j < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; z < posAr_ItemManager.GetLength(2); z++)
look very carefully at the middle tests. Also, consider hoisting the GetLength tests so you only do them once per dimension. Perhaps:
int rows = posAr_ItemManager.GetLength(0), columns = posAr_ItemManager.GetLength(1),
categories = posAr_ItemManager.GetLength(2);
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
for (int cat = 0; cat < categories; cat++)
You're using "i" to check if the loop is over in each of the dimensions. Change it to corresponded i,j,z.
for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; **j** < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; **z** < posAr_ItemManager.GetLength(2); z++)

Trying to write a 2D array to a .csv and not all the cells are copied over

I have a several arrays/matrices that I am trying to copy to a .csv using C# but I'm having issues. The one I will show you is for a [12,480] matrix. I run the code and all of the first 11 rows copy fully, but the last 20 values in the 12th row do not get copied into the .csv. I've displayed the 'myArray' in the console and all the values are there with a total matrix length of 5760. If you have any idea what is happening, please let me know. I have other arrays that fill out 474 out of 480 values, 427 out of 480 values, and 5712 out of 5760 values.
NOTE: The code is working, just the last 20 values in the array won't copy to .csv. I've also attached an image of the .csv to give you a better idea of what is happening. The end of the matrix with the missing values highlighted in yellow
private static void CreateuCSV(double[,] myArray)
{
StreamWriter file = new StreamWriter("location")
for (int i = 0; i <12; i++)
{
for (int j = 0; j < 480; j++)
{
file.Write(myArray[i,j];
file.Write(",");
}
file.Write("\n");
}
}
This generates a square array of the desired size in the csv...
class Program
{
static void Main(string[] args)
{
int rows = 12, cols = 480;
double[,] nums = new double[rows, cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
nums[i, j] = j;
}
}
CreateCSV(nums);
}
private static void CreateCSV(double[,] myArray)
{
var rows = myArray.GetLength(0);
var cols = myArray.GetLength(1);
using (StreamWriter file = new StreamWriter("2dArrayOut.csv"))
{
file.AutoFlush = true;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
file.Write($"{myArray[i, j]},");
}
file.Write("\n");
}
}
}
}

In C#, How can I iterate through a entire 2D Array and count the number of 0s?

I've been trying to iterate through an entire 2D array and count the number of 0s. How can I do this?
I'm sure I have to use the following to make the program work:
Use an outer for loop to iterate through the rows of the array
Use an inner for loop to iterate through the columns of the array
if matrix[row,col] == 0, increment a variable
To get the number of rows of the 2D array, use the method matrix.GetLength(0), and to get the number of columns of the array, use the method matrix.GetLength(1)
Here's what I have attempted so far:
public int Test9(int[,] matrix)
{
int b = 0;
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); i++)
{
b = matrix[i, j];
}
}
This is how you can do it..
public int Test9(int[,] matrix)
{
int zeroCounter = 0;
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
{
if(matrix[i, j] == 0)
{
zeroCounter++;
}
}
}

Printing a 2D-array into "squares"

I'm trying to learn how to work with 2D-array and I can't seem to understand how to print them correctly. I want to print them in a "square" like 5x5 but all I get is one line. I've tried both WriteLine and Write and changed some of the variables in the loops but I get either an error or not the result I want to have. The code is supposed to print out a 5x5 with a random sequence of 15 numbers in each column. I get the correct numbers out of it, it's only the layout that is wrong.
static void Main(string[] args)
{
Random rnd = new Random();
int[,] bricka = new int[5, 5];
int num = 0;
int num1 = 1;
for (int i = 0; i < bricka.GetLength(1); i++)
{
num += 16;
for (int j = 0; j < bricka.GetLength(0); j++)
{
bricka[j, i] = rnd.Next(num1, num);
}
num1 += 16;
}
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
}
Console.ReadKey();
}
This is my print, I would like to have the the 12 under the 8 and 14 under the 12 and so on.
http://i.imgur.com/tfyRxf1.png
You need to call WriteLine() after each line, so that each line is printed on a separate line:
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
Console.WriteLine();
}
That would be one way of doing it, anyway.

Categories

Resources