Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ixs1124-Problem Solving Algorithm: C++ Fundamentals

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19
At a glance
Powered by AI
The key takeaways are the basic components of a C++ program including comments, preprocessor directives, data types and variable declarations.

The main components of a C++ program structure are introductory comments, preprocessor directives like #include, constant and variable declarations, and executable statements enclosed in curly braces {}.

The basic data types in C++ are integers (int, long int), floating-point numbers (float, double), boolean (bool), and characters (char).

IXS1124-PROBLEM SOLVING

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

This program merely prints some messages on the screen */

#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

– Valid identifier: matrix_no, Total, item10,


length
– Invalid identifier: matrix no, ringgit$,
first.val, 20names
– Chose identifier that reflect the significance
of variable

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

– Variables having the same data type can


be grouped together and declare as a
single declaration statement
int num1, num2, num3;
int num1 = 10, num2=50, num3=7;
10
C++ FUNDAMENTALS

• 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

• Basic data type


− int, long int
− Can represent negative, zero and positive integer
values
− Limit of size of value that can be represented,
depends on the number of bytes of storage
allocated to an int variable by the computer
system and compiler
− Most compilers allocate two bytes for each
integer: range -32768 to +32767 (long
integers for outside range)
− Represented exactly in computer
12
− Example: 123 -56 0 5645
C++ FUNDAMENTALS

• Basic data type


− float, double
− Can represent any real numeric value, both whole
numbers and numbers that require digits after
decimal point
− float – ranges approximately 6 to 7 digits of
precision
− double – approximately 13 decimal digits of
precision
− Example: 16.315, -0.67, 31.567

13
C++ FUNDAMENTALS

• Basic data type


− bool
− Only hold the values true or false
− Known as boolean variables

− 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

/*This program is to calculate the nett salary for workers


based on total hours per week and rate per hour.*/

//get input from user

15
C++ FUNDAMENTALS
• C++ program
• Preprocessing
#include <iostream.h>

• Data type declaration


const double KWSP = 0.05; //rate for KWSP
int worker_no; //worker number
float total_hour; //total hours per week
float rate_hour; //rate per hour
Value float weekly_salary; //weekly salary
unchang
ed

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

• Input and Output Operator


−cin

cin>>worker_no;

19

You might also like