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

Download the starter code main.cpp, hw6.h, and hw6.cpp. You will only turn in hw

ID: 3839985 • Letter: D

Question

Download the starter code main.cpp, hw6.h, and hw6.cpp. You will only turn in hw6.cpp. Do not modify hw6.h, as you have no reason to. We have provided main.cpp to give you an idea of how we intend to use the functions. You are free to do whatever you want with main.cpp (and you'll want to make changes to it for debugging) but you won't submit it. hw6.cpp must not contain a main function. You may not use global variables. We may take off up to 20% of the total marks for poor style: make sure to name your variables reasonably, indent properly, and comment sufficiently. Submit hw6.cpp. Problem 1: (Integer power) Write the implementation of int ipow (int base int exp) which returns base to the exp power. Assume exp is nonnegative. Note that 09 1. You may not use any libraries aside from cassert. Remark. If you were to use the cmath library, you could do int i pow (int base int exp) return static-cast kint (pow double (base) double (exp))) Remark. You could consider exp an edge case. Sometimes edge cases must be handled separately with, say, an if-statement. However, you can sometimes avoid additional control structures by being mindful of how you write your loops and how you initialize your variables. Avoid additional control structures if voll can.

Explanation / Answer

#include "hw6.h"
#include <cmath.h>

int ipow(int base, int exp){
    if (exp == 0)
       return 1;
    else
       return (base * ipow(base, exp -1);    // recursion is used. so ipow(2,2) will be first
}                                            // 2 * ipow(2,1) , then 2 * 2 * ipow(2,0) and then 2 * 2 * 1

double my_min(double *arr, int n){
    double min;
    min = *arr;
    for (int i = 0; i<n; i++){
        if (*(arr+i) < min)
           min= *(arr+i);
    }
    return min;
}

double angle(double x, double y){

return (atan(y/x) * (180/3.1415));    // atan gives angle in radians. One radian equals 180/3.1415 degrees. So we will
}                                       // get answer in degrees

char * my_strcpy(char *destination, char *source){

     int count1=0;
     int count2=0;
     char *p, *q;
     if (destination == NULL || source == NULL)
        return NULL;
     p = source;
     while (*p){
        count1++;
        p++;        
     }
     p = destination;
     while (*p){
        count2++;
        p++;        
     }
     p = source;
     q = destination;
     if (count2 >= count1){
        while(*p){
           *q = *p;
            p++;
            q++;
        }
        *q = ''; // copy the null content also
     }
     else {
         return NULL   // Destination length should be equal or geater than source length
     }
     return destination;   //return destination pointer (in which source has been copied)
   
}

int my_strcmp(const char *str1, char *str2){

     int count1=0;
     int count2=0;
     char *p, *q;
     if (str1 == NULL || str2 == NULL)
        return -1;   // Error situation
     p = str1;
     while (*p){
        count1++;
        p++;        
     }
     p = str2;
     while (*p){
        count2++;
        p++;        
     }
     p = str1;
     q = str2;
     if (count2 == count1){
        while(*p){
           if (*q != *p)
              return 0;    // 0 means str1 and str2 are not equal
           p++;
           q++;
        }
         
     }
     else {
         return 0   // If str1 and str2 are of different length then they are not equal
     }
     return 1;   //1 means str1 and str2 are equal
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote