📘 CodingMarble Learn

Programming Techniques

Good programs keep data in the right shape (array, stack, queue), follow shared coding rules, read and write files record by record, design clear input and output screens, and are split into small modules that each do one job.

🎬 Step-by-step story

  1. An array is a row of boxes. Each box has a number called an index, and the first index is 0.
  2. A stack is like a pile of plates. The last plate you put on top is the first one you take.
  3. A queue is like a ticket line. The first one who joined is the first one served.
  4. A program reads records from a file one by one, changes each one, and writes it to a new file.
  5. A big job is split into small modules: Main calls Read, Calculate and Print. Each does one job.
  6. Your turn. Pick array, stack or queue. Add items and take them out. See who comes out first.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does the array start at 0 and not 1?

The index says how far the box is from the start. The first box is 0 steps away, so its index is 0.

Which item can I take from a stack?

Only the top one. To reach a lower one, you must first take those above it.

Why does the queue serve the oldest item first?

It is fair, like a ticket line: whoever came first is served first.

Why not load the whole file at once?

Some files are very large. Reading one record at a time uses little memory and works for any size.

Why split a program into modules?

Small modules are easy to test, easy to fix and can be reused. Main only calls them in order.

Can I take from the middle of a stack or queue?

No. A stack gives only the top and a queue only the front. An array lets you pick any index.

Data structures: array, stack, queue

A data structure is a planned way to keep many values together so the program can use them easily.

An array is a row of boxes of the same kind. Each box has an index, and we start counting at 0. So an array of 6 boxes has indexes 0 to 5. You can reach any box straight away: marks[3].

A stack is LIFO: Last In, First Out. Push puts an item on top. Pop takes the top item. Undo buttons and the back button of a browser use a stack.

A queue is FIFO: First In, First Out. Enqueue adds at the back. Dequeue removes from the front. A printer waiting list is a queue.

A record groups different facts about one thing, for example a student: roll number, name, marks.

Program standardisation

Standardisation means everybody in a team follows the same rules, so anyone can read and fix any part of the program.

Standard programs are easier to read, test and change, and new team members learn them fast.

File processing

A file keeps data on a disk so it stays after the program stops. A file is made of records, and each record has fields. In a sequential file the records are read one after another from the start.

The usual pattern is: open the file, read a record, process it, write the result, repeat until the end of file, then close. Common jobs are search, sort, merge two sorted files, and update records using a key such as the roll number.

Input and output design

Input and output design is about how people talk to the program: the forms they fill and the reports they read.

Good input screens are simple, in a natural order, and check the data before using it: a presence check (not empty), a type check (a number, not letters), a range check (marks from 0 to 100) and a check digit (one extra digit that catches typing mistakes in long numbers).

Good output is clear: headings, aligned columns, units, and total lines. Show helpful messages such as "Marks must be between 0 and 100".

Structured program design

Structured design builds a program from three basic shapes: sequence (steps in order), selection (if / else) and iteration (loops). It avoids messy jumping around.

We use top-down design: write the big job first (Main), then break it into smaller modules, and again into smaller ones, until each is easy. A good module does one job (high cohesion) and depends little on other modules (low coupling). Modules can be built and tested by different people and reused later.

Try it

In the 3D, go to the last step. Choose Stack, add four items, then take out one. Which came out? Now choose Queue and do the same. Predict before you press! At home, write the steps for "make tea" as three modules (Boil, Mix, Serve) and check each does only one job.

Key formulas and definitions

Worked examples

1. An array marks = [7, 3, 9, 4, 6, 2]. What is marks[3]?

Index 0 is 7, 1 is 3, 2 is 9, 3 is 4. So marks[3] = 4.

2. You push 5, then 8, then 2 on a stack and pop once. What is popped and what is on top now?

Pop removes the last one in: 2. The top is now 8.

3. You enqueue 4, 1, 6 and dequeue once. What comes out?

First In First Out: 4 comes out. The queue now holds 1, 6.

4. An array has 12 items. What is the last index?

Indexes run 0 to 11, so the last index is 11.

5. A file has 250 records of 40 bytes each. How big is it?

250 × 40 = 10,000 bytes (10 KB).

6. A marks field must accept 0 to 100. Which check catches 150?

A range check. 150 is outside 0 to 100, so the program shows an error.

Common mistakes

Practice quiz

1. Which structure is Last In, First Out?
2. The first index of an array is:
3. Which check makes sure marks are from 0 to 100?
4. A good module should:
5. In a sequential file, records are read:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between a stack and a queue?

A stack gives back the newest item first (LIFO), like plates. A queue gives back the oldest item first (FIFO), like a ticket line.

Why do programmers follow coding standards?

So any team member can read, test and fix the code quickly. Clear names, same layout and comments make code easy to share.

What is structured programming?

Building programs with sequence, selection and loops, and splitting the job top-down into small modules that each do one thing.

Where this is taught

Japan高校(専門学科)1〜3年Programming Technology

Learn first

Learn next

Related lessons

All Computer Science lessons