midterm1-sampleSolutions (2)

# midterm1-sampleSolutions (2) - CS 201 (Intro. to Computing)...

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

CS 201 (Intro. to Computing) Fall 2007 Sabancı University - Sample Questions and Solutions for Midterm 1 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 The amount of questions here is of course much more than the actual exam. QUESTIONS 1) Write a function that takes an integer type of parameter and returns its reciprocal. If the parameter is zero, then the function should return 0. In the main program, first input two integer values. Then find the sum of the reciprocals of all of the numbers between these two inputs. In the main program, you have to use the reciprocal function. #include <iostream> using namespace std; double reciprocal(int number) { if(number == 0) return 0; else return (1.0/number); } int main() { int start,end; cout <<"Enter the boundaries( start, end): "; cin >>start>>end; double sum=0; if(start > end) cout << "Start must be smaller than end!"<<endl; else { while (start<=end) { sum+= reciprocal(start); start++; } cout <<"Sum of reciprocals: "<<sum<<endl; } return 0; © CS201 Team at Sabancı University

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

View Full Document
NAME: } © CS201 Team at Sabancı University
NAME: 2) Write a function that takes three string values as parameters and displays them in lexicographical order. void order(string s1, string s2, string s3) { { cout << s1 <<" " <<s2<<" "<<s3<<endl; } { cout << s2 <<" " <<s1<<" "<<s3<<endl; } { cout << s3 <<" " <<s1<<" "<<s2<<endl; } { cout << s3 <<" " <<s2<<" "<<s1<<endl; } { cout << s2 <<" " <<s3<<" "<<s1<<endl; } { cout << s1 <<" " <<s3<<" "<<s2<<endl; } } © CS201 Team at Sabancı University

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

View Full Document
NAME: 3) a) Rewrite the following expression using C++ syntax. 3 sin a a c b c b a c - - ( (c – (a/(b-c) ) ) / sin( b/(c/a) ) ) * (a*a*a) b) What are the results of the following expressions? 5+3/5+72*!(2+5)==12%8*-10 false or 0 (20*1.5/(2*6-8)-7)*11 5.5 c) The range of short unsigned integer is [0 . . 65535]. Why is the upper limit is 65535? Since it is stored in 16 bit and there are no negative numbers, the upper limit is 2 16 -1. d) Write the statement to display the sum of the digits of a two-digit integer number (call it num ). cout << (num/10 ) + (num%10) << endl; e) We do not include anything to use int, but we need to include string header file to use string. Why is that? Because int is a built-in data type. But string is defined in a library so we have to add its header file to use it. f)
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 / 26

midterm1-sampleSolutions (2) - CS 201 (Intro. to Computing)...

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

View Full Document
Ask a homework question - tutors are online