Reading Data with scanf (Lab Report 6)

We explore how a program can read different types of data from an input source like the keyboard. You have already done some programming with scanf(). This lab extends your knowledge.

You will submit this lab for grading. Submit answers and/or code you have written for questions 1 through 6. You may use a signle text file to record your answers for non-coding questions. Please indicate the corresponding question number for each answer to help our graders. You should submit a separate .c file for each of the coding questions. Be sure that the naming of your .c file clearly indicates which problem it corresponds to.

Reminder

Some exercises in this lab utilize strings operations. In such cases, be sure to include the following lines at the beginning of your code:

#include <stdio.h>
#include <string.h>

Understanding scanf

  1. Copy program scanf-example.c to your account, compile it, and run it.
    1. Review scanf-example.c and write a short description of how it works. In your description, explain why an ampersand (&) is required for reading the number variable but is not needed for the input variable.
    2. Run the scanf-example.c program, entering two numbers (one for the initial number and one for the word). Does the program work? Why?

      Hint: what type does scanf assign input to in your code?

    3. Run the scanf-example.c program, using a number for the first input, and then using a phrase as the second input (e.g. "down the hill"). What is the result?

      Recall that scanf assigns input to a variable; when assigning input to a string, a blank space is not considered to be part of a string by scanf.

    4. Run the scanf-example.c program one more time — this time entering two names (one for the initial number and one for the word). Does the program still work? Why?

      Hint: again, consider what types scanf is assigning.

  2. Write a short program that asks for your name, stores your name using scanf, then prints the word "Hello, " and your name.

Reading Multiple Values

  1. Each of the following programs reads two numbers using scanf, using different format variations. Save, read, compile, and run each program with the suggested input variations. For each test case, explain what values are read and why the scanf assigns the given values to the variables.

Reading Values within Applications

  1. Write a program to read a person's height in feet and inches and print the person's height in centimeters (1 inch = 2.54 centimeters). The output of the program should present an equation of the form:
    5 feet 6.2 inches = 168.15 centimeters
    That is, the number of feet should be given as an integer, the number of inches to 1 decimal place, and the number of centimeters to 2 decimal places. One space should separate each number from text or the equal sign.

    There are likely many different ways you could read in the data. Be sure that your instruction prompt to the user is specific enough so that you will always receive the height in the format you expect.

Reading and Comparing Strings

  1. Some programs perform different actions based on the entered information. For instance, programs that change a password often require the user to enter the new password twice to guard against typos. If the input is not the same for both entries, the password is not changed. Write a short program that prompts the user to enter a word, then prompts the user to retype the word. If the input matches, the program should print out the line "The entered word was <word>" (angle brackets not included). If the input does not match, the program should print out a line which includes both entries.

    Hint: the <string.h> function strcmp will be helpful.

    1. Test whether the program works as intended by entering words that match, and words that do not match.
    2. Enter words which are identical, except for capitalization (for example, "apple" versus "apPle"). What happens? Why do you get this result?
    3. Modify your program so that it converts all the letters in a word to the same case, and rerun your tests. To do this, make use of the fact that an uppercase character can be identified via an expression like this: ('A' <= ch && ch <= 'Z'). Think about what you might do if such an expression evaluates to be true (or false).

Extra Practice with scanf

  1. Write a program that reads the radius of a circle and prints the circle's area and circumference in the format similar to that illustrated below:
    radius    area     circumference
    2.5       19.63    15.71
    That is, the radius, area, and circumference should appear under headings, the radius should be printed to 1 decimal place, and the area and circumference to 2 decimal places.

    Do not use the math.h library in your code. Instead, use a macro to define the value of PI.

Grading

Submit your lab to Gradescope. It will be worth 18 points. Each question will receive 0, 1, 2, or 3 points depending upon the completeness and accuracy of your response. For programs, remember to use comments and pre/post conditions for functions that you write (other than main).

Make sure you have:

  1. Identified all authors who contributed to the program
  2. Included an Academic Honesty Statement at the top of the program file that you create for questions 2,4,5,and 6.
  3. Included documentation for all code in questions 2,4,5, and 6.
  4. Included a single .txt file with your answers to the non-coding questions. Make sure the file is well organized for the graders.

You should submit a text file that includes answers to non-coding questions in the lab. For questions that have you create a program, upload that .c file. For EVERY QUESTION, indicate the lab question number associated with that question, program, or code section.