Hi everyone! I'm a second-year computer science student diving into C programming, and I'm looking for some constructive feedback on my code. The goal of my program is simple: it takes an integer input for the size of an array and then accepts values to fill that array. The twist is that it needs to sort the array based on the order of input, placing negative values on the left and positive values on the right. For instance, if the input is [-9, 20, 1, -2, -3, 15], the output should be [-9, -2, -3, 20, 1, 15]. I also have a restriction: I can only use one array throughout the program.
Here's the code I wrote:
```c
#include
#include
int main(void)
{
int size;
while (true)
{
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 0 && size < 100) break;
}
int array[size], value, positive = 0;
for (int i = 0; i = 0)
{
for (int j = positive; j >= 0; j--)
{
if (j == 0)
{
array[size - 1] = value;
positive++;
}
else
{
array[size - 1 - j] = array[size - 1 - j + 1];
}
}
}
else
{
array[i - positive] = value;
}
}
printf("n[");
for (int i = 0; i < size - 1; i++)
{
printf("%d, ", array[i]);
}
printf("%d]", array[size - 1]);
return EXIT_SUCCESS;
}
```
It's my first month learning C, so I would really appreciate any insights or suggestions. Thanks in advance for taking the time to help me out!
4 Answers
I see you used a `while (true)` loop for getting the array size. You might want to consider this approach instead:
```c
do {
// ...
} while(size 100);
```
This makes it clearer what the loop condition is just at a glance, which is a nice improvement for readability!
Hey! I checked out your code and it looks pretty good for a beginner! One suggestion I have is to break out the sorting logic into separate functions. It helps make the main function cleaner and your code more organized. In Python, you'd probably do this by default, but C can be a bit tricky with functions, so take your time learning that.
Thanks for that! I definitely agree; when I write in Python, I always use functions. I'll focus on learning how to implement them in C as well.
Instead of using variable-length arrays, why not allocate a fixed-size array? For example:
```c
int array[100] = {0};
```
You can safely handle array access because any attempt to go beyond size 100 will be a clear error. It simplifies your logic a bit! Also, splitting your input and sorting into functions would help a lot with maintainability—consider writing a function that sorts the array after all input is provided.
Good point! I’ll keep that in mind for better memory management. I plan to refactor my code to use functions once I get the hang of them.
Just curious, do you need the array itself to be sorted, or is it more about the output? If it’s just the output, you could use two positions for insertion, one for negatives and another for positives. This way, you avoid unnecessary sorting while still maintaining the correct order in your final output!
Actually, the exercise requires sorting the array itself, not just printing it in order. But thanks for the suggestion! I'll think about ways to simplify my logic moving forward.

That's much cleaner, thanks! I'll definitely use that next time.