Menu
Coddy logo textTech

Recap: Array of Structs

Part of the Logic & Flow section of Coddy's C journey — lesson 62 of 63.

challenge icon

Challenge

Easy

Create a C program that manages a product inventory system using an array of structs. Your program should:

  1. Define a struct named Product with the following members:
    • A character array name with size 30
    • A float price
    • An integer stock
  2. Write a function named findMostExpensive that:
    • Takes an array of struct Product and an integer size as parameters
    • Returns an integer representing the index of the product with the highest price
    • Uses a loop to iterate through all products and compare their prices
    • Returns the index of the first product found with the maximum price
  3. Write a function named calculateTotalValue that:
    • Takes an array of struct Product and an integer size as parameters
    • Returns a float representing the total inventory value
    • Calculates the total value by summing (price × stock) for each product
  4. Write a function named findLowStock that:
    • Takes an array of struct Product, an integer size, and an integer threshold as parameters
    • Returns an integer representing the count of products with stock below the threshold
    • Uses a loop to count products where stock is less than the threshold value
  5. In the main function:
    • Create an array of 3 struct Product named inventory
    • Read input for each product in this order: name, price, stock
    • Populate the array with the input data
    • Print each product's information in this exact format: Product [index]: [name] - Price: [price], Stock: [stock]
    • Call the findMostExpensive function and print the result in this exact format: Most expensive product: [name]
    • Call the calculateTotalValue function and print the result with exactly 2 decimal places in this exact format: Total inventory value: [total_value]
    • Read an integer representing the low stock threshold
    • Call the findLowStock function with the threshold and print the result in this exact format: Products with low stock: [count]
    • Check if the most expensive product has stock greater than 10:
      • If yes: print Most expensive product is well stocked
      • If no: print Most expensive product needs restocking

This challenge tests your ability to work with arrays of structs, implement functions that process struct arrays, and perform various calculations and comparisons on structured data. You'll practice array indexing with structs, function design for data processing, and combining multiple operations to analyze inventory information.

Try it yourself

#include <stdio.h>
#include <string.h>

// TODO: Define the Product struct here

// TODO: Implement the findMostExpensive function here

// TODO: Implement the calculateTotalValue function here

// TODO: Implement the findLowStock function here

int main() {
    // Create array of 3 products
    struct Product inventory[3];
    
    // Read input for each product
    for (int i = 0; i < 3; i++) {
        scanf("%s", inventory[i].name);
        scanf("%f", &inventory[i].price);
        scanf("%d", &inventory[i].stock);
    }
    
    // TODO: Print each product's information
    
    // TODO: Find and print the most expensive product
    
    // TODO: Calculate and print total inventory value
    
    // Read low stock threshold
    int threshold;
    scanf("%d", &threshold);
    
    // TODO: Find and print products with low stock
    
    // TODO: Check if most expensive product is well stocked
    
    return 0;
}

All lessons in Logic & Flow