final-sampleSolution

# final-sampleSolution - CS201(Intro to Computing Sample...

This preview shows pages 1–4. Sign up to view the full content.

CS201 (Intro. to Computing) Sample Questions for the FINAL Exam Those questions do not imply any favorite subject or question type for the questions in the actual exam Please also review recitation questions, samples covered in the class, homeworks and the questions in the book as well Especially review the sample questions distributed for the midterm exams since the final exam might be comprehensive QUESTIONS 1) Write a function that takes an integer parameter and returns true if that parameter is divisible by 7. In the program, input some integers and display the total number of numbers that are divisible by 7 by using the above-declared function. End of input entry is signaled by having CTRL-Z (end of file) or having a non-integer value as input. #include <iostream> using namespace std; bool IsDivBySeven(int num) { if(0 == (num%7)) return true; else return false; } void main() { int count=0; int num; while( cin >> num) { if (IsDivBySeven(num)) count++; } cout << count << endl; } © CS201 Team at Sabancı University 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2) a) What is the output of the following program? #include <iostream> using namespace std; int thefunction (int a) { static int b = 0; b++; a=a+b; return a; } int main() { int b=0; int i; for (i=1; i <=3; i++) { b=b+thefunction(i); } cout << b << endl; return 0; } Answer: 12 b) Write a function that takes a matrix of integer as parameter and returns the sum of the elements in the main diagonal of the matrix. You may assume that the matrix is a square one. { int sum = 0; for(int i=0; i<mat.numrows(); i++) //numcols also OK sum += mat[i][i]; return sum; } c) Write a templated function that takes two parameters and returns their maximum. template <class Type> Type maximum(Type a, Type b) { if (a>b) return a; else return b; } © CS201 Team at Sabancı University 2
Write a templated function that takes a vector as parameter and reverses its content. The reversed vector must return from the function as its single parameter. template <class Type> { Type temp; int s = vec.size(); for (int i=0; i<s/2; i++) { temp=vec[i]; vec[i]=vec[s-i-1]; vec[s-i-1]=temp; } } 3) a) Write a recursive function to find the n th element of the following series. - + = = 0 ) 1 ( ). 1 .( 0 1 ) ( x x f x x x x f int series(int n) //pre: n >= 0 { if(n==0) return 1; else if(n>0) { return (n*(n+1)*series(n-1)); } } b) Suppose you want to read a file and store its content in memory. What would you use, a vector or a built-in array? Why? A vector, since the size of the file is unknown. Vector can grow dynamically, but a built-in array cannot. c) Which search mechanism would you use to find an element in a sorted array, binary or sequential search? Why? Discuss on their complexities. Refer to lecture notes and textbook for the answer (Of course

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/16/2012 for the course FENS 101 taught by Professor Selçukerdem during the Fall '12 term at Sabancı University.

### Page1 / 18

final-sampleSolution - CS201(Intro to Computing Sample...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online