Weekly Sales
A table of sales by branch and day, with totals.
- Level
- Advanced
- Topic:
- Arrays
- Concepts:
- Matrices
- Arrays
- Para
- Randomness
Run, change and debug this example in your browser.
What you'll learn
Dimension ventas[3, 5] creates a matrix of rows and columns. Walking it with Para loops in one order gives totals per branch; in the other order, totals per day.
Code
Algoritmo VentasSemana Definir ventas, totalSucursal, totalDia Como Real Definir s, d Como Entero // 3 sucursales (filas) x 5 días (columnas) Dimension ventas[3, 5] Para s <- 1 Hasta 3 Hacer Para d <- 1 Hasta 5 Hacer ventas[s, d] <- Aleatorio(100, 500) FinPara FinPara Escribir "Ventas por sucursal, de lunes a viernes:" Para s <- 1 Hasta 3 Hacer totalSucursal <- 0 Escribir Sin Saltar "Sucursal ", s, ": " Para d <- 1 Hasta 5 Hacer Escribir Sin Saltar ventas[s, d], " " totalSucursal <- totalSucursal + ventas[s, d] FinPara Escribir "| total ", totalSucursal FinPara Escribir "Total por día:" Para d <- 1 Hasta 5 Hacer totalDia <- 0 Para s <- 1 Hasta 3 Hacer totalDia <- totalDia + ventas[s, d] FinPara Escribir "Día ", d, ": ", totalDia FinParaFinAlgoritmoFlowchart
Each block in the flowchart matches one instruction in the code.
Sample run
This program doesn't ask for input: this is what it shows when it runs.
Ventas por sucursal, de lunes a viernes:Sucursal 1: 247 124 223 296 189 | total 1079Sucursal 2: 336 227 136 358 219 | total 1276Sucursal 3: 237 213 457 335 454 | total 1696Total por día:Día 1: 820Día 2: 564Día 3: 816Día 4: 989Día 5: 862
Challenge
Show which branch sold the most during the week.
Show hint
When you compute totalSucursal, compare it with the best so far and remember its number.
Related examples
Shell 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.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
Advanced