POS/370
Programming
Concepts Using C++
Hello again,
I
hope you could get some information from week 1 newsletter. If you read and
understand the topics each week you will have a better understanding of the
course material. Please try to test some sample code from the text everyday.
Problem Solving: First we need problem
specification and then as follows:
1) Analyze the problem:
a- Determine the
output the user needs and how (on paper, screen, or file).
b- Determine the
input.
2) Plan the algorithm: The step by step instructions
that the computer needs to follow to transform the problem’s input into its
output.
3) Desk check the
algorithm.
4) Code the algorithm into a program.
5) Desk check the program: To use sample data for
input values to get the expected output values (manually computation).
6) Evaluate and modify (if necessary) the program
(testing and debugging):
Testing: To run the program with sample data to get
the correct result.
Debugging: To locate and remove any errors.
IPO Chart (Input,
Processing, and Output): It shows the problem’s input, processing, and
output items, as well as the algorithm needed to solve the problem. For example
Input Processing Output
TaxRate Processing items: totalPrice
Price tax
Algorithm:
1- Enter taxRate
2- Calculate tax
by multiplying
3- Calculate
totalPrice by adding
Tax to price
C++ Build Process:
1- Source code: The codes which we enter
the C++ instructions into the computer by using a text editor.
2- Object code: The compiler translates the
source code into machine code (0s and 1s). Machine code is usually called
object code.
3- Linker: It combines the object code with other
machine codes (e.g. that allows the program can communicate with I/O devices)
in order to make an executable file. The Visual C++ environment performs both
(compile and link) as needed when you “build” your project.
Programming Standards:
Documentation: It will help those persons who will use the program (end user) or who may
make changes in it (maintenance).
a- Internal
Documentation:
1- Comment (it
makes the program readable and easier to understand).
// For one line
comment (C++ style)
/* For several lines
comment */ (C style)
2- The choice of
appropriate and descriptive identifiers e.g. saleTax instead st, s, or sTax.
3- Indentation
(to start some lines in the different columns).
4- Line spacing (it is good for readability).
b- External
Documentation:
1- Flowchart
2- Algorithm
3- Pseudocode
4- IPO chart
5- A printout of
the program last version
6- User manual for end user (how the program is
to be executed, what input data it requires and what output it produces).
7- User manual
for maintenance (program limitations and more technical details).
Version Control:
1- Who wrote the
program?
2- Name of the
program.
3- Program
version number.
4- Release date
(when it published).
5- Document
control or security control (who has authorization to use it).
6- How the
program can be used with the other software.
Maintenance: Any proposed change to the
existing system must begin with examination of
the impact on the system’s specification of the requirements.
Proposed Changes:
1- To add a new
function to the current system.
2- To change a
function.
3- To add a new
output report.
4- To change the
format of an existing format report.
5- To recode a
module for efficiency.
6- To upgrade the
organization’s computer systems.
7- Incorrect
output for certain input.
8- Changing the
programming language (e.g. from COBOL to C++).
Any such changes must be documented and verified with
the user. A typical system has at least 100,000 lines of statements (larger
systems over 500,00 lines of statements).
Identifiers: They are the names to
represent variables, constants, data types, functions, and labels in our
program. An identifier (name) is a sequence of one or more letters, digits, or
underscores that begins with a letter or underscores. No punctuation characters
or spaces are allowed in the name (Max. 247 characters). The identifier (name)
cannot be a keyword (reserved words)
in page 136 textbook. Names in C++ are case sensitive. We should assign a
descriptive name to each variable and other names in
the program. Most programmers capitalize the first letter in the second and
subsequent words e.g. salesTaxAmount, employeeNumber, and newSalary.
Variables: They are references to a
memory location that holds a value. Each memory location has a 16 or 32 bit
address. We assign a name to each variable instead remembering the address. The
value assigned to the variable may change during the execution of the program.
It is a good idea to initialize variables when we declare them at the beginning
of the program.
Constant: Named constant is a variable
whose value cannot change while the program is running. e.g.
const int AGE = 65;
const char YES = ‘y’;
const float PI = 3.141593;
Declaration: We should declare variables
and constants at the beginning of the program before using them.
Data Types:
Char: One character enclosed in a
single quotation mark. 1
byte (memory required).
Integer: A whole number 54, -176, and 8594
short 2 bytes (-32,768 to 32,767)
int 4 bytes (-2,147,483,648 to
2,147,483,647)
long 4 bytes (-2,147,483,648 to
2,147,483,647)
Floating point
number: A
number with a decimal place. 3.1415
float 4 bytes (+,-3.4e-38 to +,-3.4e38)
double 8 bytes (+,-1.7e-308 to +,-1.7e308)
long double 10 bytes (+,-3.4e-4932 to +,-1.1e4932)
The first bit is for sign of the number positive or
negative.
String: Zero or more characters
enclosed in double quotation marks.
1
byte per character e.g. “Pacific Bell”
In C++, we have two ways to declare string data
type. We use array of characters (C
style).
e.g. char companyName[20] = “Pacific
The size of string is 19 characters plus one null
(\0) at the end.
or
To declare with string
keyword (C++ style) but we must
include the string directive #include<string>
and to delete .h from #include<iostream.h> but instead
we should have
using namespace std;
string coName
= “Pacific
bool: Boolean value (True or
False), we can assign true or false only.
1
byte (true or false)
e.g. bool yesNo = true;
Initialization: It is a good idea to
initialize our variables and named constant at the beginning of our program.
1- short, int, and long variables
are initialized to 0. int sale = 0;
2- float, double, and long double are initialized to 0.0. float
taxRate = 0.0;
3- char variables are initialized to a space enclosed in single
quotation mark. char yesNo = ‘ ‘;
4- string variables are initialized to the empty string “”. string name = “”;
5- bool variables to either true or false. bool yesNo = true;
Data in Internal Memory:
Numeric data: It is represented in
internal memory using the binary (base 2) number system. e.g. 58 = (111010) in base 2 58 = 32 + 16 + 8 + 0 + 2 + 0
Character
data: It is
represented in internal memory using ASCII (American Standard Code Information
Interchange) codes (page 118 textbook).
Character ASCII Binary
C 67 01000011
K 75 01001011
Literal Constants:
Numeric
literal constant is simply a number.
45 3.1415 2.5e8 (2.5 multiply by 10 to power
8) -4790
Character
literal constant is one character in single quotation marks.
‘A’ ‘$’ ‘2’ ‘’
String literal constant is zero or
more characters enclosed in double quotation marks. “Hello” “Enter
your value” “ab89” “”
Type Conversions:
Implicit
conversion:
The compiler will perform an implicit type-cast without being told to do it.
Explicit
conversion (typecasting): We can explicitly convert the data type using an explicit typecast: int
price = 0;
float taxRate = 0.0;
float tax = 0.0;
tax = taxRate * (float) price;
Arithmetic Operators: We may need to perform
calculations in our program. We write an arithmetic expression that contains
one or more arithmetic operators.
++ , -- increment and decrement
( ) parentheses
-
negation
* , / , % multiplication, division, and modulus arithmetic
+ , - addition and subtraction
The
operator % is modulus (a % b is equal to the remainder of the
division of a by b).
e.g. 7 % 2 = 1 11 % 4 = 3 3 % 5 = 0 22 % 11
= 0 2 % 7 = 0
The
order of precedence is from top to bottom. In case of equal precedence it is
from left to right. 8
/ 4 * 3 – 5 % 2 = 5
There
is a big difference between pre-increment and post-increment:
In
pre-increment, first it increments I value by one then it makes the process
(addition).
I = 10;
J = ++I + 5; //
This is pre-increment. j is 16 and I is 11
In
post-increment, first it makes the process (addition I + 5) then it increments
I by one.
I = 10;
J = I++ + 5; //
This is post-increment. j is 15 and I is 11
Main Statement Types: A statement is a C++
instruction that causes the computer to perform some action after it is
executed. All C++ statements must end with a semicolon.
1- Declaration
statement
2- Assignment
statement
3- Input / Output
statement
4- Conditional
statement
5- Repetition
statement
We
had sample programs in the class as follows: Example 1
// This program converts Fahrenheit degree to Celsius degree
Temperature
#include
<iostream.h>
#include<iomanip.h>
// this is for input/output manipulation (we can use setprecision(3))
void main( )
{
float f =
0.0; // f is for Fahrenhiet
degree
float c =
0.0; // c is for Celsius degree
cout<<"Enter the value for Fahrenheit:
"<<endl;
cin>>f;
c = ((float)5.0
/ (float)9.0) *(f – (float)32.0); //float typecasting for 5.0, 9.0, 32.0
cout<<"The celsius
value is "<<setprecision(3)<<c<<endl;//
three digits only for c
}
Example
2: The first way to declare string data type
// This program tests the string data type using array of
characters (C style)
#include<iostream.h>
void main( )
{
char coName[20] =
""; // 19 characters for company name and one \0 null
cout<<"Please enter the company name:
"<<endl;
// we can have spaces between the characters in
company name if we use:
cin.getline(coName,20,'t');
// It reads the characters and stops after character t
// cin>>coName; if we use this one we cannot have spaces between
characters
cout<<"The company name is :
"<<coName<<endl;
}
Example
3: The second way to declare string data type
// This program tests the string data type using string keyword (C++ style)
#include<iostream>
/* please notice that we cannot have .h
after iostream here
because we are
using the using namespace std; above
void main() */
#include<string>
using namespace std;
void main( )
{
string coName = "";
string name
= "Steven";
cout<<"The student name is
"<<name<<endl;
cout<<"Enter the company Name:
"<<endl;
// cin>>coName; // if we use this one we cannot have spaces in the
company name
getline(cin,coName); // we can have spaces in the company name
cout<<"Name of company is "<<coName<<endl;
}
Please
run these programs again and check for errors.
Have
a nice weekend
Esmaail M Nikjeh