fork download
  1. #include <stdio.h>
  2.  
  3. // Function to sort an array in ascending order
  4. void sortArray(int arr[], int size) {
  5. int temp;
  6. // Loop through the array elements
  7. for (int i = 0; i < size - 1; i++) {
  8. // Compare each element with the next elements
  9. for (int j = i + 1; j < size; j++) {
  10. if (arr[i] > arr[j]) {
  11. // Swap the elements if they are out of order
  12. temp = arr[i];
  13. arr[i] = arr[j];
  14. arr[j] = temp;
  15. }
  16. }
  17. }
  18. }
  19.  
  20. int main() {
  21. int arr[4]; // Fixed size array with 4 elements
  22.  
  23. // Ask user to input 4 array elements
  24. printf("Enter the array elements: ");
  25. for (int i = 0; i < 4; i++) {
  26. scanf("%d", &arr[i]); // Read each element
  27. }
  28.  
  29. // Call function to sort the array
  30. sortArray(arr, 4);
  31.  
  32. // Print the sorted array
  33. printf("Sorted array: ");
  34. for (int i = 0; i < 4; i++) {
  35. printf("%d ", arr[i]); // Output each element
  36. }
  37. printf("\n");
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5288KB
stdin
10 3 5 8
stdout
Enter the array elements: Sorted array: 3 5 8 10