Bug Hunt
20 pts.
List all errors and Identify them.
There can be ZERO or MORE Errors per line.
1 #include <stdio.h>
2 /* program with errors /*
COMMENTS SHOULD BE /* */
3
4 #define X = 3
NO EQUAL SIGN
5
6 main( void ) {
7 int z = 12;
8 int X = 5;
CAN'T GIVE A VALUE TO A PRE-DEFINED CONSTANT
CAN'T HAVE TWO VARIABLES WITH THE SAME NAME
9 if X <; z */ two printfs if X < z
IF CONDITION REQUIRES PARENTHESES
10 printf(“z is %d and is”);
NO VARIABLE FOR THE FORMAT SPECIFIER
11 printf(" larger than X\N", z);
a) NO FORMAT SPECIFIER FOR VARIABLE
b) \N should be \n
12
13 return 0;
14 }
20 pts.
True/False -- Short Answer
List the numbers of the true statements.
In the questions below:
The Variables: a, b, c are floats, x, y, z, r are ints
With values: a = 2.3, b = 3.3, c = 4.0, x = 3, y = 5, z = 4
1. T/F: High-level programming languages are not machine independent.
2. T/F: In this statement, the value of r is True.
r = ((a >= b) && ( c > y / (x++ -z) ));
3. T/F: The do-while loop is the only one that executes before
testing the condition.
4. T/F: The three levels of computer languages are machine language,
C, and C++.
5. T/F: Elements in an array are the same in type and the amount
memory required.
TRUE ANSWERS: 3, 5
10 pts.
What does this program do -- WHAT IS IT'S OUTPUT ?
#includemain() { int i, j, sum; for( i = 1; i<=10; i++ ){ for( j = i; j>0; j-- ) printf("-"); printf("%d", i); if( i % 3 == 0 ) printf("\n"); } printf("\n"); return 0; } The Output looks like this: -1--2---3 ----4-----5------6 -------7--------8---------9 ----------10 Your answer had to reflect the correct value of i, the correct number of dashes and the correct format for the output.
PROGRAMMING TIME!
40 pts.
Write a program that has two arrays:
int arr1[10];
int arr2[10];
And whatever other variables you choose.
And does the following:
1) Reads in the values for the 10 elements of arr1.
2) Your program should assign to each element i of arr2
the sum of the elements 0 through i of arr1.
3) Print all elements in both arrays.
For Example: If arr1's elements are:
_________________________________________
| | | | | | | | | | |
| 3 | 1 | 4 | 10 | 0 | 2 | 2 | 5 | 1 | 11 |
|___|___|___|____|___|___|___|___|___|____|
0 1 2 3 4 5 6 7 8 9
Then arr2's Elements will be:
______________________________________________
| | | | | | | | | | |
| 3 | 4 | 8 | 18 | 18 | 20 | 22 | 27 | 28 | 39 |
|___|___|___|____|____|____|____|____|____|____|
0 1 2 3 4 5 6 7 8 9
Notice, One way to look at it is:
arr2[0] = arr1[0];
arr2[1] = arr1[0] + arr1[1];
arr3[2] = arr1[0] + arr1[1] = arr2[2];
etc.
There was an easy and a hard way to do this:
Hard way:
for( i = 0; i < 10; i++ ){
arr2[i] = 0;
for( j = 0; j < i; j++ )
arr2[i] += arr1[j];
}
The Easy way:
You had to notice that the arr2[i-1] had the sum for arr1[0] to arr1[i-1]
arr2[0] = arr1[0];
for( i = 1; i < 10; i++ )
arr2[i] = arr2[i-1] + arr1[i];
Notice that the bounds on i are different here.
More Bugs
10 pts. All or Nothing
This program should clear an array and return. List the line numbers of all lines with bugs/problems in them and tell what the errors are. --- If Any.
LINE NUMBER: CODE:
--------------|------------------------------------------
1 | int main (void)
2 | {
3 | int i;
4 | int a[10];
5 |
6 | for( i = 0; i <= 10; i++ )
7 | a[i] = 0;
8 | return 0;
9 | }
First, this program did not require any #includes. Second, there is ONE Error.
The bounds on the for loop should be “i < 10” or “i <= 9”. You are trying read a member that is not in the array. Some compilers catch this. Some do not. The resulting behavior of this program is undefined.
graphics images Copyright 1998-2003 Elizabeth Fraley HTML code copyright 1998-2003 Elizabeth Fraley. Permission is given to provide these pages in their original, unaltered form online for educational purposes. They may not be republished or included on a CDROM, disk or other media or with any published work without the express permission of the copyright holder (this includes FAQ books).