Assignment problem

Hello! It's my first time posting in this forum and I'm having a problem. I know its basic but a big reference of help would encourage me to have a big insight into the subject.


1. In this activity, you will modify the savings account program. Modify the program to allow the user to enter the minimum and maximum interest rates, as shown in Figure 9-40. Test the program appropriately


Output required:

Deposit:1000
Minimum rate(in decimal form): 0.02
Maximum rate(in decimal form): 0.04

Rate 2%
Year 1: $1020.00
Year 2: $1040.40
Year 3: $1061.21
Rate 3%
Year 1: $1030.00
Year 2: $1060.90
Year 3: $1092.73
Rate 4%
Year 1: $1040.00
Year 2: $1081.60
Year 3: $1124.86


//my code are as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;


double twopercentrate (int amount, int rate, int y );
double threepercentrate (int amount, int rate, int x );
double fourpercentrate (int amount, int rate, int z );


int main(){
	
	int deposit = 0;
	double minrate = 0.0;
	double maxrate = 0.0;
	double accbal = 0.0;
	
	cout << "Deposit: ";
	cin  >> deposit;
	cout << "Minimum Rate (in decimal form): ";
	cin  >> minrate;
	cout << "Maximum Rate (in decimal form): ";
	cin  >> maxrate;
	
	
	
	
   cout << fixed << setprecision(2);
    for(int rate = 1; rate < 4; rate +=1){
    	cout << "Rate %" << rate << endl;
	

	cout << fixed << setprecision(2);
	for(int year = 1; year < 4; year +=1){
			
	cout << "year " << year << ": $" << endl;

    }
    
	}
	
	
}

double twopercentrate(int amount, int rate, int y ){
	double balance = 0.0;
	balance = amount * pow((2 + rate), y);
	return balance;
	
	
}

double threepercentrate(int amount, int rate, int x ){
	double balance = 0.0;
	balance = amount * pow((3 + rate), y);
	return balance;
	
	
}

double fourpercentrate(int amount, int rate, int z ){
	double balance = 0.0;
	balance = amount * pow((4 + rate), y);
	return balance;
	
	
}


I'm also having a problem on how to create a formula basing on the double function area and connect it to the main program.


a quick reply would be a big help for me.
Thank you in advance!
Last edited on
Hello zephile,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


No offense meant. It is the two links that you should read.

It is customary to post the code you are having a problem with. In this case you should post the code that you have done so people can advise you on what to do. As of now no one knows what you have done only what needs to be changed.

Generally it is best to post complete code that can be compiled and worked with. And if there is an input file post its contents so everyone is working with the same information.

Hope that helps,

Andy
Thank you for the info sir I will be sure to keep that in mind next time. :)
Welcome to the forum!

When posting code, please use code tags. You can edit your post, highlight the code and click the <> button to the right of the edit window. This will cause the code to show up with line numbers, syntax highlighting and in a fixed-width font, all of which make it easier to read and comment on.

Now to your code.

Your for loop should go from the min rate to the max rate. Let's make it the actual rate, so it should be a double instead of an int:
for (double rate = minrate; rate <= maxrate; rate += 0.01) {

Your code to compute the balance (threepercentrate, fourpercentrate(), etc) isn't right for a couple of reasons: first, what if they what 5%? Or 3.3% or 18.125? You would need a single function that takes the rate as one parameter and computes the new balance. Second, the interest compounds. After the first year, you add the interest to the balance and the second year, you take the interest on the NEW balance.

Computing the new balance is pretty easy. You don't really need a function at all, so get rid of twopercentrate, threepercentrate and fourpercentrate. If balance is the starting balance for the year and rate is the interest rate then:
1
2
double interest = balance * rate;  // compute the interest payment
balance += interest;     // add that interest to the balance, creating the new balance. 

Or with some simple math:
balance *= (1+rate);

Now the loop should print the current balance and then compute the new balance for next year.

I'm having trouble arranging this in order. Can I have a complete sample statement in which what goes where in my program?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;

int main(){
	
	int deposit = 0;
	double minrate = 0.0;
	double maxrate = 0.0;
	double balance = 0.0;
	
	cout << "Deposit: ";
	cin  >> deposit;
	cout << "Minimum Rate (in decimal form): ";
	cin  >> minrate;
	cout << "Maximum Rate (in decimal form): ";
	cin  >> maxrate;
	
	
	
	
   cout << fixed << setprecision(2);
    for (double rate = minrate; rate <= maxrate; rate +=1) {

    	cout << "Rate " << rate << endl;
		{

	cout << fixed << setprecision(2);
	for(int year = 1; year < 4; year +=1){
	      double interest = balance * rate;     
          balance *= (1+rate);

	cout << "year " << year << ": $" << endl;

    }
    
	
}

}

system("pause");
return 0;
}
I strongly urge you to indent the code so it reflects the actual block structure as defined by the { and } characters.

Line 26: You're inputting the rate in decimal form, i.e., 0.02 for 2%. That means you should increase it by 0.01 at each step.

Line 28: Since you're inputting rate as a decimal, you probably want to display rate*100 here.

Line 33 has no effect.
Line 36: you need to print the balance.

There will still be at least one bug if you do these things, but lets deal with that when we get to it.
Okay so I had at least fixed a few minor issues regarding to your post and I seem to have a problem in which the output currently lacks the required output.


Show us the latest version.
i'm currecntly using devc++ 5.1.1 in our lab
No, the latest version of your code.
I still can't solve this problem :/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;

int main(){
	
	int deposit = 0;
	double minrate = 0.0;
	double maxrate = 0.0;
	double balance = 0.0;
	
	cout << "Deposit: ";
	cin  >> deposit;
	cout << "Minimum Rate (in decimal form): ";
	cin  >> minrate;
	cout << "Maximum Rate (in decimal form): ";
	cin  >> maxrate;
	
	
	 cout << fixed << setprecision(2);
    for (double rate = minrate; rate <= maxrate; rate +=1) {

    	cout << "Rate " << rate << endl;
		{

	cout << fixed << setprecision(2);
	for(int year = 1; year < 4; year +=1){
	      double interest = balance * rate;     
          balance *= (1+rate);

	cout << "year " << year << ": $" << endl;

    }
    
	
}

}

system("pause");
return 0;
}

To give you an idea...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>

int main()
{
	double initialDeposit{};
	double minRate{};
	double maxRate{};
	double subTotal{};
	double balance{};

	std::cout <<"Deposit: ";
	std::cin >> initialDeposit;

	std::cout << "Minimum rate(in decimal form): ";
	std::cin >> minRate;

	std::cout << "Maximum rate(in decimal form): ";
	std::cin >> maxRate;

	std::cout << std::fixed << std::setprecision(2);

	/*
		Set a nested for loop. The outer loop use the minimum rate and the maximum rate as the initial parameters
		of the outer foor loop. It uses the static cast from the double variable to the integer to count the loop
		as integers.
	*/
	for (int count = static_cast<int>(minRate * 100); count <= static_cast<int>(maxRate * 100); count++)
	{
		balance = initialDeposit;
		std::cout << "\nRate " << count << "%";

		/*
			The inner loop is to display the years and perform the calculations to obtain the balance each year.
		*/
		for (int countYr = 1; countYr < 4; countYr++)
		{
			std::cout << "\nYear " << countYr << ": $";
			subTotal = balance * (count * .01);
			balance += subTotal;

			std::cout << balance;
		}

	}

	return 0;
}
Thanks for the reference! I'll make sure to make my own version of code for this one. I'll be updating this once I finish my other work.
Topic archived. No new replies allowed.