Skip to content
ExamplesEspañolWorkspace →

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

contacts.psc · 48 lines
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 = 4FinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo AgendaSíNoSíNoSíNo1234De Otro ModoNoSíAlgoritmo AgendaAlgoritmo AgendaDefinir nombres, telefonos, buscado Como CaracterDefinir nombres, telefonos,buscado Como CaracterDefinir total, opcion, i Como EnteroDefinir total, opcion, i ComoEnteroDefinir encontrado Como LogicoDefinir encontrado Como Logicototal <- 0total <- 0Dimension nombres[1], telefonos[1]Dimension nombres[1],telefonos[1]Escribir ""Escribir ""Escribir "1. Agregar 2. Ver todos 3. Buscar 4. Salir"Escribir "1. Agregar 2. Vertodos 3. Buscar 4. Salir"Leer opcionLeer opcionSegun opcion Haceropciontotal <- total + 1total <- total + 1Redimensionar nombres[total]Redimensionar nombres[total]Redimensionar telefonos[total]Redimensionar telefonos[total]Escribir "Nombre:"Escribir "Nombre:"Leer nombres[total]Leer nombres[total]Escribir "Teléfono:"Escribir "Teléfono:"Leer telefonos[total]Leer telefonos[total]Escribir "Contacto guardado."Escribir "Contacto guardado."Si total = 0 Entoncestotal = 0Escribir "La agenda está vacía."Escribir "La agenda estávacía."Para i <- 1 Hasta total HacerPara i <- 1 Hasta totalEscribir i, ". ", nombres[i], " - ", telefonos[i]Escribir i, ". ", nombres[i]," - ", telefonos[i]Escribir "¿A quién buscas?"Escribir "¿A quién buscas?"Leer buscadoLeer buscadoencontrado <- Falsoencontrado <- FalsoPara i <- 1 Hasta total HacerPara i <- 1 Hasta totalSi Mayusculas(nombres[i]) = Mayusculas(buscado) EntoncesMayusculas(nombres[i]) =Mayusculas(buscado)Escribir nombres[i], ": ", telefonos[i]Escribir nombres[i], ": ",telefonos[i]encontrado <- Verdaderoencontrado <- VerdaderoSi NO encontrado EntoncesNO encontradoEscribir "No encontré a ", buscado, "."Escribir "No encontré a ",buscado, "."Escribir "¡Hasta pronto!"Escribir "¡Hasta pronto!"Escribir "Opción no válida."Escribir "Opción no válida."Hasta Que opcion = 4opcion = 4FinAlgoritmoFinAlgoritmo

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.

Open in Codoritmo
  • ATM

    Checks your PIN and lets you check, deposit and withdraw money.

    Advanced
  • Shopping List

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

    Advanced
  • Quicksort

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

    Advanced