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 5

post #41 of 263
Quote:
Originally Posted by morethantoast View Post

It's convention to declare iteration variables such as i and j within the for loop. They certainly could be declare before the for loops, but you rarely see it done like that.
Actually, most compilers don't require that you return from main(). If a return is omitted, it'll be added at compile time. My code compiles fine in GCC.

Is it in C++ where this is not allowed?
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #42 of 263
I believe it IS allowed in C++, but not C.

For those of you who are looking for some more coding, try taking a second argument that is the character that is displayed.

For example:
Code:
$ ./program 5 # 

Would have the output of:
Code:
#
##
###
####
#####
####
###
##
#
    
CPUMotherboardGraphicsGraphics
i7 2600k Asus P8P67 Pro 2x EVGA 580GTX 3GB 2x Saphire 7970 Vapor-x GHz edition (alternate) 
RAMHard DriveHard DriveCooling
16GB G.Skill 2133 CAS9 Hitachi 3tb Corsair M4 128 CM 212+ 
OSMonitorMonitorKeyboard
Arch Linux, OSX, Win7x64 Dell U3011  HP LP3065 Leopold Mechanical Blue 
PowerCaseMouseAudio
Seasonic X1250 Modular Corsair 750d Razer Deathadder V3.5? Bose companion 3 Series II 
Audio
Sennheiser HD-280 Pro 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
i7 2600k Asus P8P67 Pro 2x EVGA 580GTX 3GB 2x Saphire 7970 Vapor-x GHz edition (alternate) 
RAMHard DriveHard DriveCooling
16GB G.Skill 2133 CAS9 Hitachi 3tb Corsair M4 128 CM 212+ 
OSMonitorMonitorKeyboard
Arch Linux, OSX, Win7x64 Dell U3011  HP LP3065 Leopold Mechanical Blue 
PowerCaseMouseAudio
Seasonic X1250 Modular Corsair 750d Razer Deathadder V3.5? Bose companion 3 Series II 
Audio
Sennheiser HD-280 Pro 
  hide details  
Reply
post #43 of 263
FreeBASIC again

Supports:
Code:
$ ./program 5 #

If no values specified, it asks you in the program
Code:
dim as integer i, j, c, quit
dim as string s, p

s = command(1)
p = command(2)

if val(s) > 0 then
    c = val(s)
else
    while quit = 0
        input "Enter a number:",s
            if val(s) > 0 then
                c = val(s)
                quit = 1
            end if
        sleep 1
    wend
end if

quit = 0

if len(p) = 0 then
    while quit = 0
        input "Enter a number:",s
            if len(s) > 0 then
                p = s
                quit = 1
            end if
        sleep 1
    wend
end if

?
? "Value chosen was: " & c
? "Symbol chosen was: " & p
?

for i = 1 to c
    for j = 1 to i
        ? p & " ";
    next
    ?
next

for i = (c - 1) to 1 step -1
    for j = 1 to i
        ? p & " ";
    next
    ?
next
CHILZ - Lan Rig
(17 items)
 
CANARY - Main Rig
(16 items)
 
CADILLAC - HTPC
(14 items)
 
CPUMotherboardGraphicsRAM
Intel i5-4570s Asus H97M-Plus AMD R9 280 G.Skill RipjawsX 16 GB (2x8) 
Hard DriveHard DriveHard DriveCooling
250 GB Samsung 840 240 GB Kingston 3 TB USB 3.0 Drive Custom Loop 
OSMonitorKeyboardPower
Windows 10 Preview BenQ GL2450 Filco MajesTouch2 Ninja PC P&C Silencer Mk III 600 W 
CaseMouseMouse PadAudio
Fractal Design Core 1000 Mionix Castor Monoprice XXL JL Amps + Custom Morel Bookshelf speakers 
Other
Scythe Kama-Panel 3 
CPUMotherboardGraphicsRAM
FX-8150 @ 4.6 GHz Fatal1ty 990FX Pro 9800 GTX+ 512 MB G.Skill Ripjaws X 1866 CL9 
Hard DriveHard DriveCoolingOS
120 GB OCZ Vertex 3 1 TB WD Black 5x 120mm + MCP350 + EK Supreme HF + MicroRes Windows 8 Consumer Preview 
MonitorKeyboardPowerCase
2x Dell U2212HM Logitech G110 Cooler Master 850W Silent Pro Cooler Master 690 II Adv. 
MouseMouse PadAudio
Razer Death Adder 3.5G Staples Gel Cushion Asus Xonar DG + Senn. PC333D 
CPUMotherboardGraphicsRAM
Q6600 Acer X1800 ATI 5670 2 GB Kingston 
Hard DriveOptical DriveOSMonitor
2 TB WD Green Asus BD-R Windows 7 Home Premium Sony 50" LCD 
KeyboardPowerCaseMouse
Acer Media 220 W SFF Acer X1800 Acer Optical 
Mouse PadAudio
The TV cabinet Denon 2808 7.1 AVR + Dahlquist 350W 8" Sub + Kl... 
  hide details  
Reply
CHILZ - Lan Rig
(17 items)
 
CANARY - Main Rig
(16 items)
 
CADILLAC - HTPC
(14 items)
 
CPUMotherboardGraphicsRAM
Intel i5-4570s Asus H97M-Plus AMD R9 280 G.Skill RipjawsX 16 GB (2x8) 
Hard DriveHard DriveHard DriveCooling
250 GB Samsung 840 240 GB Kingston 3 TB USB 3.0 Drive Custom Loop 
OSMonitorKeyboardPower
Windows 10 Preview BenQ GL2450 Filco MajesTouch2 Ninja PC P&C Silencer Mk III 600 W 
CaseMouseMouse PadAudio
Fractal Design Core 1000 Mionix Castor Monoprice XXL JL Amps + Custom Morel Bookshelf speakers 
Other
Scythe Kama-Panel 3 
CPUMotherboardGraphicsRAM
FX-8150 @ 4.6 GHz Fatal1ty 990FX Pro 9800 GTX+ 512 MB G.Skill Ripjaws X 1866 CL9 
Hard DriveHard DriveCoolingOS
120 GB OCZ Vertex 3 1 TB WD Black 5x 120mm + MCP350 + EK Supreme HF + MicroRes Windows 8 Consumer Preview 
MonitorKeyboardPowerCase
2x Dell U2212HM Logitech G110 Cooler Master 850W Silent Pro Cooler Master 690 II Adv. 
MouseMouse PadAudio
Razer Death Adder 3.5G Staples Gel Cushion Asus Xonar DG + Senn. PC333D 
CPUMotherboardGraphicsRAM
Q6600 Acer X1800 ATI 5670 2 GB Kingston 
Hard DriveOptical DriveOSMonitor
2 TB WD Green Asus BD-R Windows 7 Home Premium Sony 50" LCD 
KeyboardPowerCaseMouse
Acer Media 220 W SFF Acer X1800 Acer Optical 
Mouse PadAudio
The TV cabinet Denon 2808 7.1 AVR + Dahlquist 350W 8" Sub + Kl... 
  hide details  
Reply
post #44 of 263
Quote:
Originally Posted by Infinite-Dev View Post

I believe it IS allowed in C++, but not C.

I knew it was one of them tongue.gif
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #45 of 263
Quote:
Originally Posted by {Unregistered} View Post

Good job. But there are a few little problems with your code:
1. You declared the variables i and j within the for loop. You should have declared them outside the loop.
2. You forgot to exit from the program; it gives an exit failure when compiled.
3. There's just one more little problem: the first and the last lines of the output don't show any stars. But it doesn't really matter much.
Overall, good job.thumb.gif Much better than I could have done. "Programming" is much much more important than "coding" and it's the algorithm that matters.
P.S. I don't know C very well. I used a little bit of my Java knowledge and little bit of google. biggrin.gif

I'm guessing you're testing the code in Visual Studio because it compiles fine with GCC.

I have noticed VS and GCC don't always agree on C++ convention.
post #46 of 263
Instead of putting as "New Challenge" you should put the date that is last updated.
post #47 of 263
Quote:
Originally Posted by Infinite-Dev View Post

I believe it IS allowed in C++, but not C.
For those of you who are looking for some more coding, try taking a second argument that is the character that is displayed.
For example:
Code:
$ ./program 5 # 
Would have the output of:
Code:
#
##
###
####
#####
####
###
##
#
That's easy as it's only 1 line of code (tbh I don't really need that aside to validate input)
Quote:
Originally Posted by Plan9 View Post

Perl (just because I haven't seen a Perl entry yet):
Code:
#!/usr/bin/perl
if (!$ARGV[0] =~ m/[0-9]+/) { die "Parameter must be an integer\n" }
my $char = substr $ARGV[1], 0, 1 || "*";

my ($direction, $i) = qw(1 1);
do {
    for ($i; ($i <= $ARGV[0] && $direction == 1) || ($i > 0 && $direction == -1); $i += $direction) {
        printf "%s\n", $char x$i;
    }
    $direction *= -1;
} while ($direction != 1)
edit: oops, outputted hashes rather than asterisks.
edit2: CLI version for instant gratification:
Code:
perl -e 'my ($d, $i) = qw(1 1); do {for ($i; ($i<=$ARGV[0] && $d==1) || ($i>0 && $d==-1); $i+=$d) {printf "%s\n", $char x$i} $d*=-1} while ($d!=1)' 5 #

that said, I've not tested the code so it probably wouldn't work lol
post #48 of 263
Quote:
Originally Posted by morethantoast View Post

Quote:
Originally Posted by {Unregistered} View Post

Good job. But there are a few little problems with your code:
1. You declared the variables i and j within the for loop. You should have declared them outside the loop.
2. You forgot to exit from the program; it gives an exit failure when compiled.
3. There's just one more little problem: the first and the last lines of the output don't show any stars. But it doesn't really matter much.
Overall, good job.thumb.gif Much better than I could have done. "Programming" is much much more important than "coding" and it's the algorithm that matters.
P.S. I don't know C very well. I used a little bit of my Java knowledge and little bit of google. biggrin.gif

It's convention to declare iteration variables such as i and j within the for loop. They certainly could be declare before the for loops, but you rarely see it done like that.

Actually, most compilers don't require that you return from main(). If a return is omitted, it'll be added at compile time. My code compiles fine in GCC.
Just because it works doesn't mean you should do it. Implicit is bad. Code should show developer intent - implicitly specifying things doesn't.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #49 of 263
Quote:
Originally Posted by Infinite-Dev View Post

I believe it IS allowed in C++, but not C.

Ah, I think you guys are right. I had completely forgotten that the variables need to be declared before the for loop in C, since gcc doesn't seem to care rolleyes.gif
Just my java and c++ habits showing up where they're not welcome!
Quote:
Originally Posted by Coma View Post

Just because it works doesn't mean you should do it. Implicit is bad. Code should show developer intent - implicitly specifying things doesn't.

I agree completely, I would've added added a return but I was trying to keep the code as short as possible biggrin.gif
eyefinity
(22 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K EVGA E685 Radeon HD 6970 Radeon HD 6970 
RAMRAMHard DriveCooling
G.Skill SNIPER G.Skill SNIPER Crucial M4 128GB 6970 EK Water Block Acetal + Nickel 
CoolingCoolingCoolingOS
Swiftech MCR320-QP Radiator Swiftech MCP655 Pump EK Supreme HF CPU Block Windows 7 Ultimate 
MonitorMonitorMonitorKeyboard
Dell E228WFP Dell E228WFP Dell P2210 G510 
PowerCaseMouseMouse Pad
TX850 HAF932 BE G500 XTRAC PADS Ripper 
Audio
G930 
  hide details  
Reply
eyefinity
(22 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K EVGA E685 Radeon HD 6970 Radeon HD 6970 
RAMRAMHard DriveCooling
G.Skill SNIPER G.Skill SNIPER Crucial M4 128GB 6970 EK Water Block Acetal + Nickel 
CoolingCoolingCoolingOS
Swiftech MCR320-QP Radiator Swiftech MCP655 Pump EK Supreme HF CPU Block Windows 7 Ultimate 
MonitorMonitorMonitorKeyboard
Dell E228WFP Dell E228WFP Dell P2210 G510 
PowerCaseMouseMouse Pad
TX850 HAF932 BE G500 XTRAC PADS Ripper 
Audio
G930 
  hide details  
Reply
post #50 of 263
Here's a recursive solution (just for fun) written in Java:
Code:
public class stars
{
        public static String printStars(String character, int max)
        {
                if (character.length() < max)
                {
                        System.out.println(character);
                        printStars(character + character.substring(0, 1), max--);       
                }
                System.out.println(character);
                return character;
        }

        public static void main(String[] args)
        {
                printStars("*", 5);
        }
}

I'm trying to think of a good way to not print the max number of stars twice. Obviously I could have an if statement around the first println, but I'd rather have a better solution. Any suggestions?

Edit: formatting

Edit 2: Wow, duh, it's fixed now.
Edited by Waffleboy - 11/24/11 at 4:54pm
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  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