Select and use appropriate data types for a problem solution

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – 10.1 Data Types and Records

10.1 Data Types and Records

Learning Objective

Select and use appropriate data types for a problem solution.

Why Data Types Matter

Choosing the correct data type ensures that a program uses memory efficiently, performs calculations accurately, and avoids run‑time errors such as overflow or type‑mismatch.

Primitive Data Types

TypeTypical Size (bits)Range / ValuesTypical Use
int32\$-2^{31}\ \text{to}\ 2^{31}-1\$Whole numbers, counters, indexes
float32 (single‑precision)\$±3.4 \times 10^{38}\$ with \overline{7} decimal digitsReal‑valued measurements, scientific calculations
char8ASCII characters \$0\ldots127\$Single characters, small codes
boolean1 (conceptually)True / FalseLogical conditions, flags
stringVariableSequence of charactersNames, messages, textual data

Derived Data Types

  • Array: Ordered collection of elements of the same primitive type.
  • Record (Structure): Grouping of fields that may be of different types, representing a real‑world entity.
  • List, Set, Dictionary: Higher‑level collections (often built from arrays or records).

Records – Definition and Use

A record combines related data items into a single logical unit. Each component is called a field. Records are ideal for modelling objects such as a student, a product, or a transaction.

Designing a Record

  1. Identify the entity you need to model.
  2. List all attributes (fields) and decide the most suitable primitive type for each.
  3. Determine any constraints (e.g., maximum length of a string, range of numbers).
  4. Write the record definition in pseudo‑code or the target language.

Example: Student Record

Field NameData TypePurpose
studentIDintUnique numeric identifier
firstNamestringGiven name (max 30 characters)
lastNamestringFamily name (max 30 characters)
dateOfBirthstringFormat “YYYY‑MM‑DD”
gpafloatGrade Point Average (0.0 – 4.0)
isFullTimebooleanFull‑time status flag

Pseudo‑code definition:

record Student {

int studentID;

string firstName;

string lastName;

string dateOfBirth;

float gpa;

boolean isFullTime;

}

Accessing Record Fields

Given a variable stud of type Student, fields are accessed using the dot operator:

stud.studentID = 10234;

stud.gpa = 3.75;

if (stud.isFullTime) {

// full‑time processing

}

Selecting Data Types – Decision Checklist

  • Is the value whole or fractional? Choose int or float.
  • Will the value ever exceed the range of int? Consider long (if available) or split into multiple fields.
  • Is the data textual? Use char for single characters, string for longer text.
  • Do you need a true/false condition? Use boolean.
  • Are multiple related values needed? Define a record or an array of primitives.
  • Will the data be processed mathematically? Ensure the type supports required precision (e.g., float vs double).

Case Study: Library Book Management System

Problem: Store details of each book and allow searching by ISBN.

  1. Identify the entity – Book.
  2. Determine fields:

    • ISBN – 13‑digit numeric code → string (preserves leading zeros).
    • title – text → string.
    • author – text → string.
    • publishedYear – whole year → int.
    • price – monetary value → float (or fixed‑point decimal).
    • isAvailable – loan status → boolean.

  3. Define the record (pseudo‑code):

    record Book {

    string ISBN;

    string title;

    string author;

    int publishedYear;

    float price;

    boolean isAvailable;

    }

  4. Store multiple books in an array: Book library[1000];
  5. When searching, compare the ISBN field of each record with the user input.

Common Pitfalls

  • Using int for values that may exceed \$2^{31}-1\$ – leads to overflow.
  • Storing numeric identifiers as int when leading zeros are significant (e.g., ZIP codes) – use string.
  • Mixing units without conversion (e.g., storing distances in metres but performing calculations assuming kilometres).
  • Neglecting to initialise fields, which can cause undefined behaviour.

Summary

Effective problem solving in computer science begins with a careful analysis of the data involved. By matching each piece of information to the most appropriate primitive or derived type, and by grouping related items into records, you create clear, maintainable, and efficient programs.

Suggested diagram: Memory layout of a Student record showing offsets for each field.