Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// file: util.h /** * Function: print_arb_base * Parameters: * unsigned integer

ID: 640601 • Letter: #

Question

// file: util.h

/**
* Function: print_arb_base
* Parameters:
* unsigned integer n: the number to be converted
* unsigned integer b: the destination number-system base
* Description: prints unsigned integer n in an arbitrary base b
* where 2 <= b <= 36. Digits are drawn from 0..9a..z in the natural
* order
* Details:
*
* If an invalid base b is passed, an error message is printed
* and the function returns.
* The digits are printed on a single line from most-significant
* digit to least.
* Comments: This is a generalization of the procedure for printing
* bit-level representation, octal, hex, etc. If b is 16, for example,
* the hex representation is printed.
void print_arb_base(unsigned int n, unsigned int b);
/**

* 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


void print_arb_base(unsigned int n, unsigned int b){
   if(b >= 2 && b <= 36){
       string temp = "";
       while(n >= 1){
           int val = n % b;
           n /= b;
           if(val < 10){
               temp = (char)(val + 48) + temp;
           }
           else{
               temp = (char) ((val - 10) + 65) + temp;
           }
       }
       cout << temp << endl;
   }
   else{
       cout << "Error: invalid base" << endl;
   }
}


bool is_sorted(int arr[], int n){
   for(int i = 0; i < n - 1; ++i){
       if(arr[i] > arr[i + 1]){
           return false;
       }
   }
   return true;
}