[ home | files | links | topics | stickers | about ]



Todays Stats

Visitors: 254
Referrers: 22
User Agents: 118
Pages Served: 2439
 
Total Pages
Served:

4626701


Search


Programming Challenge 2
programming : by Tommy - February 23rd 2011, 10:16PM
programming
Ok programmers and code monkeys, it's time for Programming Challenge 2. Nothing overly complicated this time. I was just messing around and thought you'd like this quick little brain teaser.
It's a "just for fun" challenge. Choose your favorite language for this one. Here it goes:

Part A: Display/print a vertical sin wave using * characters.
Part B: Display same sin wave horizontally using * characters.

Part A should get you going in the right direction (esp. if you've never played with the sin functions in your language), but Part B is a bit more tricky. No graphics libraries, cheater.

Post source in comments (must be logged in to comment).

Winner to receive 1 small shot of self satisfaction of completing trivial problem through useless challenge on obscure blog.
Tweet|Google+|Facebook


tags: programming sin_wave


+ Paul K.
  Feb 24, 2011 07:51
  
#18769
If you want to take it up a notch, have them compete to create the shortest possible code, measured in number of bytes for source code (with all includes).

reply

+ Nate K.
  Feb 24, 2011 10:58
  
#18773
Console.WriteLine("Vertical = A, Horizontal = B");
string input = Console.ReadLine();

if (input.Contains('A') || input.Contains('a'))
{
int amplitude = Console.WindowWidth / 2 - 1;
double frequency = 0.1;
for (double i=0; i < 100; i++)
{
Console.WriteLine(new string(' ', Convert.ToInt32(amplitude * Math.Sin(i * frequency) + amplitude)) + "*");
}
}
else if (input.Contains('B') || input.Contains('b'))
{
int amplitude = 10;
double frequency = 0.2;
int height = amplitude * 2;
for (int y = 1; y <= height + 1; y++)
{
int previousX = 0;
for (int x = 1; x < Console.WindowWidth; x++)
{
int spaces = Convert.ToInt32(amplitude * Math.Sin(x * frequency) + amplitude);
if (spaces == height - y + 1)
{
for (int i = 1; i < x - previousX; i++)
Console.Write(' ');

Console.Write("*");
previousX = x;
}
}
Console.WriteLine();
}
}
Console.WriteLine("Press Enter to close.");
Console.ReadLine();

reply

+ Nate K.
  Feb 24, 2011 11:02
  
#18774
// Formatting fail

reply

+ Tommy G.
  Feb 24, 2011 17:10
  
#18775
Java vertical:
public static void main(String[] args) {
      int periods = 5;
      int text_offset = 20;
      int amplitude = 20;
      int freq = 20;
      double resolution = .01;

      for(double x=0; x        for(double z = text_offset+(amplitude*Math.sin(x*freq)); z>0; z--)
            System.out.print(" ");
        System.out.println("*");
      }

}

reply




-+- neodux blog -+-
Page generated for 54.224.140.171 in 0.02324 seconds.
rss 2.0 feed