1 / 20

Data Structures and Debugging

Data Structures and Debugging. Dr. Nancy Warter-Perez June 18, 2003. Overview. Programming workshop #1 solution Data Structures 2-D Arrays String Object Debugging Workshop #2. Programming Workshop #1. Write a C++ program to compute the hydrophobicity of an amino acid.

noelle
Download Presentation

Data Structures and Debugging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Overview • Programming workshop #1 solution • Data Structures • 2-D Arrays • String Object • Debugging • Workshop #2 Review 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 Review of C++

  4. Programming Workshop #1 - Solution #include<iostream> 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; Review 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')); } Review 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] == -5 [0] [1] [2] 2 4 6 [0] [1] -9 -7 -5 Review 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; Review of C++

  8. Class String • Preprocessor and namespace directives • #include<string> • 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. Review 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) Review 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<string> #include<iostream> using namespacestd; voidmain () { string seq; charaa; intcount = 0, i; Review 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; } Review 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) Review 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 Review 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 functions F11 • 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 variables and track their changes. • If you expect a variable to change to a specific value and it does not – you have found your bug! Review 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. Review 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 Review 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 Review of C++

  18. Other debugging and development tips • Write your program in small incremental steps (functions) and test each step thoroughly • 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 Review 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. Review 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. Review of C++

More Related