Pointers And Dynamic Memory Allocation in C++ Homework Sample

This C++ programming assignment tackles the use of pointers. Write a function to return the minimum value in an array, use pointers instead of array references (* instead of []). Convert the function provided that swaps 2 values, into one that uses pointers instead of references. Create a function to copy an array of doubles, and doubles the size of the array and copies the values doubled to the new array and return a pointer to the start of the new array. Create a function that acts like substr but for arrays, taking the start of the array, the start index, and the length, that returns a new array that is a copy of the array segment.

Solution:

Assignment3.cpp

/*
* Assignment3.cpp
*/

#include <iostream>
using namespace std;

/**
* takes an int array and the array’s size as arguments. It should return the minimum value of the array elements.
*/
int minimum(int *array, int size)
{
// first min will be the first element
int min = *array;
// Find the min value
for (int i = 0; i < size; i++)
{
// If exist element smaller than min. Update min
if (min > *(array + i))
{
min = *(array + i);
}
}
return min;
}

/**
* The following function uses reference parameters. Rewrite the
function so it uses pointers instead of reference variables. When you test this
function from the main program, demonstrate that it changes the values of the
variables passed into it.
*/
int swapTimesTen(int *xptr, int *yptr)
{
int temp = *xptr;
*xptr = *yptr * 10;
*yptr = temp * 10;
return *xptr + *yptr;
}

/**
* takes an int array and the array’s size as arguments. It should
create a new array that is twice the size of the argument array. The function
should copy the contents of the argument array to the first half of the new array,
and the contents of the argument array each multiplied by 2 to the second half
of the new array. The function should return a pointer to the new array
*/
int *doubleArray(int *array, int size)
{
if (size <= 0)
return NULL;
int *newArray = new int[size * 2];
int i = 0;
// first half
for (i = 0; i < size; i++)
{
*(newArray + i) = *(array + i);
}
// second half
for (i = size; i < size * 2; i++)
{
*(newArray + i) = *(array + i – size) * 2;
}
return newArray;
}

/**
* takes an int array, a start index and a length as arguments. It
creates a new array that is a copy of the elements from the original array
starting at the start index, and has length equal to the length argument. For
example, subArray(aa,5,4) would return a new array containing only the
elements aa[5], aa[6], aa[7], and aa[8]. The function should return a pointer to
the new array. Do not call any other functions from this function
*/
int *subArray(int *array, int start, int length)
{
if (start <= 0)
return NULL;
int *result = new int[length];
for (int i = start; i < (start + length); i++)
{
*(result + i – start) = *(array + i);
}
return result;
}

int* duplicateArray(int *array, int size)
{
if (size <= 0)
return NULL;
int *result = new int[size];
// Duplicate element
for (int i = 0; i < size; i++)
{
*(result + i) = *(array + i);
}
return result;
}

/**
* takes an int array, a start index and a length as arguments. It
behaves exactly like subArray, however, you must define subArray2 as follows:

Add the code for the definition of the duplicateArray function from the lecture
slides for Unit 3 (or from the textbook) to your program. Add the code for the
subArray2 function given below to your program. Fill in the blanks with
expressions so that the function subArray2 behaves as the first subArray
*/
int *subArray2(int *array, int start, int length)
{
if (start <= 0)
return NULL;
// Using duplicate array. New array start at start posision
int *result = duplicateArray((array + start), length);
return result;
}

int main(int argc, char* argv[])
{
cout << “testing minimum:” << endl;
int testData[] = { 1, 2, 3, 4, 5, 6, 7, -8, 9, 0 };
cout << “test data:”;
for (int i = 0; i < 10; i++)
{
cout << ” ” << *(testData + i);
}
cout << endl;
cout << “Expected minimum: -8” << endl;
cout << “Actual mininum: ” << minimum(testData, 10) << endl;
int testData2[] = { 1, 2, 3, 4, 5, 16, 7, 8, 9, 0 };
cout << “test data:”;
for (int i = 0; i < 10; i++)
{
cout << ” ” << *(testData2 + i);
}
cout << endl;
cout << “Expected minimum: 0” << endl;
cout << “Actual mininum: ” << minimum(testData2, 10) << endl;

cout << endl << “testing swapTimesTen:” << endl;
int *a = new int;
int *b = new int;
*a = 3;
*b = 5;
cout << “Expected result: 80 a: 50 b: 30” << endl;
cout << “Actual result: ” << swapTimesTen(a, b) << ” a: ” << *a << ” b: ” << *b << endl;

cout << endl << “testing doubleArray:” << endl;
int testData3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << “test data:”;
for (int i = 0; i < 9; i++)
{
cout << ” ” << *(testData3 + i);
}
cout << endl;
cout << “Expected result: 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18” << endl;
cout << “Actual result: “;
int* resultDoubleArray = doubleArray(testData3, 9);
for (int i = 0; i < 9 * 2; i++)
{
cout << ” ” << *(resultDoubleArray + i);
}
cout << endl;
cout << endl << “testing subArray:” << endl;
int testData4[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
cout << “test data:”;
for (int i = 0; i < 10; i++)
{
cout << ” ” << *(testData4 + i);
}
cout << endl;
cout << “start: 5 length: 4” << endl;
cout << “Expected result: 6 7 8 9” << endl;
int *resultSubArray = subArray(testData4, 5, 4);
cout << “Actual result: “;
for (int i = 0; i < 4; i++)
{
cout << ” ” << *(resultSubArray + i);
}
cout << endl;
cout << endl << “testing subArray2:” << endl;
int testData5[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
cout << “test data:”;
for (int i = 0; i < 10; i++)
{
cout << ” ” << *(testData5 + i);
}
cout << endl;
cout << “start: 5 length: 4” << endl;
cout << “Expected result: 6 7 8 9” << endl;
int *resultSubArray2 = subArray2(testData4, 5, 4);
cout << “Actual result: “;
for (int i = 0; i < 4; i++)
{
cout << ” ” << *(resultSubArray2 + i);
}

delete[] resultDoubleArray;
delete[] resultSubArray;
delete[] resultSubArray2;

return 0;
}