Some C Practice
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Conditionals and Enums
Create a function that takes in three values: two boundary values (boundary_l and boundary_u) and a value to be tested (sample_value). All three values are of the int type. The two boundary values delimit a numerical range that goes from boundary_l (inclusive) to boundary_u (exclusive). The function should determine if sample_value is below, within, or above the bounded region. The variable boundary_l is guaranteed to be less than or equal to boundary_u. It should return a number to denote this result. Specifically:
-1if thesample_valueis below the bounded range0if thesample_valueis within the bounded range1if thesample_valueis above the bounded range
What the function above returns is sort of weird. The values -1, 0, and 1 have meaning, but unless the code was commented and/or you were the one who wrote it, the whole idea behind how to use the output could be a little hard to determine. It would be nice if we could give more meaning to what the function returns. This is where an enum can come into play. An enum allows us to create a "type" and give it meaningful names. For the problem at hand, a enum called location could be useful. Let's define it as such:
enum location {Below=-1, Within=0, Above=1};
One could then modify their function to instead return a location type. If you wanted to return an extremely inclusive function that always returned that a number was within the specified bounded range you could do:
location friendly_boundary_determine(int sample_value, int boundary_l, int boundary_u){
return Within;
}
Note you can both specify the underlying numbers that sit behind the enum or let the system auto-number for you. There's different benefits and downsides to this depending on what you need to do:
enum location {Below=-1, Within=0, Above=1}; //specify that Below really means -1, etc
enum location {Below, Within, Above}; //let location enum be autonumbered (it'll start with 0)
Rewrite your boundary_determine function below so that it returns a value of type location. Note: location is already declared for you.
Within than 0, particularly since its name will be defined somewhere.For Loop
There's two main types of "loops" in programming, for loops and while loops. In general, for loops are used when you know ahead of time how many iterations through the loop are needed for the purpose of the algorithm. Conversely, while loops are used when you don't know ahead of time. Of course there's always exceptions to this, and usage of additional variables and conditional breaks can basically repurpose one loop as another, so don't see this as a 100% dogmatic assertion.
For example, write a function that takes the binomial coefficients n and k and determines the number of k-element combinations (subsets of k elements) from a set with n elements. This value can be computed from factorials using the formula1:
You can assume n\geq k. Remember, 0!=1.
Roots
Write a function that determines the cube root of a positive input integer value. The value returned should be the integer that when cubed, is closest to the input value.
No ^ is not the "power" symbol. No it isn't ** either. There is no power symbol in C.
You can assume the inputs will always be positive to this function (no worries about negative/imaginary stuff).