Is it possible to use 2D Array on Queues? windows form - winforms

Queue[,] inventqueue = new Queue[10,7];
for(int row = 0; row < inventqueue.GetLength(0); row++)
{
for (int col = ; col < inventqueue.GetLength(1); col++)
{
if(inventqueue[row,col].Count != 0)
{
MessageBox.Show("Theres a queue on " + row + "," + col);
}
}
}
I have been trying this out but visual studio is giving me the error "Object reference not set to an instance of an object."

You're allocating only the double array, you still need to allocate Queues for each entry in the array like:
Queue[,] inventqueue = new Queue[10,7];
for(int row = 0; row < inventqueue.GetLength(0); row++)
{
for (int col = ; col < inventqueue.GetLength(1); col++)
{
inventqueue[row,col] = new Queue();
}
}

Related

adding an array table (bidimensional) to a List (Java)

I am doing a Bingo program and now, the section of the bingoCard, to do this I am using an a bidimensional array but I need to shuffle the numbers of each row. For the shuffle part I saw that the setList is much better, but I don't know how to relate the List with the array here is a part of the code:
public static Integer[][] bingoCard(){
Integer [][] bingoCard= new Integer[3][9];
for(int x =0; x<bingoCard.length; x++){
for(int y =0; y<bingoCard[x].length; y++){
if(y <5){
int random = (int)(Math.random()*90+1);
System.out.print((bingoCard[0][y] = random) + " ");
}
if(y >4 && y <9){
System.out.print((bingoCard[0][y] = 0) + " ");
}
}
System.out.println();
}
List<Integer[]> list =Arrays.asList(bingoCard);
Collections.shuffle(list);
list.toArray(bingoCard);
return bingoCard;
}
Any question please ask me!!
Thanks.
In your code, when you do List<Integer[]> list = Arrays.asList(bingoCard), you are converting the outer array into a List and then shuffling its contents. The effect of this will be that the order of the 3 rows are shuffled rather than the contents of the 3 rows which is what you want. You could achieve this by shuffling the contents of each row within your for-loop. Or, after constructing the 2D array, you can loop over each row again and shuffle them.
Also, you have another small bug. When you assign the value, you are doing bingoCard[0][y] = ... but it should be bingoCard[x][y]. Otherwise, you are only assigning the first row new values on each iteration.
I would recommend not converting the array to a List just to shuffle it and then converting it back. Instead, you can use Random.nextInt to pick indexes of the array to assign. That would look something like this:
public static Integer[][] bingoCard(){
int numRows = 3;
int numCols = 9;
int randomNumbersPerRow = 5;
int randomNumberBound = 90;
Random random = new Random();
Integer[][] bingoCard = new Integer[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
// Initialize all spots to 0
bingoCard[row][col] = 0;
}
for (int i = 0; i < randomNumbersPerRow; i++) {
// Choose a spot to assign a random number
int indexToAssign = random.nextInt(numCols);
// Make sure we haven't already chosen it
while (bingoCard[row][indexToAssign] != 0) {
indexToAssign = random.nextInt(numCols);
}
int numToAssign = random.nextInt(randomNumberBound) + 1;
bingoCard[row][indexToAssign] = numToAssign;
}
}
// Print the bingo card
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
System.out.printf("%2d ", bingoCard[row][col]);
}
System.out.println();
}
return bingoCard;
}

How to correctly implement an insertion sort algorithm in a 2D array?

I need to sort the columns of my 2D array based on the first row of the array. This is my 2d array before sorting:
cargo
AFADA
DAFGF
DXAFA
DDFFX
GFXF
and this is the image of my 2D array:
acrgo
FAADA
ADFGF
XDAFA
DDFFX
FGXF
The correct output looks like this
acgor
FADAA
ADGFF
XDFAA
DDFXF
FGF X
. This is the code of my function to sort the array using insertion sort algorithm
void sort(char** newTable, int row, int col) {
for (int top = 1; top < col; col++) {
char item = newTable[0][top];
int i = top;
while (i > 0 && item < newTable[0][i - 1]) {
for (int j = 0; j < row + 1; j++) {
char temp = newTable[j][i];
newTable[j][i] = newTable[j][i - 1];
newTable[j][i - 1] = temp;
}
i--;
}
newTable[0][i] = item;
}
}
I called the function like this
sort(newTable, row, strlen(key));
here, key is the string 'cargo'
The definition of newTable:
char** newTable = (char**)calloc(row + 1, sizeof(char*));
for (int i = 0; i < row + 1; i++) {
newTable[i] = (char*)calloc(strlen(key), sizeof(char));
}
newTable is indeed an array of pointers and not a 2D array, so using char **newTable makes sense.
The major error is close to a typo: in the first for loop of sort, you increment col when you want to increment top. And the last line newTable[0][i] = item; is useless.
This should work (even if the loop for (int j = 0; j < row + 1; j++) suggests that row is not the number of rows in newTable but only the number of *additional rows):
void sort(char** newTable, int row, int col) {
for (int top = 1; top < col; top++) {
char item = newTable[0][top];
int i = top;
while (i > 0 && item < newTable[0][i - 1]) {
for (int j = 0; j < row + 1; j++) {
char temp = newTable[j][i];
newTable[j][i] = newTable[j][i - 1];
newTable[j][i - 1] = temp;
}
i--;
}
}
}

Neighbours algorithm

Hi a have a code like these:
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
// Do something with a[row][col];
}
}
But i want to make an operation in a grid (8-neighbours) for every a[row][col] , however when im at the corners i will have problems (i don't know how to check if im in a corner), i was thinking in code a lot of if - conditionals, but i dont know the effective way to do this..
If there is a perse method for do this types of traversal neighbours arrays i would very greatful if you could give me a link, I've spent all day looking for information and I can't find anything.
You can use these arrays which represent the changes in the indexes when you look at the neighbors from a given position:
int dx[] = {-1,0,1,1,1,0,-1,-1};
int dy[] = {-1,-1,-1,0,1,1,1,0};
At position (row, col), dx[i] represents a change in row and dy[i] represents a change in col. You can use each position in the array to look at the neighbors.
Make a helper function that checks if a position is safe:
boolean isValidPosition(int x, int y) {
return 0 <= x && x < a.length && 0 <= y && y < a[x].length;
}
Then iterate over all 8 positions and you will have access to the neighbors via a[row + dx[i]][col + dy[i]].
for (int row = 0; row < a.length; row++) {
for (int col = 0; col < a[row].length; col++) {
for (int i = 0; i < 8; i++) {
if (isValidPosition(row + dx[i], col + dy[i])) {
// Do something with a[row + dx[i]][col + dy[i]];
}
}
}
}

Converting Column Array to Rows Array in Flex

Im trying to make a multidimensional array to save the data but I obtain an error ("TypeError: Error #1010: A term is undefined and has no properties.").
var cols:int = sheet.cols;
var rows:int = sheet.values.length;
var ridx:int = 0;
var cidx:int = 0;
var out:Array = new Array();
var i:int;
for (i=0; i < cols; i++) {
out[i] = new Array();
var j:int;
for (j=0; j < rows; j++) {
out[ridx][cidx] = sheet.getCell(j, i).value;
ridx++;
if(ridx >= rows) {
cidx++;
ridx = 0;
}
}
}
what am I doing wrong?
Thanks in advance!
EDIT:
Now I have this code:
for (var i:int = 0; i < cols; i++) {
out[i] = new Array();
for (var j:int = 0; j < rows; j++) {
out[j][i] = sheet.getCell(j, i).value; //IM GETTING THE ERROR HERE
}
}
You're trying to assign a cell value to a nonexistent location in your output array.
In the first i loop, you assign out[0] = new Array();, then in the j loop, you'll set out[0][0] = sheet.getCell(0,0).value;
In the second j loop, you'll set out[1][0] = sheet.getCell(1,0).value;
In this case, sheet.getCell(1,0) may return null, but whether it does or not, out[1] is null, so your code translates to null[0] = ... -- which will trigger the error you're seeing.
You can probably fix this by swapping i and j in your inner loop.

Read a file to a 2D array in C

I'm learning C and I decided to make a text game as my learning project. So I'm trying this primitive "parser" that reads a text file into a 2D array, but there's a problem: this map doesn't use 1 character-wide cells, it uses 2 character-wide cells. For instance, the player is represented with a **, a door is represented like ## and so on.
That means, I need to read 2 characters of the text file and then assign it to the respective cell in the map.
Well, I made a test file that contains
aabbccddee
ffgghhiijj
kkllmmnnoo
ppqqrrsstt
uuvvwwxxyy
And I tried to read it with
#include <stdio.h>
#define ROWS 5
#define COLS 5
int main() {
FILE *mapfile = fopen("durr", "r");
char charbuffer[3], row, col, *map[ROWS][COLS];
/* Initializing array */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
map[row][col] = " ";
}
}
/* Reading file into array */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
map[row][col] = fgets(charbuffer, 3, mapfile);
}
}
/* Printing array */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
printf("%s", map[row][col]);
}
printf("\n");
}
fclose(mapfile);
return 0;
}
But when I execute it, I get this
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
uuuuuuuuuu
I think it has something to do with the fact that fgets return a pointer, but I'm not sure. I tried doing it with fgetc but it looks messy and I dropped it after reading about gets.
You should have strdup'ed the contents read from the file. Replace the file reading block with this:
/* Reading file into array */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
if (fgets(charbuffer, 3, mapfile))
map[row][col] = strdup(charbuffer);
}
}
and don't forget to put this at the beginning of your code too:
#include <string.h>
The main problem is that
char *map[ROWS][COLS];
only allocates space for ROWS x COLS char pointers, it does not allocate space for the strings themselves. So you're continually overwriting charbuffer as you've done it with every iteration, which explains why you end up with the same characters repeated over and over when you read out. You need to dynamically allocate the memory you need, along the lines of
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
charbuffer = (char*)malloc(sizeof(char) * 3); //****
map[row][col] = fgets(charbuffer, 3, mapfile);
}
}

Resources