Introducción
En este laboratorio, aprenderemos cómo calcular el volumen de un cuboide utilizando el lenguaje de programación C. El laboratorio te guiará a través del proceso paso a paso de leer las dimensiones del cuboide (longitud, ancho y altura) del usuario, y luego calcular el volumen utilizando la fórmula: Volumen = Longitud _ Ancho _ Altura. Finalmente, el volumen calculado se imprimirá en la consola.
El laboratorio cubre los conceptos fundamentales de los cálculos geométricos y demuestra cómo aplicarlos en un ejercicio práctico de programación en C.
Leer Longitud, Ancho, Altura
En este paso, aprenderemos cómo leer las dimensiones de un cuboide utilizando el lenguaje de programación C. Crearemos un programa sencillo que tome la longitud, el ancho y la altura como entrada del usuario.
Primero, creemos un nuevo archivo C en el directorio del proyecto:
cd ~/project
nano volume_cuboid.c
Ahora, escribamos el código para leer las dimensiones:
#include <stdio.h>
int main() {
// Declare variables to store dimensions
float length, width, height;
// Prompt user to enter length
printf("Enter the length of the cuboid: ");
scanf("%f", &length);
// Prompt user to enter width
printf("Enter the width of the cuboid: ");
scanf("%f", &width);
// Prompt user to enter height
printf("Enter the height of the cuboid: ");
scanf("%f", &height);
// Print the entered dimensions
printf("Dimensions entered:\n");
printf("Length: %.2f\n", length);
printf("Width: %.2f\n", width);
printf("Height: %.2f\n", height);
return 0;
}
Compilemos y ejecutemos el programa:
gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid
Ejemplo de salida:
Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00
Calcular Volumen = L _ W _ H
En este paso, modificaremos nuestro programa en C anterior para calcular el volumen del cuboide utilizando la fórmula: Volumen = Longitud _ Ancho _ Altura.
Editemos el archivo existente:
cd ~/project
nano volume_cuboid.c
Actualicemos el programa con el cálculo del volumen:
#include <stdio.h>
int main() {
// Declare variables to store dimensions and volume
float length, width, height, volume;
// Prompt user to enter length
printf("Enter the length of the cuboid: ");
scanf("%f", &length);
// Prompt user to enter width
printf("Enter the width of the cuboid: ");
scanf("%f", &width);
// Prompt user to enter height
printf("Enter the height of the cuboid: ");
scanf("%f", &height);
// Calculate volume
volume = length * width * height;
// Print the dimensions and calculated volume
printf("Dimensions entered:\n");
printf("Length: %.2f\n", length);
printf("Width: %.2f\n", width);
printf("Height: %.2f\n", height);
printf("Volume of the cuboid: %.2f cubic units\n", volume);
return 0;
}
Compilamos y ejecutamos el programa actualizado:
gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid
Ejemplo de salida:
Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00
Volume of the cuboid: 30.00 cubic units
Imprimir el Volumen
En este último paso, nos aseguraremos de que el volumen se imprima de manera clara y esté formateado para una mejor legibilidad. Modificaremos nuestro programa anterior para mejorar la presentación de la salida.
Editemos el archivo por última vez:
cd ~/project
nano volume_cuboid.c
Actualicemos el programa con una mejor impresión del volumen:
#include <stdio.h>
int main() {
// Declare variables to store dimensions and volume
float length, width, height, volume;
// Prompt user to enter length
printf("Cuboid Volume Calculator\n");
printf("------------------------\n");
printf("Enter the length of the cuboid: ");
scanf("%f", &length);
// Prompt user to enter width
printf("Enter the width of the cuboid: ");
scanf("%f", &width);
// Prompt user to enter height
printf("Enter the height of the cuboid: ");
scanf("%f", &height);
// Calculate volume
volume = length * width * height;
// Print detailed volume information
printf("\nCalculation Results:\n");
printf("-------------------\n");
printf("Length: %.2f units\n", length);
printf("Width: %.2f units\n", width);
printf("Height: %.2f units\n", height);
printf("Volume: %.2f cubic units\n", volume);
return 0;
}
Compilamos y ejecutamos la versión final del programa:
gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid
Ejemplo de salida:
Cuboid Volume Calculator
------------------------
Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Calculation Results:
-------------------
Length: 5.00 units
Width: 3.00 units
Height: 2.00 units
Volume: 30.00 cubic units
Resumen
En este laboratorio, aprendimos cómo leer las dimensiones de un cuboide (longitud, ancho y altura) utilizando el lenguaje de programación C. Luego, utilizamos la fórmula Volumen = Longitud _ Ancho _ Altura para calcular el volumen del cuboide. Finalmente, imprimimos el volumen calculado en la consola.
Los pasos clave incluyeron leer las dimensiones del usuario, realizar el cálculo del volumen y mostrar el resultado. Este laboratorio proporciona una introducción básica al trabajo con variables y la realización de operaciones aritméticas en C.



