17 votes

What is a real-world example of a for-loop?

Dealing cards in a poker game. for i from 1 to 5: # 5 cards in a hand for each player: deal a card to the player And then playing the game is also like ...
Barmar's user avatar
  • 361
14 votes

“What do you mean it’s plagiarism? It’s from the docs!”

So, my take on this is to sit down with the student and have roughly this talk: So, I'm a little disappointed by the lab you handed in, for two reasons. The first is that it is pushing on the edges ...
Ben I.'s user avatar
  • 33.1k
12 votes

What is a real-world example of a for-loop?

Reading a book Reading a book is probably the easiest example. ...
Robert Columbia's user avatar
8 votes

What is a real-world example of a for-loop?

The conventional for loop (in the c-like languages, at least) is a low-level iteration structure, and there are two challenges. The first is to understand the ...
Ben I.'s user avatar
  • 33.1k
8 votes

What is an example of class inheritance in every-day life which is easy to write in an object-oriented language?

I think that when dealing with new students the principle of inheritance is best explained with analogies to life outside of writing code. I'm not so sure. I prefer to use application-centric ...
ggorlen's user avatar
  • 770
8 votes

Simple Pointer Examples in C

I would teach the use of arrays and array indices first (with pictures of a long row of physical mailboxes, or school lockers, etc.) Then later explain that all of a computer's and/or C program's (...
hotpaw2's user avatar
  • 1,895
6 votes

Simple Pointer Examples in C

What you have there is an excellent start, and is completely appropriate. I would go next into a puzzle to try to get them hooked. I would type the following code in front of them on your projector, ...
Ben I.'s user avatar
  • 33.1k
6 votes

Examples of innocent looking faulty code for shock effect

My advice to all of these problems is the same. Your students do not know how to draw out memory. This is a basic technique that most people who have been programming for a while do mentally, but that ...
Green Griffin's user avatar
6 votes

Example for For vs While Loop (in Java)

If I'm interpreting your question correctly you'd like real life examples that translate to code. Maybe something like the following. You want to drive or ride your bike to the ice-cream shop a few ...
Buffy's user avatar
  • 36.1k
6 votes

What is a real-world example of a for-loop?

As a maybe overly simplistic example: Say you want to make mashed potatoes. To make it easier we'll focus on one part of the process: prepping 5 potatoes. First, you would peel one, cut it up, and ...
Rakicy's user avatar
  • 61
5 votes

What is a real-world (production-code) example of a function having default arguments?

Probably the most basic example is Python's print function, which has keyword arguments sep and ...
ncmathsadist's user avatar
  • 2,329
5 votes
Accepted

Examples of innocent looking faulty code for shock effect

It sounds like the program of study has a few bugs if 4th or 5th year students cannot reason about a loop. I taught myself programming in 9th grade by writing simple programs that printed shapes on ...
Scott Rowe's user avatar
4 votes

Simple Pointer Examples in C

Yours is a fine first lesson, although it does not show the value of pointers. I would suggest also demonstrating how pointers can be used to implement a swap function. Ask the students (or step them ...
Ellen Spertus's user avatar
4 votes

Simple Pointer Examples in C

Obviously, beginners in C will have to deal with addresses and pointers really soon. First, you owe them an explanation about addresses for ...
Michel Billaud's user avatar
4 votes

Simple Pointer Examples in C

I would use more visual examples that target conceptual knowledge first. I would do a TON of memory diagrams with code tracing. Then, introduce the idea of pointers as memory addresses like you do in ...
nova's user avatar
  • 1,995
4 votes

What's a good example of a static factory method returning instances of subclasses?

I don't have a good answer to this. However here are my thoughts. Named constructors A good use for static factory, is to implement named constructors: Java, C#, C++, et al do not have named ...
ctrl-alt-delor's user avatar
4 votes

Examples of innocent looking faulty code for shock effect

I recognize the feeling of despair! However, I don't think that you want what you are asking for. The most shocking bit of code that I know to show students is the code I wrote up here. Beware, it'...
Ben I.'s user avatar
  • 33.1k
4 votes

What is a real-world example of a for-loop?

There are a bunch of real-life algorithms that can be represented using for loops. Some examples: Emptying your suitcase in an orderly manner after returning from a journey: ...
Wrzlprmft's user avatar
  • 594
4 votes

What is an example of class inheritance in every-day life which is easy to write in an object-oriented language?

This isn't as clear cut as your question would have it be. There are several cases where a solution, proposed to be solved by inheritance, could be solved in different ways. For example, your paper/...
Flater's user avatar
  • 1,361
4 votes

What is a real-world (production-code) example of a function having default arguments?

I recently used default arguments to write an API method for a mail sending library: The method needs a subject, a body and a "To" recipient, "Cc" and "Bcc" are optional: ...
TuringTux's user avatar
  • 1,011
3 votes

What is a real-world (production-code) example of a function having default arguments?

I like TuringTux's answer quite a lot. If you're looking for a simpler example for the classroom just to illustrate, you might consider printing an array with an optional delimiter. ...
Ben I.'s user avatar
  • 33.1k
3 votes

“What do you mean it’s plagiarism? It’s from the docs!”

Not sure you can blame the learner really. If the information was obtained by RingTFM then full marks. Hacking the standard library - unconventional but shows initiative. The real problem was the ...
Jon Guiton's user avatar
3 votes

An example that compares a computational with a non-computational thinking approach

I think you have a misunderstanding about something, but I can't say precisely what. You seem to really be asking for two different sorts of CT, not CT vs something else. Both math and CS require some ...
Buffy's user avatar
  • 36.1k
3 votes

What is a real-world example of a for-loop?

In statistics and data science, there are many real-world applications where for loops make sense. One of the simplest examples that they can do themselves is calculating the arithmetic average or ...
anjama's user avatar
  • 131
2 votes
Accepted

What's a good example of a static factory method returning instances of subclasses?

My go-to for programming patterns these days are games, because it is easy to create compelling examples. It has never occurred to me before to have a factory return its own subclass, so I apologise ...
Ben I.'s user avatar
  • 33.1k
2 votes

What's a good example of a static factory method returning instances of subclasses?

I would first ask what that hierarchy buys you overall in an application and, more important, in the student understanding. In general, I find concrete subclassing to be problematic and easily abused, ...
Buffy's user avatar
  • 36.1k
2 votes

Coding assignments for algorithms and data structures?

I'm not aware of any singular, central repository for assignment ideas. My sources for project ideas when my own well begin to run dry are: Textbooks. Often, a problem in a problem set will trigger ...
Ben I.'s user avatar
  • 33.1k
2 votes

What is a real-world example of a for-loop?

Loading suitcases onto a train might work: for entry in one through the number of suitcases on the platform { load the entry'th suitcase onto the train } But ...
l0b0's user avatar
  • 121
2 votes

What is an example of class inheritance in every-day life which is easy to write in an object-oriented language?

It depends on the goal. For an initial, highly intuitive brush, I've frequently used animals. They share some advantages over cups. There are a lot of them. They already come pre-categorized into ...
Ben I.'s user avatar
  • 33.1k

Only top scored, non community-wiki answers of a minimum length are eligible