DataStructuresAndDeb..
Download
Report
Transcript DataStructuresAndDeb..
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
6/16/03
Review of C++
2
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/03
A m in o A c id
A
C
D
E
F
G
H
I
K
L
M
N
P
Q
R
S
T
V
W
Y
H y d ro p . V A L U E
1 .8
2 .5
- 3 .5
- 3 .5
2 .8
- 0 .4
- 3 .2
4 .5
- 3 .9
3 .8
1 .9
- 3 .5
- 1 .6
- 3 .5
- 4 .5
- 0 .8
- 0 .7
4 .2
- 0 .9
- 1 .3
Review of C++
3
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;
6/16/03
Review of C++
4
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/03
Review of C++
5
2-D Arrays
int nums[2][3] = {{2,4,6},{-9,-7,-5}};
nums[0][0]
nums[0][1]
nums[0][2]
nums[1][0]
nums[1][1]
nums[1][2]
6/16/03
==
==
==
==
==
==
2
4
6
-9
-7
-5
[0] [1] [2]
[0]
2
4
[1]
-9
-7 -5
Review of C++
6
6
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/03
Review of C++
7
Class String
Preprocessor and namespace directives
To declare a string object
#include<string>
using namespace std;
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/03
Review of C++
8
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
6/16/03
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++
9
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 namespace std;
void main () {
string seq;
char aa;
int count = 0, i;
6/16/03
Review of C++
10
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/03
Review of C++
11
Debugging
Definition: Bug – A mistake in your program
that can be one of the following
Compile-time error
Run-time error (when program is executing)
6/16/03
Syntax error – program will not compile with syntax
errors (fairly easy to fix)
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++
12
Debugging (2)
Key Idea:
Poor Man’s Debugger:
6/16/03
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
Print statements of program variables
(cout) – allow you to see the contents of
the variables as the program executes
Review of C++
13
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
Stepping into functions
Stepping out of functions
(good if you accidentally stepped into
F10
F11
Shift-F11
a function)
As you step through the program, you can look at the
contents of variables and track their changes.
6/16/03
If you expect a variable to change to a specific value and it
does not – you have found your bug!
Review of C++
14
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
6/16/03
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++
15
Debugging (5)
6/16/03
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++
16
Debugging (6)
Convert the nested for loop example
into a simple C++ program
Debug this simple program tracing the
values of
6/16/03
i
j
nums[i][j], as they are displayed
Review of C++
17
Other debugging and
development tips
1.
2.
6/16/03
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++
18
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/03
Review of C++
19
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
6/16/03
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++
20