Ixs1124-Problem Solving Algorithm: C++ Fundamentals
Ixs1124-Problem Solving Algorithm: C++ Fundamentals
Ixs1124-Problem Solving Algorithm: C++ Fundamentals
ALGORITHM
C++ FUNDAMENTALS
1
PROGRAM
STRUCTURE
• General format
// Introductory comments
// file name, programmer, when writtten or modified
// what program does
#include <iostream.h>
void main ()
{
constant declarations
variable declarations
executable statements
}
2
PROGRAM
STRUCTURE
• Example
/* Filename : output.cpp
Author : Morshidah Yazid
Date : 12 August 2007
Modified : 13 August 2007
#include <iostream.h>
void main ()
{
cout << “Start of program”<<endl;
cout << “My first C++ program”<<endl;
}
3
C++ FUNDAMENTALS
• Character set
– 92 characters: A – Z, a – z, 0 – 9, *, /, and
etc
• Key words
– Reserved words, can’t be used as identifier
– Given meaning to compiler
– Using lower case
– Example: case, char, else, long, register,
return, if, while, for, int, return, double,
public 4
C++ FUNDAMENTALS
• Punctuator/Separator
− Example: [ ] ( ) { } , ; :
• Operator
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
5
C++ FUNDAMENTALS
• Identifier
– Used to define names to represent
variables
– Formed by combining letters (upper &
lowercase), digits and underscore( _ )
– Rules
– Consist only letters: A – Z, a – z, 0 – 9 or
underscore symbol _
– Start with a letter
– Not a reserved words
– No space between words
– Case sensitive
6
C++ FUNDAMENTALS
7
C++ FUNDAMENTALS
• Variable
– Identifier that refers to memory location,
hold values
– Can only store one value at a time
– The content of variable may change
during program execution
– General form
data_type variable_name ;
int student_id
8
C++ FUNDAMENTALS
• Variable
– Example
int num ;
float cgpa;
double total ;
char grade ;
char name [20];
long datnum ;
9
C++ FUNDAMENTALS
• Variable Declaration
– Example
int num; //declare num as integer variable
int num=10; //declare num as integer variable and sets
the value to 10
• Variable Initialization
– Once a variable is declared, contains no
useful value
– Must be given a value using assignment
statement or input from user
– Example
num=10; //sets value of 10 into variable num
using assignment statement
cin>>number; //using input from user
int num=10; //declare num as integer variable and sets
the value into 10 11
C++ FUNDAMENTALS
13
C++ FUNDAMENTALS
− char
− Represent a single character, a letter, a digit or a
punctuation character
− Occupy one byte, giving 256 different possible
character
− Example: ‘+’, ‘A’, ‘a’, ‘*’, ‘7’
14
C++ FUNDAMENTALS
• C++ program
• Example
• Comments
15
C++ FUNDAMENTALS
• C++ program
• Preprocessing
#include <iostream.h>
16
C++ FUNDAMENTALS
• C++ program
• Equation
weekly_salary = total_hour * rate_hour;
• Selection
if (total_hour > 40)
weekly_salary = (40 * rate_hour) + ( (total_hour - 40)
* (rate_hour * 1.5));
else
weekly_salary = total_hour * rate_hour;
17
C++ FUNDAMENTALS
• Input and Output Operator
− cout
cout<<"\nEnter Worker Number: ";
cout<<"\nWorker Number: "<<worker_no;
− Escape Sequences
Sequence Meaning
\n new line
\t horizontal tab
\b backspace
\r carriage return
\f form feed
\" output a double quote character
\' output a single quote character
\\ backslash
18
C++ FUNDAMENTALS
cin>>worker_no;
19