Shopping List
Adds items to a list that grows until you type FIN.
- Level
- Advanced
- Topic:
- Arrays
- Concepts:
- Input & output
- Arrays
- Redimensionar
- Para Cada
- Mientras
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
Redimensionar makes the array bigger without losing what it already holds. Para Cada visits every element without managing indexes yourself.
Code
Algoritmo ListaCompras Definir lista, producto Como Caracter Definir cantidad Como Entero cantidad <- 0 Dimension lista[1] Escribir "Agrega productos a tu lista. Escribe FIN para terminar." Leer producto Mientras Mayusculas(producto) <> "FIN" Hacer cantidad <- cantidad + 1 // El arreglo crece de a un elemento, conservando lo que ya tenía Redimensionar lista[cantidad] lista[cantidad] <- producto Escribir "Siguiente producto (FIN para terminar):" Leer producto FinMientras Si cantidad = 0 Entonces Escribir "La lista quedó vacía." SiNo Escribir "Tu lista tiene ", cantidad, " productos:" Para Cada item de lista Hacer Escribir "- ", item FinPara FinSiFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This is what the program shows when it runs. The answers typed by the user are highlighted.
Agrega productos a tu lista. Escribe FIN para terminar.User answer: panSiguiente producto (FIN para terminar):User answer: manzanasSiguiente producto (FIN para terminar):User answer: quesoSiguiente producto (FIN para terminar):User answer: finTu lista tiene 3 productos:- pan- manzanas- queso
Challenge
Don't let the same item be added twice.
Show hint
Before saving, loop through the list with Para and compare using Mayusculas.
Related examples
Linear Search
Finds a name in a list, ignoring upper and lower case.
IntermediateShell 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.
Advanced