Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Turn the following code into one that has a main with less than 10 lines and the

ID: 3858075 • Letter: T

Question

Turn the following code into one that has a main with less than 10 lines and the rest as call functions.

Add notes to explain what is happening in the code.

/*

* C program to display the inventory of items in a store / shop

* The inventory maintains details such as name, price, quantity

* and manufacturing date of each item.

*/

#include <stdio.h>

void main()

{

struct date

{

int day;

int month;

int year;

};

struct details

{

char name[20];

int price;

int code;

int qty;

struct date mfg;

};

struct details item[50];

int n, i;

printf("Enter number of items:");

scanf("%d", &n);

fflush(stdin);

for (i = 0; i < n; i++)

{

fflush(stdin);

printf("Item name: ");

scanf("%s", item[i].name);

fflush(stdin);

printf("Item code: ");

scanf("%d", &item[i].code);

fflush(stdin);

printf("Quantity: ");

scanf("%d", &item[i].qty);

fflush(stdin);

printf("price: ");

scanf("%d", &item[i].price);

fflush(stdin);

printf("Manufacturing date(dd-mm-yyyy): ");

scanf("%d-%d-%d", &item[i].mfg.day,

&item[i].mfg.month, &item[i].mfg.year);

}

  

printf(" ***** WELCOME ***** ");

printf("---------------------------------------------------------

--------- ");

printf("S.N.| NAME | CODE | QUANTITY | PRICE

| MFG.DATE ");

printf("---------------------------------------------------------

--------- ");

for (i = 0; i < n; i++)

printf("%d %-15s %-d %-5d %-5d

%d/%d/%d ", i + 1, item[i].name, item[i].code, item[i].qty,

item[i].price, item[i].mfg.day, item[i].mfg.month,

item[i].mfg.year);

printf("---------------------------------------------------------

--------- ");

}

Explanation / Answer

Program:

/*

* C program to display the inventory of items in a store / shop

* The inventory maintains details such as name, price, quantity

* and manufacturing date of each item.

*/

#include <stdio.h>

// struct for the date

struct date

{

int day;

int month;

int year;

};

// struct for the details of an item

struct details

{

char name[20];

int price;

int code;

int qty;

struct date mfg;

};

// function to read the number of items

int readNumberOfItems()

{

int n;

printf("Enter number of items: ");

scanf("%d", &n);

fflush(stdin);

return n;

}

// function to read the inventory

void readInventory(struct details item[50], int n)

{

int i;

for (i = 0; i < n; i++)

{

fflush(stdin);

printf(" Item name: ");

scanf("%s", item[i].name);

fflush(stdin);

printf("Item code: ");

scanf("%d", &item[i].code);

fflush(stdin);

printf("Quantity: ");

scanf("%d", &item[i].qty);

fflush(stdin);

printf("Price: ");

scanf("%d", &item[i].price);

fflush(stdin);

printf("Manufacturing date(dd-mm-yyyy): ");

scanf("%d-%d-%d", &item[i].mfg.day, &item[i].mfg.month, &item[i].mfg.year);

}

}

// function to diplay the inventory

void displayInventory(struct details item[50], int n)

{

int i;

printf(" ***** WELCOME ***** ");

printf("--------------------------------------------------------------- ");

printf("S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE ");

printf("--------------------------------------------------------------- ");

for (i = 0; i < n; i++)

printf("%-4d %-12s %-6d %-8d %-8d %d/%d/%d ", (i + 1), item[i].name, item[i].code, item[i].qty, item[i].price, item[i].mfg.day, item[i].mfg.month, item[i].mfg.year);

printf("--------------------------------------------------------------- ");

}

// main function

void main()

{

struct details item[50];

int n;

// readNumberOfItems function calling

n = readNumberOfItems();

// readInventory function calling

readInventory(item, n);

// displayInventory function calling

displayInventory(item, n);

}

Output:

Enter number of items: 3

Item name: Item1

Item code: 101

Quantity: 100

Price: 100

Manufacturing date(dd-mm-yyyy): 10-10-2010

Item name: Item2

Item code: 102

Quantity: 200

Price: 200

Manufacturing date(dd-mm-yyyy): 20-20-2002

Item name: Item3

Item code: 103

Quantity: 300

Price: 300

Manufacturing date(dd-mm-yyyy): 15-15-2015

***** WELCOME *****

---------------------------------------------------------------

S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE

---------------------------------------------------------------

1 Item1 101 100 100 10/10/2010

2 Item2 102 200 200 20/20/2002

3 Item3 103 300 300 15/15/2015

---------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote