Contact Book
Add, list and search contacts in a book that grows.
- Level
- Advanced
- Topic:
- Projects
- Concepts:
- Input & output
- Arrays
- Redimensionar
- Repetir
- Segun
- Text
Open in Codoritmo
Run, change and debug this example in your browser.
What you'll learn
Redimensionar grows two parallel arrays every time you add a contact. A Segun menu inside Repetir ... Hasta Que organizes the options.
Code
Algoritmo Agenda Definir nombres, telefonos, buscado Como Caracter Definir total, opcion, i Como Entero Definir encontrado Como Logico total <- 0 Dimension nombres[1], telefonos[1] Repetir Escribir "" Escribir "1. Agregar 2. Ver todos 3. Buscar 4. Salir" Leer opcion Segun opcion Hacer 1: total <- total + 1 Redimensionar nombres[total] Redimensionar telefonos[total] Escribir "Nombre:" Leer nombres[total] Escribir "Teléfono:" Leer telefonos[total] Escribir "Contacto guardado." 2: Si total = 0 Entonces Escribir "La agenda está vacía." SiNo Para i <- 1 Hasta total Hacer Escribir i, ". ", nombres[i], " - ", telefonos[i] FinPara FinSi 3: Escribir "¿A quién buscas?" Leer buscado encontrado <- Falso Para i <- 1 Hasta total Hacer Si Mayusculas(nombres[i]) = Mayusculas(buscado) Entonces Escribir nombres[i], ": ", telefonos[i] encontrado <- Verdadero FinSi FinPara Si NO encontrado Entonces Escribir "No encontré a ", buscado, "." FinSi 4: Escribir "¡Hasta pronto!" De Otro Modo: Escribir "Opción no válida." FinSegun Hasta Que opcion = 4FinAlgoritmoFlowchart
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.
1. Agregar 2. Ver todos 3. Buscar 4. SalirUser answer: 1Nombre:User answer: AnaTeléfono:User answer: 555-1234Contacto guardado. 1. Agregar 2. Ver todos 3. Buscar 4. SalirUser answer: 1Nombre:User answer: LuisTeléfono:User answer: 555-9876Contacto guardado. 1. Agregar 2. Ver todos 3. Buscar 4. SalirUser answer: 21. Ana - 555-12342. Luis - 555-9876 1. Agregar 2. Ver todos 3. Buscar 4. SalirUser answer: 3¿A quién buscas?User answer: luisLuis: 555-9876 1. Agregar 2. Ver todos 3. Buscar 4. SalirUser answer: 4¡Hasta pronto!
Challenge
Add an option that counts how many contacts start with a given letter.
Show hint
Compare Mayusculas(Subcadena(nombres[i], 1, 1)) with the letter you read.
Related examples
ATM
Checks your PIN and lets you check, deposit and withdraw money.
AdvancedShopping List
Adds items to a list that grows until you type FIN.
AdvancedQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
Advanced