CSSE 2 Data Structures and Algorithms III Worksheet 4: Some Pointer Exercises

1. Basic Pointer Manipulation:
2. Write a program, which initializes 2 integer variables X and Y, prints the values of X and Y and their memory address. Your program should then increment X by 10, print out the new value and print out that values address.

3. Pointers and Structures:

Given a structure that has 3 fields: int Hour; int Minute; and int Second;

write functions with the following prototypes:

• void Get_Time( Time * T)
• void Print_Time( Time * T)

Use these functions to input a Time and display it to the user.

1. Pointers and Arrays:
2. Rewrite the following using a function which sums the elements in an array.

Use pointers whenever possible.

void main()

{ const int SIZE = 10;

int array[SIZE]= {1,2,3,4,5,6,7,8,9,10};

int j, sum =0;

for (j = 0; j!=SIZE; j++)

{

Sum = Sum + array[j];

}

cout << "The sum of the numbers you entered is: " <<sum << endl;

}

Strings:

3. Write a program that initializes an array of pointers to strings e.g. the days of the week. Print out the values in the array
4. Write a string copy function that has the following prototype:
5. void copystr (char*, char*). Show how this could be used in main().

6. Pointers and Classes

Given the following class template, complete the class definition by defining the methods.

Show how the class may be used in main () by declaring an array of pointers to cars, initialize all the pointers in the array to NULL, and present a menu to the user allowing the user to add cars to the array, determine the colour of the cars in the array and remove cars from the array.

Cars should be added and removed using the new and delete operators respectively.

class car

{

private:

char * colour;

public:

car(char * colour_in);

char* getColour();

~car() ;

};