Reading Data with
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.
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>
scanf-example.cto your account, compile it, and run it.
scanf-example.cand write a short description of how it works. In your description, explain why an ampersand (
&) is required for reading the
numbervariable but is not needed for the
scanf-example.cprogram again, except this time enter two numbers (one for the initial number and one for the word). Does the program still work? Why?
Hint: what type does
scanfassign input to in your code?
scanf-example.cprogram, using a phrase as input (e.g. "down the hill"). What is the result?
scanfassigns input to a variable; when assigning input to a string, a blank space is not considered to be part of a string by
scanf-example.cprogram 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
Run the program yet again.
if you are using a 64-bit computer (i.e., any MathLAN
computer), enter the following data:
if you are using a 32-bit computer, enter the following data:
- if you are using a 64-bit computer (i.e., any MathLAN computer), enter the following data:
Write a short program that asks for your name, stores your name
scanf, then prints the word "hello" and your name.
Reading Multiple Values
Each of the following programs reads two numbers
scanf, using different format variations.
scanfassigns the given values to the variables.
Reading Values within Applications
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 centimetersThat 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.
Reading and Comparing Strings
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.
strcmpwill be helpful.
- Test whether the program works as intended by entering words that match, and words that do not match.
- Enter words which are identical, except for capitalization (for example, "apple" versus "apPle"). What happens? Why do you get this result?
- Modify your program so that it makes all the letters in a word the same case, and rerun your tests.
- Write a short program that connects to the robot, asks for beep length and pitch, beeps for the assigned length and pitch, and disconnects from the robot.
- Modify the program you wrote in the previous exercise so the program continues, prompting for input and beeping, until the time entered is 0.
- Now modify the program you have written to count the number of beeps, and when the time entered is 0, print the number of beeps before exiting the program.
Write a program that reads the radius of a circle and prints the
circle's area and circumference in the format illustrated below:
radius area circumference 2.5 19.63 15.71That 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.
Write a program that reads the coefficients a, b, c of a
quadratic equation: a x2 + b x + c = 0, and prints
the roots of the equation to two decimal places.
- Use the quadratic formula
- For simplicity, you may assume that b2 – 4 a c ≥ 0.
C's square root function is called
sqrtand is defined in C's
To write a C program that uses the
math.hlibrary, your source code must "include" it,
and your compile command must explicitly link the library, using the flag
-lm. For example,
cc -Wall -lm -o quadratic quadratic.c