Download The Using Directive-Introduction to Programming-Lab Mannual and more Exercises Computer Programming in PDF only on Docsity!
The Using Directive
C++ programs can be divided in different
namespaces
A namespace is a part of the program in which
certain names are recognized; outside of the
namespace they’re unknown
using namespace std;
says that all the program statements that follow are
within the std namespace.
How to use constants
You can use constants in you program
Constants are declared by using “const”
const int my_const = 12;
Where 'int' is the data type, 'my_const' is the name
of the constant and 12 is its value
The value of this constant cant be changed
throughout the program
- Enter radius of circle: 0.
- Area is 0.
#define directive
constants can also be specified using the
preprocessor directive #define.
#define PI 3.
Appears at the beginning of your program
The identifier PI will be replaced by the text 3.
throughout the program
you can’t specify the data type of the constant using
#define, which can lead to program bugs
setw manipulator
Changes the field width of output.
You can think of each value displayed by “cout”
as occupying a field: an imaginary box with a
certain width.
The default field is just wide enough to hold the
value e.g integer 543 occupies a three
character wide feild
// demonstrates need for setw manipulator #include using namespace std; int main() { long int pop1=2425785, pop2=47, pop3=9761; cout << “LOCATION” << “POP.” << endl << “Portcity“ <<pop1 << endl << “Hightown“ <<pop2 << endl << “Lowville“ <<pop3 << endl; return 0; }
// demonstrates setw manipulator #include #include // for setw using namespace std; int main() { Long int pop1=2425785, pop2=47, pop3=9761; cout << setw(8) << “LOCATION” << setw(12) << “POPULATION” << endl << setw(8) << “Portcity” << setw(12) << pop1 << endl << setw(8) << “Hightown” << setw(12) << pop2 << endl << setw(8) << “Lowville” << setw(12) << pop3 << endl; return 0; }
Arithmetic Manipulations
Modulus operator
6 % 8 results 6
7 % 8 results 7
8 % 8 results 0
9 % 8 results 1
10 % 8 results 2
Lab Task 2
Assuming there are 7.481 gallons in a cubic
foot, write a program that asks the user to enter
a number of gallons, and then displays the
equivalent in cubic feet.
Lab Task 3
You can convert temperature from degrees
Celsius to degrees Fahrenheit by multiplying by
9/5 and adding 32. Write a program that allows
the user to enter a floating-point number
representing degrees Celsius, and then
displays the corresponding degrees Fahrenheit.
Lab Task 5
If you have two fractions, a/b and c/d Write a program that
encourages the user to enter two fractions, and then displays
their sum in fractional form. (You don’t need to reduce it to
lowest terms.) The interaction with the user might look like this:
Enter first fraction: 1/
Enter second fraction: 2/
Sum = 9/
You can take advantage of the fact that the extraction operator
(>>) can be chained to read in more than one quantity at once:
cin >> a >> dummychar >> b;
Lab Task 6
(^) In the heyday of the British empire, Great Britain used a monetary system based on pounds, shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, and two decimal points, so that, for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Pence is the plural of penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S. dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user’s interaction with the program would be Enter pounds: 7 Enter shillings: 17 Enter pence: 9 Decimal pounds = £7.