Question

Programming language: C NOT c++

Problem 2 (20 points) Both the for loop and the do-while loop can be transformed into a simple while loop. For each of the following examples a) and b), write equivalent code using a while loop instead. double rand_double)( double ret - (double)rand(); return ret/ (RAND MAX 1); int factorial (int n) int for i, ret =1; (i=2; i <= ret *= 1; n; i++) int sample_geometric_rv(double p)l double q return ret; int ne; do q rand_double); jwhile (q - p); return n a) b)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
int factorial(int n){
int i, ret = 1;
i = 2;
while(i <= n){
ret *= i;
i++;
}
return ret;
}
int main() {
int n;
printf("Enter n: \n");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
}

stdin Inputs... Interactive mode: OD OFF Focus View: OD OFF version GCC 8.1.e O Execute saveMy Result... CPU Time: .99 sec(s), Memory: 1488 RiLobyte(s Enter n: Factorial of 5 is 120

Part B:

double rand_double(){
double ret = (double)rand();
return ret/ (RAND_MAX + 1);
}
int sample_geometric_rv(double p){
double q;
int n = 0;
q = rand_double();
while(q >= p){
n++;
q = rand_double();
}
return n;
}

For any further doubts, please let me know through comments and I will help you out.

Add a comment
Know the answer?
Add Answer to:
Programming language: C NOT c++ Problem 2 (20 points) Both the for loop and the do-while...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • c++ convert do while loop below to a for loop #define MAX VALUE 5 #define START...

    c++ convert do while loop below to a for loop #define MAX VALUE 5 #define START VALUE 5 //START_VALUE always <-MAX VALUE int main() convert do while loop below to a for loop int j - MAX VALUE; do cout <<<<endl; ; while > 0); I

  • How do I write this code from while loop to for loop? (this is the whole...

    How do I write this code from while loop to for loop? (this is the whole code) NOTE: do not add any import other than: import java.util.ArrayList; import java.util.Collection;(collection not collections) import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.HashSet; code: public static int frequency(Collection values, int target) { var iter = values.iterator(); int app = 0; while (iter.hasNext()) { if(iter.next() == target) { app++; } } return app; } public static boolean isSorted(List list) { var iter = list.listIterator(); int...

  • C++ code do while loop issue. The code I posted works but when it prompts the...

    C++ code do while loop issue. The code I posted works but when it prompts the user if they want to try against with another sentence, it only takes the Y and goes straight into another loop, without letting me enter a new sentence. I need it to be able to take the Y response from a user and then let me enter another sentence. do { cout<<"Enter a sentence:"; //user input sentence cin.getline(str,80); int wordsCount = 0; for (int...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • Need help in my C language assignment... all I need to do is 1.Use recursion instead...

    Need help in my C language assignment... all I need to do is 1.Use recursion instead 2. NO LOOPS ALLOWED Output _______________________________________ What number shall I start with? 16 sequence: 16 8 4 2 1 length: 5 largest: 16 For start values from 1 to 16: longest: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 length: 20 containing largest: 15 46 23 70 35 106 53 160 80...

  • How many Iterations will this while loop perform? int ico), j(10); cout << "i = "...

    How many Iterations will this while loop perform? int ico), j(10); cout << "i = " << i << endl; cout << "j = " << j << endl; while (i > j) { cout << "j-" « j << endl; j += 2; cout << "i = " << i << endl; } cout << "i = << i << endl; cout << "j = " << j << endl; 5 6 C 8 10 Infinite times Does the...

  • c++ Some pieces of the following code are missing. Fill in the blanks of the following...

    c++ Some pieces of the following code are missing. Fill in the blanks of the following program. Each answer must fit on a single line. include <iostream> #include <cstdib #include <ctimes using namespace std; randoml_event return 2 (rand() 5 random2 intre + rand 90 return ret: random1_1000X rand) intret 100 return ret: fint 2) return nt; return n2 is oder Is_odot if (n%2 0) return return true; int sum3 (int n, int n, int n3 X int sum= n1 +...

  • The programming language has to be C. Do you remember rand()? Do you remember how it...

    The programming language has to be C. Do you remember rand()? Do you remember how it can use srand() for a seed? Did you know that rand uses that seed as the initial state of their random number generator. Did you know that a random number generator is really a deterministic sequence generator? Yeah! So your next random number might be calculated using a recursive equation like: rand_n = rand_(n-1) * coeffecient1 + coeffecient2 That is the random number produced...

  • In C++ using the while loop.. Compute factorial of a number Factorial of n = 1*2*3...

    In C++ using the while loop.. Compute factorial of a number Factorial of n = 1*2*3 *n 702

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT