It looks like you're trying to implement a Bubble Sort algorithm to sort an array of names in alphabetical order. However, there are a couple of issues in your code that are preventing it from working correctly. Let's go through them one by one and provide solutions.
1. Missing Braces in the if Statement
In C, an if statement without braces {} only applies to the first statement that follows it. In your nested loops, the if condition is intended to control the swapping of elements, but due to the missing braces, only the first strcpy is conditionally executed. The subsequent strcpy statements execute every time, regardless of whether the condition is true or not. This leads to incorrect swapping and ultimately an incorrect sort.
Incorrect Code:
if(strcmp(string[j-1], string[j]) > 0)
strcpy(dummy, string[j-1]);
strcpy(string[j-1], string[j]);
strcpy(string[j], dummy);
Corrected Code with Braces:
if(strcmp(string[j-1], string[j]) > 0) {
strcpy(dummy, string[j-1]);
strcpy(string[j-1], string[j]);
strcpy(string[j], dummy);
}
2. Improving the Output Formatting
When you print the sorted names, they are currently printed without any space or newline between them, making the output hard to read. It's better to print each name on a new line for clarity.
Incorrect Code:
for(i = 0; i < ITEMS; i++)
printf("%s", string[i]);
Improved Code:
for(i = 0; i < ITEMS; i++)
printf("%s\n", string[i]);
3. Full Corrected Code
Here is the fully corrected version of your program with the necessary fixes:
#include <stdio.h>
#include <string.h>
#define MAXCHAR 20
#define ITEMS 5
int main()
{
char string[ITEMS][MAXCHAR], dummy[MAXCHAR];
int i = 0, j = 0;
printf("Enter names of %d items:\n", ITEMS);
while(i < ITEMS)
scanf("%s", string[i++]);
// Bubble Sort Algorithm with correct braces
for(i = 1; i < ITEMS; i++)
{
for(j = 1; j <= ITEMS - i; j++)
{
if(strcmp(string[j-1], string[j]) > 0) {
strcpy(dummy, string[j-1]);
strcpy(string[j-1], string[j]);
strcpy(string[j], dummy);
}
}
}
printf("\nAlphabetical list:\n\n");
for(i = 0; i < ITEMS; i++)
printf("%s\n", string[i]);
return 0;
}
4. Additional Recommendations
-
Use More Descriptive Variable Names: Instead of string, consider using names to make the code more readable.
-
Input Validation: Currently, there's no check to ensure that the input doesn't exceed MAXCHAR characters. You might want to limit the input length in scanf to prevent buffer overflow.
scanf("%19s", string[i++]); // 19 to leave space for the null terminator
-
Consistent Indentation: Proper indentation improves readability. Ensure all blocks are properly indented.
5. Sample Output
Here's how the program should work after the corrections:
Enter names of 5 items:
Charlie
Alice
Eve
Bob
Dave
Alphabetical list:
Alice
Bob
Charlie
Dave
Eve
6. Explanation of the Sorting Logic
The corrected Bubble Sort algorithm works as follows:
- Outer Loop (
i): Runs from 1 to ITEMS - 1. It represents the number of passes through the array.
- Inner Loop (
j): Runs from 1 to ITEMS - i. It compares adjacent elements and swaps them if they are in the wrong order.
- Comparison:
strcmp(string[j-1], string[j]) > 0 checks if string[j-1] comes after string[j] alphabetically. If so, the two strings are swapped.
By ensuring that all swap operations are correctly enclosed within the if statement using braces, the algorithm correctly sorts the array in ascending (alphabetical) order.
Feel free to implement these changes and let me know if you encounter any further issues!