32
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003

A Review of C++ Dr. Nancy Warter-Perez June 16, 2003

Embed Size (px)

Citation preview

A Review of C++

Dr. Nancy Warter-PerezJune 16, 2003

6/16/03 Review of C++ 2

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++ 3

Compilers & Development Software

In this program, we’ll use Visual C++ Can use any C++ development

software but only source files workspaces and projects are not portable

Do your work on either the hard disk or zip disk (not floppy disk, A: drive – too slow!)

6/16/03 Review of C++ 4

Program DevelopmentProblem specification

Algorithm design

Test by hand

Code in target language

Test code / debug

Program

Problem solving

Implementation

6/16/03 Review of C++ 5

C++ Basics - Comments C++

// line comment/* multi-line comment */

Header comments // Description of program// Written by:// Date created:// Last Modified:

6/16/03 Review of C++ 6

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

Convention 1: alignment_sequence Convention 2: AlignmentSequence

6/16/03 Review of C++ 7

C++ Basics – Data Types (1) 3 basic data types

Integer (int) – represent whole numbers long (32-bits same as default), short (16-bits)

System dependent signed (positive and negative, default), unsigned

(positive) Ex 1: define an integer variable y

int y; // initialized to garbage Ex 2: define an unsigned short integer

variable month initialized to 4 (April) unsigned short int month = 4;

6/16/03 Review of C++ 8

C++ Basic – Data Types (2)

Floating point – represent real numbers IEEE Standards

Single-precision (float, 32-bits) Double-precision (double, 64-bits)

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

double score = 4e-3;

6/16/03 Review of C++ 9

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'

char AminoAcid = 'C'; char AminoAcid = 67; // equivalent

6/16/03 Review of C++ 10

C++ Basics – Arithmetic Operators

+ add- subract* multiply/ divide% modulus/remainder

int x, y=5, z=3;x = y + z; x = y – z; x = y * z; x = y / z; x = y % z;

x = 8x = 2x = 15x = 1x = 2

OperatorsExample

6/16/03 Review of C++ 11

C++ Basics – Auto Increment and Decrement

Pre-increment/decrement y = ++ x; equivalent to x = x + 1;

y = x; y = --x; equivalent to x = x – 1;

y = x; Post-increment/decrement

y = x++; equivalent to y = x;x = x + 1;

y = x--; equivalent to y = x;x = x – 1;

x = 3

x = 4y = 4x = 2y = 2

y = 3x = 4y = 3x = 2

6/16/03 Review of C++ 12

C++ Basics – Relational and Logical Operators

Relational operators== equal!= not equal>greater than>= greater

than or equal

<less than<= less than or

equal

Logical operators&& and|| or! not

6/16/03 Review of C++ 13

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++ 14

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++ 15

if Statement if (expression)

actionExample:

char a1 = 'A', a2 = 'C';

int match = 0;if (a1 == a2) {

match++;}

6/16/03 Review of C++ 16

if-else Statement if ( expression )

action 1else

action 2

Example:char a1 = 'A', a2 = 'C';int match = 0, gap =

0;if (a1 == a2) {

match++;} else {

gap++;}

Note: there is also the switch statement

6/16/03 Review of C++ 17

1-D Arrays char amino_acid;

Defines one amino_acid as a character

char sequence[5]; Defines a sequence of 5 elements of type

character (where each element may represent an amino acid)

1 cell

5 cells with indices

0 1 2 3 4

6/16/03 Review of C++ 18

Initializing Arrays char seq [5] = “ACTG”;

float hydro[6] = {-0.2, 0, -0.67, -3.5, 2.8};

No initialization – each cell has “garbage” – unknown value

'A' 'C' 'T' 'G' '\0'

5 cells with values

5 cells with values

seq[0] = 'A'

seq[1] = 'C'

hydro['A' - 'A'] = -.2 hydro['C' - 'A'] = -.67 hydro[5] = 0

-.2 0 -.67 -3.5 2.8 0

6/16/03 Review of C++ 19

for Statementfor( expr1; expr2; expr3

)action

Expr1 – defines initial conditions

Expr2 – tests for continued looping

Expr3 – updates loop

Examplesum = 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++ 20

while Statement

while (expression)action

Note: skipping do while

Exampleint x = 0;while(x != 3) {

x = x + 1;}Iteration 1: x=0+1=1Iteration 2: x=1+1=2Iteration 3: x=2+1=3Iteration 4: don’t exec

/ 2Infinite loop!

6/16/03 Review of C++ 21

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 b c

Then, x = 23, c1 = 'a', c2 = 'b', c3 = 'c'(will ignore white spaces)

6/16/03 Review of C++ 22

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++ 23

I/O Streams Usage Must include iostream header file

#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

6/16/03 Review of C++ 24

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

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)

6/16/03 Review of C++ 25

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++ 26

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++ 27

Steps to Creating a Visual C++ Project (Step 1) To create a project

Under File, select New Under Projects

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

6/16/03 Review of C++ 28

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 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)

6/16/03 Review of C++ 29

Steps to Creating a Visual C++ Project (Step 3) Enter your program in the editor

Notice that the editor has a color coding Comments Key words Everything else

Also notice that it automatically indents Don’t override!! If doesn’t indent to proper location –

indicates bug

6/16/03 Review of C++ 30

Steps to Creating a Visual C++ Project (Step 4) To build your program

Under Build Select Build project_name.exe

In case of compile time errors or warnings, they will be listed in the bottom window (scroll up)

Double click on error or warning to find in program

After fixing error (bug), rebuild following same steps

6/16/03 Review of C++ 31

Steps to Creating a Visual C++ Project (Step 5) To execute your program

First, create any necessary input files Under File, select New

Under Files, select Text File Assign File name and Location (default ok) It is OK to add to project (default) Click OK

To run your program (can click ‘!’ icon, or) Under Build, select Execute project_name.exe

6/16/03 Review of C++ 32

Programming Workshop #1

Write a C++ program to compute the hydrophobicity of an amino acid

Amino Acid Hydrop. VALUEA 1.8C 2.5D -3.5E -3.5F 2.8G -0.4H -3.2I 4.5K -3.9L 3.8M 1.9N -3.5P -1.6Q -3.5R -4.5S -0.8T -0.7V 4.2W -0.9Y -1.3

Program will prompt the user for an amino acid and will display the hydrophobicity

Program should prompt the user to continue