UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Data structures: arrays, records and files

Why data structures matter, one- and two-dimensional arrays, records (structs), and files and file organisation.

Our notes

2 min read

Data structures

A data structure is a way of organising data in memory so it can be used efficiently. The choice affects how fast a program runs and how simple it is. Primitive types: integer, real (floating point), character, boolean. Structured types combine them.

Arrays

An array is a fixed-size collection of elements of the same type, stored in consecutive memory locations and accessed by an index.

  • One-dimensional: a list of values. In C indices start at 0: double t[5]; holds t[0] to t[4]. (FORTRAN starts at 1 by default.)
  • Address calculation: address of t[i] = base address + i × size of element. This is why access by index is instant (constant time).
  • Two-dimensional: a table or matrix, a[i][j]. Stored row by row in C (row-major) and column by column in FORTRAN (column-major). This matters for speed when looping and when mixing languages.

Example (matrix addition in C):
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            c[i][j] = a[i][j] + b[i][j];
Strengths: fast access by index; simple. Weaknesses: fixed size; inserting or deleting in the middle means shifting elements.

Records (structures)

A record groups related items of different types under one name; each item is a field.
    struct Bolt {
        char grade[6];
        double diameter_mm;
        double tensile_MPa;
        int stock;
    };
Access with a dot: b.diameter_mm. An array of records is like a table: each record a row, each field a column (the basis of databases).

Files

Data in memory is lost when a program ends; files keep it on secondary storage.

  • Text files: human-readable characters (CSV data from a data logger).
  • Binary files: raw bytes in the program's internal format: compact and fast, but not readable in a text editor.

Basic operations: open, read, write, append, close. In C: fopen, fscanf or fread, fprintf or fwrite, fclose. Always check that a file opened successfully.

File organisation

  • Sequential: records stored one after another and read in order (magnetic tape, logs). Simple; slow to find one record.
  • Direct (random) access: any record reached by its position or address (fixed-length records).
  • Indexed sequential: records in order with an index for quick lookup.

Terms: field → record → file → database; a key field uniquely identifies each record (a student's matriculation number).

Exams usually ask

  • Define an array and compute an element's address.
  • Explain row-major and column-major storage.
  • Define a record and a file; compare sequential and direct access.

Common mistake

Going outside an array's bounds (t[5] in an array of 5 elements in C). C does not check, so the program may silently corrupt memory or crash.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.