Lab 2 -- Debugging (with DDD)

Answer Sheet

Exercise 1

Mark error(s) in the following program after you print the final answer sheet.

// ---------------------------------------------------------------                                                       
//
// This program reads a list of numbers from the command line and 
// determines whether or not they are in descending order. 
//
// usage:
//    inlab2a  number [number ...]
// ---------------------------------------------------------------

#include <cstdlib>
#include <iostream>
using namespace std;

const int SIZE = 10                // size of the array 

int main(int argc, char *argv[])
{
    int i;                // loop counter
    bool descending;      // true if the list is in descending order  	
    int list[size];       // array to hold the list
    int lstSize;          // the number of integers in the list

    // Set listSize to the number of integers on the command line, 
    // or the size of the array, whichever is smaller.
    if ( argc-1 > SIZE )
        listSize = SIZE;
    else
        listSize = argc - 1;


    // convert the numbers on the command line from ASCII to
    // integer and store them in the list.
    for (i = 1; i < listSize; i++)
        list[i] = atoi(argv[i]);

    // Check each pair of adjacent integers to see if the one with
    // the lower index has a lower value.
    for (i = 0, descending = true; (i < listSize) && descending; ++i)
        descending = (list[i] > list[i+1]);

    // Inform the user of the result
    if (descending)
        cout << "The list is in descending order.\n";
    else
        cout << "The list is NOT in descending order.\n";

    return(descending);
}

Exercise 2

Turn in a printout with corrections marked on the printout

Exercise 3

A. What is the current value of the variable howMany?



B. What is the current value of the variable total?



C. Look at the cout << statement which is two lines below the current program statement (breakpoint). This statement will output the average. Given the current value of the variables, what is the problem with this statement?



D. From reading the comment before the call to readNumbers(listOfNumbers, howMany) in main(), and from reading the header comments in the function readNumbers(), what did the programmer intend for readNumbers() to do to the variable howMany?



E. Why didn’t readNumbers() work?



F. How would you fix it?



G. How do you set a breakpoint in DDD?



H. Once all the breakpoints are set, how do you begin execution of the program in DDD?



I. How do you inspect the value of a variable in DDD?



J. How do you continue the execution of the program after a breakpoint has been reached?



K. Where is the Debugger Console Window located?



Turn in your answer sheets with the code from Exercise 1 marked with the errors you found.

----- End of Lab 2 - Debugging -----
Complete the Exercises on the Answer Sheet.
Turn in the Answer Sheet and the printouts required by the exercises.