CS1010ETutorial6Solution

Embed Size (px)

Citation preview

  • 8/2/2019 CS1010ETutorial6Solution

    1/3

    CS1010E Tutorial 6

    Problem 1a)

    Consider array of numbers,

    [1 3 5 7 9 2 4 6 8 10]

    Which of the following is the most of the efficient

    method of searching for a given number in the list?

    a) Binary Search

    b) Sequential Search

    c) Bubble Sort followed by Binary Search

    d) Selection Sort followed by Binary Search

    Explain your choice.

    Answer: A sequential search will be most efficienthere if we are only searching once. The timeneeded to first sort an unordered array then goingto search it will almost always be less than justbrute forcing with a sequential search.However, if we are going to search it multipletimes, then sorting it first will make much moresense, since the time we save on searching lateron will easily cover the sort time.

    Problem 1b)

    Now consider this array of numbers,

    1 2 3 4 5 6 7 8 9 10.

    Which method is the most efficient now?

    Answer: If we know it is ordered, then we onlyneed to apply a search algorithm. In this case,the efficiency we save for using the sequentialsort is actually not significant. The averagenumber of execution for sequential is 5 times(in a10 object array), and 2.32(log2(10)-1) for binary

  • 8/2/2019 CS1010ETutorial6Solution

    2/3

    search. However, once the array size becomes muchlarger, the save in time is considerable. If wehave a 10000 object long array, sequential will onaverage require 5000 executions, whereas binarywill require 12.3 only.

    Problem 2)

    In this question, you are given a character array (this

    is what we call a string) of length greater than 2 and

    that you are to determine if that string is a palindrome.

    A String is a palindrome if it reads the same from front

    and back. eg. kayak

    If it indeed is a palindrome, you should print out the

    palindrome.

    If not, print a message stating it is not

    To simplify the question, you may use the function

    strlen() in string.h. This function returns the length of

    the array. You may assume the length of the word given

    never exceed 100 characters.

    A skeleton code is given in palindrome.c, fill in the

    empty functions.

    Answer : Refer to palindromeSolution.c

    Problem 3)

    Imagine you are the professor teaching CS1010E, after

    giving your students a test you need to be able give them

    grades based on the accursed bell curve.

    [NOTE: The actual process for determining the bell curve

    is NOT like this.]

    Write a program that takes a list of 100 students of

    matric numbers and corresponding grades, and store them

    into 2 arrays.

    First you should sort them by marks, and assign themgrades.

  • 8/2/2019 CS1010ETutorial6Solution

    3/3

    For simplicity we have 6 types of grades A , B, C, D ,E

    and F.

    90 percentile and above give A,

    60 percentile and above give B,

    40 percentile and above give C,

    20 percentile and above give D,

    10 percentile and above give E,

    Anything below should get an F.

    After this, you will need to sort the student according

    to their matric number.

    Finally, print out the list of students with grades,

    sorted by their matric number.

    There is a sample input and output file, you can use it

    to match your data.

    The skeleton code can be found in bellCurve.c.

    This question brings you into the world of scripting, a

    very powerful tool useful for many other modules. Forboth parsing data and generating data.

    Answer : Refer to bellCurveSolution.c