Homework 2 Revision 1

Due: May 10, 2023
Points: 100


A typographical error in question 3's first example has been fixed. The answer should be 95028, not 95029.

  1. (40 points) A string is said to be reverse alphabetic if the letters in it, regardless of case, are in the reverse of dictionary order. So, for example, “miff” and “pooka” are reverse alphabetic, and “willow” and “computer” are not. In the following, assume the arguments contain only alphabetic characters.

    1. Write a program called isrevalpha that uses a recursive function to determine whether a single command-line argument is reverse alphabetic. The program prints argument: True if its argument is reverse alphabetic, and argument: False otherwise. For this version, assume the argument is either all lower case or all upper case alphabetic characters. Call your program “abcde1.c”.

    2. Change your program so that the letters can be any case, not all upper or lower case. Again, you can assume all characters in the argument are alphabetic. Call your program “abcde2.c”.

    3. Finally, modify your program to take any number of arguments. Each argument must produce one line of the type above. Call your program “abcde3.c”.

  2. (30 points) Define the function:
    f(n) = n/2 if n even, 3n+1 if n odd
    The Collatz conjecture says that, if you iterate this sequence for any initial value of n, then eventually the sequence will reach the number 1.

       For a given number n, let k be the least number of iterations needed to reach the number 1 (excluding the initial value). Then k is called the total stopping time of n.

       For example, if n = 29, then the sequence is:

    
    29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    
    and so the total stopping time of 29 is 18.

       Write a program that takes as input a positive integer and prints both the sequence and the total stopping time for that integer. Prompt the user with “> ” (note the trailing space and no newline). The output should look like (user input is in red):

    
    > 29
    29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    The total stopping time for 29 is 18
    

    Call your program “collatz.c”.

  3. (30 points) A paper proposed the following equation for the cumulative deaths for COVID-19 after x days, where x ≥ 30:
    9×10−9x6 − 6×10−6x5 + 0.0015x4 − 0.1376 x3 + 7.4558x2 − 97.284x
    Write a program that reads an integer number of days and prints the cumulative deaths. If the number of days is less than 30, reject the input. Round the number of deaths to the nearest integer.

    You will need to use the mathematical function pow. You must also include the math library header file math.h and, when you compile, put “–lm” after the compile command to link the math library (if you forget this, the compiler will say that pow is undefined).

    Here are some examples of input and output:

    
    Number of days: 150
    After 150 days, the number of deaths predicted is 95028
    

    
    Number of days: 8
    The number of days must be at least 18
    

    In the latter case, the message is written to the standard error, not the standard output. Call your program “covid.c”.

Footnote

  1. H. Kumar, P. Arora, M. Pant, A. Kumar, and S. Khan, “A simple mathematical model to predict and validate the spread of Covid-19 in India,” Materials Today: Proceedings of the Third International Conference on Computational and Experimental Methods in Mechanical Engineering, 47(13) pp. 3859–3864
    DOI: 10.1016/j.matpr.2021.03.434

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 26, 2023 at 12:28PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh