Home > Software design >  How to take a consecutive numbers into a bracket?
How to take a consecutive numbers into a bracket?

Time:01-09

so I don't know how to make a number that is a consecutive number into a bracket. for example: 1 3 6 6 4 2 2 1 6. It should be: 1 3 (6 6) 4 (2 2) 1 6. Can someone help me? This is my code for now. Thank you so much!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int lower = 1, upper = 6, count = 20, num;
    srand(time(0));
    for (int i = 0; i < count; i  ) {
    num = (rand() % (upper - lower   1))   lower;
    printf("%d ", num);
    }
    return 0;
}

CodePudding user response:

int print(FILE *fo, const int *arr, const size_t size)
{
    int insequence = 0, nchars = 0;
    if(fo && arr && size)
    {
        for(size_t curr = 0; curr < size - 1; curr  )
        {
            if(arr[curr] == arr[curr   1])
            {
                if(!insequence)
                {
                    nchars  = fprintf(fo, "(");
                    insequence = 1;
                }
                nchars  = fprintf(fo, "%d ", arr[curr]);
            }
            else
            {
                if(insequence)
                {
                    nchars  = fprintf(fo, "%d) ", arr[curr]);
                    insequence = 0;
                }
                else
                {
                    nchars  = fprintf(fo, "%d ", arr[curr]);
                }
            }
        }
        nchars  = fprintf(fo, "%d%s", arr[size-1], insequence ? ")" : "");
    }
    return nchars;
}

int main(void)
{
    int arr[] = {1, 3, 6, 6, 6, 4, 2, 2, 1, 0, 0};

    print(stdout, arr, sizeof(arr)/sizeof(arr[0]));
}

https://godbolt.org/z/acGfj6oj4

And your case example:

int main(void)
{
    int lower = 1, upper = 6, num;
    size_t count = 20;
    int arr[count];
    
    srand((unsigned)time(NULL));
    for (size_t i = 0; i < count; i  ) {
        num = (rand() % (upper - lower   1))   lower;
        arr[i] = num;
    }

    print(stdout, arr, sizeof(arr)/sizeof(arr[0]));
}

https://godbolt.org/z/nbe7ehPbn

CodePudding user response:

You need to save all random value in an array and detect them if there same to the next one and different to the previous one to let the program know whether to print the bracket.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int lower = 1, upper = 6, count = 20, num;
    if (count == 0) return 0;  // if count = 0 then don't do anything
    srand(time(0));
    // if count = 1 just print the only random number
    if (count == 1) {
        printf("%d ", (rand() % (upper - lower   1))   lower);
        return 0;
    }
    int* arr = (int*) malloc(count * sizeof(int));
    for (int i = 0; i < count; i  ) {
        num = (rand() % (upper - lower   1))   lower;
        arr[i] = num;
    }

    

    // Not to test i != 0 every loop
    if (arr[0] == arr[1]) {
        printf("(%d ", arr[0]);
    }else {
        printf("%d ", arr[0]);
    }

    for (int i = 1; i < count - 1; i  ) {
        int a = arr[i - 1] == arr[i], b = arr[i] == arr[i   1];
        if (!a && b) {
            printf("(%d ", arr[i]);
            continue;
        }else if (a && !b) {
            printf("%d) ", arr[i]);
            continue;
        }
        printf("%d ", arr[i]);
    }

    // Not to test i != count every loop
    if (arr[count - 2] == arr[count - 1]) {
        printf("%d) ", arr[count - 1]);
    }else {
        printf("%d ", arr[count - 1]);
    }

    return 0;
}
  •  Tags:  
  • Related