You will complete the implementation of a number of utility functions. The funct
ID: 3853508 • Letter: Y
Question
You will complete the implementation of a number of utility functions. The function prototypes are given to you in the form of the header file util.h which is reproduced below. Each function prototype has a banner comment above it specifying the expected behavior. Your task: Write an implementation file util.c which gives a (correct) implementation of each function. Of course you will need to test your implementations. You will not, however, need to submit your test program (we will use our own test program). Notes: you may not alter the names or parameter lists of the specified functions; however, you may add “helper functions” as you see fit (which presumably would be called by the listed functions). PROGRAM IN C!
/** * * Function: is_sorted *
Parameters: * integer array a[]
* integer n: length of array a[] * *
Description:
* Returns 1 if array a[0..n-1] is sorted in ascending order * (technically, non-descending order),
* Returns 0 otherwise. */ int is_sorted(int a[], int n);
Explanation / Answer
#include <stdio.h>
int is_sorted(int a[], int n) {
int i;
for(i=0;i<n-1;i++){
if(a[i] > a[i+1]) {
return 0;
}
}
return 1;
}
int main()
{
int a[]= {0,1,2,3,4,5,6,7,8,9};
int b[]= {0,1,2,3,4,5,6,7,9,8};
printf("Is Sorted: %d ", is_sorted(a,10));
printf("Is Sorted: %d ", is_sorted(b,10));
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Is Sorted: 1
Is Sorted: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.