2. Table Creator Program Create a .cpp program named hw5-2.cpp that will create
ID: 3737070 • Letter: 2
Question
2. Table Creator Program
Create a .cpp program named hw5-2.cpp that will create a table as shown below where the user can specify by user input how many rows and columns to display.
Sample:
Enter how many columns you want your table to have: 4
Enter how many rows you want your table to have: 3
Row 1 Column 1 Row 1 Column 2 Row 1 Column 3 Row 1 Column 4
Row 2 Column 1 Row 2 Column 2 Row 2 Column 3 Row 2 Column 4
Row 3 Column 1 Row 3 Column 2 Row 3 Column 3 Row 3 Column 4
The main module of the program should do three things: accept in user input for how many columns the user wants, accept in user input for how many rows the user wants, call a user-defined function (module) passing in these two parameters. The function will then print a table using a nested loop as shown above. Use two tabs ( ) between each “Row x Column y”. You will have to search online to find out how to not have a print command start a new line. Then, start a new line after each row. Note: If you create a lot of columns, the window may print them on a new line if it is too small to accommodate them all.
Explanation / Answer
#include <iostream>
using namespace std;
void printTable(int r,int c) {
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
cout<<"Row "<<i<<" Column "<<j<<" ";
}
cout<<endl;
}
}
int main()
{
int r,c;
cout << "Enter how many columns you want your table to have: " << endl;
cin >> c;
cout<<"Enter how many rows you want your table to have: "<<endl;
cin >> r;
printTable(r,c);
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.