-2

I should make new array out of existing one (ex. 1 0 4 5 4 3 1) so that the new one contains digits already in existing array and the number of their appearances. So, the new one would look like this: 1 2 0 1 4 2 5 1 3 1 (1 appears 2 times, 0 appears 1 time.... 3 appears 1 time; the order in which they appear in first array should be kept in new one also); I know how to count no. of times a value appears in an array, but how do I insert the no.of appearances? (C language)

 #include <stdio.h>
    #define max 100
    int main() {


int b, n, s, i, a[max], j, k;

printf("Enter the number of array elements:\n");
scanf("%d", &n);

if ((n > max) || (n <= 0)) exit();

printf("Enter the array:\n");
for (i = 0; i < n; i++)
    scanf("%d", a[i]);

for (i = 0; i < n; i++) {
    for (j = i + 1; j < n;) {
            if (a[j] == a[i]) {
                for (k = j; k < n; k++) {
                    a[k] = a[k + 1];
}}}}

    //in the last 5 rows i've tried to compare elements, and if they are same, to increment the counter, and I've stopped here since I realised I don't know how to do that for every digit/integer that appears in array//
Tanya
  • 9
  • 1
  • 3
    Since you haven't shown us your progress, I'll start from the top. Turn on the PC by pressing the power button... – DeiDei May 28 '17 at 10:32
  • Let's see. The "new array" needs to be twice the length of the original. Every odd element will hold a value equal to an element of the original. Every even element will contain the number of times the value of element before it appears in the original array. Producing an algorithm from that description should be trivial. But there are many ways of doing it. – Peter May 28 '17 at 10:56
  • @InternetAussie I tried to use loops for reading the array and adding an counter for each different digit. It wasn't really successful that's why I haven't posted it :\ – Tanya May 28 '17 at 11:12
  • @someprogrammerdude well, it doesn't have to have as much elements, because if you have an array 1 2 1 1 2 1 3 the output array should be 1 4 2 2 3 1 which isn't x*2; Take a look at the example I've given above, I haven't written the number of 1's after each appearance, but only after first. – Tanya May 28 '17 at 11:17
  • @peter yes but only problem is that I won't write the number of appearances after every appearance of digit, only after first, like in my example given above. – Tanya May 28 '17 at 11:19
  • @Tanya - that requires only a minor adjustment. – Peter May 28 '17 at 13:41

2 Answers2

0

If you know that the existing array consists of digits between 0 and 9, then you can use the index of the array to indicate the value that you are incrementing.

int in[12]  = {1,5,2,5,6,5,3,2,1,5,6,3};
int out[10] = {0,0,0,0,0,0,0,0,0,0};

for (int i = 0; i < 12; ++i)
{
  ++out[ in[i] ];
}
Stewart
  • 4,356
  • 2
  • 27
  • 59
  • There can be integers, not only 0-9 digits, and the array can have up to 100 numbers. Also, the user has to enter the array, so basically we have n (n>0, n<100) elements of array, but have to access them through a loop. :| – Tanya May 28 '17 at 11:11
  • In that case what you are looking for is a "map". That's a set of key-value pairs where the key is the integer, and the value is the frequency of that integer. – Stewart May 28 '17 at 11:14
  • 1
    I think that's exactly what I need! – Tanya May 28 '17 at 11:39
0

If you provide any code snippet, its easy for the community to help you.
Try this, even you optimize the no.of loops :)

#include <stdio.h>

void func(int in[], int in_length, int *out[], int *out_length) {        
     int temp[10] = {0}, i = 0, j = 0, value;
     //scan the input
     for(i=0; i< in_length; ++i) {
         value = in[i];
         if(value >= 0 && value <= 9) { //hope all the values are single digits
            temp[value]++;
         }
     }

     // Find no.of unique digits
     int unique_digits = 0;
     for(i = 0; i < 10; ++i) {
         if(temp[i] > 0)
           unique_digits++;
     }
     // Allocate memory for output
     *out_length = 2 * unique_digits ;
     printf("digits: %d out_length: %d \n",unique_digits, *out_length );

     *out = malloc(2 * unique_digits * sizeof(int));
     //Fill the output
     for(i = 0, j = 0; i<in_length && j < *out_length; ++i) {
        //printf("\n i:%d, j:%d val:%d  cout:%d ", i, j, in[i], temp[in[i]] );
        if(temp[in[i]] > 0 ) {
          (*out)[j] = in[i];
          (*out)[j+1] = temp[in[i]];
           temp[in[i]] = 0; //Reset the occurrences of this digit, as we already pushed this digit into output
           j += 2;
        }
     }
}

int main(void) {
    int input[100] = {1, 0, 4, 5, 4, 3, 1}; 
    int *output = NULL, output_length = 0, i = 0;

    func(input, 7, &output, &output_length );

    for(i=0; i < output_length; i+=2) {
       printf("\n %d : %d ", output[i], output[i+1]);
    }
    return 0;
}
Ajay
  • 2,483
  • 2
  • 16
  • 27
  • Hmmm you provided the code now :'(, First try this if it works, then add your input prompting here instead of my fixed input array. – Ajay May 28 '17 at 11:30
  • Alright, I will (Yes, because I haven't seen your answer before posting, sorry) – Tanya May 28 '17 at 11:32