midterm2-samplesolutions

midterm2-samplesolutions - CS201(Intro to Computing MIDTERM...

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

View Full Document Right Arrow Icon
CS201 (Intro. to Computing) MIDTERM II SAMPLE QUESTIONS & ANSWERS Spring 2008-2009 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 & ANSWERS 1) Write a program that solves the following problem: If April 23 rd of a year is a Sunday, what would be the day of week for October 29 th of the same year? Use Date class! #include <iostream> #include <string> #include "date.h" using namespace std; void main() { int month=4,day=23, year=1900; //any year value would work bool check=true; while(check) { Date date1(month,day,year); if(date1.DayName()!="Sunday") { year++; } else check=false; } month=10; day=29; Date date2(month,day,year); cout << date2.DayName()<<endl; } © CS201 Team at Sabancı University
Image of page 1

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

View Full Document Right Arrow Icon
2) a) Write a function that takes a date parameter and returns the number of days between that date and the last day of that month. int diffDay(Date thisDay) { return thisDay.DaysIn() - thisDay.Day(); } b) Write a function that takes a string parameter (call it str) and returns true if the value of str is “Turkey”, returns false otherwise. All combinations of upper and lowercase letters in “Turkey” are acceptable. For example “TuRKey” or “TUrkeY” (among many other combinations) should also return true. bool IsTurkey(string str) { string turkey="turkey"; ToLower(str); if (str == turkey) return true; else return false; } c) What is the output of the following program piece? (December 10, 2002 was a Tuesday) Date day(12,10,2002); Date horrorday = day + 3; string s=horrorday.DayName(); cout << horrorday << " " << s; December 13 2002 Friday 3) Write a function that takes a real number as parameter and returns its fractional part. For example, if the parameter is 145.943422, the function should return 0.943422. As another example, if the parameter is -2.4, the function should return 0.4. Write a program that inputs 10 real numbers from keyboard and finds out the real number with the largest fractional part. Your program should display this number. Program should use the above function. Moreover your program should check if the input numbers are real number or not. If a particular input is not a real number, it should be re-entered by the user until a real number is entered. 10 is the number of real numbers! © CS201 Team at Sabancı University
Image of page 2
#include <iostream> #include <cmath> #include <string> using namespace std; double findFract(double num) { double fraction; int temp; temp=num; fraction = num - temp; return fabs(fraction); } int main() { double largestFract = -1,resultNum,num,temp; int ctr=0; cout <<"You will enter 10 numbers - "<<endl; while(ctr<10) { if(cin >> num) { temp = findFract(num); if(temp>largestFract) { resultNum = num; largestFract = temp; } ctr+=1; } else { string temp; cin.clear(); cin >> temp; cout <<" - Enter a real number!!!"<<endl; } } cout <<endl<<resultNum<<endl; return 0; } © CS201 Team at Sabancı University
Image of page 3

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

View Full Document Right Arrow Icon
4) Suppose you have the following function defined in a library bool isTurkish (const string & str); // post: returns true if str is a syntactically correct // Turkish word
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern