How do i make it so that in the multiplication and division
How do i make it so that in the multiplication and division table, the first row and column give the dimensions of the table. For a 4x2 table, it should be something like
0 1 2 3 4
1 1 2 3 4
2 2 4 6 8
This is what I have so far!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char choice;
cout << \"Welcome to the math study guide.\" << endl;
cout << \"Which arithemetic table would you like to see?\" << endl;
cout << \"1) Addition\" << endl;
cout << \"2) Subtraction\" << endl;
cout << \"3) Multiplication\" << endl;
cout << \"4) Division\" << endl;
cout << \"X) Exit the program\" << endl;
cin >> choice;
int X,Y;
cout << \"Enter the dimensions of the table.\ \";
cout << \"Enter X: \";
cin >> X;
cout << \"Enter Y: \";
cin >> Y;
if (X==0 || Y==0)
cout << \"Invalid dimensions\" << endl;
switch (choice)
{
case \'1\': cout << \"You chose Addition.\ \";
for (int i = 0 ; i <=Y; i++)
{
for (int j = 0 ; j <=X; j++)
{
cout << setw(3) << i+j << \" \"; }
cout << endl;
}
break;
case \'2\': cout << \"You chose Subtraction.\ \";
for (int i = 0 ; i <=Y; i++)
{
for (int j = 0 ; j <=X; j++)
{
cout << setw(3)<< abs(i-j) << \" \"; }
cout << endl;
}
break;
case \'3\': cout << \"You chose Multiplication.\ \";
for (int i = 0 ; i <=Y; i++)
{
for (int j = 0 ; j <=X; j++)
cout <<setw(3) << i*j<< \" \";
cout << endl;
}
break;
case \'4\': cout << \"You chose Division.\ \";
for (double i = 0 ; i <=Y; i++)
{
for (double j = 1 ; j <=X; j++)
{
cout << setw(4)<< setprecision(2)<< (j)/i << \" \"; }
cout << endl;
}
break;
case \'X\': cout << \"Exit the program.\ \";
break;
default: cout << \"You did not enter a valid input.\ \";
break;
}
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files to run the program
 #include <iomanip>
using namespace std;
int main() // driver program to run the code
 {
    char choice; // required local initialisations of the variables
    cout << \"Welcome to the math study guide.\" << endl; // prompt for the user to enter the data
    cout << \"Which arithemetic table would you like to see?\" << endl;
    cout << \"1) Addition\" << endl;
    cout << \"2) Subtraction\" << endl;
    cout << \"3) Multiplication\" << endl;
    cout << \"4) Division\" << endl;
    cout << \"X) Exit the program\" << endl;
    cin >> choice;
    int X,Y;
    cout << \"Enter the dimensions of the table.\ \"; // prompt for the user to enter the dimensions of the matrix
    cout << \"Enter X: \";
    cin >> X;
    cout << \"Enter Y: \";
    cin >> Y;
    if (X==0 || Y==0) // checking for the condition
    cout << \"Invalid dimensions\" << endl;
    switch (choice) // switching over the choice entered
    {
    case \'1\': cout << \"You chose Addition.\ \"; // case if the addition is selected
        for (int i = 0 ; i <=Y; i++) // iterating over the loops to print the data
        {
            for (int j = 0 ; j <=X; j++)
            {
                cout << setw(3) << i+j << \" \"; // printing the data over the loops with the width of 3 decimal places
            }
            cout << endl;
        }
        break;
       
    case \'2\': cout << \"You chose Subtraction.\ \"; // case if the subtraction is selected
        for (int i = 0 ; i <=Y; i++) // iterating over the loops to print the data
        {
            for (int j = 0 ; j <=X; j++)
            {
                cout << setw(3)<< abs(i-j) << \" \"; // printing the absolute data over the loops with the width of 3 decimal places
            }
            cout << endl;
        }
        break;
       
    case \'3\': cout << \"You chose Multiplication.\ \"; // case if the multiplication is selected
        for (int i = 0 ; i <=Y; i++) // iterating over the loops to print the data
        {
            for (int j = 0 ; j <=X; j++)
            {
                if(i == 0){ // now to check the data to print the index values over the data
                cout << setw(3) << i+j << \" \"; // prints the row index values
                } else if(j == 0){
                cout << setw(3) << i+j << \" \"; // prints the column index values
                } else {
                cout << setw(3) << i*j << \" \"; // prints the inner data in the matrix
                }
            }          
            cout << endl;
        }
        break;
       
    case \'4\': cout << \"You chose Division.\ \"; // case if the division is selected
        for (double i = 0 ; i <=Y; i++) // iterating over the loops to print the data
        {
            for (double j = 0 ; j <=X; j++)
            {
                if(i == 0){ // now to check the data to print the index values over the data
                cout << setw(5) << i+j << \" \"; // prints the row index values
                } else if(j == 0){
                cout << setw(5) << i+j << \" \"; // prints the column index values
                } else {
                cout << setw(5)<< setprecision(2)<< (j)/i << \" \"; // prints the inner data in the matrix
                }
            }
            cout << endl;
        }
        break;
       
    case \'X\': cout << \"Exit the program.\ \"; // exiting the program if the option is selected
        break;
       
    default: cout << \"You did not enter a valid input.\ \"; // defaultly prints the invalid data as the prompt
        break;
    }
 }
 OUTPUT :
Case 1 :
Welcome to the math study guide.
 Which arithemetic table would you like to see?
 1) Addition
 2) Subtraction
 3) Multiplication
 4) Division
 X) Exit the program
 1
 Enter the dimensions of the table.
 Enter X: 4
 Enter Y: 2
 You chose Addition.
 0 1 2 3 4
 1 2 3 4 5
 2 3 4 5 6
   
 Case 2 :
Welcome to the math study guide.
 Which arithemetic table would you like to see?
 1) Addition
 2) Subtraction
 3) Multiplication
 4) Division
 X) Exit the program
 2
 Enter the dimensions of the table.
 Enter X: 4
 Enter Y: 2
 You chose Subtraction.
 0 1 2 3 4
 1 0 1 2 3
 2 1 0 1 2
   
 Case 3 :
Welcome to the math study guide.
 Which arithemetic table would you like to see?
 1) Addition
 2) Subtraction
 3) Multiplication
 4) Division
 X) Exit the program
 3
 Enter the dimensions of the table.
 Enter X: 4
 Enter Y: 2
 You chose Multiplication.
 0 1 2 3 4
 1 1 2 3 4
 2 2 4 6 8
   
 Case 4 :
Welcome to the math study guide.
 Which arithemetic table would you like to see?
 1) Addition
 2) Subtraction
 3) Multiplication
 4) Division
 X) Exit the program
 4
 Enter the dimensions of the table.
 Enter X: 4
 Enter Y: 2
 You chose Division.
 0 1 2 3 4
 1 1 2 3 4
 2 0.5 1 1.5 2
   
Explanation :
The i and j values have been checked so that the index values could be printed in the multiplication and division methods apart from printing the data.
Hope this is helpful.





