#include #include using namespace std; int main() { long USD, maxUSD; // Amount in USD double rate; // Exchange rate USD <-> MYR cout << "\n* * * * TABLE OF EXCHANGE * * * *\n "; cout << "* United State Dollar (USD) to Malaysia Ringgit (MYR) *\n\n"; cout << "\nPlease give the rate of exchange: "; cout <<" \n\n 1 USD in RM MYR: "; cin >> rate; cout << "\n Please enter the USD you want to change: "; cin >> maxUSD; // Outputs the table & Titles of columns: cout << "\n If Rate Now is : " << rate << endl; cout << "\n so, USD to RM exchange is : "<< endl; cout << '\n'<< setw(12) << "USD" << setw(20) << "MYR"; cout << '\n'<< setw(16) << "---------------------------------------------" ; cout << '\n'<< setw(24) << " >>> " ; // Formatting RM-MYR: cout << setprecision(2) << endl; long lower, upper, // Lower and upper limit step; // Step width // The outer loop determines the actual // lower limit and the step width: for( lower=1, step=1; lower <= maxUSD; step*= 10, lower = 2*step) { for( USD = lower, upper = step*10; USD <= upper && USD <= maxUSD; USD+=step) { cout << setw(12) << USD; cout << setw(20) << USD*rate << endl} } // The inner loop outputs a "block": cout << '\n'<< setw(16) << "---------------------------------------------" ; cout << "\n\n"; return 0; } 4.7.1 explain what the program does? 4.7.2 What is the output.
4.7 for repetition
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long USD, maxUSD; // Amount in USD
double rate; // Exchange rate USD <-> MYR
cout << "\n* * * * TABLE OF EXCHANGE * * * *\n ";
cout << "* United State Dollar (USD) to Malaysia Ringgit (MYR) *\n\n";
cout << "\nPlease give the rate of exchange: ";
cout <<" \n\n 1 USD in RM MYR: ";
cin >> rate;
cout << "\n Please enter the USD you want to change: ";
cin >> maxUSD;
// Outputs the table & Titles of columns:
cout << "\n If Rate Now is : " << rate << endl;
cout << "\n so, USD to RM exchange is : "<< endl;
cout << '\n'<< setw(12) << "USD" << setw(20) << "MYR";
cout << '\n'<< setw(16) << "---------------------------------------------" ;
cout << '\n'<< setw(24) << " >>> " ;
// Formatting RM-MYR:
cout << setprecision(2) << endl;
long lower, upper, // Lower and upper limit
step; // Step width
// The outer loop determines the actual
// lower limit and the step width:
for( lower=1, step=1; lower <= maxUSD;
step*= 10, lower = 2*step)
{
for( USD = lower, upper = step*10;
USD <= upper && USD <= maxUSD; USD+=step)
{
cout << setw(12) << USD;
cout << setw(20) << USD*rate << endl}
} // The inner loop outputs a "block":
cout << '\n'<< setw(16) << "---------------------------------------------" ;
cout << "\n\n";
return 0;
}
4.7.1 explain what the program does?
4.7.2 What is the output.
Step by step
Solved in 3 steps with 3 images