So, I'm supposed to create an array with 7 elements, output it, then use a function to duplicate that array EXCEPT make it one element larger. Then it should shift all the elements to the right, replace the first element with '0'. It should still list all the original numbers. Having a bit of problem as the output I'm getting will not display the last number in the duplicate, and I've got no idea where I'm going wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int *duplicateArray(const int *, int);
int main()
{
const int SIZE = 7;
int testArray[SIZE] = {5, 10, 15, 20, 25, 30, 35};
int *newArray;
char repeat;
do
{
newArray = duplicateArray(testArray, SIZE);
cout << "Original Array:\n";
for(int i = 0; i < SIZE ; i++)
{
cout << testArray[i] << " ";
}
cout << "\nDuplicate Array:\n";
for(int i = 0; i < SIZE ; i++)
{
cout << newArray[i] << " ";
}
cout << "\n\nWould you like to run the program again? (Y/N)" << endl;
cin >> repeat;
cout << endl;
}while(repeat == 'y' || repeat == 'Y');
return 0;
}
int *duplicateArray(const int *testArray2, int size)
{
int *newArray;
newArray = new int[size + 1];
newArray[0] = 0;
for(int i = 0; i < size; i++)
{
newArray[i + 1] = testArray2[i];
}
return newArray;
}
|
For example, the code will print out 5, 10, 15, 20, 25, 30, 35 for the original array, but for the duplicate, it will only print out 0, 5, 15, 20, 25, 30 and not the 35.
I'd really appreciate some tips/advice as to where I'm going wrong.