Professional Documents
Culture Documents
Chapter 2 Intro To PC
Chapter 2 Intro To PC
Program. Pearson
2.
Programming. Wiley
3.
Publishing, Inc.
from Wikipedia website, free
encyclopedia.
Preprocessor
Modified
Source Code
Object Code
Linker
Executable Code
Compiler
Edit
Compile
Link
Run
C / C++ Program
# include <header files>
# define P 100
/ / A typical C++ Program
Begin
End
main ( )
{
Variables declaration
.. ;
.. ;
Statements;
... ;
}
Preprocessor Directive
Comment
main function
Manipulator
Manipulator
1.
\n
( # include <iomanip.h>)
Output:
endl
: Advances the cursor to the beginning of the next line. Forces any data
remaining in the archive buffer to be written to the file.
Same as \n
3.
\t
: Horizontal tab
e.g.
Output:
Mummy The
Return
Manipulator (Cont..)
4.
setw (n)
remains
5.
setfill (?)
e.g
cout << setw(10) << 567 << setw(30) << "Pearl Harbour";
Output
^^^^^^^567^^^^^^^^^^^^^^^^^Pearl
Harbour
e.g
cout << setfill (*) setw(5) << 567 << setw(20) << Pearl
Harbour
Output
**567*******Pearl Harbour
A Simple Program
Simple C++ Program
# include <iostream.h>
# include <iomanip.h>
// Aturcara ini akan mencetak sahaja (Penggunaan arahan cout)
void main ( )
{
cout << Welcome to KUTKM \n;
cout << MELAKA BANDARAYA BERSEJARAH;
}
Output:
Welcome to KUTKM
MELAKA BANDARAYA BERSEJARAH
Exercise 1
Exercise 1:
Write a complete program to print the following output:
Welcome to
KUTKM
MELAKA
BANDARAYA BERSEJARAH
Example
#include <iostream.h>
#include <iomanip.h>
#define P 100
void main()
{
cout << "WELCOME TO UTM" << setfill('$') <<
setw(40);
cout << GOOd luck to ALL\n";
cout << " " <<P <<"\n";
cout << "SELAMAT\tMAJU\tJAYA" <<endl <<endl;
}
Output:
Exercise 2
Exercise 2:
Write a complete program to print the following output:-
******************MELAKA*******************
Exercise 2 Solution
1
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK" << endl;
cout << "
KOLEJ UNIVERSITI TEKNIKAL\n\n";
cout << "*****************MELAKA****************";
}
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n" ;
cout << "
KOLEJ UNIVERSITI TEKNIKAL";
cout << \n\n****************MELAKA****************";
}
10
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n";
cout << setw(30) <<"KOLEJ UNIVERSITI TEKNIKAL\n";
cout << "\n" << setfill('*') << setw(20) <<"MELAKA" << setfill('*') << setw(15) << endl;
}
#include <iostream.h>
#include <iomanip.h>
void main()
{
cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n";
cout << " KOLEJ UNIVERSITI TEKNIKAL\n\n";
cout << setfill('*') << setw(34) <<"MELAKA**************" << endl;
}
11
Exercise 3
Exercise 3:
1. Write a program to print the following output:
**********
**INTRO**
**********
2. Write a program that will print your name and matric number.
Please use the programming instructions you have learned to
produce a creative output.
12