Introduction
In this project, you will learn how to organize a list of programming courses using the bubble sort algorithm. The courses will be sorted in lexicographical order, allowing you to easily see and summarize the programming languages you have learned.
👀 Preview
$ gcc courses.c -o courses
$ ./courses
ASP
ASP.NET
BASIC
C
C#
C++
COBOL
JAVA
PASCAL
PHP
🎯 Tasks
In this project, you will learn:
- How to correctly calculate the length of an array
- How to modify the parameter type of a sorting function
- How to correct the comparison and swapping logic in the sorting algorithm
🏆 Achievements
After completing this project, you will be able to:
- Organize a list of programming courses using the bubble sort algorithm
- Understand the importance of proper array length calculation and function parameter types
- Apply the correct comparison and swapping logic in a sorting algorithm
- Demonstrate your ability to follow step-by-step instructions and modify existing code to achieve the desired outcome
Correct the Array Length Calculation
In this step, you will learn how to correctly calculate the length of the courses array.
Open the
courses.cfile located in the/home/labex/project/directory.Find the following line of code:
int n = strlen(courses);This line uses the
strlenfunction to calculate the length of thecoursesarray, which is incorrect. Thestrlenfunction is used to calculate the length of a string, not an array.Replace the above line with the following:
int n = sizeof(courses) / sizeof(courses[0]);This line uses the
sizeofoperator to calculate the total size of thecoursesarray, and then divides it by the size of a single element to get the number of elements in the array.
Change the Parameter Type of the Sorting Function
In this step, you will learn how to change the parameter type of the sort function.
In the
courses.cfile, find thesortfunction definition:void sort(char p[], int n)Change the parameter type from
char p[]tochar *p[]. The updated function definition should look like this:void sort(char *p[], int n)This change is necessary because we are dealing with an array of strings (an array of pointers), not a single string.
Correct the Comparison and Swapping Logic
In this step, you will learn how to correct the comparison and swapping logic in the sort function.
In the
sortfunction, find the following code:if (k != j) { tmp = p[k]; p[k] = p[i]; p[i] = tmp; }Replace the condition
if (k != j)withif (k != i). The updated code should look like this:if (k != i) { tmp = p[k]; p[k] = p[i]; p[i] = tmp; }This change is necessary to check if the smallest element has been found in the current iteration (indexed by
i).
After completing these three steps, your courses.c file should be ready for compilation and execution. You can now proceed to the next step.
In the
sortfunction, complete the// TODO:section as follows:{ if (strcmp(p[j], p[k]) < 0) { k = j; } }Execute the following command:
gcc courses.c -o courses
./courses
- Verify that the program produces the expected output.
ASP
ASP.NET
BASIC
C
C#
C++
COBOL
JAVA
PASCAL
PHP
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



