Professional Documents
Culture Documents
Repport
Repport
TARIK MOURAD
223J01
CST22
Introduction:
The provided script is a C language program designed to manage a drug inventory for a small
pharmacy or medical facility. It employs fundamental data structures and algorithms to enable
the user to perform various operations on a collection of drugs, each identified by a unique code,
name, price, and stock count.
The core of the program is encapsulated in two custom data structures: Drug, which holds the
details of a single drug item, and DrugList, which is a collection of Drug items alongside an
integer representing the current number of drugs in the inventory. The program's capacity is pre-
defined to handle up to 100 drug entries, a limit set by the MaxSize constant.
Functionality-wise, the program offers a console-based user interface that allows users to:
Add new drugs to the inventory while automatically calculating their total sales value.
Search for a specific drug by its name, utilizing a linear search algorithm to traverse the
inventory.
Sort the inventory by the price of drugs using a bubble sort algorithm, which, despite not
being the most efficient sorting method, suffices for the small scale of data the program is
expected to handle.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
o #include <stdio.h>: This directive includes the Standard Input and Output Library,
which provides functionalities for input and output operations, such as reading
from the keyboard with scanf or printing to the console with printf.
o #include <stdlib.h>: The Standard Library includes functions involving memory
allocation (malloc, free), process control (exit, system), and other utility
functions.
o #include <string.h>: This header file contains functions for manipulating arrays of
characters (strings), such as strcmp to compare two strings, strncpy to copy
strings, etc.
Struct Definitions:
typedef struct {
char code [10];
char name [50];
float price;
int count;
float totalSales;
} Drug;
typedef struct {
Drug drugs [MAX_SIZE];
int length;
} DrugList;
o Drug: This structure represents a single drug item with fields for unique code,
name, price per unit, stock count, and total sales. The char arrays hold string data,
while float and int hold numerical values.
o DrugList: This structure encapsulates the entire inventory. It contains an array of
Drug structures and an integer length that keeps track of the current number of
drugs in the inventory. This setup allows for operations on the collection as a
whole, such as adding or searching for drugs.
Function Prototypes:
o bubbleSortByPrice takes a pointer to DrugList and sorts the drugs array within
it by price, not returning any value.
Main Function:
int main() {
DrugList list;
initializeDrugList(&list);
int choice;
// Main menu loop
// ...
return 0;
}
o The main function is the entry point of the program from where the
execution starts. It's mandatory for every C program.
o A loop is typically used to display the main menu and handle user
input until the program is exited.
Function Implementations:
Drug newDrug;
printf("Enter drug code (4 characters): ");
scanf("%4s", newDrug.num);
printf("Enter drug name: ");
scanf("%19s", newDrug.name);
printf("Enter price: ");
scanf("%f", &newDrug.price);
printf("Enter count: ");
scanf("%d", &newDrug.count);
o Check for Space: It's important to ensure there is room to add a new
drug to the inventory. This is where MAX_SIZE is used to prevent
adding more drugs than the array can hold.
o Create and Populate Drug: A new Drug instance within the function is
created and populated with the input data. The totalSales might be
calculated by multiplying the price by the count, depending on the
requirements.
o Add to List: The new Drug is added to the DrugList array, and the
length counter is incremented to reflect the addition.
o Repeat Until Sorted: The outer loop ensures that this process is
repeated until the entire list is sorted.
o Since bubble sort is less efficient for large datasets, this function is
more suited for small to medium-sized inventories.
Utility Functions:
o Formatting: Details such as the drug's code, name, price, and stock
count are formatted into a human-readable string.
o The const qualifier indicates that the function will not modify the
drug's information.
Inside functions like addNewDrug, there will be checks to ensure the list isn't
already full before attempting to add a new drug. If the inventory is full, it will
print an error message and exit the function.