Author Topic: [C++] Multiplication Table!  (Read 3135 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[C++] Multiplication Table!
« on: September 24, 2005, 08:15:40 pm »
Yeah! This is the baddest-ass hello world there ever was. Please make fun of me for the way I did the padding, and then please show me how to do it right. =)

joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated MultTable.cpp -o MultTable && ./MultTable
XXX 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020
001 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020
002 002 004 006 008 010 012 014 016 018 020 022 024 026 028 030 032 034 036 038 040
003 003 006 009 012 015 018 021 024 027 030 033 036 039 042 045 048 051 054 057 060
004 004 008 012 016 020 024 028 032 036 040 044 048 052 056 060 064 068 072 076 080
005 005 010 015 020 025 030 035 040 045 050 055 060 065 070 075 080 085 090 095 100
006 006 012 018 024 030 036 042 048 054 060 066 072 078 084 090 096 102 108 114 120
007 007 014 021 028 035 042 049 056 063 070 077 084 091 098 105 112 119 126 133 140
008 008 016 024 032 040 048 056 064 072 080 088 096 104 112 120 128 136 144 152 160
009 009 018 027 036 045 054 063 072 081 090 099 108 117 126 135 144 153 162 171 180
010 010 020 030 040 050 060 070 080 090 100 110 120 130 140 150 160 170 180 190 200
011 011 022 033 044 055 066 077 088 099 110 121 132 143 154 165 176 187 198 209 220
012 012 024 036 048 060 072 084 096 108 120 132 144 156 168 180 192 204 216 228 240
013 013 026 039 052 065 078 091 104 117 130 143 156 169 182 195 208 221 234 247 260
014 014 028 042 056 070 084 098 112 126 140 154 168 182 196 210 224 238 252 266 280
015 015 030 045 060 075 090 105 120 135 150 165 180 195 210 225 240 255 270 285 300
016 016 032 048 064 080 096 112 128 144 160 176 192 208 224 240 256 272 288 304 320
017 017 034 051 068 085 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340
018 018 036 054 072 090 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360
019 019 038 057 076 095 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380
020 020 040 060 080 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400


Code: [Select]
#include <iostream.h>
#include <stdio.h>

int main() {
  int a;
  int b;
  int length;
  char padding[] = "000";

  cout << "XXX 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020" << endl;
  for(a = 1; a < 21; a++) {
    if(a >= 100) {
      cout << a << " ";
    } else {
      if(a >= 10) {
        cout << "0" << a << " ";
      } else {
        cout << "00" << a << " ";
      }
    }
    for(b = 1; b < 21; b++) {
      if(a*b >= 100) {
        cout << a*b << " ";
      } else {
        if(a*b >= 10) {
          cout << "0" << a*b << " ";
        } else {
          cout << "00" << a*b << " ";
        }
      }     
    }
    cout << "\n";
  }
  return 0;
}

EDIT -
I love it so much I wanted to share it with you all!
« Last Edit: September 26, 2005, 07:51:03 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [C++] Multiplication Table!
« Reply #1 on: September 24, 2005, 08:28:41 pm »
The way I do padding is, printf("%03d", number);

I'm afraid I don't know how with C++, but hopefully somebody here does. 

And btw, I think it would look nicer if you padded with ' ', but that's just my opinion.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C++] Multiplication Table!
« Reply #2 on: September 24, 2005, 09:36:57 pm »
I was getting errors with printf earlier, something about it not being defined. I wonder if thats fixed with the headers I have included now.

Anyhow, I took your advice of the space padding. Looks a whole lot better.

joe@JoeMomma:~/dev/cpp/helloworld $ ./MultTable
      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
  1   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
  2   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40
  3   3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60
  4   4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80
  5   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100
  6   6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 102 108 114 120
  7   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119 126 133 140
  8   8  16  24  32  40  48  56  64  72  80  88  96 104 112 120 128 136 144 152 160
  9   9  18  27  36  45  54  63  72  81  90  99 108 117 126 135 144 153 162 171 180
 10  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200
 11  11  22  33  44  55  66  77  88  99 110 121 132 143 154 165 176 187 198 209 220
 12  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240
 13  13  26  39  52  65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260
 14  14  28  42  56  70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280
 15  15  30  45  60  75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300
 16  16  32  48  64  80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320
 17  17  34  51  68  85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340
 18  18  36  54  72  90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360
 19  19  38  57  76  95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380
 20  20  40  60  80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C++] Multiplication Table!
« Reply #3 on: September 25, 2005, 12:20:35 am »
Using iago's method..

joe@JoeMomma:~/dev/cpp/helloworld $ g++ -Wno-deprecated MultTable2.cpp -o MultTable2 && ./MultTable2
   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
   1   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
   2   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40
   3   3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60
   4   4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80
   5   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100
   6   6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 102 108 114 120
   7   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105 112 119 126 133 140
   8   8  16  24  32  40  48  56  64  72  80  88  96 104 112 120 128 136 144 152 160
   9   9  18  27  36  45  54  63  72  81  90  99 108 117 126 135 144 153 162 171 180
  10  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200
  11  11  22  33  44  55  66  77  88  99 110 121 132 143 154 165 176 187 198 209 220
  12  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240
  13  13  26  39  52  65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260
  14  14  28  42  56  70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280
  15  15  30  45  60  75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300
  16  16  32  48  64  80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320
  17  17  34  51  68  85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340
  18  18  36  54  72  90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360
  19  19  38  57  76  95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380
  20  20  40  60  80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400


Code: [Select]
#include <iostream.h>
#include <stdio.h>

int main() {
    int a;
    int b;
    // Define variables
    // A is the row
    // B is the column

    for(a = 0; a < 21; a++) {
        printf("% 4d", a, " ");
    }
    printf("\n");
    // Print column tops


    for(a = 1; a < 21; a++) {  // For each row
        printf("% 4d", a, " ");  // Print the row header
        for(b = 1; b < 21; b++) { // For each column cell in the row
            printf("% 4d", a*b, " "); // Print the value
        }
        printf("\n");  // Go to the next line
    }
    return 0; // Return success
}
« Last Edit: September 25, 2005, 12:30:23 am by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Darkness

  • x86
  • Full Member
  • ***
  • Posts: 234
    • View Profile
Re: [C++] Multiplication Table!
« Reply #4 on: September 26, 2005, 07:11:44 pm »
I especially like how you close a url tag with [/b]. ;)
(01:22:35) Newby: I am listening to 9989. Torsofuck - Fistfucking Her Decomposed Cadaver (0:07 / 2:37 320kbps)
(01:22:39) Newby: I am listening to 9990. Torsofuck - Worm Infested Anal (0:01 / 3:02 320kbps)
(01:22:43) Newby: I am listening to 9991. Torsofuck - Raped By Elephants (0:02 / 4:14 320kbps)

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: [C++] Multiplication Table!
« Reply #5 on: September 26, 2005, 07:19:47 pm »
Wow Joe. For once, you have done something that isn't totally retarded.

Nice hello world. :P
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C++] Multiplication Table!
« Reply #6 on: September 26, 2005, 07:50:03 pm »
Thanks Newby.

Darkness, let me fix that.. =p
I'd personally do as Joe suggests

You might be right about that, Joe.