Almost there

I need to write a program: write a program to display a hexadecimal multiplication table for the number 1 to 16.  The first row and first column numbers should be displayed in decimal, but the contents of the table should be displayed in hexadecimal.  The program's out should look like:
Hexadecimal Multiplication Table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 1 2 3 4 5 6 7 8 9 a b c d e f 10
2 2 4 6 8 a c e 10 12 14 16 18 1a 1c 1e 20
3 3 6 9 c f 12 15 18 1b 1e 21 24 27 2a 2d 30
4 4 8 c 10 14 18 1c 20 24 28 2c 30 34 38 3c 40
5 5 a f 14 19 1e 23 28 2d 32 37 3c 41 46 4b 50
6 6 c 12 18 1e 24 2a 30 36 3c 42 48 4e 54 5a 60
7 7 e 15 1c 23 2a 31 38 3f 46 4d 54 5b 62 69 70
8 8 10 18 20 28 30 38 40 48 50 58 60 68 70 78 80
9 9 12 1b 24 2d 36 3f 48 51 5a 63 6c 75 7e 87 90
10 a 14 1e 28 32 3c 46 50 5a 64 6e 78 82 8c 96 a0
11 b 16 21 2c 37 42 4d 58 63 6e 79 84 8f 9a a5 b0
12 c 18 24 30 3c 48 54 60 6c 78 84 90 9c a8 b4 c0
13 d 1a 27 34 41 4e 5b 68 75 82 8f 9c a9 b6 c3 d0
14 e 1c 2a 38 46 54 62 70 7e 8c 9a a8 b6 c4 d2 e0
15 f 1e 2d 3c 4b 5a 69 78 87 96 a5 b4 c3 d2 e1 f0
16 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 100

Here is what I have so far
1
2
3
4
5
6
7
8
9
10
11
  cout << "Hexadecimal Multiplication Table:" << endl;
	for (int y = 1; y <= 16; ++y)
	{
		cout << setw(3) <<dec<< y << " ";
		for (int x = 1; x <= 16; ++x)
			cout << setw(5) <<hex<< x*y;
		cout << endl;
	}

	return 0;
}


The output prints correctly, but my headers is not in decimal, but in hex.
How can I set it to dec?
Your code only shows one output manipulator, and it is "hex", so you're not going to get
any dec numbers from it.

Separate the top header as it's own item, written to output in dec.

Then increment your left column to give row numbers. I used 4 column fields to fit my window.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout << "Hexadecimal Multiplication Table:" << endl;

    // Do the top row header numbers

    for (int y = 0; y <= 16; ++y )
    {
        cout << setw(4) << dec << y ;
    }

    cout << endl;

    for (int y = 1; y <= 16; ++y )
    {
        cout << setw(4) << dec << y;

        for (int x = 1; x <= 16; ++x)
        {
            cout << setw(4) <<hex<< x*y;
        }
        cout << endl;
    }

    return 0;
}

Hexadecimal Multiplication Table:
   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
   1   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f  10
   2   2   4   6   8   a   c   e  10  12  14  16  18  1a  1c  1e  20
   3   3   6   9   c   f  12  15  18  1b  1e  21  24  27  2a  2d  30
   4   4   8   c  10  14  18  1c  20  24  28  2c  30  34  38  3c  40
   5   5   a   f  14  19  1e  23  28  2d  32  37  3c  41  46  4b  50
   6   6   c  12  18  1e  24  2a  30  36  3c  42  48  4e  54  5a  60
   7   7   e  15  1c  23  2a  31  38  3f  46  4d  54  5b  62  69  70
   8   8  10  18  20  28  30  38  40  48  50  58  60  68  70  78  80
   9   9  12  1b  24  2d  36  3f  48  51  5a  63  6c  75  7e  87  90
  10   a  14  1e  28  32  3c  46  50  5a  64  6e  78  82  8c  96  a0
  11   b  16  21  2c  37  42  4d  58  63  6e  79  84  8f  9a  a5  b0
  12   c  18  24  30  3c  48  54  60  6c  78  84  90  9c  a8  b4  c0
  13   d  1a  27  34  41  4e  5b  68  75  82  8f  9c  a9  b6  c3  d0
  14   e  1c  2a  38  46  54  62  70  7e  8c  9a  a8  b6  c4  d2  e0
  15   f  1e  2d  3c  4b  5a  69  78  87  96  a5  b4  c3  d2  e1  f0
  16  10  20  30  40  50  60  70  80  90  a0  b0  c0  d0  e0  f0 100

Process returned 0 (0x0)   execution time : 0.036 s


(my first posting, pardon if I got something wrong in procedures)
Last edited on
That is Fantastic

Thank you.
You might want to adjust your output to display your hex in upper case letter-digits, depending on preferences.

Also note that I changed your top header row index to 0 so things would line up properly. Always examine your output for accuracy, what you had originally was not correct.
Topic archived. No new replies allowed.