Skip to content
EspañolWorkspace →

Arrays in PSeInt: solved examples

19 examples

How it works

An array keeps many values of the same kind under one name, in numbered slots. Instead of nota1, nota2, nota3… you have notas[1], notas[2], notas[3], and you can walk through them with a loop.

You create it with Dimension, saying how many slots it has: Dimension notas[30]. In the default profile the first slot is 1; some profiles start at 0, so check the one your course uses.

The most common pattern is to fill the array with a Para and Leer, then walk it again to compute something: the sum, the average, the largest value, how many passed. Going past its size (notas[31] in an array of 30) is an error.

Short example

The least you need to see it work. Copy it or open it in the editor.

arrays.psc · 15 lines
Algoritmo HighestGrade    Definir i, alta Como Entero    Dimension notas[5]    Para i <- 1 Hasta 5 Hacer        Escribir "Grade ", i, ":"        Leer notas[i]    FinPara    alta <- notas[1]    Para i <- 2 Hasta 5 Hacer        Si notas[i] > alta Entonces            alta <- notas[i]        FinSi    FinPara    Escribir "The highest grade is ", altaFinAlgoritmo

What it prints:

Grade 1:User answer: 74Grade 2:User answer: 59Grade 3:User answer: 98Grade 4:User answer: 61Grade 5:User answer: 86The highest grade is 98
Open it in Codoritmo

Opens in the browser editor, nothing to install.

Solved exercises

Each exercise comes with the full code, its flowchart and a sample run.

  • Grade Average

    Stores five grades in an array and finds the average and the highest.

    Beginner
  • Linear Search

    Finds a name in a list, ignoring upper and lower case.

    Intermediate
  • Store Inventory

    Records the stock of each product and flags what to restock.

    Intermediate
  • Bubble Sort

    Sorts random numbers by swapping neighbors, and stops as soon as they're in order.

    Intermediate
  • Selection Sort

    Finds the smallest of the remaining numbers and puts it in its place.

    Intermediate
  • Insertion Sort

    Inserts each number among the sorted ones, like arranging cards in your hand.

    Intermediate
  • Counting Sort

    Counts how many times each value appears, without comparing numbers.

    Intermediate
  • Binary Search

    Finds a value in a sorted array by halving the range every step.

    Intermediate
  • Linear vs. Binary Search

    Counts the comparisons each search needs on the same sorted list.

    Intermediate
  • Weekly Sales

    A table of sales by branch and day, with totals.

    Advanced
  • Shopping List

    Adds items to a list that grows until you type FIN.

    Advanced
  • Shell Sort

    Insertion sort between far-apart elements, with a gap that shrinks every pass.

    Advanced
  • Merge Sort

    Splits the array in halves, sorts each one recursively and merges them.

    Advanced
  • Quicksort

    Picks a pivot, splits smaller from larger values and sorts each side recursively.

    Advanced
  • Heap Sort

    Arranges the array as a heap, then takes the largest out again and again.

    Advanced
  • Roman Numerals

    Converts a number from 1 to 3999 into Roman numerals.

    Advanced
  • Class Grade Book

    Records three grades per student and builds a class report.

    Advanced
  • Hangman

    Guess a secret word letter by letter, with six mistakes allowed.

    Advanced
  • Contact Book

    Add, list and search contacts in a book that grows.

    Advanced

In the language reference

The instructions involved, explained one by one.

Other concepts

Codoritmo is an independent, open-source project inspired by PSeInt and compatible with its syntax. It is not affiliated with the PSeInt project. Official PSeInt website.