0

Arrays in C

Hey, I've been learning to program in C, I've been doing some exercises on arrays and there is one that is getting difficult for me, the exercise says: Make a program that given an array A of N elements, which are integers in increasing order, obtain an array B such that its elements are the number of repetitions of the first array. And the input and output would be something like this: Input: A[1, 4, 4, 4, 5, 5, 7] Output: B[1, 3, 2, 1] I would appreciate your valuable help, because this problem is giving me headaches haha. I have tried, but the output appears correct only for the first element of array A

28th Oct 2021, 4:48 AM
Alexander Álvarez
Alexander Álvarez - avatar
1 Resposta
+ 1
Can you show us what you've tried so far, so we can find out why it doesn't work? Anyway, a possible solution (if you think it can help you out) is: /* setup code */ for(i=0; i<n; ++i) // for every A { ++B[j]; // increment B if(A[i] != A[i+1]) ++j; // but when A changes, go to the next B }
28th Oct 2021, 5:48 AM
Angelo
Angelo - avatar