/********************** 04-3-S.TXT ******************** *********** Prof. Dr. B. Bartning *********************/ - Remarks about solutions - ATTENTION, please, here are only program fragments, this file cannot be compiled as a whole!! - WITHOUT guarantee! - ************************************************************** 01 // Iteration (Einmaleins) 02 #include 03 #include // für setw() 04 int main() 05 { 06 int run, result; 07 const int factor=6; 08 cout << "Multiplication table with number " << factor << "\n\n"; 09 for (run=1; run<=10; ++run) { 10 result = run*factor; 11 cout << setw(2) << run 12 << " * " 13 << setw(2) << factor 14 << " = " 15 << setw(2) << result 16 << endl; 17 } 18 return 0; 19 } **************************************************** (a) 07new const int factor=3; oder const int factor=423; **************************************************** (b) 07new int factor; cin >> factor; Settling factor not by initializing with "const int factor=...;", but by keyboard input. **************************************************** (c) 09new for (run=10; run>=1; --run) { **************************************************** (d) Output only each third line (but up to 30), i. e. 09new for (run=3; run<=30; run=run+3) { Or with the compound assignment operator (3.27): 09new for (run=3; run<=30; run+=3) { **************************************************** (e) - see (4.43), note 1- Instead of 09 until 17: run=1; while (run<=10) { result = run*factor; cout << setw(2) << run << " * " << setw(2) << factor << " = " << setw(2) << result << endl; ++run; } **************************************************** (f) Instead of 09 until 17: run=1; do { result = run*factor; cout << setw(2) << run << " * " << setw(2) << factor << " = " << setw(2) << result << endl; ++run; } while (run<=10); ****************************************************** (g) #include #include int main() { int row, col; cout << "Multiplication table\n\n"; cout << setw(4) << ' '; // oder: cout << " "; for (col=1; col<=10; ++col) cout << setw(4) << col; cout << endl; for (row=1; row<=10; ++row) { cout << setw(4) << row; for (col=1; col<=10; ++col) cout << setw(4) << row*col; cout << endl; } return 0; } Or with column/row delimiters: #include #include int main() { int row, col; cout << "Multiplication table\n\n"; cout << " | "; for (col=1; col<=10; ++col) cout << setw(4) << col; cout << endl; cout << " ---+-"; for (col=1; col<=10; ++col) cout << "----"; cout << endl; for (row=1; row<=10; ++row) { cout << setw(4) << row << " | "; for (col=1; col<=10; ++col) cout << setw(4) << row*col; cout << endl; } return 0; } ******************************************************