(60-141) Lab Exercises #2
(Due at the end of the lab period – July 9-11, 2018)
Objectives: Working with arrays and functions
Pre-requisite(s): Read and review chapters 1-6.
Write a C program that creates an array of 500 integer elements and loads it with each
value equal to the subscript’s square, and then write a sequential and a binary search
algorithm to see whether a number is a perfect square. Use a loop to load the array.
Prompt the user for a positive integer value less than 250,000. Test the functions by
calling them from a simple interactive main() function using a menu, with different values
used to select the choice of function. Overall, you should have one C program (call it
firstnameLastname_Lab2.c) containing one main() function and at least the following
functions:
1- void loadArray(int nums[], int size): To load the array nums with its
subscript’s squares (eg. {0,1,4,9,16,25,… })
2- int binarySearch(int nums[], int size, int key): To search the
input array for a key using binary search approach and return the number of
comparisons.
3- int sequentialSearch(int nums[], int size, int key): To search
the input array for a key using sequential search approach and return the number
of comparisons.
4- int getKey(): to get a valid integer key (between 0 and 250,000) from the user
to search.
The program should contain a loop that permits users to enter a new choice of function
for each loop, until exit from the loop explicitly.
Sample Interaction
Lab 2
-----
1 – Sequential Search
2 – Binary Search
0 - QUIT
Please enter a selection: 4
Invalid Input.
Lab 2
-----
1 – Sequential Search
2 – Binary Search
0 - QUIT
Please enter a selection: 1
Please enter a positive number to search (< 250,000): -1
Invalid Input.
Lab 2
-----
1 – Sequential Search
2 – Binary Search
0 - QUIT
Please enter a selection: 1
Please enter a positive number to search (< 250,000): 1500
1500 is not a perfect square.
Number of comparison using sequential search is 500.
Lab 2
-----
1 – Sequential Search
2 – Binary Search
0 - QUIT
Please enter a selection: 2
Please enter a positive number to search (< 250,000): 1600
1600 is a perfect square.
Number of comparison using binary search is 9.
Lab 2
-----
1 – Sequential Search
2 – Binary Search
0 - QUIT
Please enter a selection: 0
Goodbye!
EVALUATION OF WORK AND ATTENDANCE: Total 25 marks.
You need to show to your lab instructor the work you have completed for this lab
assignment, generally in the form. of a working program. The marks you will receive for
the lab are made of two parts, the programming part (20 marks) and lab attendance (5
marks). Do not email your work to the Instructor or to any teaching assistant.
Lab Work Mark: Your C code will be evaluated based on your solutions for the problems
based on the following scheme:
1. Does the code run and meet specifications?
Is input adequate and input data type properly validated? Is processing adequate?
Is output correct and adequate? Is the code compliable? Is the code run properly?
2. Is the code properly commented?
Is the program title, programmer's first and last name, and the date posted at the
top in a multi-line comment? Is each significant step of the program properly
commented? Are comments added to clarify details? Are comments clear,
accurate, neatly formatted, and have no misspellings?
3. Is the code properly formatted?
Are blocks of code indented according to their parent-child relationship? Do curly
braces line up vertically? Is there an empty line between significant steps (blocks)
of the program? Is camel-case notation used for variable, e.g.
employeeLastName?
IMPORTANT:
ASK QUESTIONS IF YOU GET STUCK, BUT DO YOUR OWN CODE. ANY CODE
SUSPECTED TO BE SIMILAR TO ANOTHER SUBMISSION WILL CAUSE BOTH
SUBMISSIONS TO RECEIVE A ZERO MARK ON ALL LABS AND BE REPORTED FOR
PLAGIARISM.