Homework 1

Homework 1

Due: April 14, 2025
Points: 100


All programs are to be submitted through Gradescope. Gradescope will compile and execute your program, sometimes with varying inputs or invocations. You can see your score once executions are done. You can submit your program multiple times, up to the deadline — at that point, the score you have will usually be the grade for that problem. I say “usually” because we reserve the right to look at your submission and deduct points if you do not use proper programming style.

You have to name your program as given in the problem. Otherwise you will get an odd error message indicating there is a problem with Gradescope.

Also, your output must match Gradescope’s exactly, including blanks and tabs — so pay attention to the example output!

  1. (25 points) Write a program that prints a tic-tac-toe board with an “X” and an “O” in two squares. Each square is to be 7×7, with “#” marks. The “X” is to be 5×5, centered in the upper right square and composed of “X”s, and the “O” is to be 5×5, centered in the upper middle square, and composed of “O”s. Your output will look like this:
    
           #       #
           # OOOOO # X   X
           # O   O #  X X
           # O   O #   X
           # O   O #  X X
           # OOOOO # X   X
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    
    When this is done, exit with the status code 0.

    Call your program “tic.c”.

  2. (25 points) Write a program that reads in weight w in pounds and height h in inches. Then use the following formula to calculate the BMI
    BMI = 703w/h2
    If w is a non-positive integer, give the error message
    Weight is not a positive integer!
    If h is a non-positive integer, give the error message
    Height is not a positive integer!
    If either error occurs, exit the program and give the status code 1. Otherwise, after printing the BMI, exit with a status code of 0.

    Here are some examples; each is a single execution of the program.

    
    Height> 66
    Weight> 190
    The BMI for height 66 and weight 190 is 30.663452
    

    
    Height> 48
    Weight> 65
    The BMI for height 48 and weight 65 is 19.832899
    

    
    Height> 0
    Height is not a positive integer
    

    
    Height> 43
    Weight> hello
    Weight is not a positive integer
    

    Note that the program reads integers, but it prints the BMI as a floating point number. Remember to print the error messages on the standard error, and not the standard output.

    Also notice that the program does not crash when I enter a non-integer (see the last example).

    Call your program “bmi.c”.

    Hint: Use scanf to read the input.

  3. (25 points) Now expand your program in the previous problem as follows:

    Here are some examples; as before, each is a single execution of the program.

    
    Height> 66
    Weight> 190
    The BMI for height 66 and weight 190 is 30.663452
    This is class I obese – but consult a doctor before doing anything!
    

    
    Height> 48
    Weight> 65
    The BMI for height 48 and weight 65 is 19.832899
    This is in a healthy range – but consult a doctor before doing anything!
    

    
    Height> 72
    Weight> 130
    The BMI for height 72 and weight 130 is 17.629244
    This is in a healthy range – but consult a doctor before doing anything!
    

    
    Height> 72
    Weight> 312
    The BMI for height 72 and weight 312 is 42.310184
    This is class III obese  – but consult a doctor before doing anything!
    

    
    Height> -1
    Height is not a positive integer
    

    Call your program “bmifull.c”.

  4. (25 points) The program “three.c” first prints 3, then doubles it and prints 6, then doubles that and prints 12, and continues until the calculated number is greater than 100. It then stops without printing that number, Each number is separated by a blank, and at the end a newline is printed. There is no extra blank at the end of the line; the newline immediately follows the last number,

    So it should print this, where “␣” represents a single space and “↵” represents the newline. Note there is no trailing space after 96.

    
    3␣6␣12␣24␣48␣96↵
    
    But it does not work. It doesn’t even compile! And the programmer didn’t comment anything. Your job: fix the program. Add comments, and in them identify the compile-time error(s) and the bug(s).

    Call your fixed program three.c.


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 March 29, 2025 at 11:03PM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh