I got a question where I need to write a function that reads an integer X and an array A of type int (size N) from the keyboard and eliminates all occurrences of X in A.
for example the input is:
5
1 2 3 4 3
3
and it would return:
A : 1 2 3 4 3
New A : 1 2 4
my code so far is
#include <stdio.h>
#include<stdlib.h>
#define DIM 50
int main() {
int *A;
int N, X;
int *P1, *P2;
do{
scanf("%d", &N);
}while(N<0 || N>DIM);
A= (int*)malloc(N*sizeof(int));
for(P1=A; P1<A+N ; P1++)
scanf("%d ", P1);
printf("\n");
scanf("%d",&X);
printf("A : ");
for(P1=A; P1<A+N ; P1++)
printf("%d ", *P1);
printf("\n");
but I don't know how to continue if you could please help
What you need is to write a function that will erase elements equal to the specified value and reallocate the result array.
Here is a demonstration program where such a function is shown in action.
#include <stdio.h>
#include <stdlib.h>
size_t erase_remove( int **a, size_t n, int value )
{
size_t m = 0;
for (int *p = *a, *q = *a; p != *a + n; ++p)
{
if (*p != value)
{
if (q != p) *q = *p;
++q;
++m;
}
}
if (m != n)
{
int *tmp = realloc( *a, m * sizeof( int ) );
if (tmp != NULL)
{
*a = tmp;
}
else
{
m = -1;
}
}
return m;
}
int main( void )
{
size_t n = 5;
int *a = malloc( n * sizeof( int ) );
size_t i = 0;
a[i++] = 1, a[i++] = 2, a[i++] = 3, a[i++] = 4, a[i++] = 3;
int value = 3;
size_t m = erase_remove( &a, n, value );
if (m != -1) n = m;
for (const int *p = a; p != a + n; ++p)
{
printf( "%d ", *p );
}
putchar( '\n' );
free( a );
}
The program output is
1 2 4
If the memory reallocation for the array within the function was not successful the function returns the value (size_t)-1.
The function preserves the order of elements after removing elements equal to the target value.
If to make the function more general that can deal not only with arrays dynamically allocated then it can look very simply.
size_t erase_remove( int *a, size_t n, int value )
{
size_t m = 0;
for (int *p = a, *q = a; p != a + n; ++p)
{
if (*p != value)
{
if (q != p) *q = *p;
++q;
++m;
}
}
return m;
}
In this case the caller of the function should reallocate the result dynamically allocated array (if it is required) based on the returned value m from the function.
#define N_MAX 50
#define N_MIN 0
int main(void) {
int n;
do{
scanf("%d", &n);
}while(N<N_MIN || N>N_MAX);
int *array = (int*) malloc(sizeof(int) * n);
int i; // number of elements in array
for (i = 0; i < n; i++) {
scanf("%d", array + i);
}
int x;
scanf("%d", &x);
//remove x from arr
for (int j = 0; j <= i; j++) {
if (*(array + j) == x) {
*(array + j) = *(array + i); // replace removed value by last value in array
i--; // decremment number of elements in array
}
}
// print
for (int j = 0; j <= i; j++) {
print("%d", *(array + j));
}
free(array)
}
Try this out!
#include <stdio.h>
#include <stdlib.h>
// Required Prototypes
int *get_nums(char *, size_t *);
int *remove_num(int *, size_t *, int);
void display(char *, int *, size_t);
int main(int argc, char *argv[])
{
size_t size = 0;
int *arr = get_nums("Enter numbers (seperated by space): ", &size);
int num;
printf("Enter number to be removed: ");
scanf("%d", &num);
display("Old Array: ", arr, size);
arr = remove_num(arr, &size, num);
display("New Array: ", arr, size);
free(arr);
return 0;
}
int *get_nums(char *label, size_t *size)
{
size_t length = 0;
int *arr = NULL;
printf("%s", label);
int c, num;
do {
scanf("%d", &num);
arr = realloc(arr, (length + 1) * sizeof(int));
arr[length++] = num;
} while ( (c = getchar()) != '\n' && c != EOF);
*size = length;
return arr;
}
int *remove_num(int *arr, size_t *size, int num)
{
// Copy elements to the new array
// Return the new array
size_t new_size = 0;
int *new_arr = NULL;
for (size_t i = 0; i < *size; ++i) {
if (arr[i] != num) {
new_arr = realloc(new_arr, (new_size + 1) * sizeof(int));
new_arr[new_size++] = arr[i];
}
}
*size = new_size;
free(arr);
return new_arr;
}
void display(char *label, int *arr, size_t size)
{
printf("%s", label);
for (size_t i = 0; i < size; ++i)
printf("%d ", arr[i]);
printf("\n");
}
The main idea is you create an array of integers. Then you copy those elements to a new array which you do not want to remove. And finally you display the new array. That's all. Yes, it's that simple. ;-)
Enter numbers (seperated by space): 1 2 3 4 3
Enter number to be removed: 3
Old Array: 1 2 3 4 3
New Array: 1 2 4
As #Ahmed Masud said in comments about too many reallocations, here's my modified answer. Please do note that the code below is little bit complex but far more efficient than my previous one.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *a;
size_t length;
size_t capacity;
} Array;
// Required Prototypes
Array *init_Array(void);
void destroy(Array *);
Array *get_nums(char *);
void remove_num(Array *, int);
void display(char *, Array *);
int main(int argc, char *argv[])
{
Array *arr = get_nums("Enter Numbers (seperated by space): ");
int num;
printf("Enter number to be removed: ");
scanf("%d", &num);
display("Old Array: ", arr);
remove_num(arr, num);
display("New Array: ", arr);
destroy(arr);
return 0;
}
Array *init_Array(void)
{
Array *arr = malloc( sizeof(Array) );
arr->capacity = 1;
arr->length = 0;
arr->a = malloc( sizeof(int) );
return arr;
}
Array *get_nums(char *label)
{
printf("%s", label);
Array *arr = init_Array();
int c, num;
do {
scanf("%d", &num);
// check and reallocate
if (arr->length == arr->capacity) {
arr->a = realloc(
arr->a,
(2 * arr->capacity) * sizeof(int)
);
arr->capacity *= 2;
}
arr->a[arr->length++] = num;
} while ((c = getchar()) != '\n' && c != EOF);
return arr;
}
void remove_num(Array *arr, int num)
{
int remv_idx = -1;
int *a = arr->a;
size_t count = 0;
for (size_t i = 0; i < arr->length; ++i) {
if (a[i] == num) count++;
if (a[i] == num && remv_idx == -1)
remv_idx = i;
if (remv_idx != -1 && remv_idx < i && a[i] != num)
a[remv_idx++] = a[i];
}
arr->length -= count;
arr->capacity = arr->length;
arr->a = realloc(a, arr->capacity * sizeof(int));
}
void display(char *label, Array *arr)
{
printf("%s", label);
for (size_t i = 0; i < arr->length; ++i)
printf("%d ", arr->a[i]);
printf("\n");
}
void destroy(Array *arr)
{
free(arr->a);
free(arr);
}
Here I did not consider any new array but removed the elements in place. I'm keeping both of my solution because you might not need the 2nd one if your input space is small. One more thing, since the question did not mention about any reallocations failures so I did not check it in my code.
Here is an approach:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_arr(int *arr, size_t size);
size_t remove_by_value(int *arr, size_t len, int value)
{
int count = 0; // maintain how many times we see value
int k;
for(k = 0; k < len ; k++) {
if ( arr[k] == value ) {
while(arr[count+k] == value) {
count++;
} // skip over conscutive values
if ( count + k >= len )
break;
arr[k] = arr[k+count];
arr[k+count] = value;
print_arr(arr, len);
}
}
return len-count;
}
void print_arr(int *arr, size_t size)
{
for(int k = 0; k < size; k++) {
printf("%02d ", arr[k]);
}
printf("---\n");
}
int main()
{
int test_values[] = { 0, 1, 3, 2, 3, 5, 4, 7, 8 };
size_t len = sizeof(test_values)/sizeof(int);
int *arr = malloc(len*sizeof(int));
memcpy(arr, test_values, len*sizeof(int));
print_arr(arr, len);
len = remove_by_value(arr, len, 3);
print_arr(arr, len);
arr = realloc(arr, len);
print_arr(arr, sizeof(int)*len);
return 0;
}
It bubbles the value to be extracted to the end of the array and lops it off.
The nice thing is that it doesn't use any extra memory to do its work.
The second part that is that it is NOT O(n^2) I have to think a bit about the complexity of it (seems bigger than O(n))
However it's a simple solution that keeps the order of the array, removes unneeded values simply.
i've put in the print_arr function at each step of the loop so that you can see what's happening.
Hopefully the code is clear in its purpose, if you have questions please comment and I will further explanation.
Notes
I expressly did not use sizeof(*arr) because i wanted it to be a bit more clear as to what it is. In production code one would use sizeof(*arr) instead of sizeof(int) .... However I would not be able to create a consistent example (e.g. remove_by_value would have to be similarly made generic) so I didn't to keep the example simple to understand.
Related
So I'm very new to programming and the C language, and I would like to find the simplest, fastest, and most efficient way to count all the distinct elements of a 1D array. This was actually for a school assignment, but I've been stuck on this problem for days, since my program was apparently too slow for the online judge and it got a TLE. I've used regular arrays and dynamically allocated arrays using malloc, but neither worked.
Anyways, here's the latest code of it(using malloc):
#include <stdio.h>
#include <stdlib.h>
int distinct(int *arr, int N){
int j, k, count = 1;
for(j = 1; j < N; j++){
for(k = 0; k < j; k++){
if(arr[j] == arr[k]){
break;
}
}
if(j == k){
count++;
}
}
return count;
}
int main(){
int T, N, i = 0;
scanf("%d", &T);
do{
scanf("%d", &N);
int *arr;
arr = (int*)malloc(N * sizeof(int));
for(int j = 0; j < N; j++){
scanf("%d", &arr[j]);
}
int count = distinct(arr, N);
printf("Case #%d: %d\n", i + 1, count);
i++;
}while(i < T);
return 0;
}
The most efficient way depends on too many unknown factors. One way is to sort the array and then to count distinct elements in there, skipping the duplicates as you go. If you have sorted the array and gotten this:
1 1 1 1 2 2 2 2 3 3
^ ^ ^
+-skip--+-skip--+-- end
... you can easily see that there are 3 distinct values in there.
If you don't have a favourite sorting algorithm handy, you could use the built-in qsort function:
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
Example:
#include <stdio.h>
#include <stdlib.h>
int compar(const void *l, const void *r) {
const int* lhs = l;
const int* rhs = r;
if(*lhs < *rhs) return -1; // left side is less than right side: -1
if(*lhs > *rhs) return 1; // left side is greater than right side: 1
return 0; // they are equal: 0
}
int distinct(int arr[], int N){
// sort the numbers
qsort(arr, N, sizeof *arr, compar);
int count = 0;
for(int i=0; i < N; ++count) {
int curr = arr[i];
// skip all numbers equal to curr as shown in the graph above:
for(++i; i < N; ++i) {
if(arr[i] != curr) break;
}
}
return count;
}
int main() {
int T, N, i = 0;
if(scanf("%d", &T) != 1) return 1; // check for errors
while(T-- > 0) {
if(scanf("%d", &N) != 1) return 1;
int *arr = malloc(N * sizeof *arr);
if(arr == NULL) return 1; // check for errors
for(int j = 0; j < N; j++){
if(scanf("%d", &arr[j]) != 1) return 1;
}
int count = distinct(arr, N);
free(arr); // free after use
printf("Case #%d: %d\n", ++i, count);
}
}
I'm new to programming, and i'm trying to complement this code in C to permute strings, currently it shows all the words exchanged and counts how many characters the word has.
But I would like it to count the number of lines generated by the permutation too, and in this part, the code is not working. I do not know what else to do!
Example: The word "hi", generates two lines: hi, and ih. (In this case, i want the program write "generated words: 2")
the code:
#include <string.h>
#include <stdio.h>
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a + i), (a + j));
permute(a, i + 1, n);
swap((a + i), (a + j)); //backtrack
}
}
}
int main()
{
char str[21];
int len;
int cont = 1;
int fatorial = 1;
printf("\nType a word: ");
scanf("%s", str);
len = strlen(str);
permute(str, 0, len - 1);
printf("\nNumber of letters: %d\n", len);
while (cont < len)
{
fatorial = fatorial * cont;
cont++;
}
printf("\nPossibilities:%d", fatorial);
return 0;
}
You could increment a counter in permute. Something like:
#include <string.h>
#include <stdio.h>
void
swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void
permute(char *a, int i, int n, int *count)
{
int j;
if( i == n ){
printf("%s\n", a);
*count += 1;
} else {
for( j = i; j <= n; j += 1 ){
swap(a + i, a + j);
permute(a, i + 1, n, count);
swap((a + i), (a + j));
}
}
}
int
main(int argc, char **argv)
{
char buf[1024];
char *str = argc > 1 ? argv[1] : buf;
int len;
int contador = 0;
if( argc < 2 ){
printf("\nType a word: ");
scanf("%1023s", buf);
}
len = strlen(str);
permute(str, 0, len - 1, &contador);
printf("\nNumber of words: %d\n", contador);
return 0;
}
I'm trying to write an effective function that receives an array the size of n and a and b.
The function should search all the numbers in the array such that b-a < array[i] and collect them to a new sorted array called incoming.
For instance, for the input 11,12,8,15,3,12,3,12 , b=15, a=8 the output would be a 6 size array that will contain the values 8,11,12,12,12,15 (anything that is higher than (b)15-(a)8 ).
This is my own code attempt:
#include<stdio.h>
#include<stdlib.h>
int* f5(int arr[], int n, int a, int b, int* p)
{
int i,min,minIndex;
int* incoming = (int*)malloc(*p*sizeof(int));
for ( i = 0; i < n; i++)
{
if (arr[i]>(b - a))
{
incoming[i] = arr[i];
(*p)++;
}
}
return *incoming;
}
void main()
{
int arr[] = { 12,3,12,3,15,8,12,11 };
int p, i;
int incoming[] = f5(arr, sizeof(arr) / sizeof(arr[0]), 8,15, &p);
printf("The size is: %d and the new marahc is: ", p);
for (i = 0; i < p; i++) {
printf("%d", incoming[i]);
}
free(incoming);
}
So I corrected your f5 function because you were not allocating your new array properly, since you were doing the malloc with (*p) before it was set to the number of elements you needed to store. After that, I ordered the incoming array in ascending order as shown in the example output with qsort. I also added the part where you take input values from the user, since in the code you've posted you were probably just trying that specific test case.
#include<stdio.h>
#include<stdlib.h>
// compare function for qsort
int mycompare (const void* a, const void* b) {
int val1 = *(const int*)a;
int val2 = *(const int*)b;
return (val1 - val2);
}
int* f5(int arr[], int n, int a, int b, int* p) {
int target = b - a;
int i;
for (i=0;i<n;i++) { // storing number of values > (a-b)
if (arr[i]>target) (*p)+=1;
}
int* incoming;
if ((incoming= malloc((*p)*sizeof(int)))==NULL); // should always check malloc errors
{
perror("malloc");
exit(EXIT_FAILURE);
}
int j = 0;
i = 0;
while (i<n && j<(*p)) { // storing values in new array
if (arr[i]>target) {
incoming[j] = arr[i];
j++;
}
i++;
}
return incoming;
}
void main() {
int n, a, b, i;
printf("Insert 'a' value: ");
scanf("%d", &a);
printf("Insert 'b' value: ");
scanf("%d", &b);
printf("Insert array size: ");
scanf("%d", &n);
int arr[n];
printf("Insert array values: \n");
for (i=0;i<n;i++) {
scanf("%d", &arr[i]);
}
int size = 0;
int *incoming = f5(arr, n, a, b, &size);
qsort(incoming, size, sizeof(int), mycompare); // sorting array
printf("The size is: %d and the new marahc is: ", size);
for (i=0; i<size;i++) {
printf("%d ", incoming[i]);
}
free(incoming);
}
In f5 you need to determine the required length to allocate. You could either do an initial pass of the arr[] contents to determine the required length, or just set the length to the upper bound n and resize it once the actual length is known.
First approach using an initial pass:
int* f5(int arr[], int n, int a, int b, int* p)
{
int i;
int j;
int* incoming;
j = 0;
for ( i = 0; i < n; i++)
{
if (arr[i]>(b - a))
{
j++;
}
}
incoming = malloc(j*sizeof(int));
if (incoming == NULL)
{
return NULL;
}
j = 0;
for ( i = 0; i < n; i++)
{
if (arr[i]>(b - a))
{
incoming[j++] = arr[i];
}
}
*p = j;
return incoming;
}
Second approach using upper bound for allocated size:
int* f5(int arr[], int n, int a, int b, int* p)
{
int i;
int j;
int* incoming = malloc(n*sizeof(int));
if (incoming == NULL)
{
return NULL;
}
j = 0;
for ( i = 0; i < n; i++)
{
if (arr[i]>(b - a))
{
incoming[j++] = arr[i];
}
}
int* resized_incoming = realloc(incoming, j*sizeof(int));
if (resized_incoming != NULL)
{
incoming = resized_incoming;
}
*p = j;
return incoming;
}
The above have been written to return NULL if malloc returns NULL.
A third approach would be to start with a small amount for incoming and reallocate to a larger amount when necessary.
In main, the variable incoming needs to be changed from an array type to a pointer:
int* incoming = f5(arr, sizeof(arr) / sizeof(arr[0]), 8,15, &p);
Since f5 can now return NULL, the return value should be checked and appropriate action taken:
if (incoming == NULL)
{
fprintf(stderr, "Failed to allocate memory!\n");
exit(EXIT_FAILURE);
}
You also need to add code to sort the filtered numbers.
I have a pointer to a pointer ("paths") and I want to reallocate each pointer (each "path"). But I get a crash. Generally I am trying to find all possible powers of a number, which one can compute for some amount of operations (e.g for two operations we can get power of three and four (one operation for square of a number, then another one either for power of three or four)). I figured out how to do it on paper, now I am trying to implement it in code. Here is my try:
#include <stdio.h>
#include <stdlib.h>
void print_path(const int *path, int path_length);
int main(void)
{
fputs("Enter number of operations? ", stdout);
int operations;
scanf("%i", &operations);
int **paths, *path, npaths, npath;
npaths = npath = 2;
path = (int*)malloc(npath * sizeof(int));
paths = (int**)malloc(npaths * sizeof(path));
int i;
for (i = 0; i < npaths; ++i) // paths initialization
{
int j;
for (j = 0; j < npath; ++j)
paths[i][j] = j+1;
}
for (i = 0; i < npaths; ++i) // prints the paths, all of them are displayed correctly
print_path(paths[i], npath);
for (i = 1; i < operations; ++i)
{
int j;
for (j = 0; j < npaths; ++j) // here I am trying to do it
{
puts("trying to reallocate");
int *ptemp = (int*)realloc(paths[j], (npath + 1) * sizeof(int));
puts("reallocated"); // tried to write paths[j] = (int*)realloc...
paths[j] = ptemp; // then tried to make it with temp pointer
}
puts("memory reallocated");
++npath;
npaths *= npath; // not sure about the end of the loop
paths = (int**)realloc(paths, npaths * sizeof(path));
for (j = 0; j < npaths; ++j)
paths[j][npath-1] = paths[j][npath-2] + paths[j][j];
for (j = 0; j < npaths; ++j)
print_path(paths[j], npath);
puts("\n");
}
int c;
puts("Enter e to continue");
while ((c = getchar()) != 'e');
return 0;
}
void print_path(const int *p, int pl)
{
int i;
for (i = 0; i < pl; ++i)
printf(" A^%i -> ", p[i]);
puts(" over");
}
I am not sure the problem resides with the call to realloc(), rather you are attempting to write to locations for which you have not created space...
Although you create memory for the pointers, no space is created (allocate memory) for the actual storage locations.
Here is an example of a function to allocate memory for a 2D array of int:
int ** Create2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int cols)
{
int i;
for(i=0;i<cols; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
Use example:
#include <ansi_c.h>
int main(void)
{
int **array=0, i, j;
array = Create2D(array, 5, 4);
for(i=0;i<5;i++)
for(j=0;j<4;j++)
array[i][j]=i*j; //example values for illustration
free2DInt(array, 5);
return 0;
}
Another point here is that it is rarely a good idea to cast the return of [m][c][re]alloc() functions
EDIT
This illustration shows my run of your code, just as you have presented it:
At the time of error, i==0 & j==0. The pointer at location paths[0][0] is uninitialized.
EDIT 2
To reallocate a 2 dimension array of int, you could use something like:
int ** Realloc2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = realloc(arr, space*sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
And here is a test function demonstrating how it works:
#include <stdio.h>
#include <stdlib.h>
int ** Create2D(int **arr, int cols, int rows);
void free2DInt(int **arr, int cols);
int ** Realloc2D(int **arr, int cols, int rows);
int main(void)
{
int **paths = {0};
int i, j;
int col = 5;
int row = 8;
paths = Create2D(paths, col, row);
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
//reallocation:
col = 20;
row = 25;
paths = Realloc2D(paths, col, row);
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
free2DInt(paths, col);
getchar();
return 0;
}
The realloc() does not fail. What fails is that you haven't allocated memory for the new pointers between paths[previous_npaths] and paths[new_npaths-1], before writing to these arrays in the loop for (j = 0; j < npaths; ++j).
This prog is to accept an array of chars n compress them....(aaaabbbcc-->a4b3c2)....my prog is showing error at the point where im equating the addr of the 2d array to 1d array. This is my code:
/* size1 defined as 5 and size2 as 10.... (consts)*/
void compress(char data[SIZE1][SIZE2]);
int main()
{
char data[SIZE1][SIZE2];
printf("Enter a 5x10 matrix of characters:\n");
scanf("%c", &data);
compress(data[SIZE1][SIZE2]);
_getch();
return 0;
}
void compress(char data[SIZE1][SIZE2])
{
int hold[SIZE1*SIZE2];
int cnt = 0;
hold[SIZE1*SIZE2] = data[SIZE1][SIZE2];
for (int i = 0; i < (SIZE1*SIZE2); i++)
{
if (hold[i] == hold[i + 1])
{
cnt++;
continue;
}
else
{
printf("%c%d", hold[i], cnt);
}
}
}
This didn't work so I tried to use pointers:
void compress(char data[SIZE1][SIZE2])
{
int *hold[SIZE1*SIZE2];
int cnt = 0;
hold = data[SIZE1][SIZE2];
for (int i = 0; i < (SIZE1*SIZE2); i++)
{
if (*(hold+i) == *(hold+i+1))
{
cnt++;
}
else
{
printf("%c%d", *(hold+i), cnt);
}
}
}
I thought that the addrs of 2d arrays are stored linearly, hence they can be directly =to that of 1d.But the error says "'=':left operand must be an l-value".Im very new to pointers.Any help or corrections ....pls?
#include <stdio.h>
#define SIZE1 3
#define SIZE2 3
void compress(char data[SIZE1][SIZE2]);
int main(){
char data[SIZE1][SIZE2];
printf("Enter a %dx%d matrix of characters:\n", SIZE1, SIZE2);
for(int i=0;i<SIZE1;++i)
for(int j=0;j<SIZE2;++j)
scanf("%c", &data[i][j]);//aaaabbbcc
compress(data);
(void)getchar();
return 0;
}
void compress(char data[SIZE1][SIZE2]){
char *hold = &data[0][0];
int cnt = 1, size = SIZE1*SIZE2;
for (int i = 0; i < size; i++){
if (i < size -1 && hold[i] == hold[i + 1]){
cnt++;
//continue;
} else {
printf("%c%d", hold[i], cnt);//a4b3c2
cnt = 1;
}
}
}