Thread: compress array of characters

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    compress array of characters

    I am trying to write a function compress() that takes in an input of a 2-dimensional array (5x10) of character
    data, compresses each row of the array by replacing each run of character with a single character
    and the number of times it occurs, and prints on each line the result of compression in the
    function. For example, the row with data aabbcdddee may be compressed into a2b2c1d3e2.

    Input (5x10 of characters):
    aaacccdeee
    sssseeeedd
    aaaaaaaccc
    eeedddasee
    ddeeeeeggg


    Output:
    a3c3d1e3
    s4e4d2
    a7c3
    e3d3a1s1e2
    d2e5g3




    I could achieve the compression part, but behind the compressed array, it printed out 5 extra lines of alien ASCI code, followed by printing out 5 times of everything .


    Code:
    compress array of characters-error-q2-jpg
    
    
    void compress(char data[SIZE1][SIZE2]) 
    { 
    	int i,j, k,l ,count; 
    	char data123;
    	printf("Enter your data (5x10 of characters): ");
    	for (i=0; i < SIZE1; i++) {
    		for (j=0; j < SIZE2; j++)
    			data[i][j] = getchar();
    		fflush(stdin);
    	}
    	printf("\n");	   
    
    
    	for (l=0;l<SIZE1;l++){
    
    
    		for(j = 0; j < SIZE2; j++) 
    		{ 
    			count = 0; 
    			printf("\n"); 
    
    
    			for(k = 0; k < SIZE2; k++) 
    			{ 
    				count++; 
    				
    				if (k == SIZE2-1 || data[j][k] != data[j][k+1]) 
    				{ 
    					printf("%c %d ", data[j][k], count); 
    					count = 0; 
    				} 
    			} 
    		} 
    		//printf("\n");
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How are SIZE1 and SIZE2 defined?

    You really should have posted a small complete program for this problem, then we wouldn't have to guess.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Code:
    #define SIZE1 5 
    #define SIZE2 10
    
    void compress(char data[SIZE1][SIZE2]) 
    {       int i,j, k,l ,count; 
    	char data123;
    	printf("Enter your data (5x10 of characters): ");
    	for (i=0; i < SIZE1; i++) {
    		for (j=0; j < SIZE2; j++)
    			data[i][j] = getchar();
    		fflush(stdin);
    	}
    	printf("\n");	   
    
    
    	for (l=0;l<SIZE1;l++){
    
    
    		for(j = 0; j < SIZE2; j++) 
    		{ 
    			count = 0; 
    			printf("\n"); 
    
    
    			for(k = 0; k < SIZE2; k++) 
    			{ 
    				count++; 
    
    
    				if (k == SIZE2-1 || data[j][k] != data[j][k+1]) 
    				{ 
    					printf("%c %d ", data[j][k], count); 
    					count = 0; 
    				} 
    			} 
    		} 
    	}
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So you enter SIZE1 lines and print SIZE2 lines, why would you expect a different output? By the way this is one of the reasons using meaningful variable names is highly recommended.

    Jim
    Last edited by jimblumberg; 03-15-2014 at 07:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-01-2014, 07:45 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Compress and uncompress? Please help....
    By patso in forum C Programming
    Replies: 15
    Last Post: 03-04-2010, 01:49 AM
  4. compress a file
    By rotis23 in forum Windows Programming
    Replies: 2
    Last Post: 12-03-2004, 12:04 PM

Tags for this Thread