If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Sample Code Feedback.

Alright So, this is a simple code that I came up with for passing arrays to functions and performing a
mathematical operation on its elements.
Since I have no reference point, I would like to get an Idea of where I stand (Beginner @ the
moment, I know that much)
I would like to know if I am forming any bad habits or using some code incorrectly.
Now, I understand this is not a class but any feedback would be appreciated. I'm trying to
inch my way out of the newbie hole, and I want to make sure I'm doing it as correctly as possible

Plus, I've come to learn that not everything works the way they teach you in class, so.. anyway,

Re: Sample Code Feedback.

Ah, I knew about delete and I did forget...
Ok OK OK , now that you brought that up.. My original goal was to create a pointer to an array, and let the program
determine the size of the array at RunTime. I can't leave the brackets blank, I get an error, So i left like that with a 20... But how would I create a dynamic array so that it is only as big as it needs to be?
Thank you very much by the way.

Re: Sample Code Feedback.

Originally Posted by jbarrera03

My original goal was to create a pointer to an array, and let the program
determine the size of the array at RunTime. I can't leave the brackets blank, I get an error, So i left like that with a 20... But how would I create a dynamic array so that it is only as big as it needs to be?

As Victor said, you use a std::vector. Specifically, use the push_back function to add items dynamically. The vector will take care or all the required memory management.
There is hardly ever a need in C++ to use new []; you should use vector instead.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Re: Sample Code Feedback.

I'll add that indentation is supposed to indicate the relationship of one statement to another. If a statement is indented, the implication is that it executes based on the statement above it. Looking at your code

Code:

if (temp1 != 0)
{
pa[i]=temp1;
pa[i+1]=temp2;
i++;
i++;
}

relationships are implied that don't exist. Properly formatted it would look like this.