Question

In: Computer Science

C programming Rewrite the following function using no loops, and only tail call recursion double question5...

C programming
Rewrite the following function using no loops, and only tail call recursion

double question5  (int in)
{
     int i;
     int result;
     for (result = rand(), i = 0; i < in; i += 3)
     {
         result /= i;
         result += rand();
     }
     return result;
}

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

This program causes floating point exception

Reason:

for(result = rand(),i=0;i<in;i+=3)

Initial i value is 0

result/=i;

It means that result = result/i that is result / 0 causes floating point exception.

If the i value is 1 then the equivalent program:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

double question5(int in) // recursive calling function

{

srand(time(0)); //seed random number generator

if(in<=1) // Loop runs until in greter than

return 0; // stop of recursion

return (rand()/(in-1)) + rand() + question5(in-1); // recursive calling

}

int main(void) {

int in=5; // Assign the value of in as 5

printf("%f",question5(in)); // calling function pass in value

return 0;

}

Output:


Related Solutions

How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in...
Rewrite your program for finding Pascal's Triangle to use iteration (loops) instead of recursion. Include in both algorithms code to keep track of the total time used to run the algorithms in milliseconds. Run these algorithms for n = 10, n = 20, and n = 30. Print out both the original output and the time to run for both algorithms. Please make sure you comment your code thoroughly. The code should be nicely formatted and should use proper variables.
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
C++ programming interest using for loops. I'm fairly new to C++ programming. I really don't get...
C++ programming interest using for loops. I'm fairly new to C++ programming. I really don't get for loops, and wanted help with this question. How do I go about it? 1a. Write a program to ask the user for the starting balance of their savings account, what interest rate they are earning, and how many years they are planning to keep the account open. To calculate the new balance, including compounded interest: use a for loop to loop through the...
The following questions are on recursion in C++ Programming. I had some trouble with these questions,...
The following questions are on recursion in C++ Programming. I had some trouble with these questions, can you please help me? Thank you! Consider the following recursive function void funcEx8(int u, char v) //Line 1 { //Line 2 if (u == 0) //Line 3 cout << u << " "; //Line 4 else //Line 5 { //Line 6 int x = static_cast (v); //Line 7 if (v < 'A') //Line 8 v = static_cast (x + 1); //Line 9 else...
Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT