On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote:
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote:
Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D programmers care in horizontal thinking style. Is there any additions I can make in the code to make it suitable with D style or it's fine?


Oh man! some of those answers... whoooooohah....

Here's mine, with a little of that horizontal thinking ;-)

....

And just had a little fun transposing my D code back into C.

They both produce exactly the same output.

But I tell ya.. the cognitive load .. well.. it increased dramatically ;-)

increased cognitive load bad!

// ==========================================

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

void displayResults(const char answer1, const char answer2);

int main(void)
{
    char answer1, answer2;

    while (true)
    {
printf("Would you like in list (n=negatives, p=positives, b=both)? ");
        scanf_s(" %c", &answer1, 1);
        if (answer1 == 'n' || answer1 == 'p' || answer1 == 'b')
        {
            break;
        }
        printf("%c is not a valid answer. Try again.\n", answer1);
    }

    while (true)
    {
printf("Would you like in list (e=evens, o=odds, b=both)? ");
        scanf_s(" %c", &answer2, 1);
        if (answer2 == 'e' || answer2 == 'o' || answer2 == 'b')
        {
            break;
        }
        printf("%c is not a valid answer. Try again.\n", answer2);
    }

    displayResults(answer1, answer2);

    putchar('\n');
    return EXIT_SUCCESS;
}

void displayResults(const char answer1, const char answer2)
{
// NOTE: 0 is neither positive or negative, but is considered to be even

int numbers[11] = {-3, 14, 47, -49, -30, 15, 4, -82, 99, 26, 0};

    // an array to store results from answer1
    int result1[sizeof(numbers) / sizeof(numbers[0])];

// the final array that consolidates the results from answer1 and answer2 // must be at least this size in case it stores all elements from numbers.
    int finalResult[sizeof(numbers) / sizeof(numbers[0])];

int result1Count = 0; // to keep count of number of elements aquired in answer1 int result2Count = 0; // to keep count of number of elements aquired in answer2

    // my version of a string builder ;-)
const int msgSize = 50; // should be large enough to hold the entire message.
    char *const resultMsg = (char *)malloc(msgSize);
    if (resultMsg == NULL)
    {
        exit(EXIT_FAILURE); // not enough memory!! really??!!
    }
    strcpy_s(resultMsg, 15, "\nHere are all ");

    switch (answer1)
    {
        case 'n':
for (size_t i = 0; i < sizeof(numbers) / sizeof(int); i++)
            {
                if (numbers[i] < 0)
                    result1[result1Count++] = numbers[i];
            }
            memcpy((char *)resultMsg + 14, "[negative] \0", 12);
            break;
        case 'p':
for (size_t i = 0; i < sizeof(numbers) / sizeof(int); i++)
            {
                if (numbers[i] > 0)
                    result1[result1Count++] = numbers[i];
            }
            memcpy((char *)resultMsg + 14, "[positive] \0", 12);
            break;
        case 'b':
for (size_t i = 0; i < sizeof(numbers) / sizeof(int); i++)
            {
                result1[result1Count++] = numbers[i];
            }
            break;
        default:
            // how could it ever get here??
printf("Bad input!!\n%c is not a valid answer.", answer1);
            assert(0); // don't waste my time!
    }

    // debug
    /*
    {
printf("\n[ result1Count after processing answer1 is: %d ]\n ", result1Count);
        for (int i = 0; i < result1Count; i++)
        {
            printf("%d ", result1[i]);
        }
        putchar('\n');
    }
    */

    switch (answer2)
    {
        case 'e':
            for (int i = 0; i < result1Count; i++)
            {
                if ((result1[i] % 2) == 0)
                    finalResult[result2Count++] = result1[i];
            }

            if (answer1 == 'b')
            {
                memcpy((char *)resultMsg + 14, "[even]\0", 7);
            }
            else
            {
                memcpy((char *)resultMsg + 25, "[even]\0", 7);
            }

            break;
        case 'o':
            for (int i = 0; i < result1Count; i++)
            {
                if ((result1[i] % 2) != 0)
                    finalResult[result2Count++] = result1[i];
            }

            if (answer1 == 'b')
            {
                memcpy((char *)resultMsg + 14, "[odd]\0", 6);
            }
            else
            {
                memcpy((char *)resultMsg + 25, "[odd]\0", 6);
            }

            break;
        case 'b':
            for (int i = 0; i < result1Count; i++)
            {
                finalResult[result2Count++] = result1[i];
            }
            break;
        default:
            // how could it ever get here??
printf("Bad input!!\n%c is not a valid answer.", answer2);
            assert(0); // don't waste my time!
    }

    // debug
    /*
    {
printf("\n[ result2Count after processing answer2 is: %d ]\n ", result2Count);
        for (int i = 0; i < result2Count; i++)
        {
            printf("%d ", finalResult[i]);
        }
        putchar('\n');
    }
    */

// sort the part of the array that we're interested in, in ascending order.
    int t;
    for (int i = 0; i < result2Count; i++)
    {
        for (int j = i + 1; j < result2Count; j++)
        {
            if (finalResult[i] > finalResult[j])
            {
                t = finalResult[i];
                finalResult[i] = finalResult[j];
                finalResult[j] = t;
            }
        }
    }

    printf("%s numbers:\n", resultMsg);

    // print all the elements of the array
    for (int i = 0; i < result2Count; i++)
    {
        printf("%d ", finalResult[i]);
    }

    free(resultMsg);
}


// =====================================================


Reply via email to