Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.

Slides:



Advertisements
Similar presentations
Getting started with MPLAB Launch MPLAB Set Toolbar as in the next slide by clicking the leftmost icon Open a new Source file by choosing [FILE][NEW] Type.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
COSC 120 Computer Programming
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Computer Science 1620 Loops.
An Introduction to Python – Part II Dr. Nancy Warter-Perez June 15, 2005.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Longest Common Subsequence (LCS) - Scoring Dr. Nancy Warter-Perez June 25, 2003.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Computer Science 1620 Programming & Problem Solving.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
Basic Elements of C++ Chapter 2.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
Chapter 02 (Part III) Introduction to C++ Programming.
A Review of C++ Dr. Nancy Warter-Perez June 16, 2003.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Fundamental Programming: Fundamental Programming Introduction to C++
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
CS101 Computer Programming I Chapter 4 Extra Examples.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Debugging Visual Basic.NET Programs ► ► Use debugging tools ► ► Set breakpoints and correct mistakes. ► ► Use a Watch and Local window to examine variables.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ Classes and Data Structures Jeffrey S. Childs
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 CS161 Introduction to Computer Science Topic #8.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE202: Lecture 5The Ohio State University1 Selection Structures.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Completing the Problem-Solving Process
Basic Elements of C++.
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Computer Programming I
Basic Elements of C++ Chapter 2.
Chapter 6 The while Statement
Tonga Institute of Higher Education
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Presentation transcript:

Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003

6/16/03Review of C++2 Overview Programming workshop #1 solution Data Structures 2-D Arrays String Object Debugging Workshop #2

6/16/03Review of C++3 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/03Review of C++4 Programming Workshop #1 - Solution #include using namespace std; void main () { char aa_in, aa, flag; double hydro[25] = {1.8,0,2.5,-3.5,-3.5,2.8,-0.4,-3.2,4.5,0,-3.9,3.8,1.9,-3.5,0,-1.6,-3.5,-4.5,- 0.8,0.7,0,4.2,-0.9,0,-1.3}; cout << "This program will compute the hydrophobicity of an amino acid.\n" << endl; do { cout << "Please enter the amino acid: "<< flush; cin >> aa_in; // convert to upper case if necessary if((aa_in >= 'a') && (aa_in <= 'z')) aa = aa_in - 32;

6/16/03Review of C++5 Programming Workshop #1 – Solution (2) // check if valid (not required) if(((aa - 'A' >= 0) && (aa - 'A' <= 24)) && (aa != 'B') && (aa != 'J') && (aa != 'O') && (aa != 'U') && (aa != 'X')) cout << "\nThe hydrophobicity of “; cout << aa_in << " is " << hydro[aa - 'A'] << endl << endl; else cout << "\nError: " << aa_in; cout << " is not a valid amino acid.\n" << endl; cout << "Would you like to lookup another amino acid? Enter Y/N: "; cin >> flag; } while ((flag == 'y') || (flag == 'Y')); }

6/16/03Review of C++6 2-D Arrays int nums[2][3] = {{2,4,6},{-9,-7,-5}}; nums[0][0] == 2 nums[0][1] == 4 nums[0][2] == 6 nums[1][0] == -9 nums[1][1] == -7 nums[1][2] == [0] [1] [0] [1] [2]

6/16/03Review of C++7 Nested For Loops The following nested for loop will print the 2-D array as shown in the previous slide: int i, j, nums[2][3] = {{2,4,6},{-9,-7,-5}}; for(i=0; i < 2; i++) for(j = 0; j < 3; j++) cout << "nums[i][j] == " << nums[i][j] << endl;

6/16/03Review of C++8 Class String Preprocessor and namespace directives #include using namespace std; To declare a string object string seq; Useful string functions seq.c_str() and seq.data() return a pointer to the initial element of a char array whose elements are a copy of the string being represented. seq.size() – returns the length of the string. string1 == string2 – will test for equality of two strings.

6/16/03Review of C++9 Example Revisited: 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++10 Example Revisited: Amino Acid Search (2) // This program will find the number of occurrences of an amino acid in a sequence. // Written By: Prof. Warter-Perez // Date Created: April 16, 2002 // Last Modified: #include using namespace std; void main () { string seq; char aa; int count = 0, i;

6/16/03Review of C++11 Example Revisited: Amino Acid Search (3) cout << "Please enter a sequence:" << flush; cin >> seq; cout << "Enter the amino acid for comparison: "<< flush; cin >> aa; for (i = 0; i < seq.size(); i++) { if (seq.data()[i] == aa) count++; } if(count == 1) cout << "There was 1 occurence "; else cout << "There were " << count << " occurrences "; cout << "of amino acid " << aa << " in sequence " << seq << "." << endl; }

6/16/03Review of C++12 Debugging Definition: Bug – A mistake in your program that can be one of the following Compile-time error Syntax error – program will not compile with syntax errors (fairly easy to fix) Run-time error (when program is executing) Typos – program not entered correctly, though syntax is correct (usually relatively easy to fix) Logical error – the logic of the program/algorithm is inconsistent with the problem (much harder to find/fix)

6/16/03Review of C++13 Debugging (2) Key Idea: You must understand what your program is supposed to do in order to debug it Before writing the code, create an example that can be easily verified by hand Poor Man’s Debugger: Print statements of program variables (cout) – allow you to see the contents of the variables as the program executes

6/16/03Review of C++14 Debugging (3) Using a debugger it is possible to do the following: Step through the program instruction by instruction (trace the program) Stepping over functions F10 Stepping into functionsF11 Stepping out of functions Shift-F11 (good if you accidentally stepped into a function) As you step through the program, you can look at the contents of variablesand track their changes. If you expect a variable to change to a specific value and it does not – you have found your bug!

6/16/03Review of C++15 Debugging (4) Sometimes stepping through a program is too time consuming. In this case, set breakpoints and run (go) until you reach the next breakpoint In Visual C++ Debugger – breakpoints are set by putting cursor on the line where you want to stop and clicking on the hand (or F9). You should see a stop sign on the left scroll bar. To remove a breakpoint, repeat the process and the stop sign should go away.

6/16/03Review of C++16 Debugging (5) To start debugging or continue to the next breakpoint, click on the Go button (text with a down arrow next to it) or use F5 To stop debugging, click on the Stop Debugging button (text with down arrow and red x) or use Shift-F5 While there are many other functions available in the debugger, these few key concepts are a good start

6/16/03Review of C++17 Debugging (6) Convert the nested for loop example into a simple C++ program Debug this simple program tracing the values of i j nums[i][j], as they are displayed

6/16/03Review of C++18 Other debugging and development tips 1. Write your program in small incremental steps (functions) and test each step thoroughly 2. In case you don’t follow tip 1: if there are many errors (compile-time or run-time), comment out large portions of the code and test incrementally

6/16/03Review of C++19 Programming Workshop #2 Write a sliding window program to compute the hydrophobicity of an amino acid sequence. The program should prompt the user for The amino acid sequence The window size (assume the window increment is 1) Use the Kyte and Doolittle scale from programming workshop #1. Obtain the SWISSPROT entry of bacteriorhodopsin and compute the hydrophobicity. Plot your result in EXCEL and compare your plot to the one given in yesterday afternoon’s workshop.

6/16/03Review of C++20 Extension to Programming Workshop #2 (optional) Modify your program to compute the %GC in a sequence of nucleotides. The program should prompt the user for The DNA sequence The window size (assume the window increment is 1) Test your program using the data for yesterday afternoon’s workshop.