SolvedMulti Dimensional Arrays [2D]

Posts 113 of 13 · Page 1 of 1
Multi Dimensional Arrays [2D]
My sister is in College taking C, CPP, VB , ASM, HTML and a list of other languages. Currently she is working on a work project she can't seem to get 100%, she would like to import a file into a the application as a multi dimensional array (2D), if she can get that correctly , she can finish the problem with ease, but with my VERY limited C knowledge and the apparent differences between my basic C++ knowledge and the specific needs of this project in C, it is far out of my scope to help her out.

Example of what she needs to do.

{I am avoiding the Exact example so there is only help & no direct answers}
{so there will be some arbitrary information negated}

If a text file is comprised of 8 by 8 Square.

11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888


She needs that information imported into (print) the application as is {exactly} as a multi dimensional array, not a single array or string.
The placement of each char in it's exact location ("x,Y") is very important {which she can figure out} once it is imported correctly, this way if we look at the following example

1#######
#######5
8#######
######4#
####7###
##2#####
######3#
6#######

& she needed to get the location of 7 (in the array, position 5,5) she could.

Thanks for the help, if the information is enough.
Same logic should be as any other programming language..

Readline (array) in a for cycle
Copy that array to the current line of the multi dimensional array.
Repeat..

Or the problem here is the actual code?
Removed because issue was solved & would prefer that the specifics didn't exist.
if(text = '\n')
y+=1;

just my 5 cents
Forgot that C was capable of multidimensional arrays(I thought it was introduced in C++), should've suggested this in the first place X:

Code:
int myArray[8][8];
Pastebin.com
^ That might spoil it for her, but I suggest seeing it for yourself. It's the code to load the file into the 2D array only, so she'll still have to write the searching part(which shouldn't be too hard)

edit - while loading:
(x-(x%8))/8 will be the x location in the array and x%8 will be the y location in the array, in case she wants to scan during loading
Meh, something I whipped up in C that *should* work.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _file_grid
{
	size_t width;
	size_t height;
	char **data;
} FILE_GRID, *PFILE_GRID;

PFILE_GRID readFile(const char *fpath)
{
	PFILE_GRID pData = NULL;
	FILE *fp = fopen(fpath, "rb");
	char buffer[1024];
	size_t width = 0;

	if (fp)
	{
		pData = (PFILE_GRID)calloc(1, sizeof(FILE_GRID));

		while((fgets(buffer, sizeof(buffer), fp)) != NULL)
		{
			pData->height++;
			width = strlen(buffer);
			pData->data = (char**)realloc(pData->data, sizeof(char*) * pData->height); //allocate another line worth of memory 
			//(note, this is fairly inefficient, a better way would be to start the allocation at like... 10 * sizeof(char*) and then increase the buffer by 10 units
			//each time it runs out of room, saves on realloc calls which can be expensive if you don't have enough linear memory for the allocation.
			pData->data[pData->height - 1] = strdup(buffer); //duplicate the buffer to it's own memory location.
			//trim the newline characters.
			if (pData->data[pData->height - 1][width - 1] == '\n')
				pData->data[pData->height - 1][width - 1] = '\0';

			pData->width = width;
		}
		fclose(fp);
	}
	return pData;
}

void freeFile(PFILE_GRID pGrid)
{
	for(size_t h = 0; h < pGrid->height; h++)
		free(pGrid->data[h]); //free all the strdup'd pointers, they are dynamically allocated too.
	free(pGrid->data); //free the top level pointer now that the sub-pointers have been deallocated.
	free(pGrid); //finally free the grid itself.
}
Pretty simple to use, just remember that the coordinates are reversed when indexing into the array.

i.e if i have temp.txt as follows:
Code:
12121212
34343434
56565656
And I wanted to access the char at line 2, column 4 (4), you would do something like this:
Code:
PFILE_GRID pGrid = readFile("B:\\temp.txt");
if (pGrid)
    printf("Char at (%d, %d): %c\n", 3, 1, pGrid->data[1][3]);
freeFile(pGrid);
To get the expected output:
Code:
Char at (3, 1): 4
I'll send her both @Hell_Demon & @Jason , Thanks.

Now that I believe the issue was solved.

The original source that was problematic was.

Code:
#include <stdio.h>

#define W_H_SIZE 8
#define W_W_SIZE 64
    
      int main(void)
{
    FILE *fpIn;
     char newarray[W_H_SIZE][W_H_SIZE];
     int i,j;
    
    fpIn = fopen("file.txt", "r");
        
    for(i =0; i < 8; i++)
    {
          
          fscanf(fpIn,"%s",&newarray[i]);
          }
          
          printf("%s",newarray);
    //int i;
    
    
    getchar();
    return 0;
}

Quote Originally Posted by NextGen1 View Post
I'll send her both @Hell_Demon & @Jason , Thanks.

Now that I believe the issue was solved.

The original source that was problematic was.

Code:
#include <stdio.h>

#define W_H_SIZE 8
#define W_W_SIZE 64
    
      int main(void)
{
    FILE *fpIn;
     char newarray[W_H_SIZE][W_H_SIZE];
     int i,j;
    
    fpIn = fopen("file.txt", "r");
        
    for(i =0; i < 8; i++)
    {
          
          fscanf(fpIn,"%s",&newarray[i]);
          }
          
          printf("%s",newarray);
    //int i;
    
    
    getchar();
    return 0;
}

With this done the only thing that would need more was another for to do a [][]. (easier way but not the best and the one to be expected from someone who is learning @ school for example)
for(){
....for(){
}}

But yea, HD solution has way less cycles.
Here's where she is at, she commented on problematic line(s)
{Bolded the comment(s) to make them easier to see}

Code:
#include <stdio.h>

int pawnCk(char **boardArray,int kingRow, int kingCol);

int main(void)
{
  
  char boardArray[8][8] = { 0 };
  int check = 0 ;
  int bKingRow = 8;
  int bKingCol = 8;
  int wKingRow = 8;
  int wKingCol = 8;
  int i = 0;
  int j = 0; 
  char ch;
  FILE *fpIn, *fpOut;
  
  fpIn = fopen("Board.txt", "r");
  
  while ((ch = getc(fpIn)) != EOF) {
    switch(ch) {
    case 'k': 
         bKingRow = i;
         bKingCol = j;
         boardArray[i][j] = ch;
    case 'K': 
         wKingRow = i;
         wKingCol = j;
         boardArray[i][j] = ch;
    case 'p':   case 'P': 
    case 'q':   case 'Q': 
    case 'n':   case 'N': 
    case 'b':   case 'B': 
    case 'r':   case 'R': 
      if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
     boardArray[i][j] = ch;
    case '*':
      if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
      i++;
      break;
    case '\n':
      if (i != 8) { printf("Bad file: There were only %d spaces on line %d\n", i, j); return -1; }
      i = 0;
      j++;
      if (j >= 8);
      break;
    }
}
  if (j < 7) { printf("Bad file: There were only %d lines.\n", j); return -1;}
  if (i != 8) { printf("Bad file: There were only %d spaces on the last line.\n", i); return -1;}
  
  if ((bKingRow < 8) && (bKingCol < 8))
  {
      check = pawnCk(boardArray, bKingRow, bKingCol);  //On compile:Error [Warning] Passing arg1 of 'pawCk' from incompatible pointer type.
   }
  getchar();
  return 0;
}
int pawnCk(char **boardArray,int kingRow, int kingCol)
{
    int atkRow ,atkCol1, atkCol2;
    if (kingRow = 'k')
    {
    atkRow = kingRow + 1;
    atkCol1 = kingCol - 1;
    } 
      //Rich(Nextgen) Comment: She doesn't know you, but I think I figured out why she is getting this error, I emailed her but feel free to offer other opinions as well, Please & Thank You
    if (boardArray[atkRow][atkCol1] = 'P') //On Debug: Access violation (segmentation fault) raised in your program 
    return 1;
    
    atkCol2 = kingCol + 1;
     if (boardArray[atkRow][atkCol2] = 'P')
    return 1;
    else
    return 0;
}
    if (kingRow = 'K')
    {
    atkRow = kingRow -1;
    atkCol1=kingCol - 1;

    if (boardArray[atkRow][atkCol1] = 'p')
    {
    return 1;
    }
    atkCol2 = kingCol + 1;
     if (boardArray[atkRow][atkCol2] = 'p')
    return 1;
    else
    return 0;
}
}
As always , Thank you.
She wen't with a modified version of @Hell_Demon (s') solution, but modified it slightly to work with that she needs it for.}
I see VB habbits

Comparisons in C/++ are with double = signs

Also
check = pawnCk(boardArray, bKingRow, bKingCol); //On compile:Error [Warning] Passing arg1 of 'pawCk' from incompatible pointer type.

you'll want to pass a pointer to boardArray(should fix the compiler warning as well)
check = pawnCk(&boardArray, bKingRow, bKingCol);

Off to bed now, I'll re-read it tomorrow to see if theres more
Ill send that to her, @ VB Habbits, those could be my attempt at helping her / or her own. lol.
As always, thanked.

@Hell_Demon
Yea, Got an email back, as of now the ampersand didn't help. Still receives the same 2 errors.
Also, for some reason she is getting Syntax errors before the if statements, however there is nothing there, I thought maybe she forgot to end the If statements before, but they don't need to.

Code:
#include <stdio.h>

int pawnCk(char boardArray[8][8],int kingRow, int kingCol);

int main(void)
{
	
	char boardArray[8][8] = { 0 };
	int check = 0 ;
	int bKingRow = 8;
	int bKingCol = 8;
	int wKingRow = 8;
	int wKingCol = 8;
	int i = 0;
	int j = 0; 
	char ch;
	FILE *fpIn, *fpOut;
	
	fpIn = fopen("Board.txt", "r");
	
	while ((ch = getc(fpIn)) != EOF) {
		switch(ch) {
		case 'k': 
				 bKingRow = i;
				 bKingCol = j;
				 boardArray[i][j] = ch;
		case 'K': 
				 wKingRow = i;
				 wKingCol = j;
				 boardArray[i][j] = ch;
		case 'p':	 case 'P': 
		case 'q':	 case 'Q': 
		case 'n':	 case 'N': 
		case 'b':	 case 'B': 
		case 'r':	 case 'R': 
			if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
			boardArray[i][j] = ch;
		case '*':
			if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
			i++;
			break;
		case '\n':
			if (i != 8) { printf("Bad file: There were only %d spaces on line %d\n", i, j); return -1; }
			i = 0;
			j++;
			if (j >= 8)
				break;
		}
	}
	if (j < 7) { printf("Bad file: There were only %d lines.\n", j); return -1;}
	if (i != 8) { printf("Bad file: There were only %d spaces on the last line.\n", i); return -1;}
	
	if ((bKingRow < 8) && (bKingCol < 8))
	{
			check = pawnCk(boardArray, bKingRow, bKingCol);	//On compile:Error [Warning] Passing arg1 of 'pawCk' from incompatible pointer type.
	}
	getchar();
	return 0;
}
int pawnCk(char boardArray[8][8],int kingRow, int kingCol)
{
		int atkRow ,atkCol1, atkCol2;
		if (boardArray[kingRow][kingCol] == 'k')
		{
		atkRow = kingRow + 1;
		atkCol1 = kingCol - 1;
		} 
			//Rich(Nextgen) Comment: She doesn't know you, but I think I figured out why she is getting this error, I emailed her but feel free to offer other opinions as well, Please & Thank You
		if (boardArray[atkRow][atkCol1] == 'P') //On Debug: Access violation (segmentation fault) raised in your program 
		return 1;
		
		atkCol2 = kingCol + 1;
		if (boardArray[atkRow][atkCol2] == 'P')
		return 1;
		else
		return 0;

		if (kingRow == 'K')
		{
		atkRow = kingRow -1;
		atkCol1=kingCol - 1;

		if (boardArray[atkRow][atkCol1] == 'p')
		{
			return 1;
		}
		atkCol2 = kingCol + 1;

		if (boardArray[atkRow][atkCol2] == 'p')
			return 1;
		else
			return 0;
	}
}
Changed it to
int pawnCk(char boardArray[8][8],int kingRow, int kingCol);

It compiles fine now, see if that works out for her
@Hell_Demon as always (again) thank you, I sent her the code, tested it myself in CodeBlocks, seems fine to me.
Posts 113 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?