In C, write a function void (int number[ ]) sorts the given array of integers into non-decreasing order, storing the result in the given array. The array of integers may have any number of elements, but the last element of the array must be zero
#include <stdio.h> void sort(int *arr) { int i, temp; int size = 0; while (arr[size] != 0) { size++; } for(i = 0; i < size; ++i) { for (int j = 0; j < size - 1; ++j) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } void print(int *arr, int size) { int i; for(i = 0; i < size; ++i) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {3, 8, 1, 9, 7, 2, 0}, size = 6; printf("Original array: "); print(arr, size); sort(arr); printf("Sorted array: "); print(arr, size); return 0; }
Get Answers For Free
Most questions answered within 1 hours.