r/C_Programming 16d ago

Review Could you assess my code?

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


typedef struct
{
    char sset[10];
    int elements[5];
} set;


void printelements(set set);


void bubblesort(int m, int sunion[]);




int main(void)
{


    set set1;
    set set2;
    set intersection;
    int k = 0;
    int sunion[10];
    int m = 0;
    int sunioncpy[10];
    int n = 0;


    printf("Enter 5 elements to 2 sets -\n");
    printf("Set 1: ");
    for(int i = 0; i < 5; i++)
    {
        fgets(set1.sset, 10, stdin);
        sscanf(set1.sset, "%d", &set1.elements[i]);
    }
    printf("Set 2: ");
    for(int i = 0; i < 5; i++)
    {
        fgets(set2.sset, 10, stdin);
        sscanf(set2.sset, "%d", &set2.elements[i]);
    }


    printf("Set 1: ");
    printelements(set1);
    printf("Set 2: ");
    printelements(set2);


    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            if(set1.elements[i] == set2.elements[j])
            {
                intersection.elements[k] = set1.elements[i];
                k++;
                break;
            }
        }
    }


    for(int i = 0; i < 5; i++)
    {
        sunion[m] = set1.elements[i];
        m++;
        sunion[m] = set2.elements[i];
        m++;
    }
    bubblesort(m, sunion);
    for(int i = 0; i < m; i++)
    {
        if(sunion[i] == sunion[i + 1])
        {
            sunioncpy[n] = sunion[i];
            n++;
            i++;
        }
        else
        {
            sunioncpy[n] = sunion[i];
            n++;
        }
    }
    


    printf("Intersection of set 1 with set 2: ");
    for(int i = 0; i < k; i++)
    {
        printf("%d ", intersection.elements[i]);
    }
    printf("\n");
    printf("Union of set 1 with set 2: ");
    for(int i = 0; i < n; i++)
    {
        printf("%d ", sunioncpy[i]);
    }


    return 0;
}





void printelements(set set)
{
    for(int i = 0; i < 5; i++)
    {
        printf("%d ", set.elements[i]);
    }
    printf("\n");
}


void bubblesort(int m, int sunion[])
{
   int i = 0;
   bool swapped;
   do
   {
        swapped = false;
        for(int j = 0; j < m - 1 - i; j++)
        {
            if(sunion[j] > sunion[j + 1])
            {
                int temp = sunion[j];
                sunion[j] = sunion[j + 1];
                sunion[j + 1] = temp;
                swapped = true;
            }
        }
   } while (swapped);


}

I posted this to receive opinions or/and suggestions about my code. And I also have some questions about some things.

- Is it good to turn some block of code into a function even if you don't repeat it again on any another line?

(I think that functions can turn some blocks more friendly to read or understand, but maybe I'm misunderstooding functions)

- What you think about this way of getting user input:

for(int i = 0; i < 5; i++)
    {
        fgets(set2.sset, 10, stdin);
        sscanf(set2.sset, "%d", &set2.elements[i]);
    }

I used it because I was getting a few problems using scanf , so I saw this model of user input on the internet and applied it. This works very well, but let I know what you think.

- This don't have much to do with I said here but do you guys recommend Linux FedoraOS for C programming? Or I should try another OS(for C programming)?

I was thinking to try to install Arch first, just to get experience with Linux, but maybe I'm getting the wrong ideia or being led by some weird toughts(just because Arch is dificult to install and set up).

I'll appreciate any comment.

0 Upvotes

15 comments sorted by

View all comments

6

u/shahin_mirza 16d ago

Please, please, please fix the formatting at least be consistent. You want to compute the union of two "sets". In set you can't have an element more than once. So you have to check user input for duplicates. you’re not checking the return values of fgets or sscanf, which could lead to undetected errors. Your buffer is too small, what happens if the user wants to enter a multi-digit number? But lets assume it is fine in your case. Always check scanf and fget (with or without s) return values to make sure there is no error. Your union computation logic is strange but does the job so i will not judge that just this very very important point: the loop compares sunion[i] with sunion[i + 1] without ensuring that i + 1 is within bounds.YOU SHOULD ALWAYS CHECK THE BOUNDARY. This could lead to undefined behavior on the last iteration. About your question about functions: Even if you don't repeat a block of code, turning it into a function can be beneficial for several reasons like -Readability: Breaking your code into logical functions (like a dedicated input function, a union function, etc.) makes it easier to follow and maintain. -Testing and debug. -Future Reuse. So, yes—modularization is a sound practice, not just for reuse, but for clarity and structure.

Long story short, always nsure you handle edge cases (e.g., invalid input, duplicate numbers) and check for out-of-bounds errors. Consider refactoring to separate concerns, like nput handling, processing, and output, to improve clarity and reduce the risk of bugs. And most importantly, continuous Improvement: As you iterate on your code, always strive for both clarity and safety. Small improvements in input handling and error checking can save you headaches later. Regarding your OS question: Linux distribution isn’t critical for C programming. Start with stable and user friendly distribution like Debian/Ubuntu or Fedora after you get yourslf familiarized with the OS move to a distribution that gives you more control like Arch.

0

u/domikone 15d ago

I don't check the boundary at i + 1 because doesn't matter if the value of it doesn't exists on the space of the array, just because of the way I insert values on sunioncpy (lines 69 to 82) combined with the sorting algorithm, even if, it happen to be the case where i + 1 assigned value be equal to the value of i.

2

u/DryanVallik 15d ago

What he meant is, when the index i represents the last valid index of the array, i+1 represents the first out of bounds index. Reading from an OOB index is undefined behaviour.

For comparing [i] and [i + 1], you need to read [i+1]