M4 Technical
M4 Technical
M4 Technical
CCS0006L
(COMPUTER PROGRAMMING 1)
EXERCISE
4
BASIC INPUT OUTPUT STATEMENT
Section:
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
Analyze a complex problem and identify and define the computing requirements appropriate to its solution.
[PO: B]
Design, implement and evaluate computer-based systems or applications to meet desired needs and
requirements. [PO: C]
A. Input/Output Statement
For formatted output operations, cout is used together with the insertion operator, which is
written as << (i.e., two "less than" signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen
Alternatively, the endl manipulator can also be used to break lines. For example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
In most program environments, the standard input by default is the keyboard, and the C++
stream object defined to access it is cin.
For formatted input operations, cin is used together with the extraction operator, which is
written as >> (i.e., two "greater than" signs). This operator is then followed by the variable
where the extracted data is stored. For example:
int age;
cin >> age;
Extractions on cin can also be chained to request more than one datum in a single statement:
cin >> a >> b;
This is equivalent to:
cin >> a;
cin >> b;
B. Mathematical Functions
C. FORMATTING OUTPUT
setw()
setprecision()
In order to use these manipulators, you must include the header file named iomanip.h. Here is an
example, showing how to include this header file in your C++ program.
#include<iomanip.h>
In C++, the setw() manipulators sets the width of the field assigned for the output. It takes the size of
the field (in number of characters) as parameter. Here is an example, this code fragment:
cout<<setw(6)<<"R";
generates the following output on the screen (each underscore represents a blank space).
_ _ _ _ _R
The setw() manipulator does not stick from one cout statement to the next. For example, if you want to
right-justify three numbers within an 8-space field, you will need to repeat setw() for each value, as it
shown below:
cout<<setw(8)<<22<<"\n";
cout<<setw(8)<<4444<<"\n";
cout<<setw(8)<<666666<<endl;
_ _ _ _ _ _ 2 2
_ _ _ _ 4 4 4 4
_ _ 6 6 6 6 6 6
Here are some example program demonstrating, how to format the output screen in C++
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
int i, num;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nTable of "<<num<<" is:\n\n";
for(i=1; i<=10; i++)
{
cout<<num<<setw(3)<<"*"<<setw(4)<<i<<setw(4)<<"="<<setw(4)<<num*i<<"\
n";
}
getch();
}
Here another type of C++ program, also demonstrating, output formatting in C++
In C++, the setprecision() manipulator sets the total number of digits to be displayed when floating-
point numbers are printed. Here is an example, this code fragment:
cout<<setprecision(5)<<123.456;
123.46
The setprecision() manipulator can also be used to set the number of decimal places to be displayed.
In order for setprecision() to accomplish this task, you will have to set an ios flag. The flag is set with
the following statement :
cout.setf(ios::fixed);
Once the flag has been set, the number you pass to setprecision() is the number of decimal places you
want displayed. The following code:
cout.setf(ios::fixed);
cout<<setprecision(5)<<12.345678;
12.34567
In the statement:
cout.setf(ios::fixed);
"fixed" i.e., ios::fixed is referred to as a format option. Other possible format options can be
one of the following :
Format
Meaning
Value
You can remove these options by replacing setf(used with cout, recall cout.setf) with unsetf. For
example, to get 5.8 to display as 5.80, the following lines of code are needed :
// display money
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout<<setprecision(2);
cout<<5.8;
Please note that all the subsequent couts retain the precision set with the last setprecision(). That
means setprecision() is "sticky". Whatever precision you set, sticks with the cout device until such time
as you change it with an additional setprecision() later in the program.
V. LABORATORY ACTIVITY
Write a program that solves for the hypotenuse of a right triangle. The entry of data should accept sides
with decimal portions.
Create a program that generates monthly payroll of the employees. The specifications are as follows:
Payslip Output (Note: The format of the output should look exactly like the one below)
Assuming that the late and absences of the sample employee is 30 mins for the whole month.
INCOME DEDUCTIONS
Monthly Salary Php18000.00 Lates and absences (37.50)
Philhealth (1000.00)
Pag-ibig (800.00)
SSS (1200.00)
Withholding tax (2160.00)
Total Earnings: Php18000.00
Total Deductions: Php 5197.50
Net Pay: 12802.5
Write a program that solves for side c given the two sides a, b and angle C.
Law of Cosines
a2 = b2 + c2 – 2bc cos A
b2 = a2 + c2 – 2ac cos B
c2 = a2 + b2 – 2ab cos C
Do you think we really need to format the presentation of numbers in the output? Why or
Why not?
What do you think is the importance of having a readily available mathematics functions?
VII. REFERENCES
Abraham (2015). Coding for dummies. John Wiley and Sons: Hoboken, NJ
Zak, D (2015). An Introduction to Programming with C++. 8th Edition
RUBRIC:
Criteria 4 3 2 1 Score
A completed
A completed solution is An incomplete
A completed
solution is implemented solution is
solution runs
tested and runs on the required implemented
without errors.
but does not platform, and on the required
It meets all the
meet all the uses the platform. It
specifications
specifications compiler does not
and works for
nd/or work for specified. It compile and/or
all test data.
all test data. runs, but has run.
Solution(x5) logical errors.