C++Review_P1

Download Report

Transcript C++Review_P1

A Review of C++
Dr. Nancy Warter-Perez
June 16, 2003
Overview


Overview of program development
Review of C++





Basics
1-D Arrays and loops
I/O streams
Example amino acid search program
Programming Workshop #1
6/16/03
Review of C++
2
Compilers & Development Software

In this program, we’ll use Visual C++

Can use any C++ development software
but only source files


6/16/03
workspaces and projects are not portable
Do your work on either the hard disk or zip
disk (not floppy disk, A: drive – too slow!)
Review of C++
3
Program Development
Problem
solving
Problem specification
Algorithm design
Test by hand
Implementation
Code in target language
Test code / debug
Program
6/16/03
Review of C++
4
C++ Basics - Comments

C++
// line comment
/* multi-line comment */

Header comments
//
//
//
//
6/16/03
Description of program
Written by:
Date created:
Last Modified:
Review of C++
5
C++ Basics - Variables


Variables have a data type and name or
identifier
Identifiers

Have the following restrictions:




Must start with a letter or underscore (_)
Must consist of only letters, numbers or underscore
Must not be a keyword
Have the following conventions:


All uppercase letters are used for constants
Variable names are meaningful – thus, often multi-word


6/16/03
Convention 1: alignment_sequence
Convention 2: AlignmentSequence
Review of C++
6
C++ Basics – Data Types (1)

3 basic data types

Integer (int) – represent whole numbers

long (32-bits same as default), short (16-bits)



int y; // initialized to garbage
Ex 2: define an unsigned short integer variable
month initialized to 4 (April)

6/16/03
signed (positive and negative, default), unsigned
(positive)
Ex 1: define an integer variable y


System dependent
unsigned short int month = 4;
Review of C++
7
C++ Basic – Data Types (2)

Floating point – represent real numbers

IEEE Standards



Ex 1: define a single-precision floating-point
variable named error_rate and initialize to 3.5


float error_rate = 3.5;
Ex 2: define a double-precision floating-point
variable named score and initialize it to .004
using scientific notation

6/16/03
Single-precision (float, 32-bits)
Double-precision (double, 64-bits)
double score = 4e-3;
Review of C++
8
C++ Basic – Data Types (3)

Character – represent text




ASCII – American Standard Code for Information
Interchange
Represents characters, numbers, punctuation, spacing
and special non-printable control characters
Example ASCII codes: 'A' = 65, 'B' = 66, … 'a' = 97, 'b'
= 98, '\n' = 10
Ex 1: define a character named AminoAcid and initialize
it to 'C'


6/16/03
char AminoAcid = 'C';
char AminoAcid = 67;
Review of C++
// equivalent
9
C++ Basics – Arithmetic Operators
Operators
Example
int x, y=5, z=3;
+ add
x = y + z; x = 8
- subract
x = y – z; x = 2
* multiply
x = y * z; x = 15
/ divide
x = y / z; x = 1
% modulus/remainder x = y % z; x = 2
6/16/03
Review of C++
10
C++ Basics – Auto Increment
and Decrement
x=3


Pre-increment/decrement

y = ++ x;
equivalent to

y = --x;
equivalent to
x
y
x
y
=
=
=
=
x + 1; x
y
x;
x – 1; x
y
x;
=
=
=
=
4
4
2
2
y
x
y
x
=
=
=
=
y
x;
x + 1; x
y
x;
x – 1; x
=
=
=
=
3
4
3
2
Post-increment/decrement

y = x++;
equivalent to

y = x--;
equivalent to
6/16/03
Review of C++
11
C++ Basics – Relational and
Logical Operators

Relational operators
==
!=
>
>=
<
<=
6/16/03

equal
not equal
greater than
greater than or
equal
less than
less than or equal
Review of C++
Logical operators
&&
||
!
and
or
not
12
C++ Basics – Relational
Operators
Assume x is 1, y is 4, z = 14

Expression
Value
Interpretation
x<y+z
1
True
y == 2 * x + 3
0
False
z <= x + y
0
False
z>x
1
True
x != y
1
True
6/16/03
Review of C++
13
C++ Basics – Logical
Operators
Assume x is 1, y is 4, z = 14

Expression
Value
Interpretation
x<=1 && y==3
0
False
x<= 1 || y==3
1
True
!(x > 1)
1
True
!x > 1
0
False
!(x<=1 || y==3) 0
False
6/16/03
Review of C++
14
if Statement

if (expression)
Example:
action
6/16/03
char a1 = 'A', a2 = 'C';
int match = 0;
if (a1 == a2) {
match++;
}
Review of C++
15
if-else Statement

if ( expression )
Example:
action 1
char a1 = 'A', a2 = 'C';
int match = 0, gap = 0;
if (a1 == a2) {
match++;
} else {
gap++;
}
else
action 2
Note: there is also the switch statement
6/16/03
Review of C++
16
1-D Arrays

char amino_acid;

Defines one amino_acid as a character
1 cell

char sequence[5];

Defines a sequence of 5 elements of type
character (where each element may represent an
amino acid)
5 cells with
indices
6/16/03
0
1
2
3
Review of C++
4
17
Initializing Arrays

char seq [5] = “ACTG”;
5 cells with
values
'A' 'C' 'T'
'G' '\0'
seq[0] = 'A'
seq[1] = 'C'
…

float hydro[6] = {-0.2, 0, -0.67, -3.5, 2.8};
5 cells with
values
-.2
0
-.67 -3.5
2.8 0
hydro['A' - 'A'] = -.2 hydro['C' - 'A'] = -.67

6/16/03
hydro[5] = 0
No initialization – each cell has “garbage” – unknown
value
Review of C++
18
for Statement
for( expr1; expr2; expr3 )



action
Expr1 – defines initial
conditions
Expr2 – tests for
continued looping
Expr3 – updates loop
Example
sum = 0;
for(i = 1; i <= 4; i++)
sum = sum + 1;
Iteration 1: sum=0+1=1
Iteration 2: sum=1+2=3
Iteration 3: sum=3+3=6
Iteration 4: sum=6+4=10
6/16/03
Review of C++
19
while Statement
while (expression)
action
Example
int x = 0;
while(x != 3) {
/ 2
x = x + 1;
Infinite loop!
}
Iteration
Iteration
Iteration
Iteration
Note: skipping do while
6/16/03
Review of C++
1:
2:
3:
4:
x=0+1=1
x=1+1=2
x=2+1=3
don’t exec
20
C++ I/O streams - input

Standard I/O input stream: cin

Ex:
int x;
char c1, c2, c3;
cin >> x >> c1 >> c2 >> c3;
If the following input is typed: 23 a
bc
Then, x = 23, c1 = 'a', c2 = 'b', c3 = 'c'
(will ignore white spaces)
6/16/03
Review of C++
21
C++ I/O streams - output

Standard I/O output stream: cout

Ex:
int x = 1;
char c1 = ‘#‘;
cout << “SoCalBSI is " << c1 << x << ‘!’ << endl;
The following output is displayed: SoCalBSI is #1!
6/16/03
Review of C++
22
I/O Streams Usage

Must include iostream header file


6/16/03
#include <iostream>
There are ways to format the output to
specify parameters such as the width of
a field, the precision, and the output
data type
Review of C++
23
Example: Amino Acid Search

Write a program to count the number of
occurrences of an amino acid in a
sequence.

The program should prompt the user for



6/16/03
A sequence of amino acids (seq)
The search amino acid (aa)
The program should display the number of
times the search amino acid (aa) occurred
in the sequence (seq)
Review of C++
24
Example: Amino Acid Search (2)
// This program will find the number of occurrences of an amino acid in a given sequence.
// Written By: Prof. Warter-Perez
// Date Created: April 16, 2002
// Last Modified: June 13, 2003
#include<iostream>
using namespace std;
#define MAX 42
void main () {
// Ring Finger protein 1
char seq[MAX] = "CPICLDMLKNTMTTKECLHRFCSDCIVTALRSGNKECPTC";
char aa;
int count = 0, i;
6/16/03
Review of C++
25
Example: Amino Acid Search (3)
cout << "Enter search amino acid: "<< flush;
cin >> aa;
for (i = 0; i < MAX; i++) {
if (seq[i] == aa)
count++;
}
if(count == 1)
cout << "There was 1 occurrence ";
else
cout << "There were " << count << " occurrences ";
cout << "of amino acid " << aa << " in sequence " << seq << "." << endl;
}
6/16/03
Review of C++
26
Steps to Creating a Visual C++
Project (Step 1)
To create a project


Under File, select New

Under Projects






6/16/03
Select Win32 Console Application
Assign a Project name and Location for your project
Select Create new workspace
When prompted for type of console application,
select empty project
Click Finish
Click OK
Review of C++
27
Steps to Creating a Visual C++
Project (Step 2)

To add a C or C++ source file to the
project

Under File, select New

Under Files




6/16/03
Select C++ Source File
Select Add to project (Project will be set to the
current project)
Assign a File name (use the default location
determined by the project)
Click OK (the source file will be displayed in the
editor window)
Review of C++
28
Steps to Creating a Visual C++
Project (Step 3)

Enter your program in the editor

Notice that the editor has a color coding




Also notice that it automatically indents


6/16/03
Comments
Key words
Everything else
Don’t override!!
If doesn’t indent to proper location – indicates
bug
Review of C++
29
Steps to Creating a Visual C++
Project (Step 4)

To build your program

Under Build


In case of compile time errors or warnings,
they will be listed in the bottom window
(scroll up)


6/16/03
Select Build project_name.exe
Double click on error or warning to find in
program
After fixing error (bug), rebuild following same
steps
Review of C++
30
Steps to Creating a Visual C++
Project (Step 5)

To execute your program

First, create any necessary input files

Under File, select New





To run your program (can click ‘!’ icon, or)

6/16/03
Under Files, select Text File
Assign File name and Location (default ok)
It is OK to add to project (default)
Click OK
Under Build, select Execute project_name.exe
Review of C++
31
Programming Workshop #1

Write a C++ program to compute the hydrophobicity
of an amino acid


Program will prompt
the user for an
amino acid and will
display the
hydrophobicity
Program should
prompt the user to
continue
6/16/03
A m in o A c id
A
C
D
E
F
G
H
I
K
L
M
N
P
Q
R
S
T
V
W
Y
Review of C++
H y d ro p . V A L U E
1 .8
2 .5
- 3 .5
- 3 .5
2 .8
- 0 .4
- 3 .2
4 .5
- 3 .9
3 .8
1 .9
- 3 .5
- 1 .6
- 3 .5
- 4 .5
- 0 .8
- 0 .7
4 .2
- 0 .9
- 1 .3
32