Sample Code Feedback.

• October 23rd, 2012, 03:51 PM
jbarrera03
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,

Thank You.

Code:

```//Harmonic Mean #include "stdafx.h" #include <iostream> void harmony ( double [],int ); void fill (); void main() {         fill();//Call function to fill array } void fill () {         int *pa = new double [20];  //declare pointer to array of type double of 20 blocks.         double temp1 = 0.0;         double temp2 = 0.0;         int i = 0;                         std::cout<<"Enter Pairs of Numbers Separated by a Space:"<<std::endl;         do // Prompt user for Input, use zero as a terminator.         {                 std::cout<<": ";                 std::cin>>temp1>>temp2;                                 if (temp1 != 0)                 {                         pa[i]=temp1;                                 pa[i+1]=temp2;                                         i++;                                                 i++;                                                                 }                         else                         break;         }         while(temp1 !=0);                         harmony (pa, i);                 return; } void harmony ( double arg[] , int limit ) {         double mean = 0.0;                 for(int x= 0; x < limit; x++)         {                  mean = 2 * (arg[x] * arg[x+1])/(arg[x] + arg[x+1]);                 x++;                 std::cout<<"Mean: "<<mean<<std::endl;         }         std::cin.get();                std::cin.get();                return; }```
• October 23rd, 2012, 04:15 PM
VictorN
Re: Sample Code Feedback.
1. main() has to declared as int, not void;
2. There is no need to create int *pa in the heap, because you already know its size (20); in that case you will need to delete pa[] what you missed. As an alternative you could use std::vector;
3. Your do/while loop does not check the overrun of pa array.

There may be some other problems in the code you have posted, but I currently cannot test it with some compiler...
• October 23rd, 2012, 04:23 PM
jbarrera03
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.
• October 23rd, 2012, 04:44 PM
VictorN
Re: Sample Code Feedback.
I already wrote that you could use std::vector. Or if you use MFC then MFC CArray class
• October 24th, 2012, 05:46 AM
D_Drmmr
Re: Sample Code Feedback.
Quote:

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.
• October 24th, 2012, 07:26 AM
GCDEF
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.
Code:

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