Page 1 of 1

Write a C program From previous homework you are already familiar with the math function f defined on positive integers

Posted: Thu Jul 14, 2022 2:13 pm
by answerhappygod
Write a C program
From previous homework you are already familiar with the mathfunction f defined on positive integers asf(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given anyinteger var, iteratively applying thisfunction f allows you to produce a list ofintegers starting from var and ending with 1.For example, when var is 6, this list ofintegers is 6,3,5,8,4,2,1, which has a length of 7 because thislist contains 7 integers (call this list the Collatz list for6). Write a C program that accepts threecommand linearguments a, b and n whichare assumed to be positive integerswith a<b and n between2 and 6 inclusive. Use pthread tocreate n threads to count how many integersbetween a and b inclusivehave their Collatz list length (A) even; (B) odd. Notice that theinteger 6 belongs to category (B). You should dividethis list generation and length calculating task amongthe n threads as evenly aspossible. For example, if n is 3and there are 60 integersbetween a and b inclusive,then each thread is supposed to handle 60/3=20 Collatz lists. Printout the two numbers representing the counts for (A)(B) mentionedabove. Use mutex or semaphore to avoid race conditions ifnecessary. Note: if you do not use pthread to divide the task amongthe threads, you will get zero points. A sample run of the compiledprogram is shown below.
Arguments: 11 70 3
Using 3 threads.
The number of integers from 11~70 whose Collatz list length iseven is 31.
The number of integers from 11~70 whose Collatz list length isodd is 29.