Thread: transforming string to 2D array..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    transforming string to 2D array..

    I got a string of maximum 40 cells of chars.
    In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
    so each time i can save 40/5=8 coordinates
    i got 2 chars"1" "2" which i need to put.
    in a row of cords[8][2]

    again i can have only 2 rows filled out of the available 8.
    one row per one coordinate
    i know how to change a chat to integer
    but i cant figure our how to arrange them in the 2d array??

    i tried
    Code:
    leng2=leng/5;
    for(index=0;index<leng2;index++){
        for(kndex=0;kndex<2;kndex++){
            
               tr=input[index+kndex]-'0';
                 cords[index][kndex]=tr; 
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    I got a string of maximum 40 cells of chars.
    In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
    so each time i can save 40/5=8 coordinates
    i got 2 chars"1" "2" which i need to put.
    in a row of cords[8][2]
    if you know the input to be saved is all ints, you can scan them into corresponding integer variables.
    Quote Originally Posted by transgalactic2 View Post
    again i can have only 2 rows filled out of the available 8.
    one row per one coordinate
    i know how to change a chat to integer
    but i cant figure our how to arrange them in the 2d array??
    not sure what you mean by the above

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how divide a 1d aray into pairs?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    how divide a 1d aray into pairs?
    Just wanted to point out that please try to provide relevant details instead of answering a question with another.

    That said, how does your input data look like; show a sample of the input along with its formatting so it can be parsed by relevant routines. If I get it, your program waits on stdin until you type something like (1,2) i.e. a pair of coordinates, one per line. Your program parses the input and puts it into a 2D array coords[8][2] (and this is where you need help?).

    So in the example above the program should set coords[0][0] = 1 and coords[0][1] = 2. Is that right??

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by transgalactic2 View Post
    I got a string of maximum 40 cells of chars.
    In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
    so each time i can save 40/5=8 coordinates
    i got 2 chars"1" "2" which i need to put.
    in a row of cords[8][2]

    again i can have only 2 rows filled out of the available 8.
    one row per one coordinate
    i know how to change a chat to integer
    but i cant figure our how to arrange them in the 2d array??

    i tried
    Code:
    leng2=leng/5;
    for(index=0;index<leng2;index++){
        for(kndex=0;kndex<2;kndex++){
            
               tr=input[index+kndex]-'0';
                 cords[index][kndex]=tr; 
        }
    }
    Well there are easier ways to parse a string.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int coords[8][2];
    
        char data[] = "(0,2)(1,3)(2,4)(3,5)(4,6)(5,7)(6,8)(7,9)";
    
        char * scan = data;
        int i = 0;
        while ( *scan != '\0' ) {
            int pos;
            if ( 2 == sscanf( scan, "(%d,%d)%n", &coords[i][0], &coords[i][1], &pos ) ) {
                scan += pos;
                i++;
            }
            else {
                break;
            }
        }
    
        i = 0;
        while (i < 8) {
            printf( "point %d:\n", i );
            printf( "\tx point = %d\n", coords[i][0] );
            printf( "\ty point = %d\n", coords[i][1] );
            i++;
        }
    
        return 0;
    }
    Modify as necessary.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I put this working code that cuts a string into pairs and produces a 2d int array.
    i changed
    i put this input (in option 2) "(2, 1) ( 1, 1) ( 2, 2 ) "
    i am supposed to get
    2 1
    1 1
    2 2
    array

    but it prints
    2 -16
    -48 -48
    -48 -48

    I tried to change the < if the for loops into <=
    its not working.
    i marked the transformation part with "start pair array" and "end pair array"
    ??

    Code:
    #include <stdio.h>
    
    #define N 9
    int is_valid(const char input[],int leng);
    void mainPlayGame(board);
    int playGame(char board[N][N],int size);
    void printBoard(char board[N][N],int size);
    int removeMine(char board[N][N],int size,int x,int y);
    void mainMenu();
    int unCover(char board[N][N],int size,int x,int y);
    
    int main(){
     char input[40];
     char input2[40];
    char board[N][N];
    mainPlayGame(board);
    printf("bye, please press enter to exit!\n");
    getchar();
    return 0;
    }
    
    void mainPlayGame(board){
        int opt,leng,size=-1;
      int index,kndex;
        char input[40];
        char ch_cords[40];
        char input2[40];
        int tr;
        int cords[8][2];
        int tndex;
    
        int i,k,j,ch,l;
    
        int  leng2;
        do
        {
        mainMenu();
    
        scanf("%d",&opt);
        l=getchar();
         if (opt==1){
             printf("enter board size [1..9] (no input control is needed):\n");
             scanf("%d",&size);
         }
    
         if (opt==2){
             if (size==-1){
                printf("you must choose board size first!\n");
             }
             else{
                printf("enter string\n"); //start pair array
                for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
                  {
                    input2[i] = ch;
                  }
    
                    input2[i] = '\0';
    
    
                   k=0;
                   for(index=0;index<=i;index++){
                      if(input2[index]!=' '){
                          input[k]=input2[index];
                           k++;
                        }
                   }
                   leng=k;
                  if (is_valid(input,leng))
                      {
                        leng2=leng/5;
                        k=0;
                        for(index=0;index<=leng2;index++){
                           if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
                              ch_cords[k]=input2[index];
                              k++;
                           }
                          }
                        j=0;
                        for(index=0;index<=leng2;index++){
                            for(kndex=0;kndex<2;kndex++){
                             tr=ch_cords[j]-'0';
                             cords[index][kndex]=tr;
                             j++;
                            }
                          }
    
                        for (index = 0; index < leng2; index++)
                            {
                           for (kndex = 0; kndex < 2; kndex++)
                              {
                                 printf("%d ",cords[index][kndex]);
                              }
                                printf("\n");
                            }
                        }//end pair array
        else//else
        {
            printf("%s is invalid.\n", input);
        }
    
    
    
    
    //////////////////////////////////////////////input check
    
    
    
             }
         }//end if opt 2
         if (opt==3){
         }
         if (opt==4){
         }
         if (opt==5){
         }
         if (opt==0){
         }
        }while(opt!=0);
    }
    
    int playGame(char board[N][N],int size){
    
    }
    
    void mainMenu() {
    
    
     printf("--------------------------\n");
    printf("welcome to the game\n");
    printf("1. choose board size\n");
    printf("2. place mines\n");
    printf("3. remove mines\n");
    printf("4. show mines\n");
    printf("5. start the game\n");
    printf("0. exit\n");
    printf("please enter your choice (input control is needed): \n");
    
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    
    int removeMine(char board[N][N],int size,int x,int y)
    {
        if (board[y-1][x-1]=='*'){
           board[y-1][x-1]=' ';
           printf("mine removed");
           return 1;
        }
        else
        {
        printf("wrong input or no mine");
        return 0;
        }
    
    
    }
    
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost\n");
       board[y-1][x-1]='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
    
    
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
    
     }
    }
    
    
    int is_valid(const char input[],int leng)
    {
    	int flag=3;
    	int i=1;
        /* Match "(<number>,<number>)" */
      int index;
         for(index=0;index<leng;index++){
      if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
           if (((input[1])<'0')||((input[1])>'9')){
    		   return 0;
    	   }
      }
     }
         if (input[0] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[1])<'0')||((input[1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         input++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
    	//////////////////////////////////////////////////////////////
        if (flag ==(int)NULL)
        {
            return 0;
        }
    
        /* Match zero or more " (<number>,<number>)" */
        while (input[i]!= '\0')
        {
    
    
             if (input[i] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
            if (flag ==(int)NULL)
            {
                return 0;
            }
        }
    
    ///////////////////////////////////////
        return 1;
        //////////////////////////////////////
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    48 is the ascii number for 0. (zero).

    So your program is subtracting 0 where it should not be, in that section of code.


    If you need more help on this, post back, but I believe you can find and solve this problem, with this information.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed the <= into < so it will not go "out of band"
    its still gives me a similar result to this "(2, 1) ( 1, 1) ( 2, 2 ) " input

    Code:
    #include <stdio.h>
    
    #define N 9
    int is_valid(const char input[],int leng);
    void mainPlayGame(board);
    int playGame(char board[N][N],int size);
    void printBoard(char board[N][N],int size);
    int removeMine(char board[N][N],int size,int x,int y);
    void mainMenu();
    int unCover(char board[N][N],int size,int x,int y);
    
    int main(){
     char input[40];
     char input2[40];
    char board[N][N];
    mainPlayGame(board);
    printf("bye, please press enter to exit!\n");
    getchar();
    return 0;
    }
    
    void mainPlayGame(board){
        int opt,leng,size=-1;
      int index,kndex;
        char input[40];
        char ch_cords[40];
        char input2[40];
        int tr;
        int cords[8][2];
        int tndex;
    
        int i,k,j,ch,l;
    
        int  leng2;
        do
        {
        mainMenu();
    
        scanf("%d",&opt);
        l=getchar();
         if (opt==1){
             printf("enter board size [1..9] (no input control is needed):\n");
             scanf("%d",&size);
         }
    
         if (opt==2){
             if (size==-1){
                printf("you must choose board size first!\n");
             }
             else{
                printf("enter string\n"); //start pair array
                for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
                  {
                    input2[i] = ch;
                  }
    
                    input2[i] = '\0';
    
    
                   k=0;
                   for(index=0;index<=i;index++){
                      if(input2[index]!=' '){
                          input[k]=input2[index];
                           k++;
                        }
                   }
                   leng=k;
                  if (is_valid(input,leng))
                      {
                        leng2=leng/5;
                        k=0;
                        for(index=0;index<leng2;index++){
                           if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
                              ch_cords[k]=input2[index];
                              k++;
                           }
                          }
                        j=0;
                        for(index=0;index<leng2;index++){
                            for(kndex=0;kndex<2;kndex++){
                             tr=ch_cords[j]-'0';
                             cords[index][kndex]=tr;
                             j++;
                            }
                          }
    
                        for (index = 0; index < leng2; index++)
                            {
                           for (kndex = 0; kndex < 2; kndex++)
                              {
                                 printf("%d ",cords[index][kndex]);
                              }
                                printf("\n");
                            }
                        }//end pair array
        else//else
        {
            printf("%s is invalid.\n", input);
        }
    
    
    
    
    //////////////////////////////////////////////input check
    
    
    
             }
         }//end if opt 2
         if (opt==3){
         }
         if (opt==4){
         }
         if (opt==5){
         }
         if (opt==0){
         }
        }while(opt!=0);
    }
    
    int playGame(char board[N][N],int size){
    
    }
    
    void mainMenu() {
    
    
     printf("--------------------------\n");
    printf("welcome to the game\n");
    printf("1. choose board size\n");
    printf("2. place mines\n");
    printf("3. remove mines\n");
    printf("4. show mines\n");
    printf("5. start the game\n");
    printf("0. exit\n");
    printf("please enter your choice (input control is needed): \n");
    
    }
    
    
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }
    
    int removeMine(char board[N][N],int size,int x,int y)
    {
        if (board[y-1][x-1]=='*'){
           board[y-1][x-1]=' ';
           printf("mine removed");
           return 1;
        }
        else
        {
        printf("wrong input or no mine");
        return 0;
        }
    
    
    }
    
    
    int unCover(char board[N][N],int size,int x,int y){
     int count=0;
      if (board[y-1][x-1]=='*'){
       printf("you lost\n");
       board[y-1][x-1]='X';
       printBoard(board, size);
       return 0;
      }
     else
     {
    
    
          if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
           count++;
          }
    
            if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
           count++;
          }
            if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
           count++;
          }
    
    
          if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
           count++;
          }
    
            if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
           count++;
          }
            if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
           count++;
          }
          if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
           count++;
          }
    
           board[y-1][x-1]='0' +count;
           printBoard(board, size);
       return 1;
    
     }
    }
    
    
    int is_valid(const char input[],int leng)
    {
    	int flag=3;
    	int i=1;
        /* Match "(<number>,<number>)" */
      int index;
         for(index=0;index<leng;index++){
      if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
           if (((input[1])<'0')||((input[1])>'9')){
    		   return 0;
    	   }
      }
     }
         if (input[0] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[1])<'0')||((input[1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         input++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
    	//////////////////////////////////////////////////////////////
        if (flag ==(int)NULL)
        {
            return 0;
        }
    
        /* Match zero or more " (<number>,<number>)" */
        while (input[i]!= '\0')
        {
    
    
             if (input[i] != '(')
          {
            flag=(int)NULL;
          }
    	   if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
    	   i++;
        while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        } //it wiil get char '2'
    
       if (input[i] == ',') {   //'2' differs ','
      if (((input[i+1])<'0')||((input[i+1])>'9')){
    		   flag=(int)NULL;
    	   }
      i++;
             while((input[i]<='9')&&(input[i]>='0')){
    
         i++; //increasing the  address by1
        }
            if (input[i]==')'){
             i++;
            }
            else
              flag=(int)NULL;
        }
       else{
          flag=(int)NULL;
       }
            if (flag ==(int)NULL)
            {
                return 0;
            }
        }
    
    ///////////////////////////////////////
        return 1;
        //////////////////////////////////////
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe it's just too early in the morning for me, but i don't see a "start pair array" and "end pair array", in your posted code.

    I do see a lot of "i++"'s in your last section of code. They don't appear to have adequate checking logic to stop them from going out of bounds of your array.

    If you'll step through that part of your code, and put a watch on your input values, I believe you'll see what the problem is.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got it solved .
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 01:30 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM