#include <iostream>  
using namespace std; 
int *growArray (int* p_values, int cur_size); 
int main ()
{  
int next_element = 0;  
int size = 10;  
int *p_values = new int[ size ]; 
int val; 
cout << "Please enter a number: ";  
cin >> val;  
while ( val > 0 )  
{  
if ( size == next_element + 1 )  
{    // now all we need to do is implement growArray    
p_values = growArray( p_values, size );   
}
p_values[ next_element ] = val;   
cout << "Please enter a number (or 0 to exit): ";   cin >> val;  }  } 

 int *growArray (int* p_values, int cur_size) 
 { 
 int *p_new_values = new int[ cur_size * 2 ]; 
 for ( int i = 0; i < cur_size; ++i )
 { 
 p_new_values[ i ] = p_values[ i ];  
 } 
 delete p_values; 
 return p_new_values; 

// Again i cant understand this code pplease help me

What part of the code dont you understand? Did you write this code? You should also look into indenting your code properly. The way you have it now makes it very hard to read. If I wrote this code it would look like this:

#include <iostream>  
using namespace std; 
int *growArray (int* p_values, int cur_size); 
int main ()
{  
    int next_element = 0;  
    int size = 10;  
    int *p_values = new int[ size ]; 
    int val; 
    cout << "Please enter a number: ";  
    cin >> val;  
    while ( val > 0 )  
    {  
        if ( size == next_element + 1 )  
        {    // now all we need to do is implement growArray    
            p_values = growArray( p_values, size );   
        }
        p_values[ next_element ] = val;   
        cout << "Please enter a number (or 0 to exit): ";   cin >> val;
    }  
} 

int *growArray (int* p_values, int cur_size) 
{
    int *p_new_values = new int[ cur_size * 2 ]; 
    for ( int i = 0; i < cur_size; ++i )
    { 
        p_new_values[ i ] = p_values[ i ];  
    } 
    delete p_values; 
    return p_new_values; 
}
if ( size == next_element + 1 )  
        {    // now all we need to do is implement growArray    
            p_values = growArray( p_values, size );   
        }

how does this block get executed? when the value of element isnt incremented or updated in anyway.
i feel that the value of the element is going to be 1 forever.....but when i runt it the code does what its supposed to do....somone please help me

and i am sorry for not indenting

I think the code should have been :

if (size==next_element)
  { 
     // THIS Function doesn't work yet
     p_values= growArray(p_values,size);
  }
p_values[ next_element++ ] = val

But then you will have to fix growArray that also doesn't work. I will leave that for you to figure out.

The code runs fine without any modification!!!

The code runs fine without any modification!!!

You may think it does, but it certainly doesn't.
Put a cout message before the call to growArray:

if ( size == next_element + 1 )  
{    // now all we need to do is implement growArray
    cout << "\nCalling growArray()\n";        
    p_values = growArray( p_values, size );   
}

You'll find that growArray is never called.
If you increment next_element and growArray is called, you'll strike another problem in that size never changes to reflect the new array size. The growArray function needs to be changed so that it passes a reference (or pointer) to size and in the function the value of size needs to be adjusted.
int* growArray(int* p_values, int& cur_size);

Memory allocated with new[] needs to be released using delete[], not just delete.
int *p_values = new int[size]; should have a corresponding call to delete[] p_values;

gosh c++ is so tuff... :(...but thanks nullptr and all the guyz for helping me out...m very greatful to u all

Here's a possible implementation of growArray with some printing + comments so that you can see how it works.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int* growArray(int* p_values, int& cur_size); 

int main ()
{  
    int next_element = 0;  
    int size = 10;  
    int *p_values = new int[ size ]; 
    int val; 
    cout << "Please enter a number: ";  
    cin >> val;  
    while ( val > 0 )  
    {  
        p_values[next_element++] = val;

        if (size == next_element)  
        {    
            // now all we need to do is implement growArray    
            p_values = growArray(p_values, size);   
        }

        cout << "Please enter a number (or 0 to exit): ";   
        cin >> val;
    } 

    // clean up
    delete[] p_values;

    return 0;
} 

int* growArray(int* p_values, int& cur_size) 
{
    cout << "\ngrowing...\n";
    int *p_new_values = new int[cur_size * 2];

    for ( int i = 0; i < cur_size; ++i )
    { 
        // print the current array content and 
        // copy to new array
        cout << p_values[i] << " ";
        p_new_values[i] = p_values[i];  
    } 
    cout << "\nfinished growing!\n" << endl;
    // update cur_size to the new size
    cur_size *= 2;

    delete[] p_values; 

    return p_new_values; 
}
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.