Complete 2 of the first three programs and email ONLY the cpp files to your instructor.
In each program include in the header your name and student number.
1.1 Create a program that obtains the following values from the user:
a) an integer number
b) a floating point number
c) a string up to 20 characters long
The program should display the values entered using the following formats:
a) the integer number should be displayed as a decimal, hexadecimal, and octal
value, left justified within a field, the width of which is 6.
b) the floating point number should be displayed with a +/- sign and two digits
after the decimal point using the fixed decimal point and scientific notation.
c) the string should be displayed right justified in a field, the width of which is 25;
unused spaces within the field should be filled with a $ character.
Use the information on the PDF: "Format flags in ios".
1.2 Design a named namespace that contains three variables used to store three
sides of a box, as well as a function that computes and prints the volume of a
box. Design an unnamed namespace, which contains two functions that compare
three sides of a box and display the smallest and largest side, respectively.
Create a program that uses all members defined in both namespaces.
1.3 Design a program that draws a rectangle based on the user's input. The user
should enter the length and width of a rectangle, as well as characters that will
be used to draw the borders of the rectangle and to fill the rectangle,
respectively. A sample run of the program is shown in Figure 1.11.
Enter length and width => 10 7
Enter border and fill characters => C +
Figure 1.11 Sample run of Program Project 1.3.
Extra Programs
It is strongly recommended that you do the following program for yourself. There will
be other programs in the course using these circuits.
1.4 Design a program that can be used as the impedance calculator. The program
should prompt the user first to enter the values of the resistance R, inductance
(L), capacitance C, frequency (f), and then display the menu options shown in
Figure 1.12.
Compute the impedance for the following circuits or
press 5 to exit=>
1. RC
2. RL
3. LC
4. RLC
5. Exit
Figure 1.12 Menu options in Program Project 1.4.
If the user selects an option 1, 2, 3 or 4, the program should call an appropriate
function to compute and display the magnitude and the phase angle of the
impedance. Please note that a different function is required for each circuit - i.e.,
menu option. After the impedance is displayed, the program should loop back to
the main menu until the Exit option is selected to terminate the program.
It is required to use the C++ header files, C++ I/0 and formatting, and C++
constants. Global variable declarations are not permitted.
Note: For the RLC circuit, the magnitude of impedance, Z and the phase angle, ö
are given by:
Think about the phase angle for an LC circuit. The arrow is
the impedance, Z. When R is 0, along which axis does Z
point, vertical or horizontal? Which direction on the axis
depends on which is larger, XC or XL
What are the range of angles ö can have on the diagram?
What math function can you use for tan-1() and what range of
angles does it return?
X
C = -1/ùC XL = ùL
Namespace program (extra for practice)
Create a program which calculates the equivalent resistance of two resistors, either in
parallel or series.
The program uses two namespaces called Parallel and Series. Each namespace has
float variables R1, R2 and equiv_R (the equivalent resistance). Each has a Calculate
function which finds and prints the equivalent resistance.
The prototype is void Calculate ()
In the program the user can choose either parallel or series, then ask for the two
resistor values and find and print the equivalent resistance.
Next ask the user for the voltage applied to the resistor pair and calculate and print
the current using the equivalent resistance by a function:
void Amps (float R, float V)
This is a common function, it is not in a namespace.
Here is a sample output...
This program finds the equivalent resistance of 2 resistors
Select (1) parallel or (2) series resistance >> 1
Enter R1 R2 >> 450 230
Equivalent parallel resistance: 152.206 ohm
Enter voltage >> 5
current: 0.033 amps
Note the output is set to 3 decimals
Questions
1. How would your code change if you added the directive
using namespace Series;
2. Could your program have both directives:
using namespace Series;
using namespace Parallel;
Explain why or why not.
3. Research what functions, objects, variables,.. are declared in the default
namespace std
1. Wherever use the elements of the Series namespace, would label as
Series:: R1, Series::R2, Series:: equiv_R and Series:: Calculate().
When add the directive: using namespace Series;
then would leave out the label Series::
2. Could not have both directives because statements like
cin >> Parallel::R1 >> Parallel::R2;
cin >> Series::R1 >> Series::R2;
would become:
cin >> R1 >> R2; // for Parallel
cin >> R1 >> R2; // for Series
This is called a "name collision", as two variables have the same name.