Вы находитесь на странице: 1из 9

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER

PROBLEM SOLVING

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 1


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

 Understand basic program components


 Define and use the identifier, variable, constant and
statement
 Understand the standard data type(int, float, double, char,
string, const)
 Understand the use of mathematical predefined function
(sqrt(), abs(), pow() etc.)
 Use the arithmetic operator (+, -, *, /, %)
 Understand the assignment statement (=)
 Use the input and output statement, formatted output
 Write simple programs

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 2


INPUT/OUTPUT STATEMENT
 Input/output operation is fundamental to any programming
language.
 In C++ cin and cout are used to input data and to output the
data.

cin >> is used to get data from keyboard


cout << is used to send output to the screen

 Preprocessor directive #include <iostream.h> must be used in order


to handle the input (cin>>) and output (cout <<) statement.

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 3


INPUT/OUTPUT STATEMENT
Example of Input Statement

TASK USAGE/SAMPLE INPUT DATA


int num1;
To input numeric data type float num2;
cin >> num1 >> num2; //input: 10 5.5

To input 2 character data char letter, symbol;


types cin >> letter >> symbol; //input: A #

To input string string customerName; //input: Mohd Ali


getline(cin,customerName);

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 4


INPUT/OUTPUT STATEMENT
Example of Output Statement

TASK USAGE/SAMPLE INPUT DATA


To display labels: cout << “Enter 2 numbers: ”;
Enter 2 numbers:

To display results from an cout << 15 * 2;


arithmetic expression. cout << sum /count;
To display labels Total is: and a
int total = 10;
value stored in variable total.
cout << “Total is: ” << total;
Complete output is Total is:10
To display formatted output and cout << “\n\n”;
predefined symbol, endl cout << endl;

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 5


FORMATTING NUMERIC OUTPUT
 It is important that the numerical results are presented attractively
and formatted correctly. In C++, there are a number of stream
manipulators that can be used commonly for that purpose, as
illustrated in the following table.
 To used stream manipulators, the iomanip.h header file must be
included as part of the program. This is accomplished by the
preprocessor command #include <iomanip.h>.

Manipulators Action
setw (n) Set the field width to n
Set the decimal point precision to n
setprecision(n)
places
Display the number in conventional
setiosflags(ios::fixed)
fixed‐point decimal notation

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 6


SETTING DECIMAL POINT

using namespace std;


#include <iomanip>
#include <iostream>

int main()
{ float num=88.3333;
cout<<setiosflags(ios::fixed) <<setprecision(1)<<num;

return 0;
}
Output:
88.3

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 7


SET WIDTH

using namespace std;


#include <iomanip>
#include <iostream>

int main()
{ float num=8.3333;
cout<<setw(10)<<num;

return 0;
}
Output:
8.3333

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 8


END OF CHAPTER 2

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 9

Вам также может понравиться