Multiplication Table in a Void Function

Jan 20, 2015 at 10:12am
Good Day guys, I wanted to make a multiplication table but it seems not that easy for a newbie like me. Mind taking your time and see what I am missing in my code? It would be a great help. Here's the code:

#include<iostream>
#include<conio.h>
using namespace std;

void initArray(int arg1[50][50], int r, int c)
{
int val=1;
for(int row=0; row<r; row++)
{
for(int col=0; col<c; col++)
{
arg1[row][col]=val;
val++;
}
}
}

void mulTable(int arg1[50][50], int r, int c)
{
for(int row=0; row<r; row++)
{
int mul=0;
for(int col=0; col<c; col++)
{
mul = mul * arg1[row][col];
cout << mul << " ";
}
}
}


int main(){
int arr1[50][50], val=1;
int row = 0, col = 0;

cout <<" Enter number of Row: ";
cin >>row;
cout <<"Enter number of Column: ";
cin >> col;

initArray(arr1,row,col);
mulTable(arr1,row,col);


getch();
return 0;
}



I want the output to be like this:
1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
If the user inputs 3 rows and 3 columns. Thank you. :)
Last edited on Jan 20, 2015 at 11:18am
Jan 20, 2015 at 10:36am
void mulTable(int arg1[50][50], int r, int c)
{
for(int row=0; row<r; row++)
{
int mul=1;
for(int col=0; col<c; col++)
{
mul = mul * arg1[row][col];
cout << mul << " ";
}
}
}

just initialize mul by 1 inside the mulTable Function
Last edited on Jan 20, 2015 at 10:37am
Jan 20, 2015 at 11:27am
@lusifer : It was still an error, but thanks for the effort. :)
Jan 20, 2015 at 6:40pm
Since you don't need to do anything with the results after you create them, there's no need to store the table at all. This pseudocode will print the inside of the table:
1
2
3
for row = 1 to r
    for col = 1 to c
        print row * col


Create C++ code that does this an put it in your main program. You can get rid of initArray and mulTable altogether. Once you have code working to print the inside of the table, add code to print the row and column headings.

Topic archived. No new replies allowed.