Thread: Splitting stream of bytes into block of array.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Splitting stream of bytes into block of array.

    This is one program i have written to accept a fixed 16 ASCII characters and store them as hex value in a 4x4 matrix(text[4][4].

    Code:
    .
    .
    int text[4][4]
    .
    .
    for(col =0; col <6  ; col++) {
    
    	for(row = 0; row < 4 ; row++)
    	{
    	if((plaintext  = getchar()) == '\n')break;
    	text[row][col] = plaintext;
    	
    	}
    	if(plaintext == '\n') break;
    	
    	}
    However, if i wish to enhance this function by letting the user to enter any arbitrary number of characters as they wish, therefore splitting those stream of characters automatically into multiple block of 4x4 array (character 1-16 into array1, 17-33 into array2 and so on) , how can i implement this? I have tried searching around on the net and some books but i have no luck finding them. Please help! Thank you very much!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    There won't be a tutorial for everything. You will have to probably look into dynamic memory. Here's a good answer to a good question:

    http://c-faq.com/aryptr/dynmuldimary.html

    You can keep a counter of how many characters have been entered, you can keep an array of multi-dimensional arrays and keep track of the current one that you are filling in. You could keep a limit on the number of matrices a user could enter, or you could also dynamically realloc that array to let the user enter any number of them.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, a bug in the code you posted. You're overrunning the array boundaries:
    Code:
    for(col =0; col <6  ; col++) {
    But your array is only big enough for 4 columns...
    If you understand what you're doing, you're not learning anything.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Hmm, if you don't mind me thinking out loud, here's some quickie pseudocode for what it seems like you want:
    Code:
    typedef char page[4][4];
    
    page *book = NULL;
    int ch;
    int n = 0;
    
    while ( ( ch = getchar() ) != EOF ) {
      page *new_book;
      int i, j;
    
      ++n;
      new_book = realloc ( book, n );
    
      if ( new_book == NULL )
        break;
    
      book = new_book;
    
      for ( i = 0; i < 4; i++ ) {
        for ( j = 0; j < 4; j++ ) {
          if ( ch == EOF || ch == '\n' )
            goto next_page;
    
          book[n - 1][i][j] = ch;
          ch = getchar();
        }
      }
    
    next_page:
      ;
    }
    Of course, you would probably want to keep track of how many characters are in each page.
    My best code is written with the delete key.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This could work too:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char (*array)[4][4] = NULL;
      int n_arrays = 0;
      int row = 0, col = 0;
      int ch;
      int i;
      unsigned long total = 0;
    
      while((ch = getchar()) != '\n' && ch != EOF)
      {
        if(!row && !col)
        {
          array = realloc(array, sizeof(*array) * (n_arrays + 1));
          n_arrays++;
        }
    
        array[n_arrays - 1][row][col] = ch;
        total++;
    
        if(++col == 4)
        {
          col = 0;
          if(++row == 4)
            row = 0;
        }
      }
    
      row = col = i = 0;
      while(total--)
      {
        printf("%c ", array[i][row][col++]);
        if(col == 4)
        {
          col = 0;
          putchar('\n');
    
          if(++row == 4)
          {
            row = 0;
            i++;
            putchar('\n');
          }
        }
      }
    
      free(array);
      putchar('\n');
    
      return 0;
    }
    My output:
    Code:
    itsme@dreams:~/C$ ./4x4
    This sentence is more than 32 characters long.
    T h i s
      s e n
    t e n c
    e   i s
    
      m o r
    e   t h
    a n   3
    2   c h
    
    a r a c
    t e r s
      l o n
    g .
    itsme@dreams:~/C$
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > However, if i wish to enhance this function by letting the user to enter any arbitrary number of characters
    Use fgets() to read the line(s).
    Then call another function to pack 16 chars at a time into a 4x4 array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could also do repeated [scanf("%.16s")] calls, and check for newlines or other end-of-line terminators of your choosing.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jafet
    You could also do repeated [scanf("%.16s")] calls, and check for newlines or other end-of-line terminators of your choosing.
    Being whitespace delimited, this is a step down from previous recommendations, even if there wasn't UB in the format.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM