pointers, arrays, for loop, class

This is a discussion on pointers, arrays, for loop, class within the C++ Programming forums, part of the General Programming Boards category; Alright, what I'm trying to do is use a for loop to calculate a monthly payment using a position in ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    pointers, arrays, for loop, class

    Alright, what I'm trying to do is use a for loop to calculate a monthly payment using a position in the array, and using a pointer to change the original value of the array to what I calculate. I then want to move to the next position in the array and repeat it all over again until I've used all array positions.

    I can successfully do this by using
    Code:
    	void calculatePayment( double *p )
    	{
    		for ( int i = 0; i < 5; i++)
    		{
    			p[i] = ( .05 + .05 / (pow(1 + .05, 36) -1 ) ) * p[i];
    		}
    	}
    however, I don't think that my teacher wants me to use this method for this assignment. The assignment says to "-create a function in a separate header file that uses a for loop to calculate the monthly payment for each of the loan amounts." and "-Use the pointer to the loan amounts to calculate the monthly payments to avoid returning them to main."
    She includes the following example:
    *p = (interest + interest / (pow(1 + interest,months) -1) ) *p;
    p++;

    I cannot figure out how to successfully include that in a for loop. Everything I've tried has resulted in errors that I can't make any sense out of. I would like some input on what I can do. I will include my entire program just to be safe.

    Code:
    // loan.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    using std::cout;
    using std::endl;
    using std::fixed;
    #include <iomanip>
    using std::setprecision;
    #include <cmath>
    using std::pow;
    #include "loan.h"
    
    
    int main()
    {
    	Loan myLoan;
    	double loan[] = {15000, 18000, 20000, 22000, 25000};
    	double *loanPtr;
    	loanPtr = loan;
    
    
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "Loan amount" << i << " is $" << fixed << setprecision( 2 )
    			<< loanPtr[i] << endl;
    	}
    	cout << endl;
    	
    	myLoan.calculatePayment( loanPtr );
    	
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "The payment amount for amount" << i << " is $" << fixed << setprecision( 2 )
    			<< loanPtr[i] << endl;
    	}
    
    	return 0;
    }
    Code:
    //loan.h
    #include <cmath>
    using std::pow;
    
    class Loan
    {
    public:
    	
    	void calculatePayment( double *p )
    	{
    		for ( int i = 0; i < 5; i++)
    		{
    			p[i] = ( .05 + .05 / (pow(1 + .05, 36) -1 ) ) * p[i];
    		}
    	}
    };
    Last edited by Lucid003; 10-18-2005 at 03:33 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    *p = (interest + interest / (pow(1 + interest,months) -1) ) *p;
    p++;
    Looks like an attempt to use pointer arithmetic, which in this case seems unnecessary. It can make sense when using char arrays that are null-terminated because you can do:
    Code:
    const char* str = "AString";
    for (;*str;++str)
      std::cout<<*str;
    But in this case the array isn't terminated by a special element so you would need a counter variable anyways.

    Also, in your main function you don't need the loanPtr variable, just pass the array to the function. And if the array is always 5 elements, the function prototype should look like:
    Code:
    void calculatePayment( double p[5])
    Otherwise, you need to pass the size of the array as well.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    Quote Originally Posted by JaWiB
    Code:
    *p = (interest + interest / (pow(1 + interest,months) -1) ) *p;
    p++;
    Looks like an attempt to use pointer arithmetic, which in this case seems unnecessary. It can make sense when using char arrays that are null-terminated because you can do:
    Code:
    const char* str = "AString";
    for (;*str;++str)
      std::cout<<*str;
    But in this case the array isn't terminated by a special element so you would need a counter variable anyways.

    Also, in your main function you don't need the loanPtr variable, just pass the array to the function. And if the array is always 5 elements, the function prototype should look like:
    Code:
    void calculatePayment( double p[5])
    Otherwise, you need to pass the size of the array as well.
    Thank you very much. So, basically, unless I'm using a char array, I cannot use p++? If that's the case, then I'm happy with my result.

    As for not creating the loanPtr, this assignment specifically says to "-define a pointer for the loan amounts" (I know I didn't say it before, and I'm sorry :P ), and it requires using the pointer to calculate the monthly payments to avoid returning them to main (which I did say).

    Anyway, I guess that answers my question. I do have one more that I'll throw out there, just in case: My loan amounts are 15000, 18000, 20000, 22000, and 25000. My payment amounts are SUPPOSED to come out as 449.56, 539.48, 599.42, 659.36, 749.27... however, using the equation that the teacher provided: *p = (interest + interest / (pow(1 + interest, months) -1) ) *p;
    does not yield those results. The teacher didn't include the interest or months on the assignment, so I had to ask her specifically for them. She told me that she thinks they are 5% interest and 36 months. So, can anyone see anything that I'm doing wrong as far as the math equation goes? Anyone know how I could get the required results without doing some serious trial and error? If not, I'll just ask the teacher next time I'm in class, but I'd rather have this done before then.

    Thanks again.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by Lucid003
    She includes the following example:
    *p = (interest + interest / (pow(1 + interest,months) -1) ) *p;
    p++;
    I assume the example is actually something like;
    Code:
    *p = (interest + interest / (pow(1 + interest,months) -1) ) * (*p);
    which (for simplicity), I'll re-express as
    Code:
    *p *= (interest + interest / (pow(1 + interest,months) -1) );
    I cannot figure out how to successfully include that in a for loop.

    A literal approach would be;
    Code:
    	void calculatePayment( double *p )
    	{
    		for ( int i = 0; i < 5; i++)
    		{
    		    *p *= ( .05 + .05 / (pow(1 + .05, 36) -1 ) );
                        ++p;
    		}
    	}
    which can be adjusted to;
    Code:
    	void calculatePayment( double *p )
    	{
    		for ( int i = 0; i < 5; ++i, ++p)
    		{
    		    *p *= ( .05 + .05 / (pow(1 + .05, 36) -1 ) ) ;
    		}
    	}
    or, if you want to eliminate the integer counter and play only with pointers;
    Code:
    	void calculatePayment( double *p )
    	{
                   double *end;
    		for (end = p + 5; p != end; ++p) 
    		{
    		    *p *= ( .05 + .05 / (pow(1 + .05, 36) -1 ) );
    		}
    	}
    p+5 gives the address of p[5] and you simply increment the pointer p until that address is reached. And, whatever you do, do NOT try to dereference p when it is equal to end (unless p refers to an array with more than 5 elements).

    Pointers are wonderful and mysterious things.

    You might also want to consider that this is equivalent....
    Code:
    	void calculatePayment( double *p )
    	{
    		for ( int i = 0; i < 5; i++)
    		{
    		    *(p + i) *= ( .05 + .05 / (pow(1 + .05, 36) -1 ) );
    		}
    	}
    as (for a pointer p) *(p + i) and p[i] are the same thing (they identify the i'th element of p).

    One of the more obscure corners of C and C++ is this: i[p] is the same as p[i].

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You can access the elements of an array using either array notation or pointer arithmetic. Here is an example:

    Code:
    #include <iostream>
    using namespace std;
    
    void testPointer(double* pArray, int size)
    {
    	//change the values using array notation:
    	for(int i=0; i<size; i++)
    	{
    		pArray[i] = i;
    	}
    
    	//display the array:
    	for(int x=0; x<size; x++)
    	{
    		cout<<pArray[x]<<endl;
    	}
    
    	//change the values using pointer arithmetic:
    	double* p = pArray; //set a pointer to the beginning of the array
    	for(int j=0; j<size; j++)
    	{
    		*p = j+10;  //*p is the value at the pointer
    		p++; //move the pointer along the array
    	}
    
    	//display the array:
    	p = pArray; //set the pointer back to the beginning of the array
    	for(int z=0; z<size; z++)
    	{
    		cout<<*p<<endl;
    		p++;
    	}
    }
    
    
    
    int main()
    {
    	double anArray[] = {10.0, 20.0, 30.0};
    	testPointer(anArray, 3);
    	
    	return 0;
    }
    Note: you can send an array to a function that has a pointer parameter. An array name acts like a pointer to the first element.
    Last edited by 7stud; 10-18-2005 at 07:06 PM.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Right. Like I said, pointer arithmetic is unnecessary in this case because it just makes it slightly harder to read. There's a reason subscripts exist
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Like I said, pointer arithmetic is unnecessary in this case because it just makes it slightly harder to read.
    That's not the point. Apparently, the assignment is being used to teach pointer arithmetic.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hm, well that's not how I interpreted it.

    In any case, I thought it was important to point out that this is a bad use of pointer arithemtic.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 04:58 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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