Recap: Array of Structs
Part of the Logic & Flow section of Coddy's C journey — lesson 62 of 63.
Challenge
EasyCreate a C program that manages a product inventory system using an array of structs. Your program should:
- Define a
structnamedProductwith the following members:- A character array
namewith size 30 - A float
price - An integer
stock
- A character array
- Write a function named
findMostExpensivethat:- Takes an array of
struct Productand an integersizeas 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
- Takes an array of
- Write a function named
calculateTotalValuethat:- Takes an array of
struct Productand an integersizeas parameters - Returns a float representing the total inventory value
- Calculates the total value by summing (price × stock) for each product
- Takes an array of
- Write a function named
findLowStockthat:- Takes an array of
struct Product, an integersize, and an integerthresholdas 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
- Takes an array of
- In the main function:
- Create an array of 3
struct Productnamedinventory - 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
findMostExpensivefunction and print the result in this exact format:Most expensive product: [name] - Call the
calculateTotalValuefunction 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
findLowStockfunction 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
- If yes: print
- Create an array of 3
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
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions