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 7

Quote:
Originally Posted by {Unregistered}

Sorry for the late update but I will try to post the new challenge within the next 12 or so hours.
Anyone willing to do the spreadsheet for me.

So after quite a bit of time, the Programming Challenge is back.

The Challenge:

Create a program that does the following:

1. Take a 2-dimensional array of integers with your own values and own dimensions (stored within the program - it doesn't have to be input since that would make it slightly more complicated). The values and dimensions must be customizable without having to change code in the rest of the program.

2. A new 1 - dimensional array must be created, the values of which store the sum of the values of the integers in each row of the 2-d array.

3. Then, the each value(member) of this 1-d array must be output.

So basically what we're trying to do is that find the sum of the values of each row of the 2-d array and print them.
Code:
``````Sample Input (Input can be hard-coded into the program but must be totally independent of the rest of the code):

Array = {{1}, {4,5,6}, {7,8,9,10,11}}

Output:

Sum of row 1 : 1
Sum of row 2 : 15
Sum of row 3 : 45
```
```

Notice that neither the number of rows nor the number of columns must be fixed. You can use any number and the program must be able to work properly. Even if the one of the rows is " {} ", it should simply print out the sum as 0.

I have done this in Java (which is usually my language of choice) but you may use any language. Another additional task for this challenge is that the code must be "well-commented". Please try to explain what you are doing at the important parts of the program. But this is just as a bonus.

Good luck. Hope the turn-out is just as good as last time.

~ {Unregistered}
 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
Does the array have to be jagged or can it be made rectangular? ie, empty items filled as zero in the array declaration.

Array = {{1,0,0,0,0}, {4,5,6,0,0}, {7,8,9,10,11}}
 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
 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
I have a solution using jagged arrays in VB.net.
Code (Click to show)
Code:
``````Module Module1

Sub Main()
Const dim1 = 3                          ' Number of rows in array

Dim array(dim1)() As Integer            ' Initialise a 2 dimensional array of int
array(0) = New Integer() {1, 2}         ' Initialise each row of array
array(1) = New Integer() {4, 5, 6}
array(2) = New Integer() {7, 8, 9, 10, 11}

Console.WriteLine("Array contains " & dim1 & " rows" & vbCrLf)

For i = 0 To dim1 - 1                   ' Iterate through rows
Dim sum As Integer = 0

For a = 0 To array(i).Length - 1    ' Iterate through items contained within current row
sum += array(i)(a)              ' Sum all items contained within current row
Next

Console.WriteLine("Sum of row " & i & " : " & sum)  ' Write sum of current row to the console
Next

Console.ReadLine()                      ' Pause console so we can see results

End Sub

End Module```
```
 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
 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
Got it in Java, good practice for nested loops and 2D array manipulation.

CODE (Click to show)
Code:
``````public class OCNchallengeTwo
{
public static void main(String args[])
{
//2d array initialization
int c[][] = { {1},{3,4},{53,43,56}};

//call calculate method from the SumOfArray Class, pass it the c 2d array.
SumOfArray.calculate(c);

}

}

public class SumOfArray
{
public static void calculate(int a [][])
{
//initialize 1D array for output, rows of the 1D array should equal the rows in the 2D array
int oneDarray[]=new int [a.length];

//start the main loop for the rows, while int row is less then the length of the array it is passed, continue the loop
for(int row =0;row<a.length;row++)
{
//place holder for the elements in the 1D array

int sum =0;

//nested loop to take care of the rows, keep looping while col is less then the length of the columns in the 2D array

for(int col = 0;col<a[row].length;col++)
{

sum = sum + a[row][col];
}
//end of addition, row of 1D array is equal to the total of the columns in row from the 2D array

oneDarray[row]=sum;
System.out.println("Sum of row "+(row+1)+" is "+oneDarray[row]);
}
}
}```
```

Edited by Dman - 12/4/11 at 2:43pm
 i7 on the cheap (13 items)
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7
 i7 on the cheap (13 items)
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7
Quote:
Originally Posted by Dman

Got it in Java, good practice for nested loops and 2D array manipulation. CODE (Click to show)
Code:
``````public class OCNchallengeTwo
{
public static void main(String args[])
{
int c[][] = { {1},{3,4},{53,43,56}};
SumOfArray.calculate(c);

}

}
public class SumOfArray
{
public static void calculate(int a [][])
{
for(int row =0;row<a.length;row++)
{
int sum =0;
for(int col = 0;col<a[row].length;col++)
{
sum = sum + a[row][col];
}

System.out.println("Sum of row "+(row+1)+" is "+sum);
}
}
}```
```
Warning: Spoiler! (Click to show)

Hmmm... good job. Quite a bit different from my solution. You made 2 classes instead of using different methods in just one class. One thing you missed though is (although not very important but still part of the challenge) that you directly printed out the sum of every row of the 2-d array instead of first creating a new 1-d array which holds the values of sums of each row of the 2-d one and then printing each value out.

But still, it's nice to see different approaches to the same problem.

Oh and you didn't do the additional task - commenting (It is said to be every programmer's nightmare...not really )
 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
@ARandomOWl:

I am sorry but I am not too familiar with VB.net but then again, all languages are basically the same:D so I can understand your general algorithm. It should work fine so good job to you too. And especially nicely done since your code is "well-commented" too.

Anyone with a sound knowledge of VB.net willing to double-check his code?

Edited by {Unregistered} - 12/4/11 at 1:07pm
 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
Quote:
Originally Posted by {Unregistered}

Hmmm... good job. Quite a bit different from my solution. You made 2 classes instead of using different methods in just one class. One thing you missed though is (although not very important but still part of the challenge) that you directly printed out the sum of every row of the 2-d array instead of first creating a new 1-d array which holds the values of sums of each row of the 2-d one and then printing each value out.
But still, it's nice to see different approaches to the same problem.
Oh and you didn't do the additional task - commenting (It is said to be every programmer's nightmare...not really )

I missed that too. Here is new code. I don't like the way I have used two For loops, but that is how you seem to want it to be done.
VB.Net Code (Click to show)
Code:
``````Module Module1

Sub Main()
Const dim1 = 3                          ' Number of rows in array

Dim array(dim1)() As Integer            ' Initialise a 2 dimensional array of int
array(0) = New Integer() {1, 2}         ' Initialise each row of array
array(1) = New Integer() {4, 5, 6}
array(2) = New Integer() {7, 8, 9, 10, 11}

Dim sumArray(dim1) As Integer           ' Stores the sum of each row

Console.WriteLine("Array contains " & dim1 & " rows" & vbCrLf)

For i = 0 To dim1 - 1                   ' Iterate through rows
Dim sum As Integer = 0

For a = 0 To array(i).Length - 1    ' Iterate through items contained within current row
sum += array(i)(a)              ' Sum all items contained within current row
Next

sumArray(i) = sum                   ' Store sum in new array
Next

For i = 0 To dim1 - 1                   ' Iterate through rows
Console.WriteLine("Sum of row " & i & " : " & sumArray(i))  ' Write sum of current row to the console
Next

Console.ReadLine()                      ' Pause console so we can see results

End Sub

End Module```
```

Edited by ARandomOWL - 12/4/11 at 1:22pm
 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
 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
Hehe, sometimes I feel like Python is cheating
Code:
``````multi = [[1],[4,5,6],[7,8,9,10,11]]

def sum_of_arrays(array_of_arrays):
for array in array_of_arrays:
total = 0
for value in array:
total = total + value
print total

-----------------------------------------
sum_of_arrays(multi)

1
15
45
```
```
 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
 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
Quote:
Originally Posted by {Unregistered}

Hmmm... good job. Quite a bit different from my solution. You made 2 classes instead of using different methods in just one class. One thing you missed though is (although not very important but still part of the challenge) that you directly printed out the sum of every row of the 2-d array instead of first creating a new 1-d array which holds the values of sums of each row of the 2-d one and then printing each value out.
But still, it's nice to see different approaches to the same problem.
Oh and you didn't do the additional task - commenting (It is said to be every programmer's nightmare...not really )

Fixed it so it inputs it into another array (only needed 2 lines of code) as well as commented. When I took C++ in school, I was always taught that your main() should basically just call functions and do nothing else. It's also good practice for me, I don't want to get sloppy, for later when my programs get huge.
 i7 on the cheap (13 items)
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7
 i7 on the cheap (13 items)
CPUMotherboardGraphicsRAM
i7 970 Gigabyte X58A-UD7 evga GTX470 Corsair Vengence
Hard DriveOptical DriveOSMonitor
Intel X25-M 160GB LG GBW-H20L Windows 7 Ultimate Acer GD235HZ 23.6" 120HZ
KeyboardPowerCaseMouse
Logitech G15 Rocketfish 900W 80+ Silver Corsair Obsidian 800D Cyborg Rat 7
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• The Programming Challenge Master Thread
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › The Programming Challenge Master Thread