Multiplication table program

Very small program, just need some advice

Page 1 of 1

2 Replies - 1054 Views - Last Post: 23 March 2009 - 07:42 AM

#1 Robb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 16-October 06

Multiplication table program

Post icon  Posted 22 March 2009 - 04:54 PM

Wasn't exactly sure where to post this. It's not exactly a "project", just something I made when helping someone else in the C and C++ forum, a post you may or may not have seen.

I just wanted some advice and ideas on how to clean this code up, make it look neater and delete anything unnecessary in there. I've looked at the code so much that I can't tell what is and isn't completely necessary. I need some fresh eyes on it.

#include <iostream>
#include <string>
using namespace std;

int numlength(int n)
{
    if (n == 0)
        return 1;
    int count = 0;
    while (n != 0)
    {
        n /= 10;
        count++;
    }
    return count;
}

int main(void)
{
    int bN;
    int eN;
    int rows, cols;
    
    cout << "** Multiplication Table **" << endl << "Enter a starting value: ";
    cin >> bN;
    cout << "Enter an ending value: ";
    cin >> eN;
    cout << endl;

    int rN;
    rN = bN;
    int count = 0;

    rows = eN - bN + 1;
    cols = eN - bN + 1;
    
    /* This bit adds a space to the numbers at the side if they
    are minus numbers and bN has more digits than eN */
    string spacer2 = "";
    if (bN < 0 && (numlength(bN) > numlength(eN))) spacer2 += " ";
    if (bN > 0 && (numlength(bN) < numlength(eN))) spacer2 += " ";
    
    string s = "";
    if (numlength(bN) >= 2)
    {
        int repeat;
        repeat = (numlength(bN) - 1);
        while (repeat > 0)
        {
            s += " ";
            repeat--;
        }
    }
    string minus = "";
    if (bN < 0 && bN > -10) minus += " ";
    cout << minus << s << " x" << spacer2 << " |";   // Displays " x |"
    for (int i = 0; i < rows; i++)  // Display numbers across top
    {
        string spacer = "";
        string spacer3 = "";
        if (rN < 10 && rN > 0) spacer += "  ";
        else if (rN > 0 || (numlength(bN) < 2 && bN < 0)) spacer += " ";
        if (rN >= 0 && rN - 1 < 0) spacer += " ";
        if (bN < 0 && eN > 0 && numlength(bN * eN) >= 2)
            spacer3 += " ";
        cout << s << spacer << rN << spacer3;
        rN++;
    }
    rN = bN;
    cout << endl;
    int cumulativeValue = 0;
    int valueAmount = 0;
    while (count < cols)
    {
        string minus2 = "";
        if (bN < 0 && bN+count >= 0) minus2 += " ";
        cout << minus2 << " " << bN+count;    // Display numbers down the side
        if ((bN < 0 && numlength(bN+count) == numlength(bN)) ||
         (bN > 0 && numlength(bN+count) == numlength(eN)) || eN == 0) cout << " |";
        else cout << spacer2 << " |";
        
        for (int n = 0; n < rows; n++) // Display the numbers on each column
        {
            string spacer = "";
            if ((rN * (bN+count)) < 10 && (rN * (bN+count)) >= 0) spacer = "  ";
                else spacer = " ";
            if (n == 0 && bN == 0)
                spacer = " ";
            if (rN < 0 && bN+count < 0)
            {
                int rNminus;  // Values used for following if statements
                int bNcminus;
                if (rN < 0 && rN - 1 >= bN) rNminus = rN - 1;
                    else rNminus = rN;
                if (bN+count < 0 && bN+count - 1 >= bN) bNcminus = bN;
                    else bNcminus = bN+count;
                if (n > 0 && rN < 0
                 && numlength(rNminus*bNcminus) > numlength(rN * (bN+count))
                 && (numlength(bN+count) - numlength(rN)) <= 0 
                 && numlength(rN * (bN+count)) > 1)
                    spacer += " ";
            }
            
            int rNminus;  // Values used for following if statements
            int bNcminus;
            if (rN < 0 && rN - 1 >= bN) rNminus = rN - 1;
                else rNminus = rN;
            if (bN+count < 0 && bN+count - 1 >= bN) bNcminus = bN;
                else bNcminus = bN+count;
            
            if (n == 1 && numlength(rNminus * (bN+count)) >= 2         // 1st term only
                       && numlength(rN * (bN+count)) < 2
                       && (numlength(bN * bN) > numlength(rNminus * (bN+count))
                       ||  numlength(eN * eN) > numlength(rNminus * (bN+count))))
                spacer += " "; 
            
            if (bN < 0 && eN > 0 && numlength(bN * eN) >= 2)
            {
                if (n != 0)
                    if ((rN * (bN+count)) > 0)  spacer += " ";
                else if (numlength(rN * (bN+count)) < 2 && (numlength(rNminus * (bN+count)) < 2))
                    spacer += " ";
            }
            
                cout << spacer << rN * (bN+count);
                rN++;
                cumulativeValue += (rN * (bN+count));
                valueAmount++;
        }
        count++;
        cout << endl;
        rN = bN;
    }
    cout << endl << "The sum of all the values: " << cumulativeValue;
    cout << endl << "The average of all the values: " << (cumulativeValue / valueAmount);
    cout << endl;
    return 0;
}



So any comments on how to clean the code up would be appreciated. Also, if you want to test it to see if there are any bugs or anything. I've checked it all and I can't see anything and I spend some time making sure all the numbers would align correctly, but if you spot something then brilliant and I'll fix it. :)

Also, would this be the kind of thing that would be eligible to be submitted as a code snippet if I commented it all and explained how it works?

Thanks in advance :^:

EDIT: Btw, bN stands for beginningNumber, eN for endNumber, rN for rollingNumber (a variable which transitions between bN and eN without having to change the original bN and eN variables). Any other questions, let me know. :)

This post has been edited by Robb: 22 March 2009 - 04:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication table program

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Multiplication table program

Posted 22 March 2009 - 07:23 PM

Ya for sure, first of all try instead of having to tell us what certain variables mean, try just naming them with those full names. This way, whoever reads it will actually understand what's going on without having to read a README
Was This Post Helpful? 1
  • +
  • -

#3 Robb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 16-October 06

Re: Multiplication table program

Posted 23 March 2009 - 07:42 AM

Yeah I suppose, I used those short names while programming to make it less tedious having to type them out all the time.

Thanks markhazlett9 :)
I'll make that adjustment, any other ideas?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1