chapter 2 intro to pc

12
1 By: EN MOHD HATTA BIN HJ MOHAMED ALI 1. Deitel (2007) . “C How To Program.” Pearson 2. Adrian & Kathie Kingsley Hughes (2005) Beginning Programming”. Wiley Publishing, Inc. 3. from Wikipedia website, free encyclopedia.

Upload: jaa-idris

Post on 18-Feb-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2 intro to pc

1

By: EN MOHD HATTA BIN HJ MOHAMED ALI

1. Deitel (2007) . “C How To Program.” Pearson

2. Adrian & Kathie Kingsley Hughes

(2005) “Beginning Programming”. Wiley

Publishing, Inc.

3. from Wikipedia website, free

encyclopedia.

Page 2: Chapter 2 intro to pc

2

C /C++ Programming Language

• C++ is an extension of C

• Support procedure-oriented and object-oriented programming

approach

• Invented by Bjarne Stroustrup in 1980

HOW COMPUTER RUN A PROGRAM

Edit : Source code ( type the program)

Compile : If no syntax errors -> Object Code

Link : Link to library function -> exe module

Run : Output ( Check for any semantic errors)

Source Code

Preprocessor

Modified Source Code

Compiler

Object Code

Linker

Executable Code

Page 3: Chapter 2 intro to pc

3

# include <header files>

# define P 100

/ / A typical C++ Program

main ( )

{

Variables declaration

…….. ;

…….. ;

Statements;

……... ;

}

Preprocessor Directive

Comment

main function

End

Begin

C / C++ Program

Page 4: Chapter 2 intro to pc

4

Manipulator ( # include <iomanip.h>)

1. “\n” : Advances the cursor to the beginning of the next line

e.g cout << “Mummy The Return\n\n”;

cout << “The Sixth Sense << endl;

Output: Mummy The Return

The Sixth Sense

2. 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. cout << “Mummy\tThe\tReturn\n\n”

Output: Mummy The Return

Manipulator

Page 5: Chapter 2 intro to pc

5

4. setw (n) : sets the stream’s internal field width variable to n. This setting

remains

in effect only for the next insertion.

e.g cout << setw(10) << 567 << setw(30) << "Pearl Harbour";

Output ^^^^^^^567^^^^^^^^^^^^^^^^^Pearl Harbour

5. setfill (‘?’) : This parameterized manipulator sets the stream’s fill character. The

default is a space. This setting remains in effect until the next

change.

e.g cout << setfill (‘*’) setw(5) << 567 << setw(20) << “Pearl

Harbour”

Output **567*******Pearl Harbour

Manipulator (Cont..)

Page 6: Chapter 2 intro to pc

6

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

}

A Simple Program

Output:

Welcome to KUTKM

MELAKA BANDARAYA BERSEJARAH

Page 7: Chapter 2 intro to pc

7

Welcome to

KUTKM

MELAKA

BANDARAYA BERSEJARAH

Exercise 1:

Write a complete program to print the following output:

Exercise 1

Page 8: Chapter 2 intro to pc

8

#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:

WELCOME TO UTM$$$$$$$$$$$$GOOd luck to ALL

100

SELAMAT MAJU JAYA

Example

Page 9: Chapter 2 intro to pc

9

Exercise 2:

Write a complete program to print the following output:-

SARJANA MUDA KEJURUTERAAN ELEKTRONIK

KOLEJ UNIVERSITI TEKNIKAL

******************MELAKA*******************

Exercise 2

Page 10: Chapter 2 intro to pc

10

#include <iostream.h>

#include <iomanip.h>

void main()

{

cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK" << endl;

cout << " KOLEJ UNIVERSITI TEKNIKAL\n\n";

cout << "*****************MELAKA****************";

}

1

2

#include <iostream.h>

#include <iomanip.h>

void main()

{

cout << "SARJANA MUDA KEJURUTERAAN ELEKTRONIK\n" ;

cout << " KOLEJ UNIVERSITI TEKNIKAL";

cout << “\n\n****************MELAKA****************";

}

Exercise 2 Solution

Page 11: Chapter 2 intro to pc

11

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

}

3

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

}

4

Exercise 2 Solution (cont..)

Page 12: Chapter 2 intro to pc

12

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.

Exercise 3