// This program provides an exercise on how to pass
// pointer variables as input parameters to functions.
// This program calculates the square and square root
// of an integer and returns them both to the caller.
#include
#include
using namespace std;
// MODIFY the prototype to allow it to pass pointer variables
// as input parameter instead of passing variables by reference.
// Decide which variables should be changed.
void bothways(int, int &, double &);
int main() {
int n = 100;
// MODIFY these two variables such that they are created dynamically by
// pointer variables.
int sq;
double sq_root;
// The subsequent lines should be MODIFIED accordingly so that
// the program works correctly.
bothways(n, sq, sq_root);
cout << n << " " << sq << " " << sq_root;
return 0;
}
// MODIFY the function definition to allow it to pass pointer variables
// as input parameter instead of passing variables by reference.
// Decide which variables should be changed.
void bothways(int n, int &square, double &sq_root) {
square = n * n;
sq_root = sqrt(n);
}
The given program should produce the following output:
100 10000 10Modify the program such that sq and sq_root are pointer variables (instead of variables) that are used to dynamically allocate an integer and double respectively.
Without typing out and compiling these lines of code, figure out the output by hand.
#include
using namespace std;
int main() {
int p = 5, q = 3;
int *a, *b, *c;
a = &q;
c = &p;
b = a;
*a = *a + 3;
a = c;
c = b;
cout << *a << " " << *b << " " << *c << endl;
return 0;
}
#include
using namespace std;
int main() {
float m = 0.5, n = 1.5;
float* r = &n;
float* s = &m;
while (m < *r * 4) {
cout << *s << " ";
m = m + *s;
}
return 0;
}
#include
using namespace std;
const int SZ = 3;
int main() {
int *a, d = 65;
int x[SZ] = {2, 0, 19};
char* b = new char;
char* c = b;
a = &d;
for (int i = 0; i < SZ; i++) {
*c = *a + x[i];
cout << *b;
}
return 0;
}
#include
using namespace std;
// prototypes and their brief descriptions
// this function dynamically creates an array in memory
int* createArray(int);
// This function reverses the contents of the array. To do so,
// create another array (of the same size), and transfer the contents to it
// in inverted order.
// 1st parameter: pointer to the dynamic array, 2nd parameter: size of array
void reverseArray(int*&, int);
// This function prints the contents of the array.
// 1st parameter: pointer to the dynamic array, 2nd parameter: size of array
void printArray(int*, int);
// This function checks if a value exists in the array. If it exists,
// return the location in array
// 1st parameter: pointer to the dynamic array, 2nd parameter: size of array
// 3rd parameter: the value to be searched
int findInArray(int*, int, int);
// This function checks if a value exists in the array, and proceeds to
// replace it with a new value
// 1st parameter: pointer to the dynamic array, 2nd parameter: size of array
// 3rd parameter: the value to be searched, 4th parameter: the replacement value
bool replaceInArray(int*, int, int, int);
// This function expands the array size two times its original size.
// Create a new array that is double the original size, then copy over
// the contents of the original array. Ensure the new array is passed back.
// 1st parameter: pointer to the dynamic array, 2nd parameter: size of array
void expandArray(int*&, int&);
// MAIN FUNCTION does not need any modifications
int main() {
int* ptr;
int size = 10;
ptr = createArray(size);
for (int i = 0; i < size; i++) ptr[i] = i;
printArray(ptr, size);
reverseArray(ptr, size);
printArray(ptr, size);
int value = 7;
int loc = findInArray(ptr, size, value);
if (loc == -1)
cout << "The value " << value << " does not exist in the array." << endl;
else
cout << "The value " << value << " is found at location " << loc << " of the array." << endl << endl;
if (replaceInArray(ptr, size, value, 99)) {
printArray(ptr, size);
cout << "The value " << value << " has been replaced." << endl;
}
expandArray(ptr, size);
cout << "\nAfter expanding, the array now has size " << size << endl;
printArray(ptr, size);
delete[] ptr; // Prevent memory leak
ptr = nullptr;
}
/////////////////////////////////////////////////////////////////////////////
// function definitions
int* createArray(int n) { return new int[n]; }
void printArray(int* ptr, int size) {
cout << "Array: ";
// fill up
cout << endl;
}
void reverseArray(int*& ptr, int size) {
// fill up
}
int findInArray(int* ptr, int size, int value) {
// fill up
return -1;
}
bool replaceInArray(int* ptr, int size, int value, int replacement) {
// fill up
return false;
}
void expandArray(int*& ptr, int& size) {
// fill up
}
This “long question” provides you with more practice with a program that creates multiple functions to implement several operations for dynamic array.
Among them, the ability to print array contents, reverse array, find a value in the array, replace a value in the array, and expand the size of the array.
Study the code carefully in order to complete the program.
The function prototypes have been provided, with description of what the input parameters represent.
You are to complete the function definitions (bottom part of the source file) so that they the program produces the expected output.
You do not need to modify any part of the main() function.
The expected output is given below.
Sample Run:
Array: 0 1 2 3 4 5 6 7 8 9 Array: 9 8 7 6 5 4 3 2 1 0 The value 7 is found at location 2 of the array. Array: 9 8 99 6 5 4 3 2 1 0 The value 7 has been replaced. After expanding, the array now has size 20 Array: 9 8 99 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 0 0
If you have successfully completed Exercise 1.
Try re-writing the code into an ADT class called Array.
Note: The main() function must be modified to use the Array object
Why is this a good step to take?
Structurally, it makes sense to try “package” all these functions and the data (variables) involved into a new user-defined type using a class.
Notice how long and cumbersome the function parameters are?
Every function requires us to pass in the pointer, the size and more.
By making these functions as members of a class, we are constructing behaviours that belong to the array entity.
At the same time, we can also determine that the client program (main function in this case) is not allowed to access the data directly.
There are other array operations that you can implement. Try them!
Remove an element from the front or back of the array (i.e. the first or last element)
Remove a specific element from anywhere in the array (must know the value, and that value must exist in the array for removal to take place)
Insert from the front or back of the array
Apply a specific transformation to all contents of the array. You can use a char or string to indicate type of transformation to apply.
For e.g. ‘sqrt’ applies square root to all elements, ‘sq’ squares all elements, ‘hexa’ converts all elements to hexadecimal (reference: http://www.cplusplus.com/reference/ios/hex/), etc.
Clear the entire array