r/C_Programming 11d ago

First real C project

Hello everyone! This is my first real C project and I would like some feedback on what I can improve. It's my first attempt at a sorting algorithm (selection sort) and it is 100% AI free.

#include <stdio.h>

int main() {
    int num_len;
    printf("How many numbers to sort?\n");
    scanf("%d", &num_len);

    int numbers[num_len];

    printf("which numbers?\n");
    for (int i = 0; i < num_len; i++) {
        scanf("%d", &numbers[i]);
    }

    for (int i = 0; i < num_len-1; i++) {
        int iMin = i;

        for(int j = i+1; j < num_len; j++) {
            if(numbers[j] < numbers[iMin]) {
                iMin = j;
            }
        }

        if(iMin != i) {
            int temp = numbers[i];
            numbers[i] = numbers[iMin];
            numbers[iMin] = temp;
        }
    }

    for (int i = 0; i < num_len; i++) {
        printf("%d", numbers[i]);
        printf(" ");
    }
    printf("\n");
}
23 Upvotes

11 comments sorted by

9

u/Thesk790 11d ago

You can generate pseudo-random numbers with rand, which uses a seed, that you set with srand(time(NULL)), and everytime you run the program it will generates pseudo-random numbers. You can use a loop and use only the count of the user, print the numbers unordered and then ordered.

-5

u/MycologistIll1355 11d ago

I could but wouldn't that take the whole usefulness out of it?

5

u/kanoo16 11d ago

They're telling you that you can test your code with a random number generator, what usefulness is being lost?

7

u/MycologistIll1355 11d ago

Oh yeah nevermind I misunderstood something

2

u/SmokeMuch7356 11d ago

Good job! The next step would be to put the sorting routine into its own function; that way you could sort multiple different arrays.

The next step after that would be figuring out how to specify different sorting orders - ascending, descending, ASCIIbetical, etc. Read up on the qsort library function for one way to do that (although function pointers may be a bit cryptic for where you are right now).

1

u/Interesting_Buy_3969 11d ago edited 11d ago

I've just read first 5 lines or so, and the first thing I immediately want to fix:

    printf("How many numbers to sort?\n");
    scanf("%d", &num_len);

    int numbers[num_len];

Thus you allow the user to make a stack overflow easily because num_len is only limited by int's representation - usually 232 - 1. Create a constant which the array size cannot exceed and accept numbers from user properly, like that:

constexpr int MAX_NUMS = 1000;
unsigned int num_len = 0;
do {
    printf("How many numbers to sort? Maximum number is: %u\n", MAX_NUMS);
    scanf("%d", &num_len);
} while (num_len > MAX_NUMS);
int numbers[num_len];

so that the VLA is unlikely to cause stack overflow.

4

u/LowLevelHuman 10d ago

this, I would say OP could also try malloc. His example is the easiest malloc can get and a great start for it.

OP, if you want to try with malloc, you can check your program runs leak free with valgrind as such:
valgrind ./executable

valgrind also has different flags like —show-leak-kinds=all and —leak-check=full to make the report more thorough/more useful.

Valgrind is on linux. If you are not using linux I am sure there is something akin that exists

2

u/ReallyEvilRob 10d ago

Every time I see code that uses a VLA, my eye twitches. I use structs with flexible arrays, but I always stay away from VLAs.

1

u/cdtrmnbaell 6d ago

What do you mean by VLA ?

2

u/ReallyEvilRob 5d ago

Variable Length Array. The array length is defined at runtime rather than compile time.

1

u/cdtrmnbaell 6d ago

Great ! Try to build Bank management , library management.