My program works but the numbers are not correct.

This is a discussion on My program works but the numbers are not correct. within the C++ Programming forums, part of the General Programming Boards category; here is my assisgnment and I can compile it and run it but the numbers dont seem to be correct....could ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    My program works but the numbers are not correct.

    here is my assisgnment and I can compile it and run it but the numbers dont seem to be correct....could someone take a look and see if my call function is wrong? thanks

    Bryan

    Assignment:
    If a principal amout P, for which the interest is compunded Q times per year, is placed in a savings account, then the amount of money in the account (the balance) after N years is given by the following formula, where I is the annual interest rate as a floating-point number:
    balance = P x (1 + I/Q)^NxQ

    Write a C++ program that inputs the values for P, I, Q, and N and outputs the balance for each year up through year N. Use a value-returning function to compute the balance. Your program should prompt the user appropriately, label the output values, and have a good style.

    Code:
    #include <iostream>
    
    #include <cmath>
    
    void Balance(float, float, int, int, float&);
    
    using namespace std;
    
    int main()
    
    {
    	float P,I,B;
    	int Q,i,N;
    
    	cout << "Enter the principal: ";
    	cin >> P;
    	cout << "Enter the annual interest rate: ";
    	cin >> I;
    	cout << "Enter the number of times money compounded per year: ";
    	cin >> Q;
    	cout << "Enter the number of years: ";
    	cin >> N;
    
    	for (i = 1; i <= N; i++)
    
    	{
    		Balance(P,I,Q,i,B);
    		cout << "The balance after " << i << " year(s), is: "
    			<< B << "." << endl;
    	}
    	return 0;
    
    }
    	void Balance(float P, float I, int Q, int N, float& B)
    
    {
    	B = (P * pow((1.0 + I/Q),N*Q));
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Function call looks correct. You may be getting truncation errors due to the use of the integers, however.

    Try changing 1.0+I/Q to 1.0+I/static_cast<float>(Q).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    that change gives me the same results...this is my results...I dont think this is correct.

    Enter the principla: 15000
    Enter the annual interest rate: 5.9
    Enter the number of times money componded per year: 6
    Enter the number of years: 7

    The balance after 1 year(s), is: 912989.
    The balance after 2 year(s), is: 5.55699e+007
    The balance after 3 year(s), is: 3.38232e+009
    The balance after 4 year(s), is: 2.05868e_011
    The balance after 5 year(s), is: 1.25303e_013
    The balance after 6 year(s), is: 7.62671e+014
    The balance after 7 year(s), is: 4.64207e_016
    Press any key to continue

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I needed to enter the persentage as .059 vs 5.9. I did that and it works great. Here is the final code in case any one wants to see the difference. Also I made it a return statement vs void.

    Bryan
    Code:
    #include <iostream>
    
    #include <cmath>
    
    float Balance(float, float, int, int, float&);
    
    using namespace std;
    
    int main()
    
    {
    	float P,I,B;
    	int Q,i,N;
    
    	cout << "Enter the principal: ";
    	cin >> P;
    	cout << "Enter the annual interest rate: ";
    	cin >> I;
    	I=I/100; 
    	cout << "Enter the number of times money compounded per year: ";
    	cin >> Q;
    	cout << "Enter the number of years: ";
    	cin >> N;
    
    	for (i = 1; i <= N; i++)
    
    	{
    		Balance(P,I,Q,i,B);
    		cout << "The balance after " << i << " year(s), is: "
    			<< B << "." << endl;
    	}
    	return 0;
    
    }
    	float Balance(float P, float I, int Q, int N, float& B)
    
    {
    	return B =(P * pow((1.0 + I/Q),N*Q));
    }

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Great.
    Now if you'd only sell the original version to my bank.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. somebody help me with this array program
    By hieugene in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 04:53 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Help with program code re prime numbers
    By Anna Lane in forum C Programming
    Replies: 3
    Last Post: 11-16-2002, 09:48 AM
  5. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM

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