Thread: Multiplication Table in a Void Function

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    17

    Multiplication Table in a Void Function

    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:
    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
    2 4 6
    3 6 9
    If the user inputs 3 rows and 3 columns. Thank you.

  2. #2
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    The reason your getting all 0's as your output is because in your mulTable function you set your mul variable to 0, and never increment it. So your only multiplying the values in your array by 0 each time.

  3. #3
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    Disclaimer: This may or may not be the best way of doing this.

    So, if you print your array out with the values it currently holds you get:

    1 2 3
    4 5 6
    7 8 9

    If you multiply these, you aren't going to get the values that you want.

    But, you can do exactly what you want, without even using an array doing the following code:
    Code:
    void mulTable(int r, int c)
    {
        for(int row=0; row<r; row++)
        {
            int mul=0;
    
    
            for(int col=0; col<c; col++)
            {
                mul = (col + 1) * (row + 1);
                cout << mul << " ";
            }
            cout<<"\n";
        }
    }

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    @Disclaimer : That helps dude, gonna learn from it. Have a good day.

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    @Disclaimer : Hey dude, mind taking a little time and help me with another question that I have in mind? What if I wanted an output like this:
    0 1 2 3
    1 1 2 3
    2 2 4 6
    3 3 6 9
    If the inputted row and column is 3. Just wondering.

  6. #6
    Registered User setleaf's Avatar
    Join Date
    Dec 2014
    Location
    Virginia/USA
    Posts
    47
    A very slight modification to the previous mulTable code would give you that easily. Did you play around with it at all to see if you could make it do what you wanted?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplication table
    By yatesj05 in forum C Programming
    Replies: 7
    Last Post: 04-17-2011, 02:46 AM
  2. C Multiplication Table
    By trueman1991 in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 07:58 AM
  3. Multiplication table
    By freddyvorhees in forum C++ Programming
    Replies: 6
    Last Post: 08-02-2008, 11:09 AM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM
  5. multiplication table help
    By cprogstudent in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:32 PM

Tags for this Thread