Untitled
unknown
plain_text
2 years ago
1.6 kB
7
Indexable
#include <stdio.h> #include <string.h> typedef struct { char nombre[50]; float precio; int stock; } Producto; void buscaCaro(Producto productos[100], int cantidad) { float caro = 0.0; int indice = 0; caro = productos[0].precio; for(int i = 0; i < cantidad; i++) { if(productos[i].precio > caro) { caro = productos[i].precio; indice = i; } } printf("The most expensive product is %s, with a price of %.2f€\n", productos[indice].nombre, caro); } void buscaStock(Producto productos[100], int cantidad) { int stock = 0; int indice = 0; stock = productos[0].stock; for(int i = 0; i < cantidad; i++) { if(productos[i].stock < stock) { stock = productos[i].stock; indice = i; } } printf("The product with less stock is %s, with %d units\n", productos[indice].nombre, stock); } int main() { Producto productos[100]; int nproductos = 0; char basura; printf("Number of products: "); scanf("%d", &nproductos); scanf("%c", &basura); for(int i = 0; i < nproductos; i++) { printf("Product %d, name? ", i + 1); fgets(productos[i].nombre, 50, stdin); productos[i].nombre[strlen(productos[i].nombre) - 1] = '\0'; printf("Product %d, price? ", i+1); scanf("%f", &productos[i].precio); printf("Product %d, stock? ", i+1); scanf("%d", &productos[i].stock); scanf("%c", &basura); printf("\n"); } buscaCaro(productos, nproductos); buscaStock(productos, nproductos); return 0; }
Editor is loading...