1. Home
  2. Computing & Technology
  3. C / C++ / C#

Programming Challenge 2 - (Entries Closed)

By , About.com Guide

Programming Challenge 2 requires you to read in a text file, process the contents of the file and output the results. This challenge runs through July 2007 and the ten fastest correct entries will be listed.

The file consists of 50 lines of text. Each line consists of 3 sections: First is 3 characters made up of spaces and digits that you can ignore, then 80 characters which are either a space or a + then another 2 characters. The first 3 and last 2 characters are just 1 or 2 digits line numbers padded with spaces. The important part is the 80 characters.

I.e. it will look something like this (the first line <3> etc is not part of the file).

<3><-------------------------------------------80--
--------------------------------------><2>

File starts here:
1 1
27 ++++++++ ++++ +++++++ ++++++++++++ +++ 27
28 ++++++++ ++++ +++++++ ++++++++++++ + ++++ ++++++ 28
This is a map of a game world where each location is either land or sea. Land points are +, sea are spaces. What you have to do is process the 80 x 50 map, determine how many continents there are and output how many continents there are plus a list of those continents, together with the size of each continent. The definition of a continent is one or more connected locations where each location is a +. Connected means that in a 3 x 3 grid around a +, if any of the 8 surrounding locations contains a + then the two are connected.
+
++
+
The 4 locations + are next to each other.

Download the text file.

For example your output should look something like this:

There are 5 Continents
Continent 1 has 208 +.
Continent 2 has ...

Note - The actual number of continents may be more or less than 5-that example just shows what output might look like.

You can enter in C, C++ or C#. Please add timing code to your entry to save me quite a big job! This is mandatory! as it takes way too long for me to add timing code in. The routines to do this are in the How to Sections below.

This is for timing on Windows, if you have similar code for Linux or Mac, please send it to me and I'll be happy to publish it.

Results

Thanks to everyone who entered and congrats to Faisal. The links for the ten fastest either show the source code or download a zip file if there is more than one file.

Name Language (Age,Country) Time

  1. Faisal Yousuf C++ 0.0003343
  2. Dongfang Xie C (38,Canada) 0.000409
  3. Philippe Boucher C++ (32) 0.000415
  4. Andre Prata C (19,Portugal) 0.00106
  5. Siddharth Hegde C++ 0.0016405
  6. g.v.Vidhya C (India) 0.00176
  7. Rodrigo Paniagua C (27, Spain) 0.00176
  8. Neil McCoy C++ (Australia) 0.0021464
  9. Yatin Salian C++ (27,India) 0.00255
  10. Peter Adamko C# (40,Slovakia) 0.0030043
  11. Bobby Boyle C++ (23,Canada) 0.00388
  12. Sundara Raman C++ (19,India) 0.004107
  13. Michael Hitt C++ (USA) 0.005883
  14. Michael van der Wal C# (43,The Netherlands) 0.0213939
  15. Siridiion Cabudian C++ 0.049395
  16. Pranav Prakash C++ (22,India) N/A
  17. Barry Hadder C N/A
  18. Turcu Septimu C (Romania) N/A
  19. Bobby J Knight C++ (25,USA) N/A

More Programming Challenges

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Projects
  5. Programming Challenges
  6. Programming Challenge 2 - Count Connected Locations (Entries Closed)>

©2009 About.com, a part of The New York Times Company.

All rights reserved.