INSTRUCTIONS Write a program that displays a 2D multiplication table based on ro
ID: 3528575 • Letter: I
Question
INSTRUCTIONS
Write a program that displays a 2D multiplication table based on row and column values specified by the user.The multiplication table should line up in an aesthetically pleasing manner and mimic the following format:
Please enter the number of columns: 8
Please enter the number of rows: 6
|1|2|3|4|5|6|7|8|
+---+---+---+---+---+---+---+---+
1|1|2|3|4|5|6|7|8|
2|2|4|6|8| 10| 12| 14| 16|
3|3|6|9| 12| 15| 18| 21| 24|
4|4|8| 12| 16| 20| 24| 28| 32|
5|5| 10| 15| 20| 25| 30| 35| 40|
6|6| 12| 18| 24| 30| 36| 42| 48|
Here is what I got so far:
#include<iostream>
using namespace std;
int main() {
int col,row,i, j;
char;
// ask for inputs for table
cout<<" Please enter the number of columns: ";
cin>>col;
cout<<" Please enter the number of rows:";
cin>>row;
cout<<" ";
// display column headings
for(i = 1; i <= col; i++) {
cout<< "| " << i << one;
}
cout<<" ";
// print splitter row
cout<<"+---";
for(i = 1; i <= col; i++) {
cout<<"+---";
}
cout<<" ";
// display rows
for(i = 1; i <= row; i++) {
cout<<" ", i;
for(j = 1; j <= col; j++) {
cout<<"| ", i * j;
}
cout<<" ";
}
return 0;
}
Explanation / Answer
/** there are two small mistakes with cout , corrected it, ****/
/*** working code is below***/
#include<iostream>
using namespace std;
int main() {
int col,row,i, j;
char;
// ask for inputs for table
cout<<" Please enter the number of columns: ";
cin>>col;
cout<<" Please enter the number of rows:";
cin>>row;
cout<<" ";
// display column headings
for(i = 1; i <= col; i++) {
cout<< "| " << i << one;
}
cout<<" ";
// print splitter row
cout<<"+---";
for(i = 1; i <= col; i++) {
cout<<"+---";
}
cout<<" ";
// display rows
for(i = 1; i <= row; i++) {
cout<<" "<< i;
for(j = 1; j <= col; j++) {
cout<<"| "<< i * j;
}
cout<<" ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.