Thanks to Kajal Claypool for putting together this debugging tip sheet.
#include < iostream.h >
#define MAX_SIZE 100 // the maximum number of integers which can be read
double average(int array[MAX_SIZE], int num_count); /* ALWAYS use function prototypes... the compiler
provides useful warning messages if you do */
main()
{
int array[MAX_SIZE]={0}, i=0, num=0; /* ALWAYS initialize your variables, otherwise the results */
double sum = 0.0; /* can be very unpredictable... some compilers initialize them
to 0, and some do not. Don't depend on it. */
cout<<"Started program" << endl; // say where you are in the program
cout<<"This program will read in integer values from the user and calculate their average"<< endl; // tell youself what the program does
// read the input from the user
cout<<"Enter some integers, -1 to quit"<< endl;
cin>> num; // you need an initial value to get into the loop
i = 0; /* redundant, but I ALWAYS intialize counters, summers, etc, before I
start a loop, just to be safe */
while((num != -1) && (i < MAX_SIZE)){ /* you CAN overrun arrays by mistake and */
/* cause unpredictable core dumps */
array[i]=num;
cout<< "array["<< i <<"]="<< array[i]<< endl; /* print the loop index and the value you read in, to make sure the */
/* numbers are being read correctly... don't forget the endl or /n. */
i++;
cin>> num;
}
/* if you print the loop index like I did above, you can tell if you're in an infinite loop as opposed
to just having the program hang */
cout<<"calling the average function"<< endl; /* identify where you are in the program */
sum = average(array,i);
cout<<"returned from the average function"<< endl; /* if this prints, you know the function exited properly */
cout<<"The average was "<< sum<< endl; /* print the result */
cout<<"Exiting main"<< endl; /* say you're leaving just before the LAST closing curly brace */
}
double average(int array[MAX_SIZE], int num_count)
{
double sum = 0.0; /* INITIALIZE YOUR VARIABLES */
int i=0;
cout<<"Entering the average function"<< endl; /* indicate which function you are in */
sum = 0.0; /* again, I ALWAYS initialize summers before I start the loop... in a large program,
I will often use the same summers more than once. It is a good habit to have. */
for(i=0;i < num_count;i++){ /* yes, we already set i=0, but ALWAYS initialize the loop counter */
sum = sum + (double)array[i]; // the type promotion will happen, but do it explicitly. That way
// everyone can see you understand what you're doing
cout<<"i = "<< i <<", array["<< i <<"] = "<< array[i] <<", sum = "<< sum << endl; /* print ALL the loop information */
} /* or print SOMETHING in the loop to make sure
you aren't in an infinite loop */
cout<<"sum = "<< sum<< endl; /* print all related info before and after arithmetic operations */
cout<<"num_count = "<< num_count<< endl;
sum = sum/num_count;
cout<<"sum/num_count = "<< sum << endl;
cout<<"Exiting the average function"<< endl; /* say when you are leaving the function */
return sum;
}
There are a lot of error messages in the code, true. You may not need
to use that many. However, if your program is not working properly
you have to track down where it is failing. You might start by adding
error messages at the entry and exit of every function. Otherwise,
you can make a guess on how far the program is going and put an error
message at that point to be sure. Then you can narrow the location of
the error down by putting error messages after every questionable line
of code. ALWAYS remember to put the "/n" or the "endl" at the END of
EVERY error message you print. Otherwise, the message may not print
properly. The computer fills a print buffer and only empties it when
it sees the "/n" or "endl". Also, whenever you change a line of code
you may introduce even more errors, so you may want to add an error
message after the line of code you just changed, just to be sure the
program made it that far.