C++ two dimensional char array

when I have a "table": 8*8 and i want to make a program that will change every '+' to 1, and everything else to 0. Example:
dkghft++
gfh++hfg
sad+fdg+
hgfh+gfh
+hfh++fg
++jk+ghj
++++++++
+5kjl+jk
and i want to get this:
00000011
00011000
00010001
00001000
10001100
11001000
11111111
10000100
So this is what I've done, but it's not working...
#include <stdio.h>
main(){
char r[7][7];
int a[7][7];
for(int i=0;i<8;i++)
{
scanf("%s",r[i]);
for(int j=0;j<8;j++)
{
if(r[i][j]=='+')a[i][j]=1;
else a[i][j]=0;
}
}
for(int i=0;i<8;i++)
{
printf("\n%s\n",r[i]);
for(int j=0;j<8;j++)
{
printf("%d",a[i][j]);
}
}
}
Last edited on
perhaps your dimensions need to be [8][8]
even tho you call values as 0-7 the number in the brackets when you declare the array needs to be the actual integer number of cells you want

make it:

char r[8][8];
int a[8][8];

that's just what I noticed at first
Last edited on
The code to do this is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
main() {
  char r[8]8]; // You should pass the length, not the greater index
  int a[8][8];

  for(int i = 0; i < 8; i++) {
    scanf("%s",r[i]);
    for(int j = 0; j < 8; j++) {
        if(r[i][j] == '+') a[i][j] = 1;
        else a[i][j] = 0;
    }
  }
  for(int i = 0;i < 8; i++) {
    printf("\n%s\n", r[i]);
    for(int j = 0; j < 8; j++) {
      printf("%d", a[i][j]);
    }
  } 
}

And dont forget to define the value variable r;
Last edited on
thank you, it's working now..
Topic archived. No new replies allowed.