1 / 7

CS210

CS210. Tutorial 1. Problem #1.

Download Presentation

CS210

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS210 Tutorial 1

  2. Problem #1 Describe the output produced by the following statements:int *foo, *goo;foo = new int;*foo = 1;cout << *foo << endl;goo = new int;*goo = 3;cout << *foo <<*goo <<endl;*foo = *goo +3;cout <<*foo <<*goo<< endl;foo = goo;*goo = 5;cout <<*foo <<*goo << endl;*foo = 7;cout << *foo << * goo<< endl;goo = foo;* foo = 9;cout << *foo << * goo<< endl;

  3. Problem #2 • Write C++ statements to do the following: • Allow user to enter n, the number of values to be processed; then allocate an anonymous array of n double values, storing its address in doublePtr. • Fill the array created in part a with n input values, entered from the keyboard. • Assuming that addresses are stored in 4 bytes and double values in 8 bytes, tell what output will be produced by the following statements:double dubValues [ ] = {1.1, 2.2, 3.3, 4.4, 5.5};double *dubPtr = dubValues;for (int i = 0; i<5; i++) cout <<sizeof (dubPtr +i) << " " <<sizeof(*(dubPtr +i)) << " " << *(dubPtr +i) << endl;

  4. Problem #3 a) Write a function that converts the time in GMT to Saudi time. A time object has 3 attributes: hours, minutes and seconds. The function should add 3 hours to the present time.(b) Test the function using Black box and White box techniques.(c) Write a test driver for the function.

  5. Example: Time_piece Class #include <cassert> //other include directives here const short int MIN_TIME_DIF = -25 const short int MAX_TIME_DIF = 25 const short int MIN_HOUR = 0 const short int MAX_HOUR = 23 const short int MIN_MIN = 0 const short int MAX_MIN = 59 const short int MIN_SEC = 0 const short int MAX_SEC = 59 class Time_piece { public: Time_piece(short h =0; short m = 0, short s = 0); short get_hour() const; short get_min () const; short get_sec () const; void set_hour (short h); void set_min (short m); void set_sec (short s); void convert (short tZone); void display() const; private: short hour, min, sec; };

  6. Example- Method Convert void Time_piece::convert (short tZone) { assert(tZone >= MIN_TIME_DIF && tZone <= MAX_TIME_DIF); hour += tZone; if (hour < MIN_HOUR) hour+= MAX_HOUR; if (hour > MAX_HOUR) hour %= MAX_HOUR; }

  7. Example: Driver Program for Convert() #include “time_piece.h” #include <iostream> using namespace std; int main(){ short hour, min, sec, tZone; char ans; do { cout<<“ Enter current time H M S”; cin>>hour>>min>>sec; Time_piece watch(hour,min,sec); cout<<“Enter time zone difference?”; cin>>tZone; watch.convert(tZone); watch.display(); cout<<“Continue? (Y/N)”; cin>>ans; while (ans == ‘Y’ || ans == ‘y’); return 0; }

More Related