Skip to content
ExamplesEspañolWorkspace →

Weekly Sales

A table of sales by branch and day, with totals.

Level
Advanced
Topic:
Arrays
Concepts:
Matrices
Arrays
Para
Randomness
Open in Codoritmo

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

sales-matrix.psc · 29 lines
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    FinParaFinAlgoritmo

Flowchart

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

Algoritmo VentasSemanaAlgoritmo VentasSemanaAlgoritmo VentasSemanaDefinir ventas, totalSucursal, totalDia Como RealDefinir ventas, totalSucursal,totalDia Como RealDefinir s, d Como EnteroDefinir s, d Como EnteroDimension ventas[3, 5]Dimension ventas[3, 5]Para s <- 1 Hasta 3 HacerPara s <- 1 Hasta 3Para d <- 1 Hasta 5 HacerPara d <- 1 Hasta 5ventas[s, d] <- Aleatorio(100, 500)ventas[s, d] <- Aleatorio(100,500)Escribir "Ventas por sucursal, de lunes a viernes:"Escribir "Ventas por sucursal,de lunes a viernes:"Para s <- 1 Hasta 3 HacerPara s <- 1 Hasta 3totalSucursal <- 0totalSucursal <- 0Escribir Sin Saltar "Sucursal ", s, ": "Escribir Sin Saltar "Sucursal", s, ": "Para d <- 1 Hasta 5 HacerPara d <- 1 Hasta 5Escribir Sin Saltar ventas[s, d], " "Escribir Sin Saltar ventas[s,d], " "totalSucursal <- totalSucursal + ventas[s, d]totalSucursal <- totalSucursal+ ventas[s, d]Escribir "| total ", totalSucursalEscribir "| total ",totalSucursalEscribir "Total por día:"Escribir "Total por día:"Para d <- 1 Hasta 5 HacerPara d <- 1 Hasta 5totalDia <- 0totalDia <- 0Para s <- 1 Hasta 3 HacerPara s <- 1 Hasta 3totalDia <- totalDia + ventas[s, d]totalDia <- totalDia +ventas[s, d]Escribir "Día ", d, ": ", totalDiaEscribir "Día ", d, ": ",totalDiaFinAlgoritmoFinAlgoritmo

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.

Open in Codoritmo
  • Shell Sort

    Insertion sort between far-apart elements, with a gap that shrinks every pass.

    Advanced
  • Merge Sort

    Splits the array in halves, sorts each one recursively and merges them.

    Advanced
  • Heap Sort

    Arranges the array as a heap, then takes the largest out again and again.

    Advanced