I have been working on this code (school assignment) for a few days now. It seems to be almost finished, but I am not sure what's going on.
When I run it in an online complier, it displays correctly, which is supposed to look something like this
"12345"
"1234500000"
However, when I run it in Visual Studio, it's displaying the first number correctly, but instead of 5 zeros, it's coming out with a string of negative numbers.
Any ideas? I appreciate anyone taking the time to look it over or offer any hints.

#include <iostream>

using namespace std;

int *expandArray(int [] , int);
void showArray(int [], int);

int main()
{

    const int SIZE = 5;                                         
    int array[SIZE] = {1, 2, 3, 4, 5};
    int *arrayCopy;


    arrayCopy = expandArray(array, SIZE); 

    cout << "The contents of the original array are: \n";
    showArray(array, SIZE);

    cout << "Here is the new, expanded array: \n ";
    showArray(arrayCopy, (SIZE *2));

return 0;
} //end main


int *expandArray(int arr [], int size) 
{

    int *newArray = new int [size * 2];

    for (int index = 0; index < size; ++index) 
        newArray[index] = arr[index];

    for (int newElem = 10; newElem <(size*2); ++ newElem)
        newArray[newElem] = 0;

    return newArray;
} // end *expandArray


void showArray(int arr [], int size)
{
   for (int index = 0; index < size; index++)
        cout << arr[index] << " ";
        cout << endl;
} // end showArray 

The loop on line 36 is wrong

int *expandArray(int arr [], int size) 
{

    int *newArray = new int [size * 2];
    int index;
    for (index = 0; index < size; ++index) 
        newArray[index] = arr[index];

//    for (int newElem = 10; newElem <(size*2); ++ newElem)
    while(index < (size*2))
        newArray[index++] = 0;

    return newArray;
} // end *expandArray

You can also get the same result by not using the loops at all.

int *expandArray(int arr [], int size) 
{

    int *newArray = new int [size * 2];
    memset(newArray,0,size*2*sizeof(int));
    memcpy(newArray,arr,size*sizeof(int));
    return newArray;
} // end *expandArray

Asigning array of the same data type which is bigger copies the data. so i think its more efficient this way.

nt *exp(int *,int);

int main(){

    int ar[]={4,3,7,5,7,8,6,5,4,2};
    int *foo;
    foo = exp(ar,10);
    for_each(foo,foo+10,[](int &c){ cout <<c<<endl;});


  return 0;
}


int *exp(int *arr, int size){
    int *temp=new int(size*2);
      temp = arr;
      return temp;
}

line 17: That does not copy all the elements of one array into the other array, it is mearly an assignment operator that set the address of temp to be the same as arr and the memory allocated at line 16 is lost.

this produces an error

int *exp(int *arr, int size){
    int *temp=new int(size*2);
      temp = arr;
      for(int i = size; i < size*2; i++)
          temp[i] = i*2;  // <<<<<< Runtime error here
      return temp;
}

You are right if you go about it that way.
Another best form is to use the Algorithm copy.
like this

int *exp(int *arr, int size){
    int *temp=new int(size*2);
    copy(arr,arr+size,temp);

      return temp;
}

or this.

int *exp(int *arr, int size){
    int *temp=new int[size*2];
    for(int x=0;x<size;x++)
        temp[x]=arr[x];
    return temp;
}

but for quality... i will use the copy method.

Which is identical to what I posted 21 hours ago :) Now you're just repeating what's already been said.

that was not my intention.... i wanted to explain or proof why i support the copy method instead of the old style iteration.

Also the las code was a typo i made.

int *exp(int *arr, int size){
    int *temp=new int[size*2];
        temp=arr;
        for(int i=size;i<size*2;i++)
            temp[i]=i*2;
    return temp;
}

the mistake was using simgle data alloc.
ofcourse this can be optimised to call delete on temp.
Whats your say Dragon? I need your view mate. yea it trivial but hay is fun.

My compiler, vc++ 2012, produces errors when I compile your function that uses std::copy(). I changed it like this to remove the error

vector<int> exp(int *arr, int size){
    vector<int> temp(size*2);
    std::copy(arr, arr+size,temp.begin());
    return temp;
}

I used debugger to single-step through the code of std::copy(), and just as I suspected it eventually uses the standard C function memmove() to do the actual copy. Of course other compilers may implement std::copy() differently.

That says all about MS VC++. I compiled and run copy on my phone, later at home on my ubuntu and that was perfect. Also sure copy uses memmove() because C++ is build on C but copy is one of the best and easy way to achieve the said trivial task. Thats my take. ;-)

std::copy() does quite a bit of work before calling memmove(), so why not just call memmove() directly?

Also sure copy uses memmove() because C++ is build on C

That is only partially true -- C++ standards don't tell compiler writers how to implement c++ functions, just how the functions are supposed to work. std::copy() could have been implemented any number of different ways, such as using software loops. But on Intel-based systems memmove() is the most efficient way to do it because of the underlying assembly code instructions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.