I am having difficulties with a program I have written.My program will compile, however, it does not make the numbers "expand" or double. I would appreciate if someone might be able to find out what I am doing wrong. The problem is:
Write a function that accepts an int array and the array's size as arguments. The function 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 new array, and intialize the unused elements of the second array with 0. The function should return a pointer to the new array.
//This program is used to accept an int array and the array's size as arguments, and then creates a new
//array that is twice the size of the argument array.
#include <iostream>
usingnamespace std;
int* expandSize (int*, int);
int main()
{
constint SIZE = 10; //Number of elements
int ary [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Used to hold numbers
int* num = ary;
for(int index= 0;index<SIZE;index++)
cout<<num[index]<<endl;
num = expandSize(ary, SIZE); //Used to assign num to the memory location of the return
for(int index=0;index<SIZE;index++)
cout<<num[index]<<endl;
delete[] num; //Used to delete memory
num = 0; //Used to point the unused elements of the second array with zero.
return 0;
}
//**********************************************************************************************************
//The function expandSize accepts an int array and the array's size as arguments. The function then creates*
//a new array that is twice the size of the argument array. *
//**********************************************************************************************************
int* expandSize(int* arr, int size)
{
int* expandArray=newint[size * 2];
for (int index=0;index<size;index++)
expandArray[index]=arr[index];
for (int index=size;index<size*2;index++)
expandArray[index]=0;
return expandArray;
}
Ok... I can't exactly pin point what you are referring to. Could you tell me what number line I need to work on then or what I need to correct? I apologize that I am so confused I have been working on this program for a couple days now and I simply cannot figure it out even after reading your advice.
Thank you for that, however, it is still not compiling right. It puts several zero's in it and a couple random numbers above the original numbers. I really appreciate your help and advice.
It compiles fine, otherwise you wouldn't see any output. The program itself is fine too.
You should post your new code, the output you get and just to be sure, the output you expect.
//This program is used to accept an int array and the array's size as arguments, and then creates a new
//array that is twice the size of the argument array.
#include <iostream>
usingnamespace std;
int* expandSize (int*, int);
int main()
{
constint SIZE = 10; //Number of elements
int ary [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Used to hold numbers
int* num=ary;
for(int index=0;index<SIZE;index++)
cout<<num[index]<<endl;
num=expandSize(ary, SIZE); //Used to assign num to the memory location of the return
for(int index=0;index<SIZE*2;index++)
cout<<num[index]<<endl;
delete[] num; //Used to delete memory
num = 0; //Used to point the unused elements of the second array with zero.
return 0;
}
//**********************************************************************************************************
//The function expandSize accepts an int array and the array's size as arguments. The function then creates*
//a new array that is twice the size of the argument array. *
//**********************************************************************************************************
int* expandSize(int* ary, int size)
{
int* expandArray=newint[size * 2];
for (int index=0;index<size;index++)
expandArray[index]=ary[index];
for (int index=size;index<size*2;index++)
expandArray[index]=0;
return expandArray;
}