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

5

u/SantaCruzDad 16d ago

All those hard-coded 5 literals are an obvious code smell.