Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures and arrays 2. Practice on pointer variables Step 1: Download pointer.cpp: /home/fac/testzhuy/cpsc152/lab2/download poiner.cpp Step 2: Edit pointer.cpp using emacs emacs pointer.cpp /* Your Name * pointer.cpp *demonstrate use of pointers #include <iostream> using namespace std; int main() { int x = 5; //declare a pointer variable ptr1 pointing to x //display the value of x using ptr1 //change the value of x to 10 through ptr1 /*declare a double data type pointer variable ptr2 which points to a dynamically allocated memory //input data into the newly allocated memory using cin //display the double data value using cout //increase this double data value by 2.5
//dispay the value using cout //destroy the dynamically allocated memory return 0; Step 3: Complete the code as indicated by the comments in main() Step 4: Save the file and exit Emacs. Step 5: Compile the file and execute it. (Or you can adapt your Makefile.) g++ -Wall -o pointer pointer.cpp./pointer 3. Parameter passing by pointers and by references Step 1: Download para.cpp: Step 2: Edit para.cpp home/fac/testzhuy/cpsc152/lab2/download para.cpp emacs para.cpp /* Your Name: * param.cpp, * demonstrate calling functions by value, by pointers, and by references #include <iostream> using namespace std; void swap1(int x, int y); //exchange the two inputed values //add your swap2 and swap3 function prototype here
int main() { int num1, num2; cout << "\nenter two numbers separated by a space: " << endl; cin >> num1 >> num2; cout << "\n num1 is " << num1 << ", num2 is " << num2 << endl; swap1(num1, num2); //exchange their values cout << "\n num1 is " << num1 << ", num2 is " << num2 << endl; //add your swap2 function call to exchange values of num1 and num2 //display num1 and num2 //add your swap3 function call to exchange values of num1 and num2 //display num1 and num2 return 0; void swap1(int x, int y) { int tmp = x; x = y; y = tmp; Step 3: Save the file and exit Emacs. Compile and execute it. Do the values get exchanged? Step 4: Add swap2 function that takes parameters by pointers. Save the code, compile it and execute it to see the difference. Step 5: Add swap3 function that takes parameters by references. Save the code, compile it and execute it to see the difference.
4. Combining pointers, structures and dynamic arrays Step 1: Download para.cpp: home/fac/testzhuy/cpsc152/lab2/download dynamic.cpp Step 2: Edit para.cpp emacs dynamic.cpp /* Your name: * dynamic.cpp, * demonstrate combination of pointers, structures, and dynamic arrays #include <iostream> #include <string> using namespace std; struct Student { int ID; //student ID string name; // student name double gpa; //GPA short year_in_school; // Year in school, e.g., 2003 ; /* * Return a numer of n students' info from user inputs * PRE: assume a positive integer n * POST: return a dynamic array holding n students' info Student* getstudentdata(int n); /* * Display a student's data in a line * PRE: A pointer to a student structure * POST: none void displaystudentdata(const Student* stud); int main() { int num_of_students = 0; do { cout << "Number of students you want to create: ";
cin >> num_of_students; while (num_of_students <= 0); /* call getstudentdata(num_of_students) to get the student data into a pointer variable ptr which is a pointer to Student structure /* Use a loop to call displaystudentdata() to display all students in the array by manipulating the pointer variable ptr // destroy the dynamic memory return 0; //Complete the function Student* getstudentdata(int n) { //Complete the function void displaystudentdata(student* stud) { Step 3: Complete the code. In particular, you need to finish up getstudentdata(), displaystudentdata() and main(). Step 4: Save the file, compile and execute it. Take-home message: Always check first whether a pointer is valid before using it! 5. Extra Exercises on Two-Dimensional Dynamic Arrays You can create a new program named twodim.cpp which handles two-dimensional dynamic arrays. You mainly have to implement two functions: int** getmatrix(int m, int n) --- The function takes two parameters m and n, specifying a matrix s number of rows and number of columns respectively. It allocate a 2-dimensional dynamic int array for this matrix, and initialize element at [i,j] with i*n + j.
void destroymatrix(int** matrix, int m) --- This function deallocate the dynamic memory of the 2-dimensional array with m rows. void displaymatrix(int** matrix, int m, int n) --- This function display matrix with m rows and columns. In your main(), you need to test these three functions.