introducing c++ elements. csce 1062 outline main algorithms’ constructs general form of a c++...
Post on 14-Dec-2015
218 Views
Preview:
TRANSCRIPT
CSCE 106 2
Outline
Main algorithms’ constructs General form of a C++ program {section
2.5} C++ language elements {section 2.1} Executable statements {section 2.4} Reserved words and symbols {section
2.2} Data types {section 2.3}
CSCE 106 3
Main Algorithms’ Constucts An algorithm is written as a step-by-step
procedure (sequence) in which choices can be made where necessary (selection), and all or part of the algorithm can be repeated (repetition).
Thus, the basic control structures of algorithms are:1- Sequence2- Selection 3- Repetition
CSCE 106 4
SequenceAn algorithm is based on the
notion of sequence, which is the ordering of steps/statements.
Step n cannot be started until step n-1 is complete.
CSCE 106 5
General Form of a C++ Program// File: filename// Program description: ….
#include directivesusing namespace std;
void main(){
Variables declaration section
Executable statements section}
CSCE 106 6
General Form of a C++ Program (cont’d)
Function (a collection of related statements) is the basic unit in C++
A C++ program must contain a main function
void main ()void - function returns no valuemain - lower case followed by (){ } - braces define the function body
CSCE 106 7
General Form of a C++ Program (cont’d)
General form of function body partsDeclaration statements
Variables and constantsExecutable statements
C++ statements
CSCE 106 8
Comments Comments make a program easier to
understand They are ignored (i.e. not translated) by the
compiler // used to signify a comment on a single line /* Text text */ used for comments on
multiple lines
CSCE 106 9
Compiler Directives #include
Compiler directiveProcessed during compilation process Instructs on what you want in the program
#include <iostream>Adds library class/file called iostream to
programUsed with < >Also “ “ user defined
CSCE 106 12
<iostream>Included in iostream
cout refers to the standard output device; i.e. the screen
cout << "Hello!"; << output operator
(insertion operator) cin refers to the standard input
device; i.e. the keyboardcin >> N1 >> N2;
>> input operator (extraction operator) directs input to variable
CSCE 106 13
Executable Statements
cout displays output on the screencout << “Enter the distance in miles: ”;
cin gets input from the keyboardcin >> miles;
Assignmentkms = KM_PER_MILES * miles;
CSCE 106 14
Reserved Words and Symbols Reserved words have special meanings
Can NOT be used for other purposes (const, float and void are some examples)
Special symbols / delimitersC++ has rules for special symbols
= * ; { } ( ) // << >> [ ] , + -
CSCE 106 15
Data Types Predefined data types
int (integer)Positive or negative whole number1000 12 199 100000The size of an int depends on the machine and the
compiler. On a PC it is usually a word (16 bits).Other integers types are:
• short: uses less bits (usually a byte)• long: typically uses more bits (usually 2 words)
CSCE 106 16
Data Types (cont’d) float (floating point / real number)
Positive or negative decimal number 10.5 1.2 100.02 99.88 Integer part and fraction part The number 108.1517 breaks down into the following
parts• 108 - integer part• 1517 - fractional part
Other floating-point data types are:• double• long double
bool (boolean) true false
CSCE 106 17
Data Types (cont’d)
char (character)Represents a characterIndividual character value (letter or
number)Character literal enclosed in single quotes
‘A’Characters are encoded using a scheme
where an integer represents a particular character
CSCE 106 18
Exercise
ProblemAnalyse, design (using a flow chart), and implement (using C++) an algorithm that calculates, and outputs the sum (sum) of three numbers (n1, n2 & n3) input by the user.
Analysis Input
float n1: first variablefloat n2: second variablefloat n3: third variable
Outputfloat sum: sum of n1, n2, & n3
CSCE 106 19
Exercise (cont’d)
INPUTn1, n2, n3
OUTPUTsum
START
STOP
sum = n1 + n2 + n3
Design
Implementation (C++)#include <iostream>using namespace std;void main ()
{float n1, n2, n3, sum;
cout << “Please input three numbers”;cin >> n1 >> n2 >> n3;
sum = n1 + n2 +n3;
cout << “The sum is” << sum;
}
Proc
essin
g
CSCE 106 20
Addition.cpp/* FILE: Addition.cpp PROGRAM: Adds three numbers input by the user */
#include <iostream>using namespace std;
void main (){ float n1, n2, n3, sum; // declaring variables
cout << “Please input three numbers”; cin >> n1 >> n2 >> n3; // inputting 3 variables sum = n1 + n2 + n3; // adding the 3 variables
cout << “The sum is:” << sum; // outputting sum}
top related