fork(8) download
  1. #include <stdio.h>
  2. #define SIZE 4
  3.  
  4. void compress2D(int data[SIZE][SIZE], int rowSize, int colSize)
  5. {
  6. int i , j , counter, tempInt;
  7. for (i = 0 ; i < rowSize ; i++)
  8. {
  9. tempInt = data[i][0];
  10. counter = 1 ;
  11.  
  12. for (j = 1 ; j < colSize ; j++ )
  13. {
  14. if (data[i][j] == tempInt)
  15. counter++ ;
  16. else
  17. {
  18. printf("%d %d " , tempInt,counter);
  19. tempInt = data[i][j];
  20. counter = 1;
  21. }
  22. }
  23. if(counter)
  24. printf("%d %d",tempInt, counter);
  25. printf("\n");
  26. }
  27. }
  28.  
  29. int main(void) {
  30. int data[SIZE][SIZE];
  31. int rowSize = SIZE, colSize= SIZE, i, j;
  32.  
  33. for (i = 0 ; i < rowSize ; i++)
  34. for (j = 0 ; j < colSize ; j++ )
  35. scanf("%d", &data[i][j]);
  36. compress2D(data, SIZE, SIZE);
  37. return 0;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. /*void compress2D(int data[SIZE][SIZE], int rowSize, int colSize)
  47. {
  48. int i , j , counter0, counter1;
  49. for (i = 0 ; i < rowSize ; i++)
  50. {
  51. counter0 = 0;
  52. counter1 = 0;
  53.  
  54. for (j = 0 ; j < colSize ; j++ )
  55. {
  56. (data[i][j] == 0) ? counter0++ : counter1++;
  57. }
  58.  
  59. if(data[i][0])
  60. {
  61. if(counter1) printf("1 %d ", counter1);
  62. if(counter0) printf("0 %d", counter0);
  63. }
  64. else
  65. {
  66. if(counter0) printf("0 %d ", counter0);
  67. if(counter1) printf("1 %d", counter1);
  68. }
  69. printf("\n");
  70. }
  71. }*/
  72.  
Success #stdin #stdout 0s 9432KB
stdin
1 1 1 0
0 1 1 0
1 1 1 1
0 0 0 0
stdout
1 3 0 1
0 1 1 2 0 1
1 4
0 4