You are on page 1of 14

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0006L
(COMPUTER PROGRAMMING 1)

MACHINE PROBLEM

4
BASIC INPUT OUTPUT STATEMENT

Student Name / Group


Name:
Name Role
Members (if Group):

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]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


 Select and apply appropriate program constructs in developing computer programs. [CLO: 2]
 Develop, test and debug computer programs based on a given specification using the fundamental
programming components. [CLO: 3]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Know the importance of input/output statement and apply in the creation of C++ programs
 Enumerate and use mathematical library functions in solving problems.
 Create C++ programs using assignment operators and formatting output techniques.

IV. BACKGROUND INFORMATION

A. Input/Output Statement

a. Standard output (cout)


On most program environments, the standard output by default is the screen, and the C++
stream object defined to access it is cout.

 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

 Multiple insertion operations (<<) may be chained in a single statement:


cout << "This " << " is a " << "single C++ statement";
cout << "I am " << age << " years old and my zipcode is " << zipcode;

CCS0003L-Computer Programming 1 Page 2 of


14
 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.";

This produces the following output:


First sentence.
Second sentence.
Third sentence.

 Alternatively, the endl manipulator can also be used to break lines. For example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;

This would print:


First sentence.
Second sentence.

b. Standard input (cin)

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

CCS0003L-Computer Programming 1 Page 3 of


14
C++ provides various mathematical functions that aid in mathematical calculations.

C++ Mathematical Functions List

Function Description Example

The ceil() function returns the


ceil(1.03) gives 2.0 ceil (-
ceil smallest integer represented as a
1.03) gives -1.0
double not less than num

The cos() function returns the cos(val)


cos cosine of arg. (val is a double type
The value of arg must be in radians identifier)

The exp() function returns the


exp(2.0) gives the value of
exp natural logarithm e raised to the arg
e2
power

The floor() function returns the


floor(1.03) gives 1.0
floor largest integer (represented by
floor(-1.03) gives -2.0
double) not greater than num

The pow() function returns base


pow(3.0, 0) gives value of
raised to exp power i.e., base exp.
30
pow A domain error occurs if base = 0
pow(4.0, 2.0) gives value
and exp <= 0. Also if base < 0 and
of 42
exp is not integer.

The sin() function returns the sin of sin(val)


sin arg. (val is a double type
The value of arg must be in radians identifier)

The sqrt() function returns the


sqrt square root of num. sqrt(81.0) gives 9.0
If num < 0, domain error occurs

The tan() function returns the


tan tangent of arg. tan(val)
The value of arg must be in radians

C. FORMATTING OUTPUT

CCS0003L-Computer Programming 1 Page 4 of


14
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>

The setw() Manipulator

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;

The output will be (each underscore represents a blank space):

_ _ _ _ _ _ 2 2
_ _ _ _ 4 4 4 4
_ _ 6 6 6 6 6 6

CCS0003L-Computer Programming 1 Page 5 of


14
C++ Formatting Output Example

Here are some example program demonstrating, how to format the output screen in C++

/* C++ Formatting Output - The setw() Manipulator */

#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 is the sample run of the above C++ program:

Here another type of C++ program, also demonstrating, output formatting in C++

/* C++ Formatting Output - The setw() Manipulator */

CCS0003L-Computer Programming 1 Page 6 of


14
#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();
}

Here is the sample run of this C++ program:

The setprecision() manipulator

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;

CCS0003L-Computer Programming 1 Page 7 of


14
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;

generates the following output on the screen (notice no rounding):

12.34567

Additional IOS flags

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

left left-justify the output

right right-justify the output

displays decimal point and trailing


zeros for all floating point numbers,
showpoint
even if the decimal places are not
needed

CCS0003L-Computer Programming 1 Page 8 of


14
display the "e" in E-notation as "E"
uppercase
rather than "e"

display a leading plus sign before


showpos
positive values

display floating point numbers in


scientific
scientific ("E") notation

display floating point numbers in


fixed normal notation - no trailing zeroes
and no scientific notation

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

ACTIVITY 4.1: You are Right!

Write a program that solves for the hypotenuse of a right triangle. The entry of data should accept sides
with decimal portions.

Program: (save as [surname_4_1.cpp])

CCS0003L-Computer Programming 1 Page 9 of


14
Output:(screenshot of the output)

ACTIVITY 4.2: Simple Payroll

Create a program that generates monthly payroll of the employees. The specifications are as follows:

 Employee Information (Input)


- Payroll period (date)
- Employee ID
- Employee name
- Monthly salary
- Lates and absences (in minutes)
Note: For lates and absences, convert the minutes to hours by dividing it by 60 minutes.
Hourly rate of the employee can be computed by dividing the monthly salary by 30 then divide
the result by 8 hours per day.
 Deduction
- Lates and absences (amount)

CCS0003L-Computer Programming 1 Page 10


of 14
- Philhealth employee contribution (constant value of 1000)
- Pag-ibig employee contribution - (constant value of 800)
- SSS employee contribution (constant value of 1,200)
- Tax (12% of the Monthly Salary)

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.

FEU – Institute of Technology

Employee ID: 201401234 Payroll Period: January 1-31, 2019


Employee Name: Annie Batumbakal

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

Program: (save as [surname_4_2.cpp])

CCS0003L-Computer Programming 1 Page 11


of 14
Output:(screenshot of the output)

ACTIVITY 4.3: Cos You’re my Angle

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

Program: (save as [surname_4_3.cpp])

CCS0003L-Computer Programming 1 Page 12


of 14
Output:(screenshot of the output)

VI. QUESTION AND ANSWER

Directions: Briefly answer the questions below.

 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

CCS0003L-Computer Programming 1 Page 13


of 14
 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

CCS0003L-Computer Programming 1 Page 14


of 14

You might also like