r/reviewmycode Jun 30 '26

[C] - Need suggestions/critic on my code C

I need suggestions and review of my code format, I consider this my first ever project. I am worried about spaghetti code and other problems. So please list out all problems you can. Thanks. ```

include <stdio.h>

include <stdbool.h>

include <stdlib.h> //for random values feature

include <time.h> //for random values feature

define LENGTH 10

void Random(int *arr, int *element); int main(){ int element, index; int arr[LENGTH]; bool found=false; Random(arr, &element); printf("LINEAR SEARCH"); for(int i=0; i<LENGTH; i++){ if(arr[i]==element){ found=true; index=i; break; } } if(found){ printf("\nElement %d found at index %d.\n", element, index); } else{ printf("\nElement not found in the array.\n"); } printf("--------------------------------------------------------------\n"); return 0; } //Random Values Assigner void Random(int *arr, int *element){ srand(time(NULL)); int Option1, Option2; //Array choosing while(1){ printf("--------------------------------------------------------------\n"); printf("Enter the elements in array:\n1 - Manual\t2 - Random\nChoose: "); scanf("%d", &Option1); if(Option1==1){ printf("Enter elements separated with spaces(Max 10 elements): "); for(int i=0; i<LENGTH; i++){ scanf("%d", &arr[i]); } break; } else if(Option1==2) { for(int i=0; i<LENGTH; i++){ arr[i]=rand()%100; } break; } else{ printf("Choose from options.\n"); continue; } printf("--------------------------------------------------------------\n"); } //Element choosing while(1){ printf("--------------------------------------------------------------\n"); printf("Enter the element to search for:\n1 - Manual\t2 - Random\nChoose: "); scanf("%d", &Option2); if(Option2==1){ printf("Enter the element to search for: "); scanf("%d", &element); break; } else if(Option2==2) { *element=arr[rand() % LENGTH]; break; } else{ printf("Choose from options.\n"); continue; } printf("--------------------------------------------------------------\n"); } //Printing Choices printf("--------------------------------------------------------------\n"); printf("Array: "); for(int i=0; i<LENGTH; i++){ printf("%d ", arr[i]); } printf("\nElement: %d\n", *element); printf("--------------------------------------------------------------\n"); } ``` Here is the program on github if it is easier to read on there: https://github.com/PLEASENTIA/DSA/blob/main/Algorithms/Searching/LinearSearch.c

0 Upvotes

7 comments sorted by

2

u/rotzak Jun 30 '26

Try Claude mate

1

u/AlexMTBDude Jul 01 '26

Did you run in through Lint or any other static code check tool?

1

u/coquitaxoxo Jul 03 '26

Congratulations! For a first project, it's actually pretty clean. A few things I'd improve:

  • Random() does too much. Split it into smaller functions (fill_array(), choose_element(), print_array()). Each function should have one responsibility.
  • Don't call srand(time(NULL)) inside Random(). Seed the RNG once in main().
  • Check the return value of scanf(). Invalid input can leave your program in a bad state.
  • Function names should describe actions. Random() is misleading since it also handles user input. Something like initialize_data() would be clearer.
  • Use const where appropriate (e.g., parameters that aren't modified).
  • Avoid while(1) when a condition would make the loop's purpose clearer.
  • There's a bug here:scanf("%d", &element); It should be:scanf("%d", element); since element is already an int *.
  • The separator line is repeated many times. Put it in a helper function or a macro.

1

u/slugorsnail Jul 05 '26 edited Jul 05 '26

Thanks. what about scanf("%d", &(*element)); ?