//
// main.cpp
// Week 2 Assignment 2
//
// Created by Adam Windsor on 8/29/14.
// Copyright (c) 2014 Adam Windsor. All rights reserved.
//
#include <iostream>
#include <iomanip>
usingnamespace std;
//Prototypes
int* expandArray(int[], int);
void showArray(int [ ], int);
int main()
{
constint SIZE = 5;
int array1[SIZE] = {1,2,3,4,5};
expandArray(array1, SIZE);
return 0;
}
int* expandArray(int array1[], int size)
{
int* array2;
int expanded; // to double the array
expanded = size *2;
array2 = newint[expanded];
for (int count = 0; count < expanded; count++) {
array2[count] = array1[count];
cout << array2[count] << " ";
}
return array2;
}
When I run the program, the output is 1,2,3,4,5,0,0,0,5,0. So I'm doing something right because the array is doubling in size! My first question is where is the 5 in the 9th spot coming from and how do I fix it to get it to display a 0?
line 37 - the values of count (0 through 9) will be valid for array2 but not the original array1 which only has 5 elements. Going out of bounds on an array may have undefined results.
Only half the new array needs to be copied from the old one. Maybe you want to initialize the other half with 0?