Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 101: Introduction to Computing Final, Fall 2013 Date: Dec 13th 2013 Section A, B, C, D, G and H Time: 3 hours Name: ________________________ Roll No: _______________________ Section: _____________ Instructions: You may ask for extra sheet for rough work, however, it will not be collected Make sure your provide answers in the space provided. I follow this rule quite strictly! Follow the conventions discussed in class, correctness alone will not earn you full marks Q1 – Dry Run (10 marks) int confuse1(int b,int &a,int &c) { int array1[6] = {1,2,3}; int abc = confuse2(a,array1,c,b,3); c = c*10; b = a +c; for (int i = 0; i<6; i++) cout<<array1[i]<<" "; return abc; cout<<"\nleaving confuse1\n"; } void inputNums(int a, int b, int c) { cin>>a>>b>>c; //whatever you like. } int confuse2(int &a,int e[],int b,int &c, int size) { inputArray(e, size); a = e[5] +e[4]; b = b + e[3]; c = c*c; return (a*b); } void inputArray(int a[], int size) { for(int i = 3; i<size*2;i++) cin>>a[i]; //suppose user enters // 5 every time } WRITE THE OUTPUT OF THE PROGRAM HERE void main () { int a=-1, b=1, c= 2; inputNums(a,b,c); int d = confuse1(a,b,c); cout<<"The result is: "<<d; cout<<endl<<a<<" "<<b<<" "<<c; getch(); } Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 1 of 6 Name: ________________________ Roll No: _______________________ Section: _____________ Q2 – Find It! (10 marks) () Here is a word game. A word can occur horizontally or vertically in the following 2D matrix of characters. For example “hello” occurs horizontally and “fast” occurs vertically: Write a function that takes a 2DMatrix of size M x 100, and a word as input parameters and computes the indices where the word is found, along with the information whether it occurs horizontally or vertically. If the word is not found then the function should indicate this to the caller. You have to construct the function prototype yourself. a S s m k q x l c S s c i p a m c F c u y u e b w A h e l l o a d S y u w s w t f T b v c q z d q K v x d w x s Q2 – TextTwist (10 marks) () TextTwist is a word game in which the player is asked to find as many valid words as possible from the letters of a given word, using no less than three letters. For example, from the word ENCYCLOPEDIA, it is possible to create words such as Ape, Pin, Pine, Nap, Nip, Paid, Pine, Cycle, Pencil and so on. However, you cannot create the word Cool. Your job is to write a function which accepts a word w and another word s, both character arrays, and returns true if s is a valid word that the user can create from w, and false otherwise. Name your function validTwist. Another sample example: w: a+b+c, can create the words ++a, c++, cab etc. Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 2 of 6 Name: ________________________ Roll No: _______________________ Section: _____________ Q2 – Find IT or TextTwist(contd) Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 3 of 6 Name: ________________________ Roll No: _______________________ Section: _____________ Q3 – Prime Number List (10 marks) A prime number is a number that is only fully divisible by itself and 1. For example, the number 5 is prime because it can only be divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6. We will not consider 1 as prime. 1. Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. 2. Use the function isPrime to accomplish the following task. Every integer can be expressed as an unique product of prime numbers only. For example, 27 = 3*3*3, 12 = 2*2*3, 1330 = 2* 5 * 7 * 19, etcetera. The only prime factor of a prime number is the number itself. Also note that 1 is not a prime number hence not included as a prime factor. Write a function called primeFactors which receives a number n and an array facts with enough space to store the prime factors (you don’t have to worry about running out of space in facts). The function finds all the prime factors of n and stores them in facts, then it returns the number of prime factors found. For example, if n was 12, the function will return 3 and place 2, 2 and 3 in facts. int primeFactors(int n, int facts[]) Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 4 of 6 Name: ________________________ Roll No: _______________________ Section: _____________ Q4 - Catalan Numbers (10 marks) The following sequence of numbers is called the Catalan Sequence: 1, 1, 2, 5, 14, 42, 132, 429, 1430… The 0th Catalan number is C0 = 1 (as you see in the sequence above), after that, the (n+1)th Catalan number is defined generally as: n 1 C n Ci C n i 1 C 0 C n 1 C1C n 2 C 2 C n 3 ... C n 1C 0 i 0 For example, , , , C4 C0 C3 C1C2 C2 C2 C3C0 = 14 and so on. Write a function kthCatalan that takes as input an integer k (you may assume that it only gets nonnegative numbers) and returns the kth Catalan number . Only write the function, you need not write the main program. Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 5 of 6 Name: ________________________ Roll No: _______________________ Section: _____________ Extra Credit (5 marks) – Scratch the Head! Consider the following minimal piece of code. Add additional code anywhere such that the value of toughVar is modified and printed as square of the input value. For example, if the user enters 6 at statement 1, 36 is printed at statement 2. int main() { int toughVar; cin>>toughVar; //statement 1, assume user enters an integer cout<<toughVar<<endl; //statement 2, toughVar is now square of original value } Restrictions: (what’s the fun without some!) 1. You may use toughVar in your code only once! 2. Even for that one allowed usage, you cannot use toughVar on LHS of an equal sign, i.e., toughVar= something is not allowed 3. Oh and there will be no partial marking of this question, it’s a hit or a miss. Department of Computer Science National University of Computer & Emerging Sciences, Lahore Page 6 of 6