Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › The Programming Challenge Master Thread
New Posts  All Forums:Forum Nav:

The Programming Challenge Master Thread - Page 21

post #201 of 263
Thread Starter 
Quote:
Originally Posted by bruflot View Post

blaaaah.. Knowing that my code wasn't made correctly was driving me nuts, so here I am, once again nerdsmiley.png
Does this look better to you? Warning: Spoiler! (Click to show)
Code:
void OCN1(int input){
    int i = input;
    string chr = "*";
    
    for(int p = 0; p < input; p++){
        cout << chr << endl;
        chr += "*";
    }
        
    for(int m = input; (m = input); i--){
        cout << chr << endl;
        chr.erase(i);
    }
}

Let's continue this here.

The part up till the first loop looks fine.

The second loop will not work correctly because the second argument you have given (m = input) is not a boolean condition. Also, even if it were correctly written (m == input), you haven't iterated or changed the value of m in anyway throughout the execution of the loop so the loop would run infinitely.
I see what you're trying to do with multiple variables in the loop. If you just fix what's wrong with 'm', i.e., fix the condition (m > 0) and add in code in the loop that decreases its value by one, it should work properly.

Sorry for confusing you a bit in the PM.
Edited by {Unregistered} - 7/11/12 at 9:13am
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #202 of 263
Won't the loop break when "i" is below 0?
It may not exit right, but that should be pretty easy to fix.

Anyhow, I can see how the syntax is wrong now.
Give me 5 minutes, I found another bug as well. I'll try to fix it smile.gif

Edit: Sorry for not replying to your PMs, I had to a couple of things first.
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
post #203 of 263
Thread Starter 
Quote:
Originally Posted by bruflot View Post

Won't the loop break when "i" is below 0?
It may not exit right, but that should be pretty easy to fix.
Anyhow, I can see how the syntax is wrong now.
Give me 5 minutes, I found another bug as well. I'll try to fix it smile.gif
Edit: Sorry for not replying to your PMs, I had to a couple of things first.

See edited post. smile.gif
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #204 of 263
Haha, yeah, I did kinda realize that smile.gif
The subtraction part messes up my head badly, I can't think clearly about it tongue.gif

Trying to fix it now, will "report" back later.

Edit: How does this look? biggrin.gif

Code:
void OCN1(int input){
    string chr = "*";
    
    for(int p = 0; p < input; p++){
        cout << chr << endl;
        chr += "*";
    }
        
    for(int m = input - 1; m > 0; m--){
        chr.erase(m);
        cout << chr << endl;
    }
}

Edited by bruflot - 7/11/12 at 9:29am
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
Citra ♥
(15 items)
 
Big Mac
(10 items)
 
 
CPUMotherboardGraphicsRAM
[Intel] Core i7 2600K [Asus] P8Z68-V Pro [Gainward] Geforce 9800 GTX+ [Corsair] Vengeance 16GB @ 1600 
Hard DriveHard DriveCoolingOS
[Kingston] HyperX 120GB SSD [Intel] 520 series 60GB [Corsair] H80 [Canonical] Ubuntu 12.10 
MonitorKeyboardPowerCase
[BenQ] XL2420T [Corsair] Vengeance K60 Performance [OCZ] ZX 850W [CM] Storm Enforcer 
MouseAudioOther
[Razer] Mamba 4G 2012 [Beats] urBeats [NZXT] 24/8/6 pin extensions 
CPUMotherboardGraphicsRAM
[Intel] Core i5-3210M [Apple] Proprietary [Intel] HD 4000 [Hynix] H5TC2G83CFR DDR3L 8GB 
Hard DriveOSMonitorKeyboard
[Samsung] 128GB SSD [Apple] OS X 10.8.2 ML 13" 2560x1600 Retina [Apple] Backlit Keyboard 
CaseMouse Pad
[Apple] Unibody [Apple] Trackpad 
  hide details  
Reply
post #205 of 263
Thread Starter 
Looks good. thumb.gif
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #206 of 263
frown.gif
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #207 of 263
MOAR!
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #208 of 263
Just tried out for my universities programming team today. Didn't do too hot but the questions were pretty tough. I'd participate in the next challenge whenever there is one though.
post #209 of 263
Quote:
Originally Posted by Aximous View Post

Yes, as Waffleboy said goto is basically no-no in anything other than assembly.

Reading this I took a deep thought - wouldn't it be cool to do this challenge purely in assembly ? biggrin.gif I used to do some little things under DOS asm many years ago, so now I decided to do it as 32 bit native linux application. (which makes it my first linux assembly program I ever made)

It expects number of asterisks (the length of the maximum line) as the program parameter. It validates that the provided string is really a number and also that the number fits into <0, MAX_ALLOWED_SIZE> interval (I set MAX_ALLOWED_SIZE to 200 asterisks as on my system it doesn't make much sense to have more asterisks on one line.) If no argument or too many arguments are provided it shows short usage information. It doesn't include any external library dependencies (everything is handled "manually") thus the resulting binary size is only 568 bytes. smile.gif
Oh and it reads much much worse than bruflot's first attempt biggrin.gif

Tested on lubuntu 11.10, if you don't have linux installed you can try it using some live CD

Steps to build:
Code:
nasm -f elf asterisks.asm
ld -s -o asterisks asterisks.o

make sure you have nasm and ld installed (sudo apt-get install nasm ... on ubuntu)

run:
Code:
./asterisks 20

copy paste this into asterisks.asm:
Warning: Spoiler! (Click to show)
Code:
section      .text:
    global _start                       ; must be declared for linker (ld)

_start:                                 ; linker entry point

        pop     eax     ; get number of parameters into eax
        pop     ebx
        pop     ebx     ; get second parameter into ebx

        call    parseArguments ; eax = N = max number of asterisks
        mov     edi, eax       ; save to edi as well for future references

        shl     eax, 1
        dec     eax ; eax = total number of lines to print = 2*N - 1

        xor     ebx, ebx; ebx = lines counter  = 0


write_next_line:        
        cmp     ebx, eax
        jge     exit ; when ebx(line counter) >= eax(total num lines to write) that means we are finished

        ; determine number of asterisks to print on this line
        mov     esi, ebx
        inc     esi
        cmp     esi, edi
        jle     print_asterisks
        sub     esi, edi
        neg     esi
        add     esi, edi
        

print_asterisks:

        ; now esi contains number of asterisks to print on this line
        
print_next_asterisk:
        mov     edx, 1 ; write only one character
        mov     ecx, ASTERISK ; text to write
        call    print

        dec     esi ; esi -= 1
        jne     print_next_asterisk ; if esi != 0 goto print_one_asterisk

print_NEW_LINE:
        mov     edx, 1 ; write only one character
        mov     ecx, NEW_LINE ; text to write
        call    print

        inc     ebx ; increment lines counter
        jmp     write_next_line

exit:
        mov     eax,1   ; system call number (sys_exit)
        int     0x80    ; call kernel


; printing routine, epxect string and length to print in ecx, edx
print:
        push    ebx
        push    eax
        mov     ebx,1   ; file descriptor (stdout)
        mov     eax,4   ; system call number (sys_write)
        int     0x80    ; call kernel
        pop     eax;
        pop     ebx
        ret    


; routine that checks input arguments count and stores program's argument into eax 
; (as properly parsed number)
; expects number of arguments in eax, pointer to first program argument string in ebx
; any argument error is reported to screen and program ends, if no argument is provided 
; then short usage information is displayed and program eds
parseArguments:
                
        cmp eax, 2
        jne parseError

        call parseNumber

        ret

parseError:
        
        ; print usage
        mov edx, USAGE_LEN
        mov ecx, USAGE
        call print
        
        ; exit
        mov eax, 1
        int 0x80


; routine that checks if string in ebx pointer contains only characters '0'..'9' 
; and the number is < MAX_ALLOWED_SIZE  
; and returns the integer it represents in eax (or ends with displaying error)
parseNumber:
        xor     ecx, ecx ; counter of read characters, initialized to 0
        xor     edx, edx ; intermediate value of the number processed so far, initialized to 0

parse_next_char:
        cmp     edx, MAX_ALLOWED_SIZE
        jg      checkNumberError ; out of range error

        movzx   eax, byte [ebx + ecx] ; store next byt of the string into eax
        cmp     eax, 0
        je      parseNumberEnd ; ending byte == 0 has been reached -> goto end

        ; edx = edx * 10
        mov     esi,edx
        shl     esi,3
        mov     edi, edx
        shl     edi,1
        mov     edx, esi
        add     edx, edi

        ; check character is between '0'..'9'
        cmp     eax, '0'
        jl      checkNumberError
        cmp     eax, '9'
        jg      checkNumberError

        ; everything OK, so we can add the next digit to our intermediate value
        sub     eax, '0'
        add     edx, eax

        inc     ecx
        jmp     parse_next_char
        
checkNumberError:
        ; print error message
        mov     edx, BAD_FORMAT_LEN
        mov     ecx, BAD_FORMAT
        call    print

        ; exit
        mov     eax, 1
        int     0x80

parseNumberEnd: 
        
        ; make sure that at least one character was read 
        ; (under normal circumstances this should be always true)
        cmp     ecx, 0
        je      checkNumberError

        ; intermediate value in edx is now result, store it into eax and return
        mov     eax, edx
        ret     


section .data
ASTERISK        db      '*'
NEW_LINE        db      0x0A
MAX_ALLOWED_SIZE equ    200
BAD_FORMAT      db      'Bad format or out of range',0x0A
BAD_FORMAT_LEN  equ     $ - BAD_FORMAT
USAGE           db      'Usage: asterisks NUM_ASTERISKS',0x0A
USAGE_LEN       equ     $ - USAGE

Enjoy smile.gif
post #210 of 263
Thread Starter 
Now accepting suggestions for Challenge #6 via PM!
The sixth challenge will posted soon after!

... soon is in {Unregistered} time, of course.wink.gif
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › The Programming Challenge Master Thread