If you still need suggestions I will be glad to help you, but don’t think that I am trying to be a SMART GUY!
Suggestion I - always use constant instead of numbers. In this case you can only make changes in one plays instead of looking in all program to change each number.
Suggestion II - use functions as much us possible, even if function will contain one line of code. And ALWAYS use names that describe what this function supposes to do.
> If you wish to mark some section of code that makes something specific, put it into function.
> If you repeat some code many times, put it into function.
> If you don’t know how to solve particular problem, transform it into function and back to this problem later.
> Try not to use variable in function declaration, only they types.
Suggestion III - Grope particular types of variable in one grope (int with int…). The best way to prevent “hair tear” problems, it’s to set all variables to 0 in declaration section. And ones more use names that mean something, and not just: a, b, c, d, kuku, kiki, hruku... even if this name will contain two and more words!
Suggestion IV - Try not repeat your code (for this you have functions). Because of this make right conditions that will “answer” on much as possible questions.
Suggestion V - If you wish to make space (interval) use Tab instead of space. And try not to leave “white” spaces without reason.
Suggestion VI - Don’t stick your code in one big sausage, make some space, as do Microsoft Word, Open Office, what ever…
And more important:
You should always annotate your code with lots of comments. This will make your life easier and longer
I think it’s what I can suggestions you for a future. About the program, I hope it will help you!
- # include <stdio.h>
-
-
# define ARRAY_LENGTH 10 // Suggestion I
-
-
// Suggestion II
-
void F_SortArrayContents (int [], char);
-
void F_SwapContentsOfTwoCells (int [], int);
-
-
void main ()
-
{
-
// Suggestion III
-
int index = 0;
-
int array [ARRAY_LENGTH] = {12, 45, 86, 2, 78, 11, 56, 10, 96, 25};
-
char sort_direction;
-
-
printf("> How do you want your array sorted\n");
-
printf ("> Pease enter A for ascending order or D for descending order: ");
-
scanf ("%c", &sort_direction);
-
F_SortArrayContents (array, sort_direction);
-
printf("> The new order of array, is: ");
-
for(; index < ARRAY_LENGTH; index++)
-
{
-
printf ("%3d ", array [index]);
-
}
-
}
-
-
void F_SortArrayContents (int array [], char direction)
-
{
-
int index_f = 0; // This variable moves forward in array "index_forword"
-
int index_b = ARRAY_LENGTH - 1; // This variable moves backward in array "index_backword"
-
-
// Suggestion IV
-
do
-
{ // Suggestion V
-
do
-
{
-
if ((array [index_f] > array [index_f + 1]) && (direction == 'a' || direction == 'A')) // Suggestion VI
-
{
-
F_SwapContentsOfTwoCells (array, index_f);
-
}
-
else if ((array [index_f] < array [index_f + 1]) && (direction == 'd' || direction == 'D'))
-
{
-
F_SwapContentsOfTwoCells (array, index_f);
-
}
-
}
-
while (++index_f < index_b);
-
index_f = 0;
-
}
-
while (--index_b > 0);
-
}
-
-
void F_SwapContentsOfTwoCells (int array [], int index)
-
{
-
int t_number = 0; // temporary_number
-
-
t_number = array [index];
-
array [index] = array [index + 1];
-
array [index + 1] = t_number;
-
}
-