Вы находитесь на странице: 1из 4

CENG 151

Fall 2015-16

Lab Exam 2 Questions


Using Visual Studio, do the following problems:
1. Write a program that reads in the side of a square and then prints that hollow square
out of asterisks. You need a function printPattern() with one input parameter(side of
the square) that prints the pattern. Your program should work for squares of all side
sizes between 1 and 20. For example, if your program reads a size of 5, it should print:

You need to write 3 different C programs by using:


a) While loop
b) Do-While loop
c) For loop.

2. Use a single-subscripted array to solve the following problem.


Read in 20 numbers, each of which is between 10 and 100, inclusive. Write an
algorithm that removes duplicated numbers and return the unique numbers in the
original array. You need a function removeDuplicate() that performs the required
operation.
3. Write a program to model a simple calculator. Each data line should consist of
the next operation to be performed from the list below and the right operand.
Assume the left operand is the accumulator value (initial value of 0). You need
a function scan_data with two output parameters that returns the operator
and right operand scanned from a data line. You need a function do_next_op

CENG 151

Fall 2015-16

that performs the required operation. do_next_op has two input parameters
(the operator and operand) and one input/output parameter (the accumulator).
The valid operators are:
+ add
- subtract
* multiply
/ divide
^ power (raise left operand to power of right operand)
q quit
Your calculator should display the accumulator value after each operation.
A sample run follows:
+ 5.0
result so far is 5.0
^2
result so far is 25.0
/ 2.0
result so far is 12.5
q0
final result is 12.5
4. A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit
code scanned by comparing the codes last digit (called a check digit ) to its
own computation of the check digit from the first 11 digits as follows:
1. Calculate the sum of the digits in the odd-numbered positions (the first,
third, , eleventh digits) and multiply this sum by 3.
2. Calculate the sum of the digits in the even-numbered positions (the second,
fourth, , tenth digits) and add this to the previous result.
3. If the last digit of the result from step 2 is 0, then 0 is the check digit.
Otherwise, subtract the last digit from 10 to calculate the check digit.
4. If the check digit matches the final digit of the 12-digit UPC, the UPC is
assumed correct.
Write a program that prompts the user to enter the 12 digits of a barcode
separated by spaces. The program should store the digits in an integer array,
calculate the check digit, and compare it to the final barcode digit. If the
digits match, output the barcode with the message validated. If not, output
the barcode with the message error in barcode. Also, output with labels the
results from steps 1 and 2 of the check-digit calculations. Note that the first

CENG 151

Fall 2015-16

digit of the barcode will be stored in element 0 of the array.


Try your program on the following barcodes, three of which are valid. For the first
barcode, the result from step 2 is 79 (0 _ 9 _ 0 _ 8 _ 4 _ 0) * 3 _ (7 _ 4 _ 0 _ 0 _ 5).
079400804501
024000162860
011110856807
051000138101

5. The bubble sort is a technique for sorting an array. A bubble sort compares
adjacent array elements and exchanges their values if they are out of
order. In this way, the smaller values bubble to the top of the array (toward
element 0), while the larger values sink to the bottom of the array. After the
first pass of a bubble sort, the last array element is in the correct position; after
the second pass the last two elements are correct, and so on. Thus, after each
pass, the unsorted portion of the array contains one less element. Write and
test a function that implements this sorting method.
6. Lets improve the bubble sort program. Use two functionsbubbleSort and
swap. Function bubbleSort sorts the array. It calls function to exchange the
array elements array[j] and array[j + 1] . Remember that C enforces information
hiding between functions, so swap does not have access to individual array elements
in bubbleSort. Because bubbleSort wants swap to have access to the array elements
to be swapped, bubbleSort passes each of these elements call-by-reference to
swapthe address of each array element is passed explicitly. Although entire arrays
are automatically passed by reference, individual array elements are scalars and are
ordinarily passed by value. Therefore, bubbleSort uses the address operator (&) on
each of the array elements in the swap call to effect call-by-reference as follows:
swap( &array[ j ], &array[ j + 1] );
Write and test these two functions that implement this sorting method.

CENG 151

Fall 2015-16

Вам также может понравиться