Calcuate average with pointer

This is a discussion on Calcuate average with pointer within the C++ Programming forums, part of the General Programming Boards category; Hi Guys I am doing some homework and I have finished the assignment apart from one part. I need to ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Calcuate average with pointer

    Hi Guys

    I am doing some homework and I have finished the assignment apart from one part. I need to use a function that calcuates the average of all the passed grades. The grade total must be an integer based pointer ( for some bizzare reason ). The grades are integer. The below snippet compiles fine without error but when I run the debug and display the results I get 0.00 for the average.

    Code:
    void GradeBook::calculateGradeAverage( double m_StudentAverage, int *pm_GradeTotal, const int SIZE )
    {
       m_StudentAverage = (*pm_GradeTotal) / SIZE;
    }
    I am thinking the problem is that I am assigning an integer based pointer to a double value? Would a static_cast be an option?

    I can post the full code if you need more info.

    Thanks in advance.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The right-hand size is an integer division. Cast one of the two operands to double.
    But the code still won't have an effect, because modifying the local parameter m_StudentAverage (m_ ? Every convention that uses m_ uses it as a member variable prefix.) has no effect.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    i.e. change the double parameter to double&

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Or remove it completely if there really is a member variable with the same name.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    Or remove it completely if there really is a member variable with the same name.
    Yeah, that name really is confusing to use for function parameters, especially if the function is a lot longer than this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21