Upload
kevin-ochieng
View
34
Download
0
Embed Size (px)
DESCRIPTION
jfhfjfg
Citation preview
SESSION 1
C++ PROGRAM
C++ is a general purpose programming languages created by Bjarne Strustus of AT&T Bell labs
C++ was added to the C features to support O.O. programming. Object Oriented Programming
is a program technique that uses objects. An Object is a program construction that has data
associated with it and can perform certain actions and the actions that an object can take are
called Methods. C++ has several versions
- STD C++ - Turbo C++ - Borland C++ - Visual C++
Visual C++ is a product of Microsoft corp. and its alike with STD C++ but has added features and takes full advantage of windows O.S.
C++ PROGRAM LAYOUT
1. Comments
Theyre represented by // and though use of /*.*/ is permissible.
Example Of C++ Program
# include //include directive
int main() //beginning of the program
{ //Opening braces
Excutable statements;
:
:
return 0; //End of the program
} //Closing braces
2. Include Directive
It tells the Compiler where to find the information about certain items that are found in the
program. e.g. #include or
using namespace std; #include , #include
3. Header File
It includes input and output streams e.g iostream.h or math.h or string.h
NB:
io denotes C++ input and output
Example of C++ Input and Output Program
// The program outputs the word HELLO!
#include
int main()
{
cout as input operators.
The name cout refers the output stream that library iostream associated with programs standard output device usually the screen.
An output stream is a destination to which output is sent as a stream of characters.
5. Input operator
Cin is our input operator and its the name that iostream library associates with standard input i.e the keyboard the operators are called Extraction Operators because they extract variable
characters from storage of a data value.
VARIABLES & ASSIGNMENT IN C++
The name of a variable is called an identifier.
- An identifier must start with either a letter, an underscore, symbol or all characters that follow
must be letters, digit or an underscore symbol e.g rate, rate2, rate1
- C++ is case sensitive e.g rate, Rate and RATE are 3 distinct variable.
- C++ identifiers can be of any length e.g. rate_of_interest.
- C++ has special class of identifies called keywords or reserved words and therefore cant be used as identifies eg. Double, long, float etc.
1. Variable Declaration
Before we declare a variable, we write the variable type or data type.
Syntax
variable_type variable1, variable2,., variable n;
Example
int x,y,z;
double a,b,c;
char l,j,k;
Therefore 2 natural places to declare a variable
(1) Just as its used
(2) At the start of the main function
Example
//This prgm multipys 2 nos
#include
int main( )
{
int a,b,c;
cout a >> b;
c = a * b;
cout
Escape sequence
The back slash presiding a character tells the compiler that the sequence following the
backslash doesnt have the same meaning as the character itself.
syntax.
\n - Newline
\t - Horizontal tab. See table below
Example
//calculate the total weight of tins in the carton
#include
int main()
{
int no_of_tins;
double weight, total_weight
cout > n
o_of_tins>> weight;
total_weight = no_of_tins * weight;
cout>> The total wt. of the tins is >> total_weight >> endl; return 0;
}
THE SUMMARY OF THE STEPS OF PRODUCING STRUCTURED PROGRAMS
1. Analyze the Problem
This involves specifying the programs output, input, variables, constants and the general steps (tasks) needed to transform the inputs into the outputs.
2. Design the Program using a Design Tool
A programs design is a step-by-step description of how the above steps (in part 1) should be exactly carried out so as to solve the problem. It represents the exact logic of the solution to the
problem. It includes all the appropriate formulae to be used in the solution. The design is also
known as an algorithm.
What are the qualities of a good algorithm?
It should be clear, precise and easy to follow. This is important in avoiding errors, as well as making the design modifiable by future programmers.
A programs design should also show the exact logic of the program. For the calculations that need to be done, it should show the appropriate sequence of formulae.
It should also be general i.e. not specific to a programming language, such that it can be converted into a program using any programming language.
Two most used tools for designing a program are pseudo codes and flowcharts diagrams.
(a) Pseudo Codes are English-like statements that look similar to many procedural-programming languages.
e.g.
Input x
area = length * Width
(b) Flowchart Symbols are symbols used to design a program. They are a diagrammatic way of representing the programs logic. There are many flowchart symbols used in the design of programs.
Symbols Meaning
Begin, End
Process (e.g. calculations)
Input, Output
Shows logic flow
Decision making (Test condition inside. If true,
follow the down-ward arrow else follow the right-
side arrow).
Page connectors. Used to connect diagrams to next
page.
3. Code the program
The algorithm produced in step (2.) above is then converted into actual program (code) using an
appropriate programming language e.g. C++. The actual statements of the program are also
known as the code of the program.
4. Compile (or convert it into a machine language program), Test and run
5. Maintain the program
This involves making changes to improve the functionality of the program (e.g. to improve on
the program), to remove possible previously undetected errors, to cater for newly discovered user
needs, or to cater for changed technology.
EXAMPLE
Design a program to compute the area and the circumference of a circle.
SOLUTION
(a) Analysis of the problem
(i) Outputs Variables
The area of a circle Area
The circumference of a circle Circumference
(ii) Inputs
The radius of a circle Radius
(iii) Tasks
Input the radius
Compute the area
Compute the circumference
Output the area and the circumference
Constant
The pi of a circle pi = 3.14
(b) The Programs Design (i) Pseudo code
Begin
pi=3.14
Input Radius
Area=pi*Radius*Radius
Circumference=2*pi*Radius
Output Area, Circumference
End
(ii) Flowchart Diagrams
(iii) Code of the program
#include
//Declare a constant for pi=3.14
const PI=3.14; // #define pi 3.14; is allowed.
int main()
{
//Declare variable to hold the radius, area, circumference
double Radius, Area, Circumference;
//Input the radius
coutRadius;
//Compute the area and the circumference
Area=PI*Radius*Radius;
Circumference=2*PI*Radius;
//Outputs area, circumference
cout
ASSIGNMENTS
1. Write a simple C++ program to output even numbers between 2 12.
2. Write a short program that declares and initializes double variables one, two, three, four and five to the values 1.000, 1.414, 1.732, 2.000 and 2.236 respectively. Then it writes an
output table that generates the following:
N Square Root
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236
3. Write a program that will prompt you to give the number of pods and the number of beans per pod, and calculates the total number of beans.