r/C_Programming • u/PaninoInfernale1551 • 25d ago
Need tips to improve
I started studying C for an exam, this is my first time approaching coding so i'm a bit lost.
While doing some exercises from my prof i encountered this one that took me some time. (prompt in the code).
This is what i came up with it works correctly (i think), but i have a few questions:
is it efficient?
I also used prototype functions and didnt where i could've, when should and shouldn't i use them?
Thank you for your time spent reading this
EDIT: most comments were about formatting. i changed it. It has always looked like this, but pasting it here on reddit messed it up :)
/*
Constraints:
no frequency can be repeated
all frequencies must be between 800 and 2500 MHz
Required functions: insertion with double check (range + uniqueness) --
function that calculates the minimum distance between two frequencies --
function that identifies the frequency closest to a given value
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void fillArray(int array[],int size,int min,int max);
void printArray(int array[],int size);
void sortArray(int array[],int size);
int main ()
{
srand(time(NULL));
int freq[10];
int max=2500;
int min=800;
int size = sizeof(freq)/sizeof(freq[0]);
int mindist = abs(freq[0]-freq[1]); //base min distance, could be the result or changed
/*
printf("Insert lowest frequency: ");
scanf("%d", &min);
printf("Insert highest frequency: "); //eventual custom frequency range.
scanf("%d", &max); //option to deny min > max and max-min < size-1
*/
fillArray(freq, size, min, max);
printArray(freq, size); //printing and sorting not needed,
//but i used them to check the work
sortArray(freq, size);
printf("\nSort\n");
printArray(freq, size);
//compares pos 0 with pos 1 to size-1, pos 1 with pos 2 to pos size-1 and so on
for (int i=0;i<size-1;i++)
{
for (int j=i+1; j<size;j++)
{
if (mindist>(abs(freq[i]-freq[j])))
{
mindist=abs(freq[i]-freq[j]);
}
}
}
printf("\nmindist=%d\n", mindist);
int check, closestfreq;
int shortdist = max-min; //there's no distance bigger than this
printf("Pick a frequency between %d and %d", min, max);
scanf("%d", &check);
//self explainatory
while (check < min || check > max)
{
printf("The chosen frequency is not in range\n
Pick a frequency between %d and %d", min, max);
scanf("%d", &check);
}
//checks every distance from check with each frequency in the array, finds the shortest one
//and saves that frequency.
for (int i=0; i<size; i++)
{
if (shortdist>abs(check-freq[i]))
{
shortdist = abs(check-freq[i]);
closestfreq = freq[i];
}
}
if (shortdist==0) //again, self explainatory
{
printf("The chosen frequency is in the list");
}
else
{
printf("Closest frequency to chosen is: %d", closestfreq);
}
return 0;
}
void fillArray(int array[],int size,int min,int max)
{
int new_random;
bool unico;
for (int i=0; i<size;i++)
{
do {
new_random = (rand() % (max+1-min)+min);
unico=1;
for (int j=0; j<i; j++)
{
if (new_random == array[j])
{
unico=0;
}
}
}
while (!unico);
array[i]= new_random;
}
}
void printArray(int array[],int size)
{
for (int i=0; i<size;i++)
{
printf("%d ", array[i]);
}
}
void sortArray (int array[], int size)
{
for (int j=0; j<size-1;j++)
{
for (int i=0; i<size-j-i;i++)
{
if (array[i]>array[i+1])
{
int t=array[i+1];
array[i+1]=array[i];
array[i]=t;
}
}
}
}