User Tag List

Results 1 to 12 of 12

Thread: Fun Programming Problem

  1. #1
    Moderator
    • abir's Gadgets
      • Motherboard:
      • Gigabyte G41M-ES2L | Intel 965 HP
      • CPU:
      • Intel Pentium Dual Core E5400 2.7GHz 800MSB| Intel T5670 Core 2 Duo 1.8GHz 800MHz FSB
      • RAM:
      • (2+2) 4GB DDR II (800MHz) | 1GB RAM
      • Hard Drive:
      • 320 GB SATA Samsung | 160GB SATA
      • Graphics Card:
      • Sapphire Radeon 4770 512MB | Intel GMA X3100
      • Display:
      • DELL Inspiron 18.5" LCD |14.1" TFT LCD
      • Sound Card:
      • Realtek Builtin Audio
      • Speakers/HPs:
      • Creative Inspire 2.1
      • Keyboard:
      • Genius Slimstar 355 Gaming Keyboard | A4Tech Anti RSI (USB PS2)
      • Mouse:
      • A4tech 7K Office (USB)| HP
      • Power Supply:
      • Deluxe DLP 388A 450W
      • Optical Drive:
      • ASUS 16X DVD Drive & Liteon DVDW| Asus DVD RAMRW LightScribe
      • USB Devices:
      • 4GB Apacer Pendrive
      • UPS:
      • OVO 650VA
      • Operating System:
      • Windows 7 x64 | Windows XP SP2 & Ubuntu 9.04
      • ISP:
      • Smile Internet (Bronze)
    abir's Avatar
    Join Date
    Feb 2008
    Location
    Azimpur
    Posts
    7,913

    Talking Fun Programming Problem

    take the following code:
    Code:
    int i, n = 20; 
    for (i=0; i<n; i--) 
    { 
    cout << "x" << endl; 
    }
    by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

    find 3 ways to make the above code print x 20 times (by changing only one character).

    If you find all the THREE ways, post in spoiler.
    Don't post result without SPOILER.

    "Game after game after game, I realized what is most important of my life - FOOTBALL.."
    I bleed red, Man Utd 4ever..
    ---------------------
    অনেক দূরের একলা পথে, ক্লান্ত আমি ফিরি তোমার কাছে, মুখোশ খুলে বসে রই জানলার ধারে..

  2. #2
    Member
    • furiousTaher's Gadgets
      • Motherboard:
      • Asus z170p || Asus p8 P67m pro
      • CPU:
      • i7 6700k || i5 2400
      • RAM:
      • 2 x Ripjaws 8gb 3200C16D 16GVKB || (Transcend 4gb + Adata 4gb) 1333
      • Hard Drive:
      • Evo 850 250gb + Toshiba 2tb || WD 500gb blue
      • Graphics Card:
      • Zotac gtx 1060 amp || Sapphire r7 260x 1GB
      • Display:
      • Asus vx229h ||Samsung 21.5&quot; S22A300B
      • Sound Card:
      • Xonar dgx
      • Speakers/HPs:
      • Microlab 223 || AltecLansing VS2621 + a4tech hs100
      • Mouse:
      • a4tech x7
      • Power Supply:
      • Adata HM 850w || Thermaltek 600 TR2 S
      • Optical Drive:
      • Asus dvd writer 24x max
      • USB Devices:
      • Phantom 240 red/black || Vatyn 664b (Tyrannosaurus)
      • UPS:
      • Power guard 1200va
      • Comment:
      • :D
    furiousTaher's Avatar
    Join Date
    Apr 2010
    Location
    Dhaka
    Posts
    8,151

    Default

    Quote Originally Posted by abir View Post
    take the following code:
    Code:
    int i, n = 20; 
    for (i=0; i<n; i--) 
    { 
    cout << "x" << endl; 
    }
    by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

    find 3 ways to make the above code print x 20 times (by changing only one character).

    If you find all the THREE ways, post in spoiler.
    Don't post result without SPOILER.
    what????
    :O @abir bhai shoytan i-- disen issa kore emnitei unlimited astese

  3. #3
    Noblesse Oblige
    • CvP's Gadgets
      • Motherboard:
      • ASUS P8Z77 V Delux
      • CPU:
      • Intel Core i7 3770K @3.9GHz
      • RAM:
      • GSkill TridentX 2x8GB DDR3 @2400MHz
      • Hard Drive:
      • Crucial M4 256GB SSD; WD Caviar Black 2TB; Hitachi 250GB; Samsung S3 1.5TB; Hitachi 2TB
      • Graphics Card:
      • EVGA 980Ti 6GB
      • Display:
      • Samsung 17&quot;+22&quot;+22&quot;
      • Sound Card:
      • Realtek HD Audio &lt;internal&gt;
      • Speakers/HPs:
      • Creative Inspire 5:1
      • Keyboard:
      • Razer BlackWidow Ultimate
      • Mouse:
      • G7
      • Power Supply:
      • Seasonic X 850W 80Plus Gold
      • Optical Drive:
      • External DVD-RW
      • USB Devices:
      • Logitech Carl Zeiss Tessar HD 1080p Webcam
      • UPS:
      • Mercury 1500VA with Large Lead-Acid Battery
      • Operating System:
      • Genuine Windows 10 Pro
      • Comment:
      • Everything packed in a NZXT Phantom 820
      • ISP:
      • Link3/Amber IT
      • Download Speed:
      • 10000
      • Upload Speed:
      • 10000
    CvP's Avatar
    Join Date
    Feb 2008
    Location
    Dhaka, Bangladesh
    Posts
    9,019

    Default

    solution #1:
    Code:
    int i, n = 20; 
    for (i=0; i<n; n--) 
    { 
    cout << "x" << endl; 
    }


    solution #2:
    Code:
    int i, n = 20; 
    for (i=0; i+n; i--) 
    { 
    cout << "x" << endl; 
    }

    solution #3:
    Code:
    int i, n = 20; 
    for (i=0;-i<n; i--) 
    { 
    cout << "x" << endl; 
    }
    Last edited by CvP; January 15th, 2012 at 20:22.
    The abuse of greatness is when it disjoins remorse from power.
    Please do not PM me for support. You will NOT get a reply. Post in the relevant forum section.

  4. #4
    Moderator
    • abir's Gadgets
      • Motherboard:
      • Gigabyte G41M-ES2L | Intel 965 HP
      • CPU:
      • Intel Pentium Dual Core E5400 2.7GHz 800MSB| Intel T5670 Core 2 Duo 1.8GHz 800MHz FSB
      • RAM:
      • (2+2) 4GB DDR II (800MHz) | 1GB RAM
      • Hard Drive:
      • 320 GB SATA Samsung | 160GB SATA
      • Graphics Card:
      • Sapphire Radeon 4770 512MB | Intel GMA X3100
      • Display:
      • DELL Inspiron 18.5&quot; LCD |14.1&quot; TFT LCD
      • Sound Card:
      • Realtek Builtin Audio
      • Speakers/HPs:
      • Creative Inspire 2.1
      • Keyboard:
      • Genius Slimstar 355 Gaming Keyboard | A4Tech Anti RSI (USB PS2)
      • Mouse:
      • A4tech 7K Office (USB)| HP
      • Power Supply:
      • Deluxe DLP 388A 450W
      • Optical Drive:
      • ASUS 16X DVD Drive &amp; Liteon DVDW| Asus DVD RAMRW LightScribe
      • USB Devices:
      • 4GB Apacer Pendrive
      • UPS:
      • OVO 650VA
      • Operating System:
      • Windows 7 x64 | Windows XP SP2 &amp; Ubuntu 9.04
      • ISP:
      • Smile Internet (Bronze)
    abir's Avatar
    Join Date
    Feb 2008
    Location
    Azimpur
    Posts
    7,913

    Default

    Quote Originally Posted by CvP View Post
    solution #1:
    Code:
    int i, n = 20; 
    for (i=0; i<n; n--) 
    { 
    cout << "x" << endl; 
    }
    solution #2:
    Code:
    int i, n = 20; 
    for (i=0; i+n; i--) 
    { 
    cout << "x" << endl; 
    }
    There is another one, i found that first but missed your #2! you can consider whitespace as a character

    Quote Originally Posted by Taher fu®ious™ View Post
    what????
    :O @abir bhai shoytan i-- disen issa kore emnitei unlimited astese
    Yah your duty is make it print 20 times just changing a character in 3 ways

    "Game after game after game, I realized what is most important of my life - FOOTBALL.."
    I bleed red, Man Utd 4ever..
    ---------------------
    অনেক দূরের একলা পথে, ক্লান্ত আমি ফিরি তোমার কাছে, মুখোশ খুলে বসে রই জানলার ধারে..

  5. #5
    Member
    • furiousTaher's Gadgets
      • Motherboard:
      • Asus z170p || Asus p8 P67m pro
      • CPU:
      • i7 6700k || i5 2400
      • RAM:
      • 2 x Ripjaws 8gb 3200C16D 16GVKB || (Transcend 4gb + Adata 4gb) 1333
      • Hard Drive:
      • Evo 850 250gb + Toshiba 2tb || WD 500gb blue
      • Graphics Card:
      • Zotac gtx 1060 amp || Sapphire r7 260x 1GB
      • Display:
      • Asus vx229h ||Samsung 21.5&quot; S22A300B
      • Sound Card:
      • Xonar dgx
      • Speakers/HPs:
      • Microlab 223 || AltecLansing VS2621 + a4tech hs100
      • Mouse:
      • a4tech x7
      • Power Supply:
      • Adata HM 850w || Thermaltek 600 TR2 S
      • Optical Drive:
      • Asus dvd writer 24x max
      • USB Devices:
      • Phantom 240 red/black || Vatyn 664b (Tyrannosaurus)
      • UPS:
      • Power guard 1200va
      • Comment:
      • :D
    furiousTaher's Avatar
    Join Date
    Apr 2010
    Location
    Dhaka
    Posts
    8,151

    Default

    Quote Originally Posted by abir View Post
    Yah your duty is make it print 20 times just changing a character in 3 ways
    ami parsi 1bar tao 2ta character change kore...++ ke -- the easiest way [SPOILER]
    Code:
    int i, n = 20;  for (i=0; i<n; i++)  {  cout << "x" << endl;  }
    [/SPOILER]

    ---------- Post added at 19:19 ---------- Previous post was at 19:17 ----------

    Quote Originally Posted by CvP View Post
    solution #2:
    Code:
    int i, n = 20; 
    for (i=0; i+n; i--) 
    { 
    cout << "x" << endl; 
    }
    porer code ta bujlam na
    Last edited by CvP; January 15th, 2012 at 20:43.

  6. #6
    Moderator
    • abir's Gadgets
      • Motherboard:
      • Gigabyte G41M-ES2L | Intel 965 HP
      • CPU:
      • Intel Pentium Dual Core E5400 2.7GHz 800MSB| Intel T5670 Core 2 Duo 1.8GHz 800MHz FSB
      • RAM:
      • (2+2) 4GB DDR II (800MHz) | 1GB RAM
      • Hard Drive:
      • 320 GB SATA Samsung | 160GB SATA
      • Graphics Card:
      • Sapphire Radeon 4770 512MB | Intel GMA X3100
      • Display:
      • DELL Inspiron 18.5&quot; LCD |14.1&quot; TFT LCD
      • Sound Card:
      • Realtek Builtin Audio
      • Speakers/HPs:
      • Creative Inspire 2.1
      • Keyboard:
      • Genius Slimstar 355 Gaming Keyboard | A4Tech Anti RSI (USB PS2)
      • Mouse:
      • A4tech 7K Office (USB)| HP
      • Power Supply:
      • Deluxe DLP 388A 450W
      • Optical Drive:
      • ASUS 16X DVD Drive &amp; Liteon DVDW| Asus DVD RAMRW LightScribe
      • USB Devices:
      • 4GB Apacer Pendrive
      • UPS:
      • OVO 650VA
      • Operating System:
      • Windows 7 x64 | Windows XP SP2 &amp; Ubuntu 9.04
      • ISP:
      • Smile Internet (Bronze)
    abir's Avatar
    Join Date
    Feb 2008
    Location
    Azimpur
    Posts
    7,913

    Default

    @Taher fu®ious™
    Two character replacing is not allowed.

    Also edit your post please, you are supposed to post answer within spoiler. Hide the CvP's one with spoiler tag.

    ---------- Post added at 19:50 ---------- Previous post was at 19:32 ----------

    @Taher fu®ious™

    Here is the explanation of that code:
    explanation:
    for( ; ; )
    In the for loop, semi colon (; ) separated fields are -
    The 1st field is for initialization.
    The 2nd field is for condition.
    The 3rd field is for action per loop.

    So the `i+n` is the condition. when the condition is not false or not 0, the loop continues.
    In the 20th iteration the `i+n` becomes `-20+20`= 0 and the loop breaks.

    "Game after game after game, I realized what is most important of my life - FOOTBALL.."
    I bleed red, Man Utd 4ever..
    ---------------------
    অনেক দূরের একলা পথে, ক্লান্ত আমি ফিরি তোমার কাছে, মুখোশ খুলে বসে রই জানলার ধারে..

  7. #7
    Member
    • furiousTaher's Gadgets
      • Motherboard:
      • Asus z170p || Asus p8 P67m pro
      • CPU:
      • i7 6700k || i5 2400
      • RAM:
      • 2 x Ripjaws 8gb 3200C16D 16GVKB || (Transcend 4gb + Adata 4gb) 1333
      • Hard Drive:
      • Evo 850 250gb + Toshiba 2tb || WD 500gb blue
      • Graphics Card:
      • Zotac gtx 1060 amp || Sapphire r7 260x 1GB
      • Display:
      • Asus vx229h ||Samsung 21.5&quot; S22A300B
      • Sound Card:
      • Xonar dgx
      • Speakers/HPs:
      • Microlab 223 || AltecLansing VS2621 + a4tech hs100
      • Mouse:
      • a4tech x7
      • Power Supply:
      • Adata HM 850w || Thermaltek 600 TR2 S
      • Optical Drive:
      • Asus dvd writer 24x max
      • USB Devices:
      • Phantom 240 red/black || Vatyn 664b (Tyrannosaurus)
      • UPS:
      • Power guard 1200va
      • Comment:
      • :D
    furiousTaher's Avatar
    Join Date
    Apr 2010
    Location
    Dhaka
    Posts
    8,151

    Default

    :O thnx a lot!! 10ta repu paona!

  8. #8
    Member
    • areek's Gadgets
      • Motherboard:
      • ASRock Z87 Extreme4
      • CPU:
      • Intel Core i5-4670k CPU 3.4GHz
      • RAM:
      • G.SKILL Ripjaw DDR3 1600MHz 2x4gb
      • Hard Drive:
      • Corsair Force LX 256gb + 500gb WD caviar green + 2TB WD caviar green + Kingston 500gb USB
      • Graphics Card:
      • Gigabyte ATI Radeon R9 280X Windforce 3X
      • Display:
      • Dell S2240L
      • Sound Card:
      • Asus Xonar Essence STX
      • Speakers/HPs:
      • Altec Lansing 2.1 speakers|| HIFIMAN HE400
      • Keyboard:
      • Razer Blackwidow Ultimate Dragon Age II edition
      • Mouse:
      • Logitech G402 on QCK+ NaVi Splash
      • Controller:
      • logitech drivefx axial feedback wheel
      • Power Supply:
      • Thermaltake Toughpower gold 750W
      • Optical Drive:
      • Asus 24B1ST
      • USB Devices:
      • Samsung ML-1666 laser printer, CanoScan Lide 25
      • UPS:
      • yes
      • Operating System:
      • Windows 7 Ultimate SP1 64-bit
      • ISP:
      • Link3
      • Download Speed:
      • 128
      • Upload Speed:
      • 128

    Join Date
    Feb 2008
    Location
    বাংলাগেমার.কম
    Posts
    2,216

    Default

    So, addition n deletion aren't allowed???

    ---------- Post added at 20:17 ---------- Previous post was at 20:12 ----------

    wrong ans:
    int i, n = -20;
    for (i=0; i<n; i--)
    {
    cout << "x" << endl;
    }


    ---------- Post added at 20:18 ---------- Previous post was at 20:17 ----------

    I think this is the ans u r lookin for @abir vai, what is left of CvP bro???
    Not sure abt it tho, i didnt put it in a compiler, just thought.
    Last edited by areek; January 15th, 2012 at 21:36. Reason: ans is wrong :P

  9. #9
    Moderator
    • abir's Gadgets
      • Motherboard:
      • Gigabyte G41M-ES2L | Intel 965 HP
      • CPU:
      • Intel Pentium Dual Core E5400 2.7GHz 800MSB| Intel T5670 Core 2 Duo 1.8GHz 800MHz FSB
      • RAM:
      • (2+2) 4GB DDR II (800MHz) | 1GB RAM
      • Hard Drive:
      • 320 GB SATA Samsung | 160GB SATA
      • Graphics Card:
      • Sapphire Radeon 4770 512MB | Intel GMA X3100
      • Display:
      • DELL Inspiron 18.5&quot; LCD |14.1&quot; TFT LCD
      • Sound Card:
      • Realtek Builtin Audio
      • Speakers/HPs:
      • Creative Inspire 2.1
      • Keyboard:
      • Genius Slimstar 355 Gaming Keyboard | A4Tech Anti RSI (USB PS2)
      • Mouse:
      • A4tech 7K Office (USB)| HP
      • Power Supply:
      • Deluxe DLP 388A 450W
      • Optical Drive:
      • ASUS 16X DVD Drive &amp; Liteon DVDW| Asus DVD RAMRW LightScribe
      • USB Devices:
      • 4GB Apacer Pendrive
      • UPS:
      • OVO 650VA
      • Operating System:
      • Windows 7 x64 | Windows XP SP2 &amp; Ubuntu 9.04
      • ISP:
      • Smile Internet (Bronze)
    abir's Avatar
    Join Date
    Feb 2008
    Location
    Azimpur
    Posts
    7,913

    Default

    Quote Originally Posted by areek View Post
    So, addition n deletion aren't allowed???
    ---------- Post added at 20:17 ---------- Previous post was at 20:12 ----------
    ans:
    int i, n = -20;
    for (i=0; i<n; i--)
    {
    cout << "x" << endl;
    }

    ---------- Post added at 20:18 ---------- Previous post was at 20:17 ----------
    I think this is the ans u r lookin for @abir vai, what is left of CvP bro???
    Not sure abt it tho, i didnt put it in a compiler, just thought.
    To be precise, you can replace a whitespace character, that means you can add.

    Your answer is not correct. n =-20 it doesn't satisfy the condition i<n (0<-20) so the inside for loop won't be executed no x will be printed.

    CvP added the 3rd solution already

    "Game after game after game, I realized what is most important of my life - FOOTBALL.."
    I bleed red, Man Utd 4ever..
    ---------------------
    অনেক দূরের একলা পথে, ক্লান্ত আমি ফিরি তোমার কাছে, মুখোশ খুলে বসে রই জানলার ধারে..

  10. #10
    Member
    • areek's Gadgets
      • Motherboard:
      • ASRock Z87 Extreme4
      • CPU:
      • Intel Core i5-4670k CPU 3.4GHz
      • RAM:
      • G.SKILL Ripjaw DDR3 1600MHz 2x4gb
      • Hard Drive:
      • Corsair Force LX 256gb + 500gb WD caviar green + 2TB WD caviar green + Kingston 500gb USB
      • Graphics Card:
      • Gigabyte ATI Radeon R9 280X Windforce 3X
      • Display:
      • Dell S2240L
      • Sound Card:
      • Asus Xonar Essence STX
      • Speakers/HPs:
      • Altec Lansing 2.1 speakers|| HIFIMAN HE400
      • Keyboard:
      • Razer Blackwidow Ultimate Dragon Age II edition
      • Mouse:
      • Logitech G402 on QCK+ NaVi Splash
      • Controller:
      • logitech drivefx axial feedback wheel
      • Power Supply:
      • Thermaltake Toughpower gold 750W
      • Optical Drive:
      • Asus 24B1ST
      • USB Devices:
      • Samsung ML-1666 laser printer, CanoScan Lide 25
      • UPS:
      • yes
      • Operating System:
      • Windows 7 Ultimate SP1 64-bit
      • ISP:
      • Link3
      • Download Speed:
      • 128
      • Upload Speed:
      • 128

    Join Date
    Feb 2008
    Location
    বাংলাগেমার.কম
    Posts
    2,216

    Default

    Quote Originally Posted by abir View Post
    To be precise, you can replace a whitespace character, that means you can add.

    Your answer is not correct. n =-20 it doesn't satisfy the condition i<n (0<-20) so the inside for loop won't be executed no x will be printed.

    CvP added the 3rd solution already
    hmm....it was a blind guess

  11. #11
    Member
    • pollob.codename13's Gadgets
      • Motherboard:
      • Gigabyte G31
      • CPU:
      • Intel Pentium Duel Core E5200 2.5GHz
      • RAM:
      • 1+1 GB DDR2 667 MHz
      • Hard Drive:
      • Hitachi 500GB
      • Graphics Card:
      • Sapphire ATI Radeon HD 4770 GDDR5 512 MB
      • Display:
      • Samsung SyncMaster B2030
      • Sound Card:
      • Realtek built-in
      • Speakers/HPs:
      • csm
      • Keyboard:
      • A4 Tech
      • Mouse:
      • Toshiba
      • Controller:
      • Sonic Dual Shock
      • Power Supply:
      • Normal
      • Comment:
      • I always feel my pc
    pollob.codename13's Avatar
    Join Date
    Sep 2008
    Location
    Lalbag
    Posts
    6,118

    Default

    2 ta solution bair korte parsi

    1:
    Code:
    int i, n = 20; 
    for (i=0; i<n; n--) 
    { 
    cout << "x" << endl; 
    }


    2:
    Code:
    int i, n = 20; 
    for (i=0;-i<n; i--) 
    { 
    cout << "x" << endl; 
    }


  12. #12
    Member
    • akk's Gadgets
      • Motherboard:
      • MSI G 41 P33 Combo
      • CPU:
      • Intel Core 2 duo E 8400 3.00GHZ
      • RAM:
      • Transcend 4 GB DDR 3 1600BUS
      • Hard Drive:
      • WD Caviar Green 2 TB
      • Graphics Card:
      • XFX ATI RADEON HD 4650 1GB
      • Display:
      • Samsung 18.5 IN LCD TFT (WIDE)
      • Sound Card:
      • Realtek Built IN
      • Speakers/HPs:
      • 1.Perfect 5.1 +2 Sony Speakers Customized 7.1 &amp; 2.A4 Tech HS -100
      • Keyboard:
      • A4tech AntiRSI Multimedia+Logitech K200
      • Mouse:
      • A4 Tech+Logitech
      • Controller:
      • 2 Dilong PU -305, Dilong 401T &amp; A.tech dual vibe
      • Power Supply:
      • DELL 300Wx2
      • Optical Drive:
      • Asus 52x DVD RW multispeed &amp; multilayer compaitable
      • USB Devices:
      • Prolink USB Modem(T&amp;T)
      • UPS:
      • apollo 650 VA
      • Operating System:
      • Windows 7 x64 Ultimate
      • Benchmark Scores:
      • 6.6 out of 7.9
      • Comment:
      • Works Fine
      • ISP:
      • Royal Green Online
      • Download Speed:
      • ~34 KB
      • Upload Speed:
      • ~ 20 KB
      • Console:
      • 128
    akk's Avatar
    Join Date
    Jun 2011
    Location
    Dhaka
    Posts
    143

    Default

    panic of 1/1

Similar Threads

  1. Programming contest result
    By shafi in forum Miscellaneous Topics
    Replies: 3
    Last Post: July 6th, 2011, 17:10
  2. game programming
    By pollob.codename13 in forum Programming
    Replies: 15
    Last Post: October 13th, 2008, 23:25
  3. The programming language u use.
    By areek in forum Programming
    Replies: 29
    Last Post: August 13th, 2008, 17:48
  4. [ahk][programming]Use gamepads as a mouse
    By xK::CREATOR in forum General Discussion
    Replies: 11
    Last Post: June 19th, 2008, 23:16

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 0.24675 seconds with 14 queries.