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 16

post #151 of 263
I just wrote it >.< I would never plagiarize. I guess we both thought of the solution in a similar manner. I also wrote challenge # 2. I'm pretty sure I did it correctly.

Here's the code:
Code:
#include <iostream>
using namespace std;
int main(){
        const int col = 9,
                  row = 4,
                  arr[row][col] = {{2, 3, 5}, {5, 5, 15}, {15, 10, 20, 6}, {15, 8, 6, 2, 1, 1, 1, 1, 10}};
        int sum = 0;
        for(int r = 0; r < row ; r++){
                for(int c = 0; c < col ; c++)
                        sum += arr [r][c];
                cout << "Sum of row " << r+1 << ": " << sum << endl;
                sum = 0;
        }
        return 0;
}

Here's what it does:

OCN2.png
Edited by Kyleinator - 12/31/11 at 5:31pm
My rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
4770k M power EVGA GTX 980 SC G.SKILL Sniper (2 x 4GB) DDR3 1600 9-9-9-24 
Hard DriveCoolingOSMonitor
512GB Samsung 940 Pro h100 Windows 8 Home premium Asus VG248QE 
MonitorKeyboardPowerCase
Asus VG248QE Corsair K90 Kingwin Lazer Gold 850w Fractal Design Arc Midi R2 
MouseMouse PadAudioAudio
Logitech G700s Corsair Asus Xonar M-Audio BX5 d2 
Audio
krk 10s 
  hide details  
Reply
My rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
4770k M power EVGA GTX 980 SC G.SKILL Sniper (2 x 4GB) DDR3 1600 9-9-9-24 
Hard DriveCoolingOSMonitor
512GB Samsung 940 Pro h100 Windows 8 Home premium Asus VG248QE 
MonitorKeyboardPowerCase
Asus VG248QE Corsair K90 Kingwin Lazer Gold 850w Fractal Design Arc Midi R2 
MouseMouse PadAudioAudio
Logitech G700s Corsair Asus Xonar M-Audio BX5 d2 
Audio
krk 10s 
  hide details  
Reply
post #152 of 263
Here is my submission for Challenge #2, which is also written in C. It may be a bit overkill, but oh well smile.gif Here it is, comments and all.
Code:

/* Dynamically allocates memory for the 2D array of integers and
   for the array of sums. Populates the 2D array with random
   integers limited by MAX_INT and creates the 2D array with an
   initial row and column limit defined by INIT_ROWS and
   INIT_COLS. The number of rows and the number of columns in
   each row are randomized, thus a ragged array is created. This
   program can easily be modified to take in user input rather
   than randomizing the number of rows, columns, and/or integers
   that populate the 2D array. */

#include <stdio.h>
#include <stdlib.h>

#define MAX_ROWS 10
#define MAX_COLS 10
#define MAX_INT 100

static void *smalloc(size_t);

int main() {
    int **arr;
    int *sums;
    int num_rows, num_cols, row, col;

    srand(5);    /* Seed the random int generator */
    num_rows = rand() % MAX_ROWS;    /* Randomize number of rows */
    arr = smalloc(sizeof(int *) * num_rows);
    sums = smalloc(sizeof(int *) * num_rows);

    for (row = 0; row < num_rows; row++) {
        num_cols = rand() % MAX_COLS;    /* Randomize the number of columns in the row */
        sums[row] = num_cols;    /* Store the number of columns in the row in the sums array */
        arr[row] = smalloc(sizeof(int) * num_cols);
        for (col = 0; col < num_cols; col++) {
            arr[row][col] = rand() % MAX_INT;    /* Populate cell with random int */
        }
    }

    /* Store the sum of all integers in each row into the sums array and print results */
    for (row = 0; row < num_rows; row++) {
        num_cols = sums[row];    /* Retrieve the number of columns in the row */
        sums[row] = 0;    /* Clear the value in current cell of the sums array */
        for (col = 0; col < num_cols; col++) {
             sums[row] += arr[row][col];
        }
        printf("Sum of row %d : %d\n", row, sums[row]);
    }

    /* Free all allocated memory */
    for (row = 0; row < num_rows; row++) {
         free(arr[row]);
    }
    free(arr);
    free(sums);

    return 0;
}

/* A safe version of malloc(). Combines error checking with the original malloc()
   call in order to improve code readability. */
 static void *smalloc(size_t size) {
    void *ptr;

    if (!(ptr = malloc(size))) {
        perror("Memory Allocation Error");
        exit(1);
    }

    return ptr;
}


Here is the output:

126
Edited by Leeeeeeeeee - 1/3/12 at 3:19am
Wind Tunnel
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i5 3570K ASUS P8Z77-V LK ASUS AMD Radeon HD 7970 Diamond AMD Radeon HD 7970 
RAMHard DriveHard DriveHard Drive
8 Gb G.Skill Ripjaws 120Gb OCZ Vertex 2 620gb WD Caviar Black 2x 1Tb WD Caviar Black  
Optical DriveCoolingOSMonitor
2x DVD Drive Zalman CNPS9700 Windows 7 Ultimate 64-Bit Alienware Optx AW2310 
KeyboardPowerCaseMouse
Das Model S Professional Rosewill Lightning 1300W Antec 1200 Logitech G500 
Mouse PadAudioOther
Mionix Propus 380 Creative Sound Blaster X-Fi Titanium HD Audio-Technica ATH-A900X 
  hide details  
Reply
Wind Tunnel
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i5 3570K ASUS P8Z77-V LK ASUS AMD Radeon HD 7970 Diamond AMD Radeon HD 7970 
RAMHard DriveHard DriveHard Drive
8 Gb G.Skill Ripjaws 120Gb OCZ Vertex 2 620gb WD Caviar Black 2x 1Tb WD Caviar Black  
Optical DriveCoolingOSMonitor
2x DVD Drive Zalman CNPS9700 Windows 7 Ultimate 64-Bit Alienware Optx AW2310 
KeyboardPowerCaseMouse
Das Model S Professional Rosewill Lightning 1300W Antec 1200 Logitech G500 
Mouse PadAudioOther
Mionix Propus 380 Creative Sound Blaster X-Fi Titanium HD Audio-Technica ATH-A900X 
  hide details  
Reply
post #153 of 263
Here is my submission for challenge #1 (even though its over). Its done in python. Ive been teaching myself it so this was a good challenge for me!
Code:
star = "*"
max_star = 1
while max_star != 0:
    max_star = int(input("How many stars do you want?"))
    count = 0
    while max_star >= count:
        print(star * count)
        count = count + 1
    while count != 0:
        print(star * (count - 1))
        count = count - 1
print("goodbye")

Would you mind telling me anything i could do better? I tried doing it very simply.
Skyship
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 1090T BE ASUS Crosshair IV XFX 5770 OCZ AMD Edition  
Hard DriveOSMonitorKeyboard
Seagate Windows 7 Pro Sceptre 22" widescreen Saitek Eclipse 1 
PowerCaseMouseMouse Pad
XION 800w modular Cooler Master Storm Scout Razer DeathAdder Steel Series QCK 
  hide details  
Reply
Skyship
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II 1090T BE ASUS Crosshair IV XFX 5770 OCZ AMD Edition  
Hard DriveOSMonitorKeyboard
Seagate Windows 7 Pro Sceptre 22" widescreen Saitek Eclipse 1 
PowerCaseMouseMouse Pad
XION 800w modular Cooler Master Storm Scout Razer DeathAdder Steel Series QCK 
  hide details  
Reply
post #154 of 263
I know I'm really late but I wanted to do them regardless. Here are both my entries though rolleyes.gif

(I'm not sure I completed challenge two correctly; probably because I didn't exactly understand the question tongue.gif) Challenge 1 (Click to show)
Code:
#include<iostream>

using namespace std;

void printStars(int num = 0);

int main()
{
int maxStars = 0;
  
do
{
      cout<<"Enter a number from 1 - 10: ";
      cin>>maxStars;
      cin.get();
}while(maxStars < 0 || maxStars > 10);

for(int i = 0; i < maxStars; ++i)
printStars(i);
                  
for(int i = maxStars; i != 0; --i)
printStars(i);
      
cout<<"\nPress enter to exit";
cin.get();      
return 0;
}
               

void printStars(int num) //for printing stars to the screen
{
     for(int i = 0; i < num; ++i)
     cout<<"*";
     cout<<"\n"; 
}
307
Challenge 2 (Click to show)
Code:
#include<iostream>

using namespace std;

int main()
{
    const int ROWS = 5; 
    const int COLUMNS = 5;
    int myArray[ROWS][COLUMNS] = {
        {5,3,34,6},
        {7,3,8,24,64},
        {38,5,2,72,1},
        {5,12,23},
        {62,24,3,6,16}
    };
    
    int sumArray[ROWS];
    
    for(int i = 0; i < ROWS; i++) //assigning all elements 0
    sumArray[i] = 0;
    
    for(int i = 0; i < ROWS; i++) //used to iterate through rows of myArray and through positions in sumArray 
    {
            for(int j = 0; j < COLUMNS; j++) //used to iterate through columns in myArray
            { 
            sumArray[i] += myArray[i][j]; //adds the element in row 'i,j' in myArray to the element 'i' in sumArray
            }
    }
    
    for(int i = 0; i < ROWS; i++) //outputs the elements in sumArray AKA the sums of each row in myArray
    cout<<"Sum of row "<<i<<": "<<sumArray[i]<<endl;

cin.get();
return 0;
}
306
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
post #155 of 263
I just started to learn Java and this challenge has been amazing in helping me put into practice everything I've learnt.

I know I'm a little late to the party but this is my submission to the first challenge, written in Java.
Challenge 1: (Click to show)
Code:
import java.util.Scanner;

public class Challenge1 {
        public static void main(String[] args){
                
                /* Prompt user to input max number of stars */
                char star = '*';
                Scanner input = new Scanner(System.in);
                System.out.println("Enter the maximum amount of stars: ");
                int maxStars = input.nextInt();
                
                /* Start increasing the number of stars until max star count is reached */
                for(int starCounter=0; starCounter<=maxStars; ++starCounter){
                        /* Inner loop increases the amount of stars per line */
                        for(int starsPerLine=0; starsPerLine<starCounter; ++starsPerLine)
                                System.out.print(star);
                                System.out.print("\n");                       
                }
                
                /* Start decreasing the number of stars to zero after max star count has been reached */
                for(int StarCounter2=maxStars-1; StarCounter2>=0; --StarCounter2){
                        /* Inner loop decreases the amount of stars per line */
                        for(int starsPerLine2=0; starsPerLine2<StarCounter2; ++starsPerLine2)
                                System.out.print(star);
                                System.out.print("\n");
                }
                
        }
}

If you guys have any suggestions how I could clean up this code I'd be very appreciative. smile.gif
Now I'm off to try Challenge 2...

EDIT: OK, finished the 2nd challenge. Sort of on a roll now and am loving it!
Challenge 2: (Click to show)
Code:
public class Challenge2 {
        public static void main(String[] args){
                
                int array[][] = {{2, 56, 77, 3}, {4, 7, 33}, {66, 3, 56, 15}, {2, 55}};                 // Multidimensional array which values can easily be edited
                int sum = 0;                                                                                                            // Initialise sum of rows equal zero
                
                for(int row=0; row<array.length; row++){                                                             // Loop through the rows
                        for(int column=0; column<array[row].length; column++)                        // Loop through the column values
                                sum += array[row][column];                                                              // Add the column values within the row
                                System.out.printf("Sum of row %d: %d\n", row+1, sum);         // Output the row number and sum of each row
                                sum=0;                                                                                                  // Reset sum value to 0
                }
        }
}

Edited by Rypac1502 - 1/7/12 at 1:41am
post #156 of 263
Thread Starter 
Hello guys!

I am sorry that I haven't been able to update the Programming Challenges as often as I would have liked to (or as I should) but I have just been really really busy lately.

However, the good news is that I have much more time on my hands now and I will try to dedicate more of it toward improving these challenges and generally taking care of the Programming section.

I will be looking closely at all the solutions you guys post from now on. I will also be starting the "Programmer of the Week" thing from now on (I mentioned this in a post some time back). Also, as you may already know, each programming challenge will have a separate thread for it and it will be linked to in the first post of this thread. That means that this thread will be used for mentioning updates or general discussions about programming including any suggestions or comments you guys may have.

Meanwhile, I will also try to catch up with ByteMyAscii to see how we can update and improve the "Coding and Programming FAQ". If you guys have any suggestions at all, I will very much appreciate them. smile.gif


Thank you very much for your participation.

@Rypac1502: Welcome to OCN! I am glad you found this useful.
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 #157 of 263
𝕴 𝖙𝖍𝖎𝖓𝖐 𝖋𝖔𝖗 𝖙𝖍𝖊 𝖓𝖊𝖝𝖙 𝖈𝖍𝖆𝖑𝖑𝖊𝖓𝖌𝖊 𝖎𝖙 𝖒𝖎𝖌𝖍𝖙 𝖇𝖊 𝖋𝖚𝖓 𝖙𝖔 𝖌𝖊𝖙 𝖙𝖍𝖊𝖒 𝖙𝖔 𝖙𝖆𝖐𝖊 𝖆𝖓 𝖆𝖗𝖇𝖎𝖙𝖗𝖆𝖗𝖞 𝖆𝖑𝖕𝖍𝖆𝖓𝖚𝖒𝖊𝖗𝖎𝖈 𝖘𝖙𝖗𝖎𝖓𝖌 𝖆𝖓𝖉 𝖈𝖔𝖓𝖛𝖊𝖗𝖙 𝖎𝖙 𝖎𝖓𝖙𝖔 𝖆𝖓 𝖔𝖇𝖓𝖔𝖝𝖎𝖔𝖚𝖘 𝖚𝖓𝖎𝖈𝖔𝖉𝖊 𝖘𝖚𝖇𝖘𝖊𝖙 𝖑𝖎𝖐𝖊 𝖇𝖑𝖆𝖈𝖐𝖑𝖊𝖙𝖙𝖊𝖗. 𝓞𝓻 𝓲𝓽 𝓶𝓲𝓰𝓱𝓽 𝓪𝓬𝓽𝓾𝓪𝓵𝓵𝔂 𝓫𝓮 𝓫𝓮𝓽𝓽𝓮𝓻 𝓽𝓸 𝓾𝓼𝓮 𝓼𝓸𝓶𝓮𝓽𝓱𝓲𝓷𝓰 𝓵𝓲𝓴𝓮 𝓫𝓸𝓵𝓭 𝓼𝓬𝓻𝓲𝓹𝓽, 𝓫𝓮𝓬𝓪𝓾𝓼𝓮 𝓲𝓽𝓼 𝓶𝓸𝓻𝓮 𝓻𝓮𝓪𝓭𝓪𝓫𝓵𝓮.
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
post #158 of 263
Thread Starter 
Programming Challenge #4 to be updated soon...
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 #159 of 263
Completely off topic, but you're missing a semi-colon in your thread heading: System.out.println("Programming Essentials: ");

tongue.gif
post #160 of 263
http://ideone.com/vGyGU
that should be task 1

Thanks for doing these challenges! I never know what to code...
    
CPUMotherboardGraphicsRAM
i5 2500k @ stock (h61...) msi h61MU-e35 MSI 570 @ 770/2000 4 gb xms 3 
PowerCase
CM GX 650 HP PAVILION (EFF YES) 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i5 2500k @ stock (h61...) msi h61MU-e35 MSI 570 @ 770/2000 4 gb xms 3 
PowerCase
CM GX 650 HP PAVILION (EFF YES) 
  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