For loop and conditional statement to calculate value

Attempting to calculate the value of the Interest variable using the conditions below, but having problems returning 0 or if I reorder the for loop, it returns 6471 for each account. It would be great if you could help me fix the for loop so that the right interest is displayed on the console.

#include <iostream>

using namespace std;



int main()

{

int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
int interest = 0;

//add your code here



cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;
for (int i = 0; i < 8; i++)

    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

for (int i = 0; i < 8; i++)
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);



system("pause");
return 0;

}

here is my modification:

#include <iostream>

using namespace std;



int main()

{

int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
int interest = 0;

//add your code here


for (int i = 0; i < 8; i++)
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);

cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;
for (int i = 0; i < 8; i++)

    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;





system("pause");
return 0;

}
0
source share
3 answers
for (int i = 0; i < 8; i++)
{
    if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);
    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}

Output:

Account Number  Balance         Days    Interest
1001            4254.4          20      127
7940            27006.2         35      1620
4382            123.5           2       3
2651            85326.9         14      5119
3020            657             5       19
7168            7423.34         360     445
6245            4.99            1       0
9342            107864          45      6471
0
source
int interest = 0;

Your code first sets the variable interestto 0.

for (int i = 0; i < 8; i++)
    cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

, , , interest . interest 0, 0, .

, , - , interest . , , interest.

interest , .

+1

cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;

. 6471, , , 6471 , . , :

    for (int i = 0; i < 8; i++){
        if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
            interest = (Balance[i] * 0.06);
        else
            interest = (Balance[i] * 0.03);
cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}

Also, interest should be float . Hope this helps you: D

0
source

Source: https://habr.com/ru/post/1627633/


All Articles