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

Hey guys I\'ve got some simple programming homework in C that I need some help o

ID: 3868775 • Letter: H

Question

Hey guys I've got some simple programming homework in C that I need some help on ::

Write a program that calculates the value of an integer raised to a power without using recursion (use a for loop instead ) and prints the result. You may hard - code the integer and power values as part of your program. How many clock cycles does the demo code with recursion take to complete? How many clock cycles does your implementation take? Explain the difference.

Your program’s output should be like the following:

4 raised to the power 2 is 16

Explanation / Answer

#include<stdio.h>
#include<time.h>
#include <sys/time.h>

long cal(int n, int pow){
    if (pow == 1)
       return n;
    return n * cal(n,pow-1);
}

int main(){
  
   int i;
   int n = 2;
   int power = 30;   //Calculating 4 raise to the power 2000
   long prod = 1;
   double time_spent;
   struct timeval t1, t2,t3,t4;

  
   gettimeofday(&t1, NULL);
   for (i = 0; i<30; i++)
        prod = prod * 2;
   gettimeofday(&t2, NULL);

   printf("2 raised to the power 30 is % ld ", prod);

   printf ("Total clocks for iteration = %f clocks ",((double) (t2.tv_usec - t1.tv_usec) / 1000000 +
         (double) (t2.tv_sec - t1.tv_sec)) * CLOCKS_PER_SEC);

   gettimeofday(&t3, NULL);
   prod = cal(2,30);
   gettimeofday(&t4, NULL);

   printf ("Total clocks for recursion = %f clocks ",((double) (t4.tv_usec - t3.tv_usec) / 1000000 +
         (double) (t4.tv_sec - t3.tv_sec)) * CLOCKS_PER_SEC);
   return 0;
}

Someimes recursion takes more time and sometimes iteration take more time.However general notion is that iteration will be faster than recursion as recusrion needs lot of overhead regarding call stacks.

#include<stdio.h>
#include<time.h>
#include <sys/time.h>

long cal(int n, int pow){
    if (pow == 1)
       return n;
    return n * cal(n,pow-1);
}

int main(){
  
   int i;
   int n = 2;
   int power = 30;   //Calculating 2 raise to the power 30
   long prod = 1;
   double time_spent;
   struct timeval t1, t2,t3,t4;

  
   gettimeofday(&t1, NULL);
   for (i = 0; i<30; i++)
        prod = prod * 2;
   gettimeofday(&t2, NULL);

   printf("2 raised to the power 30 is % ld ", prod);

   printf ("Total clocks for iteration = %f clocks ",((double) (t2.tv_usec - t1.tv_usec) / 1000000 +
         (double) (t2.tv_sec - t1.tv_sec)) * CLOCKS_PER_SEC);

   gettimeofday(&t3, NULL);
   prod = cal(2,30);
   gettimeofday(&t4, NULL);

   printf ("Total clocks for recursion = %f clocks ",((double) (t4.tv_usec - t3.tv_usec) / 1000000 +
         (double) (t4.tv_sec - t3.tv_sec)) * CLOCKS_PER_SEC);
   return 0;
}

Someimes recursion takes more time and sometimes iteration take more time.However general notion is that iteration will be faster than recursion as recusrion needs lot of overhead regarding call stacks.