Computer Science 1511
Computer Science I

Laboratory Assignment 2
Introduction to C at UMD (5 points)
Due at the end of Lab

Introduction

The purpose of this lab assignment is to familiarize you with the different types of errors you will encounter while programming in C.

Syntax errors: are caused by misspelling a word in your program (such as fr for for) or by omitting a punctuation mark (such as the semi-colon at the end of each statement). This type of error is caught by the compiler and is the easiest to correct.

Run-time errors: are caused by an abnormal condition that occurs while your program is running (such as a divide-by-zero error). This type of error is not caught by the compiler and will typically result in your program crashing (stopping) while you are running the program.

Logic errors: are errors in your solution to a problem (such as forgetting to start the loop at zero when summing up an array of integers). This type of error is the most difficult to spot and correct because the compiler will not catch them but the program will appear to run but will produce incorrect output.

The Program

Paste the program in your Borland C++ editor. Compile/run the C program:

/*1 :*/	#include <stdio.h>
/*2 :*/
/*3 :*/	#define MAX_NUMS	5
/*4 :*/
/*5 :*/	void main(int argc, char *argv[])
/*6 :*/	{
/*7 :*/		int array[MAX_NUMS];	/*  array to store nums to sum  */
/*8 :*/		int n;			/*  number of nums to sum  */
/*9 :*/		int i;			/*  loop index  */
/*10:*/		int total;		/*  the total of all entered numbers  */
/*11:*/		
/*12:	
  13:		Get the number of integers to sum.
/*14:*/	
/*15:*/		fprintf(stdout,"Enter how many number you would like to average (max. of %d) ", MAX_NUMS);
/*16:*/		fscanf(stdin, "%d", &n)
/*17:	
  18:		Check whether the Number is less than or equal to MAX_NUMS
  19:*/   	
/*20:*/		if(n<=MAX_NUMS)
/*21:*/		{
/*22:			
  23:				Ask the user to enter those numbers.
  24:			*/
/*25:*/			fprintf(stdout, "\n\n");
/*26:*/			for(i = 0; i < n; i++)
/*27:*/			{
/*28:*/				fprintf(stdout, "Enter integer number %d: , i + 1);
/*29:*/				fscanf(stdin, "%d", array + i);
/*30:*/			}
/*31:			
  32:				Total all entered numbers.
  33:			*/
/*34:*/			total = 0;
/*35:*/			fr(i = 1; i < n; i++)
/*36:*/				total += array[i];
/*37:			
  38:				Print out average.
  39:			*/
/*40:*/			fprintf(stdout, "\n\n");
/*41:*/			fprintf(stdout, "The average is: %f.\n", total / (float)n);
/*42:*/		}
/*42:*/		else	
/*43:*/			fprintf(stdout,"The entered Number should be less than %d",MAX_NUMS);	
/*44:*/		fflush(stdin);		
/*45:*/		getchar();
/*46:*/ }

Syntax Errors

The first set of errors we will have to fix are the syntax errors. We will do this one-at-a-time, concentrating on the first error the compiler reports, fixing it, and then recompiling and moving on to the next error.

The first syntax error we will have to fix actually occurs at line number 16. The compiler reports Statement missing ; in function main at line 20. When we look at line 20 we see that everything looks OK. This sometimes happens with syntax errors, the problem that occurred could be related to a previous line in the file. Looking at the previous lines we see that the last line without a comment is line 16, and this line has no semi-colon at the end. This is the error. Put a semi-colon at the end of line 16 and recompile.

The next error the compiler reports is Unterminated string or character string in function main at line 28. Looking at line 28 we see that a double quote (") is missing before the last comma. Putting in this double-quote, line 28 should now look like:

28:		fprintf(stdout, "Enter integer number %d: ", i + 1);
Recompile the program.

The final syntax error we have to fix is at line 35. The compiler reports Function call missing ) in function main. The compiler also reports Statement missing ; in function main at Line 35. Looking at line 35 we realize that we are not trying to make a function call but are trying to do a for loop. But the word "for" has been misspelled as "fr". Add the o so line 35 reads:

35:	for(i = 1; i < n; i++)
Recompile and run the program.

Logic Errors

Run the program and try to average two numbers: 10 and 20. The response the computer returns is 10 but the correct answer should be 15. This is known as a logic error. Since the program is syntactically correct the compiler compiles the program without errors. Somewhere in the program the programmer has made a mistake. If we look at line 35 again we notice that we are summing all of the elements of the array from 1 to n - 1. But in C, arrays start at 0. So we change the loop so that it starts summing at 0 instead of 1. Line 35 should now look like the following:

35:	for(i = 0; i < n; i++) 
Recompile and run the program with the previous input (average two numbers: 10 and 20). The program should now give the correct answer: 15.

Run-time Errors

Try running the program now with all of the syntax errors fixed but try to average 0 numbers. The results you observe are from a run time error in line 41. The program cannot continue past this line because n is 0 on this line, and dividing by n causes an error that stops the program. To fix this, replace lines 40 and 41 with the following:

:40	if(n > 0)
:41	{
:42		fprintf(stdout, "\n\n");
:43		fprintf(stdout, "The average is: %f.\n", total / (float)n);
:44	}
:45	else
:46		fprintf(stdout, "Cannot average 0 numbers\n");
The program should now execute without problems.

Practice

Now that you have seen some examples of errors, try making small changes to the program to see what types of errors you can produce. Try to get C to report three types of syntax errors that were not discussed above.

What to turn in

You will need to turn in a hard copy of the source code for the corrected program, as well as test runs of the program. Be sure to include test cases for the average of zero numbers and greater than zero numbers (do not attempt to average more than the maximum number of numbers, that case is not accounted for in this program). Also, include descriptions of two errors that can be introduced into the program and what message the compiler will give you when these errors are introduced (the errors should be different from each other and from other syntax errors described in the lab). Be sure to include a cover sheet with your name, course number (CS 1511), section number, and lab number.