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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply