This must be in C language. my teacher gave us this format and told us to write
ID: 672384 • Letter: T
Question
This must be in C language. my teacher gave us this format and told us to write functions. I have no clue how to write functions. Need help please. #include <stdio.h> #include <stdlib.h> /** * Instructions: * * Below are templates for six utility functions that you are to * implement. * * Each function that is part of the assignment has "TODO" in the * banner comment above the function template. * * Read the description of each functions expected behavior and complete * the function body to achieve the specified behavior. * * RULES: * * - You may NOT change the names, parameter lists or return type * of any of the functions. * * - Pay close attention to the restrictions in the banner comments. * * - For functions that operate on arrays, you may NOT create any additional * arrays to complete your task. * * - At the bottom of this file, there is also a template for a main() function. * Write your main function as a tester of the utility functions you have written. * Give some thought to good test cases, etc. * * - You will also notice some mysterious lines before main and after: * * #ifndef _LIB * * // main function here * * #endif * * DO NOT REMOVE THESE LINES. WHEN YOU COMPILE AND RUN YOUR PROGRAM IT WILL BEHAVE * EXACTLY AS IT WOULD IF THOSE LINES WERE NOT THERE AT ALL. * * HOWEVER, THESE LINES WILL ALLOW US TO RUN TESTS WHICH CALL YOUR FUNCTIONS DIRECTLY * FROM OUR OWN TESTER PROGRAM (THIS IS WHY THEY ARE THERE TO BEGIN WITH). * * - NO GLOBAL VARIABLES * * - You may write additional "helper functions" if you find it useful in writing the * assigned functions. * * NOTES: * - The code compiles as-is, but of course the functions don't do anything. If you * compute tge code as-is, you will receive some warnings because it detects that * functions which are supposed to return a value have no return statement (because * their bodies are empty right now!). Don't let this throw you off! * */ /** * TODO * Function: exp * Description: computes and returns x^n * * Restrictions: does not call any library functions!!! */ double exp(double x, unsigned int n) { } /** * TODO * Function: arr_max * Description: determines the largest value in array a[] and * returns it. * * Assumptions: parameter n specifies the length array a[] * * Restrictions: the array a[] may not be modified. * */ int arr_max(int a[], int n){ } /** * TODO * Function: is_sorted * Description: determines if the array a[] is in * sorted order (from smallest to largest, * duplicates allowed). * returns 1 if it is sorted * returns 0 otherwise. * * Assumptions: parameter n specifies the length array a[] * * Restrictions: the array a[] may not be modified. */ int is_sorted(int a[], int n){ } /** * TODO * Function: num_occurrences * Description: determines how many times parameter val * appears in the array a[] and returns * the count. * * Assumptions: parameter n specifies the length array a[] * * Restrictions: the array a[] may not be modified. */ int num_occurrences(int a[], int n, int val){ } /** * TODO * Function: same_contents * Description: determines if arrays a[] and b[] contain * exactly the same values, but not necessarily * in the same order. * returns 1 if they do have the same contents * returns 0 otherwise. * Note: if there are multiple occurrences of the * same value, both arrays must contain the same * number of such occurrences to be considered * to have the same contents. * * Assumptions: parameter n specifies the length of both a[] and * b[] * * Restrictions: the arrays a[] and b[] may not be modified. */ int same_contents(int a[], int b[], int n){ } /** * TODO * Function: legal_sudoku_row * Description: returns 1 (true) if the array row[] * represents a legal sudoku row. * Returns 0 (false) otherwise. * * Legal sudoku row: is a permutation of * 1,2,3,4,5,6,7,8,9 * * legal: 2 9 1 8 4 3 5 7 6 * illegal: 1 2 8 1 3 4 6 5 9 * illegal: 1 2 8 7 3 4 6 5 12 * * Restrictions: the array row[] cannot be modified; no * extra temporary arrays are allowed either! */ int legal_sudoku_row(int row[9]){ } /** * Function: p_array * Description: simple utility function for displaying the * contents of an integer array. * * Utilize it for your testing! */ void p_array(int a[], int n) { int i; printf("[ "); for(i=0; i<n; i++) { printf(" %i ", a[i]); } printf("] "); } /** DO NOT REMOVE THE LINE BELOW!! ***/ #ifndef _LIB int main(){ int data[] = {29, 12, -9, 11, -14, 109, 19}; int x; int m; // x = a[2]; m = arr_max(data, 7); if(m != 109){ printf("Failed arr_max test 1! "); } /* tester code here */ double ans1 = exp(2.1, 0); printf("exp(2.1, 0) returned %f; expected 1.0 ", ans1); } /** DO NOT REMOVE THE LINE BELOW!!! **/ #endif
Explanation / Answer
Implemented both the max array and the exp function.
Working c code
#include <stdio.h>
#include <stdlib.h>
/**
* Instructions:
*
* Below are templates for six utility functions that you are to
* implement.
*
* Each function that is part of the assignment has "TODO" in the
* banner comment above the function template.
*
* Read the description of each functions expected behavior and complete
* the function body to achieve the specified behavior.
*
* RULES:
*
* - You may NOT change the names, parameter lists or return type
* of any of the functions.
*
* - Pay close attention to the restrictions in the banner comments.
*
* - For functions that operate on arrays, you may NOT create any additional
* arrays to complete your task.
*
* - At the bottom of this file, there is also a template for a main() function.
* Write your main function as a tester of the utility functions you have written.
* Give some thought to good test cases, etc.
*
* - You will also notice some mysterious lines before main and after:
*
* #ifndef _LIB
*
* // main function here
*
* #endif
*
* DO NOT REMOVE THESE LINES. WHEN YOU COMPILE AND RUN YOUR PROGRAM IT WILL BEHAVE
* EXACTLY AS IT WOULD IF THOSE LINES WERE NOT THERE AT ALL.
*
* HOWEVER, THESE LINES WILL ALLOW US TO RUN TESTS WHICH CALL YOUR FUNCTIONS DIRECTLY
* FROM OUR OWN TESTER PROGRAM (THIS IS WHY THEY ARE THERE TO BEGIN WITH).
*
* - NO GLOBAL VARIABLES
*
* - You may write additional "helper functions" if you find it useful in writing the
* assigned functions.
*
* NOTES:
* - The code compiles as-is, but of course the functions don't do anything. If you
* compute tge code as-is, you will receive some warnings because it detects that
* functions which are supposed to return a value have no return statement (because
* their bodies are empty right now!). Don't let this throw you off!
*
*/
/**
* TODO
* Function: exp
* Description: computes and returns x^n
*
* Restrictions: does not call any library functions!!!
*/
double exp(double x, unsigned int n) {
if( n == 0)
return 1;
else if (n%2 == 0)
return exp(x, n/2)*exp(x, n/2);
else
return x*exp(x, n/2)*exp(x, n/2);
}
/**
* TODO
* Function: arr_max
* Description: determines the largest value in array a[] and
* returns it.
*
* Assumptions: parameter n specifies the length array a[]
*
* Restrictions: the array a[] may not be modified.
*
*/
int arr_max(int a[], int n){
int max=a[0];
for (int i=1;i<n;i++)
{
if (a[i]>max)
max=a[i];
}
return max;
}
/**
* TODO
* Function: is_sorted
* Description: determines if the array a[] is in
* sorted order (from smallest to largest,
* duplicates allowed).
* returns 1 if it is sorted
* returns 0 otherwise.
*
* Assumptions: parameter n specifies the length array a[]
*
* Restrictions: the array a[] may not be modified.
*/
int is_sorted(int a[], int n){
}
/**
* TODO
* Function: num_occurrences
* Description: determines how many times parameter val
* appears in the array a[] and returns
* the count.
*
* Assumptions: parameter n specifies the length array a[]
*
* Restrictions: the array a[] may not be modified.
*/
int num_occurrences(int a[], int n, int val){
}
/**
* TODO
* Function: same_contents
* Description: determines if arrays a[] and b[] contain
* exactly the same values, but not necessarily
* in the same order.
* returns 1 if they do have the same contents
* returns 0 otherwise.
* Note: if there are multiple occurrences of the
* same value, both arrays must contain the same
* number of such occurrences to be considered
* to have the same contents.
*
* Assumptions: parameter n specifies the length of both a[] and
* b[]
*
* Restrictions: the arrays a[] and b[] may not be modified.
*/
int same_contents(int a[], int b[], int n){
}
/**
* TODO
* Function: legal_sudoku_row
* Description: returns 1 (true) if the array row[]
* represents a legal sudoku row.
* Returns 0 (false) otherwise.
*
* Legal sudoku row: is a permutation of
* 1,2,3,4,5,6,7,8,9
*
* legal: 2 9 1 8 4 3 5 7 6
* illegal: 1 2 8 1 3 4 6 5 9
* illegal: 1 2 8 7 3 4 6 5 12
*
* Restrictions: the array row[] cannot be modified; no
* extra temporary arrays are allowed either!
*/
int legal_sudoku_row(int row[9]){
}
/**
* Function: p_array
* Description: simple utility function for displaying the
* contents of an integer array.
*
* Utilize it for your testing!
*/
void p_array(int a[], int n) {
int i;
printf("[ ");
for(i=0; i<n; i++) {
printf(" %i ", a[i]);
}
printf("] ");
}
/** DO NOT REMOVE THE LINE BELOW!! ***/
#ifndef _LIB
int main(){
int data[] = {29, 12, -9, 11, -14, 109, 19};
int x;
int m;
// x = a[2];
m = arr_max(data, 7);
if(m != 109){
printf("Failed arr_max test 1! ");
}
/* tester code here */
double ans1 = exp(2.1, 0);
printf("exp(2.1, 0) returned %f; expected 1.0 ", ans1);
}
/** DO NOT REMOVE THE LINE BELOW!!! **/
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.