Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Programming challenge


  • Please log in to reply

#16
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
Yuck! :tazz:
  • 0

Advertisements


#17
DeathOutdone

DeathOutdone

    Member

  • Member
  • PipPipPip
  • 241 posts
My challenge: Program a complete operating system and host it here so we can all test it LOL.
  • 0

#18
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
I know it's been more than a week, but I just saw the challenge today. Whether the submission is valid or not, it's hard for me to pass up a programming challenge! :tazz:

I'm a little confused about the language requirement - that it must support matrices. It kinda sounds like it should be done in matlab or something. I don't have anything like that, so I decided to use C#.

The attached .exe requires the .net framework version 1.1, which everyone that is up-to-date on the windows updates should have. The code should work if compiled with framework version 1.0, and it shouldn't be too bad to translate it to C++ if anyone so desires.

Here's my code:

using System;

namespace Determinant
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			int size = -1;

			Console.WriteLine("\nWelcome to Ricci's Matrix Determinant Calculator!");
			Console.WriteLine("(Press Ctrl-C to quit at any time.)\n");

			size = ConsoleReadInt("Enter the size of the square matrix: ");
			while(size < 2)
			{
				Console.WriteLine("The matrix must be at least 2x2.");
				size = ConsoleReadInt("Enter the size of the square matrix: ");
			}
			double[,] matrix = new double[size, size];

			ConsoleReadMatrix(matrix);

			Console.WriteLine();
			ConsoleWriteMatrix(matrix);

			Console.WriteLine();
			try
			{
				double determinant = ComputeDeterminant(matrix);
				Console.WriteLine("The value of the determinant is: {0}", determinant);
			}
			catch(StackOverflowException)
			{
				Console.WriteLine("Out of memory, result cannot be computed.");
			}
		}

		static int ConsoleReadInt(string prompt)
		{
			bool success = true;
			int val = 0;

			Console.Write(prompt);

			try
			{
				val = int.Parse(Console.ReadLine());
			}
			catch
			{
				success = false;
			}

			while(!success)
			{
				Console.WriteLine("Invalid integer format, try again or press Ctrl-C to quit.");
				Console.Write(prompt);

				try
				{
					val = int.Parse(Console.ReadLine());
					success = true;
				}
				catch{}
			}

			return val;
		}

		static double ConsoleReadDouble(string prompt)
		{
			bool success = true;
			double val = 0;

			Console.Write(prompt);

			try
			{
				val = double.Parse(Console.ReadLine());
			}
			catch
			{
				success = false;
			}

			while(!success)
			{
				Console.WriteLine("Invalid integer format, try again or press Ctrl-C to quit.");
				Console.Write(prompt);

				try
				{
					val = double.Parse(Console.ReadLine());
					success = true;
				}
				catch{}
			}

			return val;
		}

		static void ConsoleReadMatrix(double[,] matrix)
		{
			Console.WriteLine("\nEnter the values of the matrix, one at a time.");

			for(int row = 0; row < matrix.GetLength(0); row++)
			{
				for(int col = 0; col < matrix.GetLength(1); col++)
				{
					matrix[row, col] = ConsoleReadDouble(string.Format("Enter the value for row {0}, col {1}: ", row+1, col+1));
				}
			}
		}

		static void ConsoleWriteMatrix(double[,] matrix)
		{
			Console.WriteLine("Your matrix:");
			for(int row = 0; row < matrix.GetLength(0); row++)
			{
				Console.Write(matrix[row, 0]);
				for(int col = 1; col < matrix.GetLength(1); col++)
				{
					Console.Write("\t{0}", matrix[row, col]);
				}
				Console.WriteLine();
			}
		}

		static double ComputeDeterminant(double[,] matrix)
		{
			if(matrix.GetLength(0) != matrix.GetLength(1))
			{
				throw new Exception("The matrix must be square.");
			}
			if(matrix.GetLength(0) < 2)
			{
				throw new Exception("The matrix must be at least 2x2.");
			}

			double result = ComputeDeterminantRecurse(matrix);

			return result;
		}

		static double ComputeDeterminantRecurse(double[,] matrix)
		{
			double result = 0.0;

			if(matrix.GetLength(0) == 2)
			{
				result = (matrix[0,0] * matrix[1,1]) - (matrix[0,1] * matrix[1,0]);
			}
			else
			{
				result = ComputeLargeMatrixDeterminant(matrix);
			}

			return result;
		}

		static double ComputeLargeMatrixDeterminant(double[,] matrix)
		{
			double total = 0.0;

			for(int col = 0; col < matrix.GetLength(1); col++)
			{
				double termVal = matrix[0, col] * ComputeDeterminantRecurse(MatrixWithoutFirstRowAndColN(matrix, col));
				if((col) % 2 == 0)
				{
					total += termVal;
				}
				else
				{
					total -= termVal;
				}
			}

			return total;
		}

		static double[,] MatrixWithoutFirstRowAndColN(double[,] matrix, int n)
		{
			double[,] newMatrix = new double[matrix.GetLength(0)-1, matrix.GetLength(1)-1];

			for(int row = 1; row < matrix.GetLength(0); row++)
			{
				int newCol = 0;

				for(int col = 0; col < matrix.GetLength(1); col++)
				{
					if(col != n)
					{
						newMatrix[row-1, newCol] = matrix[row,col];
						newCol++;
					}
				}
			}

			return newMatrix;
		}
	}
}

-Ricci

Attached Files


Edited by ricci, 17 November 2005 - 02:20 PM.

  • 0

#19
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Man, I've never tried to add an attachment before, and I can't tell if it's there or not. I'll attach it to this too, just in case.

-Ricci
  • 0

#20
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
Congrats! :tazz:
You really did'nt leave any room for any user-input related errors.
Your program also inaugurated my Visual Studio 2005 Professional. :)
Now its your turn to give us a qus.


It will be best if you attach the exe, as user are more likely to have the .NET Framework Redistrituable than the .NET SDK or Visual Studio .NET 2003 or 2005.
To attach follow the steps below:
Click on the Edit Button, under your last post.
The Click Full Edit.
Below the text editor you will see the Browse button, click it. Choose your file and don't forget to click on the Add This Attachment button.
  • 0

#21
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Ok, I finally figured out what wasn't working when I tried to attach my file. I had to zip it up and attach the zip file. I'm not sure if the problem was that it was an "exe" file or that it was too large or what, but the zip worked...

So, the next quiz. Write a program that takes the name of a text file on the command line. The program should then scan the file and count the number of times all of the words in the file are used. For simplicity's sake, words are seperated by tabs, carriage returns, line feeds, and spaces only. Consider punctuation part of the word. Ignore upper/lower case differences. After scanning the file, output how many times each word was used. Order does not matter.

Sample Input:

Introduction

Understanding COM takes time and energy. I can say with great confidence that there is always more to learn. However, as with most technologies, COM does provide a core body of knowledge that works as the backbone for just about everything else.


With this input file, your output should look like the following (barring any typo's or other mistakes as I do this by hand):

Introduction - 1
Understanding - 1
COM - 2
takes - 1
time - 1
and - 1
energy. - 1
I - 1
can - 1
say - 1
with - 2
great - 1
confidence - 1
that - 2
there - 1
is - 1
always - 1
more - 1
to - 1
learn. - 1
However, - 1
as - 2
most - 1
technologies, - 1
does - 1
provide - 1
a - 1
core - 1
body - 1
of - 1
knowledge - 1
works - 1
the - 1
backbone - 1
for - 1
just - 1
about - 1
everything - 1
else. - 1

Happy coding,
Ricci
  • 0

#22
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
*cough cough hash table cough * :tazz:
  • 0

#23
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
I've heard there was a pragma written for some language, seems like it was Perl but maybe not, that stops broken while loops, so while I believe it was probably just a rumor and not true, that inspired me to write this challenge, which I would consider expert difficulty.

--> Use any object-oriented language you want..except Java because I hate Java.
--> Make a program that will could search any of the following Perl code chunk (or their equivilants in whatever language you're using; I'm sure you can transpose them) and report that the while loops within it are nonterminating, and give the line number of the loops' initiation:
#!usr/bin/perl -w

my $x = 1
my $y = 2
while ( $x <= $y ) {
  $x = $x+1
  $y = $y+1
}
#!usr/bin/perl -w

my $x = 1
while ( $x != -1 ) {
  $x = $x+1
}
#!usr/bin/perl -w

my $x = 1
my $y = 3
while ( $x != $y ) {
  $x = $x+1
  $y = $y-2
}

Edited by W-Unit, 21 November 2005 - 12:58 PM.

  • 0

#24
am9886

am9886

    New Member

  • Member
  • Pip
  • 1 posts
Here's a solution to ricci's implemented with a hash table :tazz: in C++, source code included.

I wrote the hash table and singly linked list only writing the functions needed to complete this challenge.

Attached Files


Edited by am9886, 22 November 2005 - 04:25 AM.

  • 0

#25
darth_ash

darth_ash

    Member 1K

  • Member
  • PipPipPipPip
  • 1,382 posts
am9886,
When I run the exe I get no output at all. (I also ran it from cmd by typing the name as you didnt put a system("PAUSE"):tazz:
I also tries re-compiling the files an I get some errors.

Wonder if the other are facing the same problem.
  • 0

Advertisements


#26
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Sorry for the delay in response. I kinda took last week off from most things electronic.

I didn't run the exe. I compiled the code and ran the exe the compiler produced. Doing this and providing an input file gave me some results.

It basically seems to work. There are, however, lots of special cases where it doesn't. I don't know how much we want to get hung up on this kind of thing, but here are some input examples that mess it up:

1.

this

This input produces no results. Note that the input contains only 4 characters (not including EOF character). There is no carriage return on the end.

2.

[this] [is] [a] [bracket] [test].

This input causes a crash.

3.

This is a test.

This input yields the following results:

a - 1
is - 1
This - 1

Note that "test" is not included in the results. This is a problem with the hash function used.

Anyways, these problems can be fixed with simple tweeks to am9886's code. Since he/she has already completed the bulk of the work, I propose that am9886 submit the next challenge.

-Ricci
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP