tags:

views:

331

answers:

11

What is the best way to solve programming questions when you are given a question to write a program, in an exam for example, where you have no computer to test it. Is there a certain technique that people use to help them solve these type of written problems? Or is it all down to knowledge of the language?

+2  A: 

The best way if you have the choice is to explain the algorithm carefully in words without writing explicit code - perhaps in pseudo code. Then you can show you have fully understood the problem and discussed various factors which might change the solution without having to code up a specific example which will no doubt have bugs.

If you are forced to write some code in a specific language then just be very, very careful and check it many times. Include comments so that if you do make a mistake it is clear what you meant.

Mark Byers
+4  A: 

I think you'll find that the intricacies of a language are not part of the exam. Writing pseudocode will be sufficient.

NomeN
+15  A: 

Edsger W. Dijkstra said,

Computer science is no more about computers than astronomy is about telescopes, biology is about microscopes or chemistry is about beakers and test tubes. Science is not about tools. It is about how we use them, and what we find out when we do.

Language and computer are not required for problem solving.

For problem solving you need skills , pencil & paper.

I would suggest Flow Charts for exams.

TheMachineCharmer
+1 for flow charts, the original and still the best!
APC
A: 

In my exams for my degree a few years ago it was there to show you had understanding of the theory of programming and applying it to problems. The thing to do is read the question carefully, if it asks for code then make sure you write code, if it says how would you apply it then explain and use pseudo code with comments.

The main thing is if you write code then be very careful and make sure you have enough brackets, begin/ends etc to make the code readable. As well as this if you want to say "do something" and you're not sure about how you would code it or remember the method of a .net class (for instance) then explain it. There's nothing else you can do.

Be clear and keep calm :-)

WestDiscGolf
+1  A: 

To add to the other answers - I remember back in school we used to have to make tables, where each row was a line of code, and each column was a variable. You'd run through the code manually and fill in the values for each variable as the program "executed", line by line. Tedious, but it helps to make sure you're doing it right :)

Jeriko
A: 

I think it is better to create a Standard Diagram (like UML - flowchart) and then try to implement some classes (from class diagram) in snippet codes.

Nasser Hadjloo
A: 

If by "to test it" you mean to make sure that what you've written is compilable code, assuming you'd be asked to write code that compiles, then you do need to know the language pretty well. But what you really need to do is practice, and learn from your mistakes.

Come up with an algorithm, write it out with pen and paper, and then immediately code the solution as is and see what errors you get. Write those down, and keep a list that you refer to. Each time you do the exercise, keep a mental note of the errors that you frequently do, and make sure to verify those. This advice is not limited to writing out code - it applies to writing essays as well.

Once you do that, you'll be able to write code that compiles without the computer, but that doesn't mean your algorithm will be correct. To test that, you need to think about edge cases, and mentally run through the algorithm for the edge cases and boundaries if there are some. Always try to mentally go through your algorithm with a typical value, and the lower and upper bound, as well as one below and one above, and finally with some totally invalid value (like a different type).

JRL
A: 

I sat a C exam once where once given a problem we had to code an entire app by hand on coding sheets (essentially graph paper), which then had to be typed in as written and it was expected to compile and run as per the spec.

I don't know, kids these days....

Ira Rainey
I just had an image of an exam room filled with noise as students furiously try to implement an algorithm to compute the Fibonacci sequence by punching holes in cards. Now _that_ is where you need to be careful. Be sure not to drop them and get them out of order, either.
Dan Bryant
A: 

I think best way to solve the problems without using computer would be detailing out the flow of information using White Board and Pen.

Rachel
A: 

Think of functions, objects (classess) etc. You can use a lott of diagrams (like UML). For testing, you need a pencil and paper and just desktop testing. Think of situations and think of every thing works ok.

VDVLeon
+3  A: 

Usually in a programming course you're required to write in the specific language, which you need to prepare in advance by writing a lot of code, like a ton of code in the language required by the exam. I can tell you right now that the best way to learn programming in one specific language is to write programs that solves problems you've had recently.

This is how you do problem solving really, attempting to tackle on a solution and remembering it. The smarter the solution (easy to follow and employs reuse) the better, because it'll be easier to retain once you get a problem in an exam that is similar. Much like solving puzzles in magazines or whatever, if you've experienced a similar solution before in some then the answer is already given. Most of the time you'll only be applying a solution that you've had earlier, hopefully in a tad smarter way than before.

If the exam you're going to write is algorithms and datastructures or any other computer science related that requires you to write code, it is often required for you to at least write pseudo code (which can be anything that at least it describes the algorithm somehow) or the language that is sanctioned by the school (usually these days it is Java).

I'd suggest you also learn write code in a scripting language as well such as Python, Ruby, Go or even something as esoteric as Lolcode (which is more interesting as pseudocode rather than an actual language)... as long as you can write some kind of script quickly for yourself, then you have come a long way. Hopefully you'll realize that many languages are similar since it is all about Sequences, Conditions and Loops in different syntax; everything else is fluff that makes your life easy.

So how do you go on about solving problems? In real life, you google it up first. But you can't do that in an exam, but the first step in an exam is similar. You need to understand the question first and the problem at hand, much like finding answers in Google you need to find out what the most optimal search terms are. Ask yourself what kind of resources you have in the context of the question and what they can do for you, sometimes you'll find patterns that'll help you.

Learn how to sketch for solutions. Flow charts have saved me so many times as it gives you a good visual overview of complex conditionals and making them simpler by walking them through.

If you're going to write object oriented, a lot of students trip on terminologies and the difference between objects and classes that it's not even funny. Always think in terms of Class Responsibility Collaborator (CRC) models and learn how to sketch out class diagrams to be able to think in. Something as simple as the sketch below will go a long way to explain things for yourself:

+-------------------------+
|       MojoFactory       | <- Classname
|-------------------------|
| +createMojo() : Mojo    | <- Collaborators: Mojo
| +remojofy(Mojo) : void  |
+-------------------------+

Responsibilities: Creates mojos and remojofy a non valid mojo.

If you can't think of a classname for a class, then it probably also has too much responsibilities. You need to split it up so each class has its own responsibility. Conversely, if a class has too much of a responsibility you should split it up into other collaborators.

Spoike
+1 for the mojofy example
RCIX
I sort of realized now that I could've replaced "Mojo" with "Unicorn" and "remojofy" with "cornify". That'd be so awesome.
Spoike