tcb2073 prepare for final exam sept 2012

Upload: ji-ro

Post on 14-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    1/8

    TCB2073 Structured Programming and Database

    September 2012

    Instructions to students

    Students have to complete 30 questions in three weeks starting from week 1.These questions are a form of revision for students who have taken Foundation

    Programming 1. The topics covered by these questions are variables, constants, controlstructures (i.e. if-else, switch-case, loops) and 1D array. The students will need tosubmit answers for certain questions during lectures and in lab session 2. Student canonly submit to the lecturers and GA in charge of their lectures and lab sessions theyregistered. This rule is imposed to avoid students losing their answers and not gettingany marks for their work.

    Question 1

    Recall the process involved in adding 2 numbers together (you learnt this in primaryschool). Suppose the numbers are 1019 and 1897. The working involved is as shownfollows:

    1 11 0 1 9

    + 1 8 9 7- - - - - - - -

    2 9 1 6

    Write a program that reads in 2 integers, each as a sequence of digits, and prints outthe working.Hint: Use arrays to store the digits of the numbers (first and second number, the carry-over and the result).

    Question 2

    Assume that the following array has been defined:int arr[5];What is wrong with the following code:

    a) arr[1] = 10; arr[2] = 12; arr[3] = 14; arr[4] = 4; arr[5] = 10;

    b) for (int i=1; i

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    2/8

    TCB2073 Structured Programming and Database

    September 2012

    f) int i = 0;while (i < 5) {

    arr[i] = 25;}

    g) int i = 0;

    do {i++;arr[i] = 25;

    } while (i < 5);

    h) arr[5] = 15;for (int i=0; i

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    3/8

    TCB2073 Structured Programming and Database

    September 2012

    Question 6

    What is the output for the code below?a) int main()

    {float a = 0.7;

    if(0.7 > a)printf("Hi\n");

    elseprintf("Hello\n");

    return 0;}

    b) int main(){

    charstr = "Asia";printf("%s\n", str);return 0;

    }

    Question 7

    What do these loops print?

    a. for(i = 0; i < 10; i = i + 2)cout

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    4/8

    TCB2073 Structured Programming and Database

    September 2012

    Question 9

    Produce a multiplication table. Top left hand corner will show 1x1 and bottom rightshows 12x12, as below:

    1 2 3 4 5 6 7 8 9 10 11 122 4 6 8 10 12 14 16 18 20 22 243 6 9 12 15 18 21 24 27 30 33 364 8 12 16 20 24 28 32 36 40 44 485 10 15 20 25 30 35 40 45 50 55 606 12 18 24 30 36 42 48 54 60 66 727 14 21 28 35 42 49 56 63 70 77 848 16 24 32 40 48 56 64 72 80 88 969 18 27 36 45 54 63 72 81 90 99 10810 20 30 40 50 60 70 80 90 100 110 12011 22 33 44 55 66 77 88 99 110 121 13212 24 36 48 60 72 84 96 108 120 132 144

    Question 10

    Write a program that act as a guessing game in which the user has eight tries to guessa randomly generated number. The program will tell the user each time whether he orshe guessed too high or too low.

    Question 11

    Write a program to print the first 7 positive integers and their factorials. (The factorial of1 is 1, the factorial of 2 is 1 * 2 = 2, the factorial of 3 is 1 * 2 * 3 = 6, the factorial of 4 is 1

    * 2 * 3 * 4 = 24, etc.)

    Question 12

    Write a program to compute the average of the ten numbers 1, 4, 9, , 81, 100, that is,the average of the squares of the numbers from 1 to 10.

    Question 13

    Write a program to print the first 7 positive integers and their factorials. (The factorial of1 is 1, the factorial of 2 is 1 * 2 = 2, the factorial of 3 is 1 * 2 * 3 = 6, the factorial of 4 is 1* 2 * 3 * 4 = 24, etc.)

    Question 14

    Write a program that accepts the numerator and the denominator of a fraction, andoutput the simplified fraction. For example, given an input numerator of 15, and an inputdenominator of 20, the following is the output:15 / 20 ====> 3 / 4

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    5/8

    TCB2073 Structured Programming and Database

    September 2012

    Question 15

    Write a program that provides a tutorial on fraction multiplication. The program mustshow the simplification that can be done, before the actual multiplication is done. Forexample, suppose the user specifies 1 fraction to be 4/20 and the other to be 6/12.The program will then proceed as follows:You asked, what is: 4 6--- x ---20 12Simplifying the first fraction, we obtain:1 6--- x ---5 12Simplifying the second fraction, we obtain:1 1--- x ---5 2Performing the multiplication, we obtain:1---

    10

    Question 16

    Write a program to print the first 10 Fibonacci numbers. Each Fibonacci number is thesum of the two preceding ones. The sequence starts out 0, 1, 1, 2, 3, 5, 8,

    Question 17

    Write a program that takes in three arguments, a start temperature (in Celsius), an endtemperature (in Celsius) and a step size. Print out a table that goes from the starttemperature to the end temperature, in steps of the step size, each time printing out the

    Fahrenheit equivalent.Sample run:Please give in a lower limit, limit >= 0: 10Please give in a higher limit, 10 > limit

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    6/8

    TCB2073 Structured Programming and Database

    September 2012

    assumed to occur with a certain fixed probability every day. As an example, supposeimmigration of a single person to the population occurs with probability P each day. In asimulation, we then do as follows at every time unit:1. Generate a random number X that distributes uniformly between 0 and 1.2. If X < P, we assume an immigration has occurred and the population increases by

    1. Otherwise, immigration did not occur- the population size has not increased.Note that the change of the population size within a day is assumed to be at most +1 or-1.

    Your program will then require the following input:1. initial population size2. the number of days to be simulated3. the probability, P, for immigration of a single person to the population per day4. the probability, Q, for emigration of a single person out from the population per

    day

    It will compute and print the population sizes over the duration of the specified timeperiod, at each day. It will also compute and print the average population sizes own theduration of weeks and months.

    Question 19

    Modify your program in Q18 so that P and Q are both dependent on population size.Let N be the population size. If N = 10000, Pwill be 0 and Q will be 1. Otherwise, we have the following relation:

    P = (N-1000)/1000Q = 1-P

    Question 20

    Write a program that implements the following functionality:a) reads in a list of 20 numbersb) prints the list of numbers readc) compute the average of the numbers readd) quit

    The user selects the functionality to be executed using a text menu..Question 21

    Expand the program in Q1 by including the following functionality: plot a horizontalgraph. For example, if the list of numbers read is 4 5 3, then the horizontal plot is asfollows:4 ****5 *****3 ***

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    7/8

    TCB2073 Structured Programming and Database

    September 2012

    Question 22

    Further expand the program in Q20 by including the following functionality: compute andprint the population standard deviation of the numbers read. See http://ww.mathsisfun.com/data/standard-deviation.html for an explanation of the termstandard deviation.

    Question 23

    Further expand the program in Q4 by including the following functionality: compute andprint the median of the numbers read. See http://www.mathsisfun.com/median.html foran explanation on the term median.

    Question 24

    Write a program that reads in a list of 20 words, and prints them out in the reverseorder.

    Question 25

    Write a program that reads in a list of up to 20 words, and prints them out inalphabetical order.

    Question 26

    Write a program that maintains students examination record for 3 papers. The programoffers through a text menu the following functionality:

    read in the list of ids and marks for a particular examination paper compute the average for a particular examination paper compute the average mark for a particular student (over the 3 papers) print out the best score (and the corresponding id) for a particular examination

    paper

    print out the best 3 scores (and the corresponding id) for a particular examinationpaper print out the marks & id list in sorted order (from highest to lowest order) for a

    particular examination paper

    Question 27

    A factory manager maintains a list of 5 grinding machines, each with a different grindingrate. Jobs to be sent to the machine are characterized by its volume. Given a list of

    jobs, there are many ways to assign it to the machines: select the fastest machine available (not busy) select the slowest machine available

    send the biggest job to the fastest machine send the slowest job to the fastest machine

    Write a program that accepts as input the list of grinding rates, and the list of jobvolumes, attempts each of the assignment methods, and reports for each the overallgrinding time.

  • 7/29/2019 TCB2073 Prepare for Final Exam Sept 2012

    8/8

    TCB2073 Structured Programming and Database

    September 2012

    Question 28

    A program is needed to schedule a list of up to 10 chess players against one another.The schedule must obey the following constraints:

    There can only be 1 match in a particular time slot. As best as possible, no player shall play in 2 consecutive matches.

    Question 29

    Write a program that reads in a list of up to 20 GPS coordinate (longtidude and latitude), andcompute and print the two points which are furthest from each other. The formula to use is in:http://www.movable-type.co.uk/scripts/latlong.html

    Question 30

    Determine the output for each of the following loopa) int i=0;

    while (i=0; j--) {

    cout