Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "A Review of C++ Dr. Nancy Warter-Perez June 16, 2003."— Presentation transcript:

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

2 6/16/03Review 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

3 6/16/03Review 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!)

4 6/16/03Review of C++4 Program Development Problem specification Algorithm design Test by hand Code in target language Test code / debug Program Problem solving Implementation

5 6/16/03Review of C++5 C++ Basics - Comments C++ // line comment /* multi-line comment */ Header comments // Description of program // Written by: // Date created: // Last Modified:

6 6/16/03Review 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

7 6/16/03Review 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;

8 6/16/03Review 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;

9 6/16/03Review 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

10 6/16/03Review 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 = 8 x = 2 x = 15 x = 1 x = 2 OperatorsExample

11 6/16/03Review of C++11 C++ Basics – Auto Increment and Decrement Pre-increment/decrement y = ++ x;equivalent tox = x + 1; y = x; y = --x;equivalent tox = x – 1; y = x; Post-increment/decrement y = x++;equivalent toy = x; x = x + 1; y = x--;equivalent to y = x; x = x – 1; x = 3 x = 4 y = 4 x = 2 y = 2 y = 3 x = 4 y = 3 x = 2

12 6/16/03Review 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

13 6/16/03Review of C++13 C++ Basics – Relational Operators Assume x is 1, y is 4, z = 14 ExpressionValueInterpretation x < y + z1True y == 2 * x + 30False z <= x + y0False z > x1True x != y1True

14 6/16/03Review of C++14 C++ Basics – Logical Operators Assume x is 1, y is 4, z = 14 ExpressionValueInterpretation x<=1 && y==30False x<= 1 || y==31True !(x > 1)1True !x > 10False !(x<=1 || y==3)0False

15 6/16/03Review of C++15 if Statement if (expression) action Example: char a1 = 'A', a2 = 'C'; int match = 0; if (a1 == a2) { match++; }

16 6/16/03Review of C++16 if-else Statement if ( expression ) action 1 else 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

17 6/16/03Review 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

18 6/16/03Review 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 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

19 6/16/03Review of C++19 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

20 6/16/03Review of C++20 while Statement while (expression) action Note: skipping do while Example int x = 0; while(x != 3) { x = x + 1; } Iteration 1: x=0+1=1 Iteration 2: x=1+1=2 Iteration 3: x=2+1=3 Iteration 4: don’t exec / 2 Infinite loop!

21 6/16/03Review 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)

22 6/16/03Review 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!

23 6/16/03Review of C++23 I/O Streams Usage Must include iostream header file #include There are ways to format the output to specify parameters such as the width of a field, the precision, and the output data type

24 6/16/03Review 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)

25 6/16/03Review 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 using namespace std; #define MAX 42 void main () { // Ring Finger protein 1 char seq[MAX] = "CPICLDMLKNTMTTKECLHRFCSDCIVTALRSGNKECPTC"; char aa; int count = 0, i;

26 6/16/03Review 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; }

27 6/16/03Review 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

28 6/16/03Review 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)

29 6/16/03Review 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

30 6/16/03Review 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

31 6/16/03Review 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

32 6/16/03Review of C++32 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


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

Similar presentations


Ads by Google