here is question:

Write a function that accepts an array of integers and its size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array.

here is what i got so far. I am so confuse right now!

#include<iostream>
using namespace std;

int *Shifted_array(const int *arr,int size)
{
    int *New_array=new int[size+1];
    int i,m;
    New_array[0] = 0;
    m=1;
    for(i=0;i<size;i++)
    {
        New_array[m] = arr[i];
        m++;
    }
    return New_array;
}

int main ()
{
    int array[30];
    int *Shifted;
    int size;
    int i;

    cout << "Enter size of array: " << endl;
    cin >> size;

    cout << "Enter elements: ";
    for(i=0;i<size;i++)
        cin >> array[i];
    Shifted=Shifted_array(array,size);
    cout << "The elements after shifting the array: " << endl;
    for(i=0;i<size+1;i++)
        cout << *(Shifted+i) << endl;

}

Recommended Answers

All 4 Replies

Your program looks correct -- what are you confused about?

when i run it. it doesnt return any thing

Program logic looks correct what's your confusion?

It worked for me

Enter size of array:
5
Enter elements: 1 2 3 4 5
The elements after shifting the array:
0
1
2
3
4
5
Press any key to continue . . .

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.