Struct Array

PHOTO EMBED

Wed Jun 12 2024 04:14:25 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #function #struct

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

struct Product
{
    char name [20];
    char brand [20];
    float price ;
    int quantity;

};

void print_product(struct Product a_product);

int main(void)
{
    struct Product a_product[3];
    
    sprintf(a_product[0].name,"Xbox One S");
    sprintf(a_product[0].brand,"Microsoft");
    a_product[0].price= 395.50;
    a_product[0].quantity = 35;
    
    sprintf(a_product[1].name,"PlayStation 4 Pro");
    sprintf(a_product[1].brand,"Sony");
    a_product[1].price= 538.00;
    a_product[1].quantity = 71;
    
    sprintf(a_product[2].name,"Switch");
    sprintf(a_product[2].brand,"Nintendo");
    a_product[2].price= 529.95;
    a_product[2].quantity = 8;
    
    for(int i = 0; i < 3; i++)
    {
        print_product(a_product[i]);
    }
	return 0;
}

void print_product(struct Product a_product)
{
	printf("Item name: %s\n", a_product.name);
	printf("Brand:     %s\n", a_product.brand);
	printf("Price:     $%.2f\n", a_product.price);
	printf("Quantity:  %d\n\n", a_product.quantity);
}
content_copyCOPY

Objective Create a C program that defines a structure named Product, which contains information about different products in an inventory. The program should display the inventory data in a specified format. Structure Definition Declare a structure named Product with the following members: Item name (type: string) Brand (type: string) Retail price (type: float) Quantity in stock (type: int) Main Function Tasks In the main() function, declare an array of Product structures. Initialize the array with the following inventory data: Inventory Data Item Name Brand Price Quantity in Stock Xbox One S Microsoft $395.50 35 PlayStation 4 Pro Sony $538.00 71 Switch Nintendo $529.95 8 For example: Result Item name: Xbox One S Brand: Microsoft Price: $395.50 Quantity: 35 Item name: PlayStation 4 Pro Brand: Sony Price: $538.00 Quantity: 71 Item name: Switch Brand: Nintendo Price: $529.95 Quantity: 8