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 8

Here is my code in PHP. I just started learning it recently, so this is good practice. I also know Java.
Long Version (Click to show)
Code:
``````<?php

//Init the arrays
\$twoD = array(array(1,2,3,4,5),array(1,2,3,4,6),array(1,0,0,0,1,4,3,2,4,5,7,3,4,5,6,763,3345,6,3,6));
\$oneD = array();

//Loop throught the first dimension of \$twoD
for(\$counter = 0; \$counter < count(\$twoD); \$counter++ ){

//Init \$sum variable and set it to zero. This is where the final sum is temporarily stored
\$sum = 0;

//Loop through second dimension of \$twoD
for(\$c = 0; \$c < count(\$twoD[\$counter]); \$c++){

//Add the value of the integer in the second dimension of \$twoD to the variable named \$sum
\$sum = \$sum + \$twoD[\$counter][\$c];
}

//When all of the variables are added into \$sum, add sum to the array \$oneD
\$oneD[\$counter] = \$sum;
}

//Loop through the array \$oneD and display the values
for(\$counter = 0; \$counter < count(\$oneD); \$counter++){
echo'Sum of row '.\$counter.' : '.\$oneD[\$counter].'<br>';
}

?>```
```

EDIT: We can't edit the spreadsheet

EDIT2: Shorter way without code comments and second array Short Version (Click to show)
Code:
``````<?php

\$twoD = array(array(1,2,3,4,5),array(1,2,3,4,6),array(1,0,0,0,1,4,3,2,4,5,7,3,4,5,6,763,3345,6,3,6));
\$oneD = array();

for(\$counter = 0; \$counter < count(\$twoD); \$counter++ ){
\$sum = 0;

for(\$c = 0; \$c < count(\$twoD[\$counter]); \$c++){
\$sum = \$sum + \$twoD[\$counter][\$c];
}

echo'Sum of row '.\$counter.' : '.\$sum.'<br>';
}

?>```
```

Edited by Jtvd78 - 12/4/11 at 5:31pm
Sorry guys, I forgot that the new OCN doesn't automatically add the address of the webpage when you add the link. The link was present but it was not displayed.

You can enter your name and language of choice from the form now.

Sorry for being late too. Time zone difference.

I'll go over all your submissions in a minute. But first I would like to ask you whether it would be a good idea to have just one programming challenge per week. Then, when everyone has their entries in, I can make a poll in which we can vote for the person who had the best most efficient (or whatever criteria we decide on) code. The person who gets the most votes gets the title "Programmer of the Week" and his name is displayed in the OP.
I know it's not very important but it would be fun. And then, I can add another column in the spreadsheet showing how many "Programmer of the Week" titles each person has won.

Would you like this? I'm going to set up the poll now, so vote their.

And, finally, as always thank you for your interest.

EDIT: Oh and the 7 people who want to help, PM me or something. And DMan, sorry for not replying to your PM but I did not understand what you were talking about.
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 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
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 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
I like that idea, will do this challenge today, little busy with the uni now
 Footrest (17 items) server (17 items) HTPC (12 items)
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi
OSOSOSPower
unRaid pfSense Ubuntu CX430v2
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV
KeyboardCase
Lenovo n5901a Silverstone SG-05
 Footrest (17 items) server (17 items) HTPC (12 items)
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi
OSOSOSPower
unRaid pfSense Ubuntu CX430v2
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV
KeyboardCase
Lenovo n5901a Silverstone SG-05
This is done using Visual Studio - 2008 - C#
Code:
``````private void button1_Click(object sender, EventArgs e)
{
int[][] objarray = new int[3][];

objarray[0] = new int[] {  };
objarray[1] = new int[] { 3, 4, 5 };
objarray[2] = new int[] { 7, 8, 9, 10, 11 };
GetArray(objarray);

}

private int[] GetArray(int[][] objarray)
{
int[] objfinal = new int[objarray.GetUpperBound(0)+1];
for (int i = objarray.GetLowerBound(0); i <= objarray.GetUpperBound(0); i++)
{
objfinal[i] = objarray[i].Sum();
}
return objfinal;
}```
```
 My System (13 items)
CPUMotherboardGraphicsRAM
i5 2500k ASUS - P8Z68-V/GEN3 GTX 770 DDR3 - 8G
Hard DriveOSMonitorPower
WD Green Win 7 x64 Dell ST2320L Corsair 750TX
CaseMouse
Coolermaster 310 Logitech G502
 My System (13 items)
CPUMotherboardGraphicsRAM
i5 2500k ASUS - P8Z68-V/GEN3 GTX 770 DDR3 - 8G
Hard DriveOSMonitorPower
WD Green Win 7 x64 Dell ST2320L Corsair 750TX
CaseMouse
Coolermaster 310 Logitech G502
Does language English works? But i prefer C++ lol xD

Challenge #2
Code:
``````#include <iostream>                                                     // include for input / output operations

using namespace std;                                            // namesapced used so you don't write std::_function_

const int ROWS          = 5;                                    // number of ROWS for input array, change here, not in code
const int COLUMNS       = 8;                                    // number of columns for input array, change here, not in code

int main(){
int outputArray[ROWS];                                  // our output array of ROWS size to store sums
int inputArray[ROWS][COLUMNS] = {{1,2}, {4,8,5,6,8,1}, {7,8,9,10,5,2}, {5,1}};  // input array with values
int sum = 0;                                                    // variable to store sum of rows before saving into output array

for (int x = 0; x < ROWS; x++)                  // for to iterate ROWS in input array
{
for (int y = 0; y < COLUMNS; y++)       // for to iterate COLUMNS in input array
{
sum += inputArray[x][y];                // sum values in the current ROW
}
outputArray[x] = sum;                           // save sum total in output array
sum = 0;                                                        // reset sum variable for next iterarion
}

cout << "Output: " << endl;

for (int x = 0; x < ROWS; x++)                  // iterate outputArray to print output
{
cout << "\tSum of row " << x + 1 << " : " << outputArray[x] << endl;    // show result. Sum +1 to X to match requested output in challange xD Arrays start from 0
}

cin.get();                      // If using Visual Studio you need something like this or the command window will blink
return 0;
}```
```
Code:
``````Output:
Sum of row 1 : 3
Sum of row 2 : 32
Sum of row 3 : 41
Sum of row 4 : 6
Sum of row 5 : 0```
```

Edited by NorCa - 12/5/11 at 5:03am
 Atroc.V3.2 (16 items) Home Server (9 items)
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w
Case
[NZXT] Gamma
 Atroc.V3.2 (16 items) Home Server (9 items)
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w
Case
[NZXT] Gamma
Ooooh I so want to do this , is it too late to do the 2nd challenge? Since I've got like 20 minutes before I jet off to school... Maybe I'll finish it there.
 Instanty By A Week (8 items)
CPUMotherboardGraphicsRAM
i5 2500k Asus P8P67 XFX HD 6950 G.Skill 2x4GB
CoolingOSPowerCase
CM Hyper 212+ Windows 7 / Linux Mint Seasonic S12ii 620 HAF 912
 Instanty By A Week (8 items)
CPUMotherboardGraphicsRAM
i5 2500k Asus P8P67 XFX HD 6950 G.Skill 2x4GB
CoolingOSPowerCase
CM Hyper 212+ Windows 7 / Linux Mint Seasonic S12ii 620 HAF 912
Code:
``````#!/usr/bin/perl

use List::Util qw(sum);

my @mda = (
[1,2,3,4,5,6,7,8,9,0],
[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,0],
[1,3,5,7,9],
);
my \$count = \$#mda;

while (@mda) { printf "Sum of row %i : %i\n", \$count-\$#mda+1, sum(@{shift(@mda)}) }```
```

List::Util should be provided as part of the core modules so I've used that (C++ libraries will run faster than pure Perl), but you're getting an error, then I can include a pure Perl version too.

it's written this way because I wanted to do things a little different to the other examples shown. So rather than a nested loop, what this does is 1 loop that calls a sum function against an array and then destroy the first index of the top level array. It could have easily been written using a for loop, but I wanted to try something different from the obvious
Edited by Plan9 - 12/5/11 at 5:24am
You can still submit your entry for the next couple of days. I will check back your solutions in a few hours since I am a little busy.

GO! GO! GO! GO!
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 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
 MacBook Pro 13" (6 items) Toshiba Satellite L655 (13 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
Code:
``````#include <iostream>
using namespace std;

const int columns = 3;
const int rows = 5;
int lol[columns][rows]={{1},{4,5,6},{7,8,9,10,11}}; //2D array

void main(){
int temp[rows];
for(int x=0;x<rows;x++)
{
temp[x]=0; //initialize 1D temp array to zeroes
}
for(int x=0;x<rows;x++) //loop through rows
{
for(int y=0;y<columns;y++) //loop through columns
{
temp[x]+=lol[y][x]; //sum of values in current row stored in current index of temp array
}
}

for(int x=0;x<rows;x++)
{
cout << "Sum of row " << x+1 << ": " << temp[x] << endl; //loop through 1D array to display contents
}
system("pause");
}```
```

Almost the same as Norca
Edited by icyblizard - 12/5/11 at 5:24am
 Budget Build! (15 items)
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3
 Budget Build! (15 items)
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3
I'll be joining you guys as of next challenge
 T3h R1g[FTW] (17 items) R1g, nIc3 (In Progress) (14 items)
CPUMotherboardGraphicsGraphics
Q9550 E0 @ 3.88 GHz (1.26v) Evga 780i SLi FTW XFX 9800 GT XFX 9800 GT
RAMHard DriveHard DriveHard Drive
OCZ Blade PC8500 CL5 OCZ Vertex WD Caviar Green WD Caviar Green
CoolingOSMonitorKeyboard
Noctua NH-U12P Windows 7 Pro x64 Samsung P2350 23" 2ms Razer Lycosa
CM Real Power RS-520 CM 690 Roccat Kone QcK SK Limited
Audio
Sennheiser PC 151
CPUMotherboardGraphicsRAM
i7 950 @ 4 GHz no HT Asus Sabertooth X58 Evga GTX480 SC Mushkin Redline CL6
Hard DriveCoolingCoolingOS
Seagate EK Supreme Cupper/Acetal EK FC480 GTX Nickle/Acetal Win 7 Pro x64
MonitorKeyboardPowerCase
Samsung 23" Razor Lycosa Coolermaster Silent Pro M 850 Silverstone TJ07B-W
Roccat Kone QcK Limited
 T3h R1g[FTW] (17 items) R1g, nIc3 (In Progress) (14 items)
CPUMotherboardGraphicsGraphics
Q9550 E0 @ 3.88 GHz (1.26v) Evga 780i SLi FTW XFX 9800 GT XFX 9800 GT
RAMHard DriveHard DriveHard Drive
OCZ Blade PC8500 CL5 OCZ Vertex WD Caviar Green WD Caviar Green
CoolingOSMonitorKeyboard
Noctua NH-U12P Windows 7 Pro x64 Samsung P2350 23" 2ms Razer Lycosa
CM Real Power RS-520 CM 690 Roccat Kone QcK SK Limited
Audio
Sennheiser PC 151
CPUMotherboardGraphicsRAM
i7 950 @ 4 GHz no HT Asus Sabertooth X58 Evga GTX480 SC Mushkin Redline CL6
Hard DriveCoolingCoolingOS
Seagate EK Supreme Cupper/Acetal EK FC480 GTX Nickle/Acetal Win 7 Pro x64
MonitorKeyboardPowerCase
Samsung 23" Razor Lycosa Coolermaster Silent Pro M 850 Silverstone TJ07B-W