Programming challenge
#16
Posted 05 November 2005 - 03:02 PM
#17
Posted 05 November 2005 - 06:48 PM
#18
Posted 17 November 2005 - 12:54 AM
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.
#19
Posted 17 November 2005 - 12:59 AM
-Ricci
#20
Posted 17 November 2005 - 10:07 AM
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.
#21
Posted 17 November 2005 - 04:41 PM
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):
Happy coding,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
Ricci
#22
Posted 17 November 2005 - 04:45 PM
#23
Posted 21 November 2005 - 12:53 PM
--> 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.
#24
Posted 22 November 2005 - 04:24 AM
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.
#25
Posted 28 November 2005 - 01:55 AM
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")
I also tries re-compiling the files an I get some errors.
Wonder if the other are facing the same problem.
#26
Posted 28 November 2005 - 11:54 AM
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 input produces no results. Note that the input contains only 4 characters (not including EOF character). There is no carriage return on the end.this
2.
This input causes a crash.[this] [is] [a] [bracket] [test].
3.
This input yields the following results:This is a test.
Note that "test" is not included in the results. This is a problem with the hash function used.a - 1
is - 1
This - 1
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
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users