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

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
True or false A variable of type char can hold the value 301. ( F )
COSC 120 Computer Programming
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Computer Science 1620 Programming & Problem Solving.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
An Introduction to Python Dr. Nancy Warter-Perez April 15, 2004.
An Introduction to Python and Its Use in Bioinformatics Dr. Nancy Warter-Perez.
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Beginning C++ Through Game Programming, Second Edition
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Chapter 05 (Part III) Control Statements: Part II.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
An Introduction to Python and Its Use in Bioinformatics
Computing Fundamentals
Basic Elements of C++.
Basics (Variables, Assignments, I/O)
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
2.6 The if/else Selection Structure
Programming Introduction to C++.
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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

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

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

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

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

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;

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;

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

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

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

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

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

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

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

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

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

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] =

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

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!

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)

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!

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

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)

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;

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; }

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

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)

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

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

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

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