Assignment #1
Due: Thursday, May 25
Objective
This assignment will consist of writing two small programs, each giving
practice with basic I/O (printf/scanf), involving text and integer
variables and operations.
Task
Write the following programs, each in a separate file. Filenames should
be:
(Note that the filenames are all lowercase)
Exercise 1
Write a program (filename rectangle.c) that does the
following:
Prompt the user to type in the length of a rectangle, then the width
(you may assume both of these will be integers). Then compute and print
out the perimeter and the area of the rectangle, as shown in the examples
below. Your output must match mine exactly.
Remember that for a rectangle:
- The perimeter is the sum of all the sides. This can also be
calculated as
(length + width) X 2
- The area is the length times the width
Sample run 1: (the user input is underlined in the example)
Please enter the length of the rectangle: 30
Please enter the width of the rectangle: 23
For a 30 x 23 rectangle:
perimeter = 106
area = 690
Sample run 2:
Please enter the length of the rectangle: 100
Please enter the width of the rectangle: 5
For a 100 x 5 rectangle:
perimeter = 210
area = 500
Exercise 2
Write a program (filename digits.c) that does the following:
Prompt the user and let them enter a 5 digit integer. (You may assume
that the entry will be a 5 digit positive integer value). Break up the
value into the component digits and add the digits together. Print the
digits and the addition result in the format shown below. Your output
must match mine exactly.
Hint: In lecture, we talked about the / and %
operators (quotient and modulus) for integer types, and some of their
uses. These operators are your friends! (And they can help you split a
number up into digits!)
Sample run 1: (user input is underlined in example)
Please enter a 5-digit number: 45678
The sum of the digits:
4 + 5 + 6 + 7 + 8 = 30
Sample run 2:
Please enter a 5-digit number: 97882
The sum of the digits:
9 + 7 + 8 + 8 + 2 = 34
Sample run 3:
Please enter a 5-digit number: 99999
The sum of the digits:
9 + 9 + 9 + 9 + 9 = 45
Requirements for both programs
- No global variables
- All input and output must be done with printf and
scanf, using the library stdio.h
- You may only use the stdio.h library (you should not need
any others for these tasks)
- The programs should compile with the Visual C++ compiler
- When you write source code, it should be readable and
well-documented. Here are some general style guidelines for follow for
this program:
- The body of the main() function should be indented, as in
code examples we've seen
- Use meaningful variable names, so that it is clear what they are
for
- Leave a blank line between variable declarations and executable
statements. This separates the two types of statements and is easier
on the eye. (It's a good idea to separate logical groupings of code
with blank lines, as well. See examples in the notes).
- Use comments appropriately, to make your code clear and understandable.
See the link documentation.txt
for further instructions on this topic. As specified at this link,
make sure your code includes the header information (in a comment
section at the top).
Submitting:
Program submissions should be done through the submission web page, which
is linked from the main course web site. The passwords for the submission
page were distributed in recitation on May 16, and you should have had the
opportunity to practice with the submission page to ensure that you know
how to use it. If you did not attend that class, please e-mail me ASAP to
obtain your password. Do not send program submissions through
e-mail -- e-mail attachments will not be accepted as valid
submissions.
Submit only your source code files. As there are two of them, this will
require filling out the submission page twice. Submit the files:
rectangle.c
digits.c