Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need this in c programming assignment is given and also code is given you just have to code the lines where it says TODO

I need this in c programming

assignment is given and also code is given you just have to

code the lines where it says TODO here

please edit the code that i have given and please send me screenshot of the output as well

ITs a single assignment so please do answer both parts

please only edit this below code shown. any other form will not work

image text in transcribed

#include #include #include

int next_number(int n) { //TODO

}

int happy(int n) { //TODO

}

int main() { int i; for(i=1; i

image text in transcribedimage text in transcribed

#include #include

int direction() { //Generate a random number to determine the direciton of the next step if(rand()%2 == 0) return -1; else return 1; }

//Implement the following function //The random walker starts from 0. //At every step, the walker can go to clockwise direction if the direction() functions returns 1 //or go to counter-clockwise direciton if the direction() function returns -1 //The function returns after the walker returns to 0. //The return value of the function is the number of steps taken until the walker returns to 0 int circular_random_walk(int k) { //Fill in your code below

} //Do not change the following code. int main() { int i; double sum; int steps; //Set the seed of the random number generator to 12345 srand(12345); int k; printf("Enter k: "); scanf("%d", &k); sum = 0.; int n = 1000000; int max = 0; for(i=1; i max) max = steps; } printf("average=%.3lf, max = %d ", sum, max); return 0; }

image text in transcribedimage text in transcribed

#include

long int recursive_mod(int b, int e, int m) { //Fill in your code below

}

long int loop_mod(long int b, long int e, long int m) { //Fill in your code below

} //Do not change the code below int main(void) { int b,e,m; printf("Enter b:"); scanf("%d",&b); printf("Enter e:"); scanf("%d",&e); printf("Enter m:"); scanf("%d",&m); printf("Result using loop:%ld ",loop_mod(b,e,m)); printf("Result using recursion:%ld ",recursive_mod(b,e,m)); return 0; }

image text in transcribed

untitledhappy.c include 2 #include 3 #include 4 5 int next_number (int n) //TODO 8 9 10 int happy (int n) 12 / /TODO 14 int main() 16 17 18 19 20 int i; for (i-1; i #include 3 4 int direction () //Generate a random number to determine the direciton of the next step if(rand ()%2 -0) return -1; else return 1; 6 7 8 9 10 11 //Implement the following function 12 //The random walker starts from . 13 //At every step, the walker can go to clockwise direction if the direction() functions returns1 14 //or go to counter-clockwise direciton if the direction) function returns -1 15 //The function returns after the walker returns to . 16 //The return value of the function is the number of steps taken until the Walker returns to 17 int circular_random_walk (int k) 18 //Fill in your code below 23 25 26 27 untitled circular-random.c modexpo.c x include 2 3 4 long int recursive_mod (int b, int e, int m) 6 //Fill in your code below 8 9 10 12 14 15 long int loop_mod (Long int b, long int e, long int m) //F1LL in your code below 17 18 19 20 21 Exercise 1. (50 points) Circular Random Walk In this problem we will write a program to simulate a circular random walk. Imagine we have the following numbers 0,1,2,..., k -2,k - 1 evenly spaced on a circle in clockwise direction just like a clock. A random walker starts from 0 and randomly walks along the circle. With probability 0.5, he walks clockwise to the next number; with probability 0.5, he walks counter-clockwise to the next number. For example, at 0, he can walk to 1 in the clockwise direction, or walk to 9 in the counter-clockwise direction We will implement a function circular random walk in circular-random .. This function takes a single parameter k as described above, and returns the number of steps the walker takes until it returns to 0 In the main() function, a user is allowed to enter a value for k, and the program will run the simulation 1,000, 000 times and report the average and the maximum steps for the walker to return 0 You should know it if your code is correct because the results are very special and interesting Exercise 2. (50 points) Modular Power Calculator In this problem, we will write a program that calculates be mod m for given integers b, e and m. We assume that b>0, e > 0, and m> 1 and all of them are less than 231-1. Here mod is the Modulus operator In our implementation, we will leverage the following fact a x b mod m - [(a mod m) x (b mod m)] mod m Consider the following example where b -5,e-3, and m -7. We need to calculate 53 mod 7. By applying the above fact, we have the following 53 mod7 52 5 mod 7 (52 mod 7) x (5 mod 7)] mod 7 {(5 mod 7) (5 mod 7) mod 7]x(5 mod 7)) mod 7 In general, we have the following recursion be mod m = [(F-1 mod m) x (b mod m)) mod m Write two functions one will solve the problem using for loop and the other one using a recursive functiorn Sample output >./modeexpo Enter b:5 Enter e:3 Enter m:7 Result using recursion:6 Result using loop:6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions