cs210

7
CS210 Tutorial 1

Upload: libby-moreno

Post on 02-Jan-2016

16 views

Category:

Documents


1 download

DESCRIPTION

CS210. Tutorial 1. Problem #1. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS210

CS210

Tutorial 1

Page 2: CS210

Problem #1Describe 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;

Page 3: CS210

Problem #2 Write C++ statements to do the following:

1. 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.

2. Fill the array created in part a with n input values, entered from the keyboard.

3. 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;

Page 4: CS210

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.

Page 5: CS210

Example: Time_piece Class#include <cassert>//other include directives hereconst short int MIN_TIME_DIF = -25const short int MAX_TIME_DIF = 25const short int MIN_HOUR = 0const short int MAX_HOUR = 23const short int MIN_MIN = 0const short int MAX_MIN = 59const short int MIN_SEC = 0const short int MAX_SEC = 59class 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;

};

Page 6: CS210

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;}

Page 7: CS210

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;

}