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.
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 ", altaFinAlgoritmoWhat 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
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.
BeginnerLinear Search
Finds a name in a list, ignoring upper and lower case.
IntermediateStore Inventory
Records the stock of each product and flags what to restock.
IntermediateBubble Sort
Sorts random numbers by swapping neighbors, and stops as soon as they're in order.
IntermediateSelection Sort
Finds the smallest of the remaining numbers and puts it in its place.
IntermediateInsertion Sort
Inserts each number among the sorted ones, like arranging cards in your hand.
IntermediateCounting Sort
Counts how many times each value appears, without comparing numbers.
IntermediateBinary Search
Finds a value in a sorted array by halving the range every step.
IntermediateLinear vs. Binary Search
Counts the comparisons each search needs on the same sorted list.
IntermediateWeekly Sales
A table of sales by branch and day, with totals.
AdvancedShopping List
Adds items to a list that grows until you type FIN.
AdvancedShell Sort
Insertion sort between far-apart elements, with a gap that shrinks every pass.
AdvancedMerge Sort
Splits the array in halves, sorts each one recursively and merges them.
AdvancedQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
AdvancedRoman Numerals
Converts a number from 1 to 3999 into Roman numerals.
AdvancedClass Grade Book
Records three grades per student and builds a class report.
AdvancedHangman
Guess a secret word letter by letter, with six mistakes allowed.
AdvancedContact 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.