Вы находитесь на странице: 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, control structures (i.e. if-else, switch-case, loops) and 1D array. The students will need to submit answers for certain questions during lectures and in lab session 2. Student can only submit to the lecturers and GA in charge of their lectures and lab sessions they registered. This rule is imposed to avoid students losing their answers and not getting any marks for their work. Question 1 Recall the process involved in adding 2 numbers together (you learnt this in primary school). Suppose the numbers are 1019 and 1897. The working involved is as shown follows: 11 1019 +1897 -------2916 Write a program that reads in 2 integers, each as a sequence of digits, and prints out the working. Hint: Use arrays to store the digits of the numbers (first and second number, the carryover 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<=5; i++) arr[i] = i; c) for (int i=0; i<5; i++) arr[i] = i- 1; d) for (int i=0; i<5; i++) arr[i] = arr[i- 1]; e) for (int i=0; i<5; i++) arr[i] = arr[i+1];

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<5; i++) arr[i] = 100 / i; i) int i = 0; while (i<5) arr[i] = i; i++;

Question 3 Write a program that uses array and control structures to print this shape: ********** ********** ********** ********** ********** Question 4 Write a program that uses control structures to print this shape: *********** ***$$$$*** ***$$$$*** ***$$$$*** *********** Question 5 Explain the following C or C++ features. Use a short fragment of code to complement your explanation. (a) variable (b) variable initialization (c) the difference between for and while loop (d) the difference between if and if-else statement

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"); else printf("Hello\n"); return 0; } b) int main() { char str = "Asia"; printf("%s\n", str); return 0; }

Question 7 What do these loops print? a. for(i = 0; i < 10; i = i + 2) cout << i << endl; b. for(i = 100; i >= 0; i = i - 7) cout << i << endl; c. for(i = 1; i <= 10; i = i + 1) cout << i << endl; d. for(i = 2; i < 100; i = i * 2) cout << i << endl;

Question 8 Write a program to print the numbers from 1 to 10 and for each the largest integer less than its square and divisible by 3:
10 23 36 4 15 5 24 ... 10 99

TCB2073 Structured Programming and Database September 2012

Question 9 Produce a multiplication table. Top left hand corner will show 1x1 and bottom right shows 12x12, as below: 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 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 guess a randomly generated number. The program will tell the user each time whether he or she guessed too high or too low. Question 11 Write a program to print the first 7 positive integers and their factorials. (The factorial of 1 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 of 1 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, and output the simplified fraction. For example, given an input numerator of 15, and an input denominator of 20, the following is the output: 15 / 20 ====> 3 / 4

TCB2073 Structured Programming and Database September 2012

Question 15 Write a program that provides a tutorial on fraction multiplication. The program must show the simplification that can be done, before the actual multiplication is done. For example, 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 12 Simplifying the first fraction, we obtain: 16 --- x --5 12 Simplifying the second fraction, we obtain: 11 --- x --52 Performing the multiplication, we obtain: 1 --10

Question 16 Write a program to print the first 10 Fibonacci numbers. Each Fibonacci number is the sum 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 end temperature (in Celsius) and a step size. Print out a table that goes from the start temperature 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: 10 Please give in a higher limit, 10 > limit <= 50000: 20 Please give in a step, 0 < step <= 10: 4 Celsius Fahrenheit ------- ---------10.000000 50.000000 14.000000 57.200000 18.000000 64.400000

Question 18 Wiki: Population dynamic is the branch of life sciences that studies short-term and longterm changes in the size and age composition of populations, and the biological and environmental processes influencing those changes. Population dynamics deals with the way populations are affected by birth and death rates, and by immigration and emigration, and studies topics such as ageing populations or population decline. While there are various mathematical models that describe population dynamics, you are to implement a simple immigration-emigration model in C/C++. In this model, immigration to or emigration out from a population, involving a single individual, is

TCB2073 Structured Programming and Database September 2012

assumed to occur with a certain fixed probability every day. As an example, suppose immigration of a single person to the population occurs with probability P each day. In a simulation, 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 size 2. the number of days to be simulated 3. the probability, P, for immigration of a single person to the population per day 4. 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 time period, at each day. It will also compute and print the average population sizes own the duration 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 <= 1000, P will be 1, and Q will be 0. If N >= 10000, P will be 0 and Q will be 1. Otherwise, we have the following relation: P = (N-1000)/1000 Q = 1-P Question 20 Write a program that implements the following functionality: a) reads in a list of 20 numbers b) prints the list of numbers read c) compute the average of the numbers read d) 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 horizontal graph. For example, if the list of numbers read is 4 5 3, then the horizontal plot is as follows: 4 **** 5 ***** 3 ***

TCB2073 Structured Programming and Database September 2012

Question 22 Further expand the program in Q20 by including the following functionality: compute and print the population standard deviation of the numbers read. See http:// ww.mathsisfun.com/data/standard-deviation.html for an explanation of the term standard deviation. Question 23 Further expand the program in Q4 by including the following functionality: compute and print the median of the numbers read. See http://www.mathsisfun.com/median.html for an explanation on the term median. Question 24 Write a program that reads in a list of 20 words, and prints them out in the reverse order. Question 25 Write a program that reads in a list of up to 20 words, and prints them out in alphabetical order. Question 26 Write a program that maintains students examination record for 3 papers. The program offers 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 examination paper 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 grinding rate. 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 job volumes, attempts each of the assignment methods, and reports for each the overall grinding time.

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), and compute 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 loop a) int i=0; while (i<3) { for (int j=3; j>=0; j--) { cout << i << space << j << endl; } i--; } b) int i=0; do { for (int j=0; j<3; j++) cout << i << space << j << endl; }while (i<3) c) int i=0, j=0; for (i=0; i<0; i++) for (j=0; j<3; j++) { if ((i+j)%2==0) cout << i << space << j << endl; } cout << i << space << j << endl; } d) for (int i=0; i<13; i++) for (int j=0; j<13; j++) for (int k=0; k<13;k++) { if (k+j == 2*i) cout << i << space << j << space << k << endl; } -End of Question-

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