Sample Code Feedback.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Sample Code Feedback.

  1. #1
    Join Date
    Oct 2012
    Posts
    17

    Post 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;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    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...
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2012
    Posts
    17

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: Sample Code Feedback.

    I already wrote that you could use std::vector. Or if you use MFC then MFC CArray class
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Sample Code Feedback.

    Quote Originally Posted by jbarrera03 View Post
    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,229

    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++;
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center