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 2

post #11 of 263
FreeBASIC

stars.bas
Code:
dim as integer i, j, c, quit
dim as string s

s = command(1)

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

? "Value chosen was: " & c
?

for i = 1 to c
    for j = 1 to i
        ? "* ";
    next
    ?
next

for i = (c - 1) to 1 step -1
    for j = 1 to i
        ? "* ";
    next
    ?
next

Usage:
Code:
stars.exe 5

Would output:
Code:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Or you could just run it straight and it prompts you for a number
Edited by axipher - 11/22/11 at 11:36am
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 #12 of 263
Codepad isn't working for me. Can I just submit my code? If so, here it is. It's in c++ and takes the number of asterisks you want in the largest/center line as a command line argument. Usage is: asterisks number_of_asterisks
Code:
#include<iostream>
#include<cstdlib>
using namespace std;

class A{
    public:
        void print();
        int n;
        int start;
};

void A::print(){

    int i, s;
    s = start;

    if(start == n){
        for(i = 0; i < n; i++){ cout << "*"; }
        cout << endl;
        return;
    }
    for(i = 0; i < s; i++){ cout << "*"; }
    cout << endl;
    start++;
    print();
    for(i = 0; i < s; i++){ cout << "*"; }
    cout << endl;

    return;
}


int main(int argc, char **argv){

    int n, i, j;
    A *a;

    if(argc != 2){
        cout << "Usage: asterisks number-of-asterisks" << endl;
        exit(1);
    }
    n = atoi(argv[1]);

    a = new A;
    a->n = n;
    a->start = 1;
    a->print();

    return 0;
}

Edited by superhead91 - 11/22/11 at 12:00pm
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #13 of 263
http://ideone.com/xXb2o
Code:
public class Main {
    public static void main(String[] args) {
        int count=5;
        
        for (int i=1;i<=count*2;i++) {
            if (i == count) {
            for (int j=0;j<count;j++)
                System.out.print("*");
            } else { 
                for (int j=0;j<(count-Math.max(count-i, i-count))%count;j++)
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Output:
Quote:
*
**
***
****
*****
****
***
**
*

Edited by Aximous - 11/22/11 at 12:04pm
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 
  hide details  
Reply
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 
  hide details  
Reply
post #14 of 263
http://codepad.org/y1mcS8Sf
Code:
#include <iostream> 

using namespace std;

int main()
{
        int numberOfCarac = 5;
        int i, h;

        for(i = 0; i < numberOfCarac; i++)
        {
                for (h = 0; h < i; h++)
                {       
                        cout << "*";
                }
                cout << endl;
        }

        for(i = numberOfCarac; i > 0; i--)
        {
                for (h = i; h > 0; h--)
                {       
                        cout << "*";
                }
                cout << endl;
        }

        return 0;
}

Here mine, codepad was not working and so I could not test it.
    
CPUMotherboardGraphicsRAM
AMD FX-8320 3.5Ghz @ 3.5ghz Gigabyte 990FXA-UD3 Gigabyte 6850 OC windforce 1GB GDDR5  8GB (2x4GB) G.SKILL RipjawsX F3-12800CL9D-8GBXL 
Hard DriveHard DriveHard DriveCooling
2TB Caviar Green 250GB Hitachi 64GB Crucial M4 Noctua D14 
OSMonitorMonitorMonitor
Linux + Windows 7 64Bits LG Flatron Wide 20.1" 1680x1050 LG Flatron W2442PA 24" 1080p Samsung B2430HD 24 With TV Tuner 1080p 
KeyboardPowerCaseMouse
CM Quickfire Rapid Blue Corsair TX750V2 CM 690 II Logitech G5 
Mouse PadAudio
Steelseries QCK Cloth medium Logitech x-540 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD FX-8320 3.5Ghz @ 3.5ghz Gigabyte 990FXA-UD3 Gigabyte 6850 OC windforce 1GB GDDR5  8GB (2x4GB) G.SKILL RipjawsX F3-12800CL9D-8GBXL 
Hard DriveHard DriveHard DriveCooling
2TB Caviar Green 250GB Hitachi 64GB Crucial M4 Noctua D14 
OSMonitorMonitorMonitor
Linux + Windows 7 64Bits LG Flatron Wide 20.1" 1680x1050 LG Flatron W2442PA 24" 1080p Samsung B2430HD 24 With TV Tuner 1080p 
KeyboardPowerCaseMouse
CM Quickfire Rapid Blue Corsair TX750V2 CM 690 II Logitech G5 
Mouse PadAudio
Steelseries QCK Cloth medium Logitech x-540 
  hide details  
Reply
post #15 of 263
Interesting to see the different ways people tackle the challenge smile.gif

http://www.ideone.com/BKjk4
Code (Click to show)
Code:
#include <iostream>
using namespace std;
 
int main() {
 
        int num, i, k;
        
        cin >> num;
        if (num < 1){
                cout << "Number entered must be above 0"; 
                return 0;
        }
 
        for (i = 0; i <= 2*(num)-1; i++){
                for (k = (i<num) ? (2*(num-i)) : 0; k <= 2*(num)-i-1; k++) cout << "*";
                cout << "\n";
        }
 
        return 0;
 
}
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 #16 of 263
Here's what I came up with using C#:
Code:
static void Challenge1()
{
    Console.WriteLine("Enter Number");
    Int32 numOfAsterisks = Int32.Parse(Console.ReadLine());
    Console.WriteLine();

    for (Int32 i = 1; i <= numOfAsterisks; i++)
    {
        for (Int32 t = 0; t < i; t++)
        {
            Console.WriteLine("*");
        }

        Console.WriteLine();
    }

    for (Int32 i = (numOfAsterisks - 1); i >= 1; i++)
    {
        for (Int32 t = i; t > 0; t--)
        {
            Console.WriteLine("*");
        }
     
        Console.WriteLine();
    }
}

Coded it on my work comp and then typed up on my laptop separately, but it should be correct.
Nevermore
(17 items)
 
  
Reply
Nevermore
(17 items)
 
  
Reply
post #17 of 263
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
    int i,j; int step = 1; int star = 1;
    int count = 2*atoi(argv[1])-2;
    if(argc == 2){
        for(i = 0; i <= count; i++){
            for(j = 0; j < star; j++) printf("*");
            printf("\n");
            if(i == count/2) step = -step;
            star += step;
        }
    }
}

in C, takes a command line argument for the number of stars, so stars 5 would output


*
**
***
****
*****
****
***
**
*
    
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 #18 of 263
Code:
import java.io.*;
import java.util.*;

public class OCNChallenge
{
  
  
  
  
  public static void main(String args[])
  {
    Scanner kbReader = new Scanner(System.in);
    
    System.out.print("Please enter the maximum amount of stars: ");
    int num = kbReader.nextInt();
    DrawStars draw = new DrawStars(num);
    draw.Draw();
    

  }
}

Code:
public class DrawStars
{
  
  public DrawStars(int a)
  {
    numstars=a;
     
  }
  
  
  public void Draw()
  {
    String s = "*";
    for(int i=0;i<numstars;i++)
    {
      for(int j=0;j<=i;j++)
        System.out.print(s);
      System.out.print("\n");
     
    }
    for(int k=0;k<numstars;k++)
    {  
      for(int l=(numstars-1);l>k;l--)
        System.out.print(s);
      System.out.print("\n");
    }
    
  }
  
  private int numstars;
 
}
j
usage:
javac OCNChallenge.java
java OCNChallenge

Takes user input for number of * to draw. * could also be replaced with any other symbol.
Edited by Dman - 11/22/11 at 8:29pm
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 
  hide details  
Reply
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 
  hide details  
Reply
post #19 of 263
Quote:
Originally Posted by ARandomOWl View Post

http://www.ideone.com/BKjk4

Nice website, I think I will try it.

It's cool to look at how people do it, and what language they use.

I love to use on-site compiler so that I can program on any computer (even my tablet) without any problem.


For some other challenge, you could ask to make some form with *.
I remember doing it 2 years ago when I first start my computer technology course at school. Doing a square, a triangle, a diamond. Full or empty. And even some more complex form like a circle... didn't manage to do this one...
Edited by spikexp - 11/22/11 at 4:59pm
    
CPUMotherboardGraphicsRAM
AMD FX-8320 3.5Ghz @ 3.5ghz Gigabyte 990FXA-UD3 Gigabyte 6850 OC windforce 1GB GDDR5  8GB (2x4GB) G.SKILL RipjawsX F3-12800CL9D-8GBXL 
Hard DriveHard DriveHard DriveCooling
2TB Caviar Green 250GB Hitachi 64GB Crucial M4 Noctua D14 
OSMonitorMonitorMonitor
Linux + Windows 7 64Bits LG Flatron Wide 20.1" 1680x1050 LG Flatron W2442PA 24" 1080p Samsung B2430HD 24 With TV Tuner 1080p 
KeyboardPowerCaseMouse
CM Quickfire Rapid Blue Corsair TX750V2 CM 690 II Logitech G5 
Mouse PadAudio
Steelseries QCK Cloth medium Logitech x-540 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
AMD FX-8320 3.5Ghz @ 3.5ghz Gigabyte 990FXA-UD3 Gigabyte 6850 OC windforce 1GB GDDR5  8GB (2x4GB) G.SKILL RipjawsX F3-12800CL9D-8GBXL 
Hard DriveHard DriveHard DriveCooling
2TB Caviar Green 250GB Hitachi 64GB Crucial M4 Noctua D14 
OSMonitorMonitorMonitor
Linux + Windows 7 64Bits LG Flatron Wide 20.1" 1680x1050 LG Flatron W2442PA 24" 1080p Samsung B2430HD 24 With TV Tuner 1080p 
KeyboardPowerCaseMouse
CM Quickfire Rapid Blue Corsair TX750V2 CM 690 II Logitech G5 
Mouse PadAudio
Steelseries QCK Cloth medium Logitech x-540 
  hide details  
Reply
post #20 of 263
Python here, went for simple to understand rather than fancy code, but it gets the job done.
Code:
NUMBER_OF_STARS = 5
i = 1
while i <= NUMBER_OF_STARS:
    print ('*' * i)
    i = i + 1
while i > 0:
    print ('*' * i)
    i = i - 1
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
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