10/6/00 down to 9/18/00
Due Monday 9/18
1. Write a program that works as shown in the sample output below. This program must be
handwritten
and ready to be handed in. (You are encouraged to validate your
solution program on your computer.)
| Finding the area of a circle given its circumference By Anna Enter circumference: 18.84 The area of the circle is: 28.26 |
Use 3.14 for p in both programming exercises and use this
test data (or your own) to validate your
program solutions.
| Circumference | Area |
| 0.0 | 0.0 |
| 6.28 | 3.14 |
| 18.84 | 28.26 |
| 25.12 | 50.24 |
| 12.56 | 12.56 |
2. Write a program that works as shown in the sample output below. This program may be
printed out
from your computer and need not be handwritten.
| Finding the circumference of a circle given its area By Anna Enter area: 50.24 The circumference of the circle is: 25.12 |
1. You will not be asked to program, but you will be asked questions about program
code.
2. You are responsible for the program development cycle and other concepts covered in
class.
3. You are responsible for the arithmetic operators, the sqrt and pow
functions, the float and double types.
4. Emacs: You are NOT responsible for block, window, searching, and replacing-text
commands.
This assignment requires the use of either one of the two functions
protyped below. To use either of these standard C++ math functions, include the math.h
header file. The given examples should suffice. This program must be handwritten and ready to be handed
in. (You are encouraged to validate your solution program on your computer.)
double floor(int x);
// returns value of x rounded down to next integer
// EXAMPLE: floor(23.9) equals 23.0, floor(-23.9) equals -24.0, and
floor(23.0) equals 23.0
double ceil(int x);
// returns value of x rounded up to next integer
// EXAMPLE: ceil(23.1) equals 24.0 and ceil(-23.9) equals -23.0
Given the functions prototyped above, write a program that works as shown in the sample
output below.
| Rounding to nearest integer, nearest 10th, and nearest 100th By Anna Enter a non-negative number with 3 or more decimal places: 79.538 To nearest integer: 80 |
In this assignment, use the properties of the int data
type to find the minimum number of total coins in quarters,
dimes, nickels, and pennies that produce the specified amount of money as shown in the
sample output below. Make
sure you use a cin statement to get the $ amount from the
keyboard. Hint: in addition to the int and double data types,
consider using int(), %, int division properties to facilitate your solution. If you get
stuck, try completing the program
started below.
| Finding the minimum number of total coins to produce the given $ amount By Anna Enter dollar amount: 2.74 (2.74 comes from the keyboard, NOT from an assignment statement.) Number of quarters: 10 Number of dimes: 2 Number of nickels: 0 Number of pennies: 4 $ in quarters: 2.5 $ in dimes: 0.2 $ in nickels: 0 $ in cents: 0.04 Total = 2.74 |
#include <iostream.h>
#include <math.h>
int main() {
double amount;;
int quarters, dimes, nickels, cents, totalCents;
cout << "Finding the minimum number of total coins to produce the given
$ amount" << endl
<< "By Anna"
<< endl << endl;
cout << "Enter dollar amount: ";
cin >> amount;
totalCents = int(100 * amount);
quarters = totalCents / 25;
totalCents = totalCents - 25 * quarters;
//--------------------------------
// continue from here
...
...
//---------------------------------
return 0;
}
1. Finish the minimum number of coins problem: it can be handwritten or
typed. See if you can find an
appropriate way to exploit the % operator.
2. On a separate sheet of paper, make all relevant corrections of your work on the exam.
Make it collectible.
1. The specifications of the quadratic formula program are given in the
sample output below.
Use this test data (or work up your own) to validate your solution
program. Submit handwritten
solution on Monday.
1. 2x2 - x - 6 = 0 {2, -1.5}
2. x2 - 100 = 0 {10, -10}
3. x2 -7x = 0 {0, 7}
4. x2 - 2 = 0 {1.41421, -1.41421}
5. 4x2 + 20x + 25 = 0 {-2.5, -2.5}
6. x2 + 1 = 0 What do you suppose will happen
when you enter this on your system?
| Solving ax^2 + bx + c = 0 By Pat Enter integral coefficients a, b, and c Enter a: 2 Enter b: -1 Enter c: -6 x1 = 2 x2 = -1.5 |
2. Run the short program below on the system on which you compile and run your C++
programs and report
the output. (The program, error.cpp, can be downloaded
from the /usr/local/MCX1/jaye directory.)
// FILE: error.cpp
// PURPOSE: demonstrate results of undefined arithmetic operations
// on the system on which this
program is run
#include <iostream.h> #include <math.h>
int main() {
double x(7.0), y(0.0), z;
cout << endl << endl;
cout << "Testing undefined numerical operations" << endl << endl;
z = sqrt(-4.0); cout << "sqrt(-4.0) = " << z << endl; z = x/y; cout << "When y is 0.0, x/y = " << z << endl; z = x/0.0; cout << "x/0.0 = " << z << endl; double amount(2.69); cout << "When amount is 2.69, int(100 * amount) = " << int(100 * amount) << endl; z = pow(-8, 1.0/3); cout << "pow(-8, 1.0/3) = " << z << endl << endl;
return 0; }
Topics include--but are not limited to--the following:
1. the development cycle (again)
a. filling out the diagram
b. identifying syntax and run-time errors (and distinguishing them from
each other)
2. properties of int and floating point data types and order of operations
3. tracing the execution of a program and determining the values of the variables
upon completion of the program (see question 5 from Quest 2A)
4. completing short program or program fragments given sample output
and additional specifications, where necessary
5. The test is cumulative.
6. Much of the material we covered can be found in Hubbard sections
2.1, 2.4-2.6, 2.8-2.10,
and 2.12.
NOT ON TEST: the marine biology case study!!
1. First read Hubbard, section 2.4 (very short). Then turn to Appendix A (p. 342) and
find the ASCII codes
for the newline character, '\n', the tab character, '\t', and the
vertical tab character, '\v'. Hint: they're at the
beginning of the table.
a. Use your browser to copy and paste the program given below into your
editor. Then assign the appropriate
numerical codes to the variables to newlineCode,
horizTabCode, and vertTabCode, and run the program.
Note that the codes fail to affect the output
in the intended fashion.
b. Find a way to convert the second occurrence (in
red below) of the variables to newlineCode, horizTabCode,
and vertTabCode in the cout
statements in which they are employed to enable the codes to have their
intended affect on the output. (Don't use
symbolic values '\n', '\t', '\v': use the codes.) Handwritten
solution
for the three relevant cout
statements. (Sorry Reid, but I don't think this will be as long as War and Peace.)
#include <iostream.h>
int main() {
int newlineCode, horizTabCode, vertTabCode;
newlineCode = 125; horizTabCode = 125; vertTabCode = 125; //
125 is wrong code! Replace it
cout << "The code for the newline character is: " <<
newlineCode << newlineCode
<< " Is this a new line?"
<< endl;
cout << "The code for the tab character is: " << horizTabCode
<< horizTabCode
<< " Is this text
tabbed?" << endl;
cout << "The code for vertical tab character is: " <<
vertTabCode << vertTabCode
<< " Is this text vertically
tabbed?" << endl;
return 0;
}
2. Use the table of ASCII codes on p.342 to find a general way to complete the program
fragment below.
Assume that a lower case character is always entered and assign the
corresponding upper case value entered
from the keyboard to the variable chUpper. Hint: look for
patterns in the table!!! Your solution must be
handwritten. (About
the length: see apology above.)
char chLower, chUpper;
cout << "Enter a lower case character: ";
cin >> chLower;
| // Your code below: |
cout << "Upper case " << chLower << " = " << chUpper << endl;
1. Read Hubbard 2.10 (reviews promotion and explicit conversion or type casting)
and 2.11--2.13 (new
material).
2. You will need to download the very helpful summary sheet of apstring
class at the College
Board's AP CS web site. Follow A or B below for instructions. (A is
recommended)
A. This will provide you with a summary of
all six of the AP classes
1. Click on Summary
2. Print the page.
B. This will limit the output to the apstring
class. You need the Adobe Acrobat Reader for this.
1. Click on: Appendix
2. From the left-side panel, select
Appendix D
3. Print the 1st page in Appendix
D, page 30.
3. Make sure apstring.h and apstring.cpp are in the directory or
folder where you compile or are where your C++
programming system can find them. Copy the test program below with your
browser. (Highlight the text with your
mouse, select Edit from your browser's top menu bar, and
select the Copy command. Then paste the text into
your editor.) Then compile, link, and run the program to test
your system's setup for the AP class files. If you run
into problems building the program, see if it's a linking error.
#include <iostream.h>
#include "apstring.h"
int main() {
apstring title("This is a program to test the apstring
class files"), st;
cout << "\n\n\n\n\n" << title
<< endl << endl;
st = "Hi Pat";
cout << st << endl;
cout << st << " contains " <<
st.length() -1 << " letters and one space character." << endl;
return 0;
}
4. OPTIONAL: For additional background, go to to these links of Skylight
Publishing's AP CS web site
and read the relevant material. Of particular interest (don't be
put off if a lot of it sounds like Greek) are:
1. What are
the AP classes?
2. Why do
we need AP classes? Just read this.
3. What
do I have to do to use the apstring class?
1. Complete the short swapping program started below. The challenge is to execute the
swap with two rather than
three variables. (Use your mouse to select the text, use the Edit entry
on your browser's top menu bar and choose
the Copy command. Then Paste the text into your program editor---if you
use GNU's CD-ROM for Windows, for
example, use the Paste command in NT-Emacs' Edit menu.)
#include <iostream.h>
int main() {
double x, y;
cout << "Swapping values in x and y\n\n";
cout << "Enter x: "; cin >> x;
cout << "Enter y: "; cin >> y;
//------------------------------------------------------------
// Your code:
//------------------------------------------------------------
cout << "\nNow x = " << x << " and y =
" << y << endl;
return 0;
}
1.Read Hubbard 9.4 (C++ strings) for background on apstring class, which is a modified
subset of the
C++ string class.
2. Use your browser to copy the short program below into the editor you use for your C++
programs. Replace
the assignments to apstring objects st1 and st2, "Replace
this.", with values that produce this output:
| "Hello" he said. I said "Bye" sadly. The new line character is '\n'. |
#include <iostream.h>
#include "apstring.h"
int main() {
apstring st1, st2;
st1 = "Replace this";
st2 = "Replace this";
cout << "\n\n\n\n\n" << "st1 = " << st1
<< endl
<< "st2 = "
<< st2 << endl;
return 0;
}
1. Use your browser to copy the short program below into the editor you use for your
C++ programs. Using
apstring member functions length(), substr(), and find(), and the
concatenation operator, +, fill in the missing
expressions in a manner that enables the program to realize the sample
output shown below in the most
general way. In other words, your solutions should NOT be depend
on the fact that the apstring object in
this example represents "ABCDE", since your solutions must
account for strings of different length!!
1. You may assume that the apstring object contains at least 3
characters.
2. Try to replace "?" with a single (although complicated)
expression, if possible. This probably
requires using the concatenation operator and
more than one call to apstring member functions.
SAMPLE OUTPUT st = ABCDE |
#include <iostream.h>
#include "apstring.h"
int main() {
apstring st("ABCDE"), t, u, v;
cout << "\n\n\n\n" << "st = " << st
<< endl;
// 1. Replace "?" with and expression that assigns t the value
represented
// by st WITHOUT the leading character and the last
character
t = "?";
cout << "1. Without st's lead and last characters, t = " << t
<< endl;
// 2. Replace "?" with an expression that assigns u the value of st
// with the lead and last character interchanged
u = "?";
cout << "2. With st's lead and last characters swapped, u = "
<< u << endl;
// 3. Replace "?" with an expression that assigns u the value of
st
// without its "middle" character. Let
numerical value of middle be defined as st.length()/2
v = "?";
cout << "3. Without st's middle character, v = " << v << endl;
return 0;
}
1. Read Hubbard 3.1-3.5 (Don't worry too much about scope, introduced in 3.5. We'll
review scope soon)
2. Review Questions 3.1-3.3 on p. 49.
3. Use your browser to copy the program below into your C++ editor. Study the given sample
output frames
below and complete the program in a manner that enables it to
realize the given output. Handwritten
Remember: You may only use SIMPLE (not compound)
conditions in your decision block.
Hint: You might want postpone getting the
program to work for 0 until it works for the 1st three cases.
Am I a perfect square integer? |
| Am I a perfect square integer? Enter me: -100 ERROR: number can't be negative. |
| Am I a perfect square integer? Enter me: 64 64 IS a perfect square. |
| Am I a perfect square integer? Enter me: 0 0 IS a perfect square. |
// FILE: psquare.cpp #include <iostream.h> #include <math.h>
int main() {
int n;
cout << "\n\n\n\n\n" << "Am I a perfect square integer?" << endl << endl;
cout << "Enter me: "; cin >> n;
//-------------------------------------------------
//YOUR CODE:
//-------------------------------------------------
return 0;
}
1. Read Hubbard 3.6-3.8
2. Review Questions 3.5, 3.8, 3.9 on p. 49.
3. Use your browser to copy the program below into your C++ editor. Study the given sample
output frames
below and complete the program in a manner that enables it to
realize the given output. In other words
replace the two dummy conditions in the decision block with
conditions that work!! Handwritten or
printed
Specifications:
1. The order in which the lengths are entered shouldn't
affect the answer. Hint: the conditions
you need will be very
long.
2. In this version, assume that the entered lengths are
valid. (We'll remove this assumption for Monday)
3. Best version: test for acuteness first, obtuseness
second, and use else for rightness. If you can't solve
it this way, use else for
acutenesss.
4. Test data (in addition to the sample output): (a,
b, c) = type:
(13, 12, 6) is acute, (12, 6, 13)
is acute, (13, 5, 12) is right, (12, 13, 5) is right, (12, 5, 13) is right
Classifying triangle ABC by angle |
| Classifying triangle ABC by angle Enter sides a b c (spaces between the numbers): 14 5 12 Triangle is obtuse |
| Classifying triangle ABC by angle Enter sides a b c (spaces between the numbers): 12 5 14 Triangle is obtuse |
| Classifying triangle ABC by angle Enter sides a b c (spaces between the numbers): 6 13 12 Triangle is acute |
// FILE: triangle1.cpp // PURPOSE: Use compound conditions to classify ABC: acute, obtuse, right
#include <iostream.h>
int main() {
int a, b, c;
cout << "\n\n\n\n\n\t\t" << "Classifying triangle ABC by angle" << endl
<< "\t\t" << "Given sides a, b, c form a triangle" << endl << endl;
cout << "Enter sides a b c (spaces between numbers): ";
cin >> a >> b >> c;
cout << endl;
if ( 1 )
cout << "Triangle is acute\n\n";
else if ( 0 )
cout << "Triangle is obtuse\n\n";
else //
cout << "Triangle is right\n\n";
return 0; }