Overclock.net banner

1 - 20 of 263 Posts

·
Premium Member
Joined
·
1,563 Posts
Discussion Starter #1
Hello guys!

The old "Programming Challenge" thread is quite outdated and desperately needs to be updated for this decade!
tongue.gif
I, therefore, have created a new thread.

We have some programmers on this site and it would be good to kick off these programming challenges again.

Note about challenge : This is geared toward beginner or intermediate programmers. The challenges will be easy, yet hard enough to be challenging for beginners. There will be no restriction to which languages you can use. You can use an online compiler to show that it executes and you can post your code here.
Each challenge will have a time duration of about a week so be sure to submit your entry within that time.
Thank you.

LATEST PROGRAMMING CHALLENGE: Challenge # 7 posted!



If you are participating, add in your name HERE

https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Am3S8fDXvrogdE01TlNReU5fTFBQU2ZueWw4ZFI2ZWc&single=true&gid=0&output=html&widget=true

~ {Unregistered}
 

·
Premium Member
Joined
·
1,563 Posts
Discussion Starter #2
Okay, so honestly the poll turn out is better than I expected.
thumb.gif


I'll try to issue the first programming challenge by today or tomorrow but I'd like to tell you guys that I'm a little busy during this week and can only guarantee that I'll issue the challenge by the end of this week.

If any of you guys want to volunteer and post the challenge or would like to contribute in any way, then do post here.

By the way, this is also an extra bump to make sure more people see this.
smile.gif


Hopefully, this will be up and running by today!
thumb.gif
 

·
Premium Member
Joined
·
1,563 Posts
Discussion Starter #4
Quote:
Originally Posted by Kyleinator View Post

How advanced will the challenges be? I have taken intro to C++, c, and Java. Will that be enough or is this aimed towards advanced programmers?
No, this is geared toward beginner or intermediate programmers. The challenges will be easy, yet hard enough to be challenging for beginners. There will be no restriction to which languages you can use. You can use an online compiler to show that it executes and you can post your code here.

Glad you asked; I think I should add this to the OP.
 

·
Premium Member
Joined
·
1,563 Posts
Discussion Starter #7
Okay so guys, here is the very first programming challenge of this thread. Remember that there is no restriction as to which language you have to use (even though I do expect that most of you will be using C++, Java or Python).

I'm going to start of with something really simple (depending on your programming skill of course), since I'm not really sure how well you're going to perform. The difficulty levels of future challenges will be based on your response in this challenge.

Here's what you have to do:

Write code which just displays a pattern similar to the one below:

*
**
***
****
*****
****
***
**
*

You'll have to make it customisable so that it is possible to change the number of maximum *'s that appear. For example, in the pattern shown above, the maximum number of *'s is 5. The pattern increases the number of stars in every row until it reaches the maximum number and then decreases the number of stars (asterisks). You can either allow it to add input from the user, or you can add input from an object. The only requirement is that you must be able to change the size of the pattern (number of *'s) without re-doing or adding anything significant to the code.

Hoping that at least the 16 people who voted actually make an effort to work on this.

~{Unregistered}
 

·
Registered
Joined
·
436 Posts
You should post the challenge in a spoiler box in the first post, so we can easily go back to see it without going to far in the topic.

How do we submit the code?
I did a little something with c++ on codepad.org.
I'm at school and I don't have access to anything better (but still, console c++ is quite basic).
I also can't put a user input because it doesn't support it.
 

·
Moderator
Joined
·
13,090 Posts
FreeBASIC?
 

·
Premium Member
Joined
·
1,563 Posts
Discussion Starter #10
Quote:
Originally Posted by spikexp View Post

You should post the challenge in a spoiler box in the first post, so we can easily go back to see it without going to far in the topic.
How do we submit the code?
I did a little something with c++ on codepad.org.
I'm at school and I don't have access to anything better (but still, console c++ is quite basic).
I also can't put a user input because it doesn't support it.
Will do.

As for the code, codepad.org is fine. Instead of adding user input, you can just make it so that changing the value of a particular variable can allow you to change the number of stars printed. Input is the easy part; the actual important part of the program is that which actually executes your planned algorithm. If you can do that, then it's absolutely fine.
 

·
Moderator
Joined
·
13,090 Posts
FreeBASIC

stars.bas

Code:

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:

Code:
stars.exe 5
Would output:

Code:

Code:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Or you could just run it straight and it prompts you for a number
 

·
Super Moderator
Joined
·
3,662 Posts
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:

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;
}
 

·
Registered
Joined
·
490 Posts
http://ideone.com/xXb2o

Code:

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:
*
**
***
****
*****
****
***
**
*
 

·
Registered
Joined
·
436 Posts
http://codepad.org/y1mcS8Sf

Code:

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.
 

·
Registered
Joined
·
494 Posts
Interesting to see the different ways people tackle the challenge
smile.gif


http://www.ideone.com/BKjk4
Code:

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;

}
 

·
*THIS SPACE FOR RENT*
Joined
·
595 Posts
Here's what I came up with using C#:

Code:

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.
 

·
Premium Member
Joined
·
12,992 Posts
Code:

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

*
**
***
****
*****
****
***
**
*
 

·
Premium Member
Joined
·
3,532 Posts
Code:

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:

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.
 

·
Registered
Joined
·
436 Posts
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...
 

·
Registered
Joined
·
511 Posts
Python here, went for simple to understand rather than fancy code, but it gets the job done.

Code:

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
 
1 - 20 of 263 Posts
Top