Professional Documents
Culture Documents
CCS0006L
(COMPUTER PROGRAMMING 1)
MACHINE PROBLEM
4
BASIC INPUT OUTPUT STATEMENT
3-E
Section:
Professor: Mr. Jabez Mendoza
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
To insert a line break, a new-line character shall be inserted at the exact position
the line should be broken. In C++, a new-line character can be specified as \n
(i.e., a backslash character followed by a lowercase n). For example:
cout << "First sentence.\n";
cout << "Second sentence.\nThird sentence.";
Alternatively, the endl manipulator can also be used to break lines. For example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
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:
B. Mathematical Functions
C. FORMATTING OUTPUT
Formatting output in C++, is important in the development of the output screen, which
can be easily read and understood. C++ offers the programmer several input/output
manipulators. Two of these (widely used) I/O manipulators are:
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
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();
}
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
void main()
{
clrscr();
int i;
long int num;
cout<<"Enter a number: ";
cin>>num;
cout<<"\nMultiplying (by 5) and printing the number 10 times with 3
columns:\n";
for(i=0; i<10; i++)
{
cout<<num<<setw(25)<<num<<setw(25)<<num<<"\n";
num = num * 5;
}
getch();
}
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;
will print the following output to the screen (notice the rounding) :
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;
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);
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
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?
ANS. I think we need to format the presentation of numbers in the output
so that we can read it clearly and we can understand it.
What do you think is the importance of having a readily available mathematics functions?
ANS. The importance of having a readily available mathematics function is it makes
our work easier. Because we just need to analyze or pick the mathematics function that we
need to use.
VII. REFERENCES
Abraham (2015). Coding for dummies. John Wiley and Sons: Hoboken, NJ
Zak, D (2015). An Introduction to Programming with C++. 8th Edition
Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th Edition).Sams
Publishing
McGrath, M. (2017). C++ programming in easy steps (5th ed.). Warwickshire, United
Kingdom: Easy Steps Limited
Tale, T. (2016). C++: The Ultimate Beginners Guide to C++ Programing. CreateSpace
Independent Publishing Platform