Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the code in C programming language All you have to do is to write (in the file functions. c) the three fairly simple

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please write the code in C programming language

All you have to do is to write (in the file functions. c) the three fairly simple functions described below, which only require using basic C features: variables, expressions, conditional statements, and loops. They don't even need arrays. The functions just return values based on their parameters- they do not read any input, or produce any output. You can calculate their results however you want, except that, as Appendix C says, you cannot use any C library functions in implementing your functions. (Read that sentence again, and be sure not to forget it.) 3.1 short compare_rectangles(int 11 , int w1, int 12 , int w2 ) This function is given the length and width of two rectangles (11 and w1 are the length and width respectively of the first rectangle, and similarly 12 and w2 for the second rectangle), and it should return a value indicating how their areas compare. The function should return: - 0 if the two rectangles have equal areas. - 1 if the first rectangle's area is less than the second rectangle's area. - 1 if the first rectangle's area is greater than the second rectangle's area. - It is an error case for any dimension to be negative (this doesn't make sense). So if any of the parameters are less than zero the function should return 2. Note that having a dimension of zero (or both dimensions of zero) is fine and not an error case, because a dimension of zero does make sense. In this case the rectangle will just have an area of zero and 0,1, or 1 should be returned, depending on the area of the other rectangle. 3.2 int ith factor(int a, int b, int i ) This function should return the i 'th common positive factor of its parameters a and b, or 1 if they do not have at least i common factors. For example, suppose a is 6240 and b is 288 (or, equivalently, a is 288 and b is 6240). Their first positive common factor is obviously 1 , because 1 is a factor of any number, so if i is 1 the function should return 1 . Their second common factor is 2 because 2 is a factor of both of these numbers, so if i is 2 then 2 should be returned. Their third common factor is 3 and their fourth common factor is 4 , while their fifth common factor is 6 , so if i was 5 the function should return 6. Their twelfth common factor is 96 , so if i was 12 the function should return 96 . These values of a and b do not have any common factors larger than 96 so 1 should be returned for them if i has any value greater than 12 . Note that negative numbers have positive factors, so for example ith factor (624,48,7)=i th factor (624, 48,7)=i th f factor (624,48,7)=i ith factor(624,48,7)=12. However, there is no such thing as the zero'th common factor of two numbers, and there is no meaningful interpretation of what the result should be if i was negative, so the function should return 1 if i is zero or less than zero. Mathematically we could say that any number is a factor of zero. However, we only want the function to determine and return the i'th factor of values of a and b that are nonzero. So if either of a or b are zero then 1 should just be returned. (Note that this is the behavior that the function will likely produce naturally if you write it to handle the cases above, so if it's not necessary to handle this as a special case you should not do so.) 33 long pell(short n ) The Pell number sequence is used in mathematics to approximate the square root of 2 , as well as to solve some other numerical problems. Pell numbers can be expressed in different ways but the simplest way is: pell(n)=2pell(n1)+pell(n2)foranyngreaterthan1ifnis0ifnis For example, pell (0) (the first Pell number) is 0 , pell(1) is 1 , pell(2) is 2 , pell(3) is 5 , pell(4) is 12 , and pell(5) is 29. Pell numbers can be computed recursively, based on the definition above, but they can also be computed iteratively as well, using a loop with two variables that store the values of the two previous Pell numbers at every iteration. The Pell number sequence starts at 0 (meaning pell (0) ). It is not defined for negative numbers. So if n is negative the function should just return 1 to indicate that this is an error case. You can calculate Pell numbers however you want, provided that (just like for the previous functions) you do not use any C library functions. But because the sequence of Pell numbers grows exponentially, there is a limit to how large you can calculate them. If you write the function in the most obvious way it would start taking prohibitively long to run on the Grace machines if it was called on values of n that were getting close to 50 . (For example, coding the function in this way takes around a minute to compute pell (50) on Grace.) So we will not test your function on any value of n greater than 45. (So you do not need to waste time trying to test your function yourself for values of n greater than 45 .) Note that it is possible to write the function efficiently, so it will work fast on values of g greater than 45 . But if you do so and try to call it on arguments that are sufficiently large you will get an incorrect result. For example, pell(115) is 6,623,843,019,776,607,541 (and if you don't believe me you can calculate it by hand and see), but when I call my inction to compute pell(116) the result returned is 7,884,135,764,956,931,236, which is wrong. We will discuss n lecture shortly why this occurs. But you do not need to worry about (or test yourself) what results your function oroduces for any arguments that would cause such behavior, because as mentioned above we will just not test your unction on any values of n larger than 45 . Note that the format specifier \%ld prints a value of type long so you will have to use it if you want to print the value eturned by your function during development or debugging. Remember, you cannot use any C library functions at all in writing your functions for this project

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

More Books

Students also viewed these Databases questions

Question

design a simple disciplinary and grievance procedure.

Answered: 1 week ago