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

C programming tweak needed for a specific program. This is the complete program

ID: 3780160 • Letter: C

Question

C programming tweak needed for a specific program.

This is the complete program up and running. except for one modification that my teacher is requiring but it is confusing me.

The program needs to allocate disjoint memory and use that.

right now im allocating a continous block of memory on the heap but I need it to do disjoing memory instead.

// Purpose: Program will allow a reseller to save their sales and calculate their profits.
// Also able to view highest profitable items and remove any sales they no longer want in their sales inventory.

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define PAUSE system("pause")
#define CLS system("cls");
//#define FLUSH fflush(stdin);


//////////////// SRUCTS BELOW //////////////////////////
typedef struct
{
    char itemName[40];
    float purchasePrice;
    float soldPrice;
    float shippingPrice;
    float ebayFee;
    float payPalFee;
    float profit;
}SALES;

////////// FUNCTION PROTOTYPE BELOW /////////////
void addSale(SALES *Inventory, int *InventoryCounter);
void displayMenu(int *InventoryCounter);
void displayProfit(int *InventoryCounter, SALES *Inventory);
void displaySales(int *InventoryCounter, SALES *Inventory);
int getChoice(int *InventoryCounter);
void searchSale(SALES* Inventory, int InventoryCounter);
void sortProfits(SALES* Inventory, int InventoryCounter);
void removeSale(SALES* Inventory, int *InventoryCounter);


main() {

    int SIZE;
    int userChoice;
    int InventoryCounter = 0;

    //printf("********************* ");
    //printf("* Welcome Reseller! * ");
    //printf("********************* ");
    printf("Enter the size of todays inventory:");
    scanf("%i", &SIZE);

    SALES *Inventory;
    Inventory = calloc(SIZE, sizeof(SALES));//Using dynamic memory to allocate space on the heap

    do {
        userChoice = getChoice(&InventoryCounter);
        switch (userChoice) {
        case 1:// Add a Sale
            if (InventoryCounter == SIZE) // statement checks so we dont exceed the size of the inventory user wants to enter.
            {
                printf("You Have Reached Max Inventory ");
                PAUSE;
            }
            else {
                addSale(&(Inventory[InventoryCounter]), &InventoryCounter);
            }
            break;
        case 2: // Display all sales records
            displaySales(&InventoryCounter, Inventory);
            PAUSE;
            break;
        case 3: // Display all profits
            sortProfits(Inventory, InventoryCounter);
            displayProfit(&InventoryCounter, Inventory);
            PAUSE;
            break;
        case 4: // Display Inventory
            searchSale(Inventory, InventoryCounter);
            break;
        case 5:// remove a sale
            removeSale(Inventory, &InventoryCounter);
            break;
        case 6: // Quit
            break;
        default:
            printf("Please choose a valid option ");
            PAUSE;
            break;
        }// end of switch
    } while (userChoice != 6);
    free(Inventory);

} // End of main


//////////////////////////////////////////// FUNCTIONS BELOW //////////////////////////////////////////////////

void addSale(SALES *Inventory, int *InventoryCounter)// Function that adds sale and calculates the fees in the background.
{
    float payPalFee = 0; // variable to store the paypal fee from 1 sale
    float ebayFee = 0; // variable to store ebay fee for 1 sale

/////////////// GETTING INVENTORY DETAILS ////////////////   

    printf("Enter the Item name: ");
    scanf(" %[A-Z a-z]", Inventory->itemName);

    printf(" Enter the item's purchase price: ");
    scanf(" %f", &(Inventory->purchasePrice));

    printf(" Enter the items Sold Price : ");
    scanf(" %f", &(Inventory->soldPrice));

    printf(" Enter the item Shipping Price: ");
    scanf(" %f", &(Inventory->shippingPrice));

    ///////////////////////////////////////////////////////////

    /////////////// Fees Calculated below ////////////////


    payPalFee = (Inventory->soldPrice) * (.03) + .30; // Calculating Paypals fee Which is 3% of sold price plus .30 cents
    ebayFee = (Inventory->soldPrice)* (.10); // Caluculating Ebays fee which is 10% of sold price
    Inventory->payPalFee = payPalFee; // Saving the paypal fee for this specific item in struct
    Inventory->ebayFee = ebayFee; // Saving the ebay fee for this specific item in struct
                          
        // calculating the profit for this 1 secific item based on details provided (line below) //
    Inventory->profit = Inventory->soldPrice - Inventory->payPalFee - Inventory->ebayFee - Inventory->purchasePrice - Inventory->shippingPrice;
    
    (*InventoryCounter)++; // Incrementing Inventory counter ( amount of sales)

} // End of addSale

void searchSale(SALES* Inventory, int InventoryCounter) // function that searches for a sale by name
{
    char name[100]; // array to hold item searched name
    char found = 'N'; // Flag variable
    int i; // loop control variable

////// Ask User for item name //////
    printf("What item are you searching for? ");
    scanf("%s", name);


    for (i = 0; i < InventoryCounter; i++)
    {
        if (strcmp(name, Inventory[i].itemName) == 0)// returns 0 if item was found, then we can display the item details.
        {
            printf(" |Name| |Purchase Price| |soldPrice| |Profit| ");
            printf(" %.6s $%.2f $%.2f $%.2f ", Inventory[i].itemName, Inventory[i].purchasePrice, Inventory[i].soldPrice, Inventory[i].profit);
            found = 'Y';//flag changed to illustrate item was found
            PAUSE;
        }// end if statment
    }// end for loop
    if (found == 'N') // if strcmp does not return 0 and flag stays 'N' then item will not be in inventory
    {
        printf("Item not found ");
        PAUSE;
    }
}// end search Sales function

void displayProfit(int *InventoryCounter, SALES *Inventory) // Function that displays the profit details of each item.
{
    int i; // loop control variable
    CLS;
    printf("|Name| | Profit| ");
    for (i = 0; i < *InventoryCounter; i++)// loops through all the items and displays their name and profit detials.
    {
        printf("%s %.2f ", Inventory[i].itemName, Inventory[i].profit);
    } // End of for loop
} // End of displayProfit

void displayMenu(int *InventoryCounter) // Fucntion that displays the Main Menu
{
    CLS;
    printf("******** Main Menu ********* ");
    printf("1. Add a sale. ");
    printf("2. Display all Inventory records. ");
    printf("3. Display all Inventory profits (high to low). ");
    printf("4. Search inventory for a specific item . ");
    printf("5. Remove a Sale from Inventory ");
    printf("6. Quit. ");
    printf("Current Sales: %i ", *InventoryCounter);
    printf("Enter your choice: ");
} // End of displayMenu

void displaySales(int *InventoryCounter, SALES *Inventory) // Fucntion that Displays all sales records
{
    int i; // loop control variable
    CLS;
    printf(" |Name| |Purchase Price| |soldPrice| |shippingPrice| ");

    for (i = 0; i < *InventoryCounter; i++) // loops through all the inventory and displays details
    {
        printf(" %.6s $%.2f $%.2f $%.2f ", Inventory[i].itemName, Inventory[i].purchasePrice, Inventory[i].soldPrice, Inventory[i].shippingPrice);
    } // End of for loop
} // End of displaySales

int getChoice(int *InventoryCounter)// Function that returns the user Choice from menu
{
    int result;
    displayMenu(InventoryCounter);
    while (getchar() != ' ') {};
    scanf(" %i", &result);
    return result;
} // End of getChoice

void sortProfits(SALES *Inventory, int InventoryCounter)// Function that sorts all the profits from highest to lowest
{
    int i, j;// loop control variable
    SALES temp;// temp place holder.

    for (i = 0; i < (InventoryCounter - 1); i++)
    {
        for (j = 0; j < InventoryCounter - i - 1; j++)
        {
            if (Inventory[j].profit < Inventory[j + 1].profit)
            {
                temp = Inventory[j];
                Inventory[j] = Inventory[j + 1];
                Inventory[j + 1] = temp;
            }
        }
    }// end for loop through all inventory
}// End Sort Profits function


void removeSale(SALES* Inventory, int *InventoryCounter) {
    char name[100];// char array to hold item searched name
    int i, j; // loop control variable
    char found = 'N';// Flag variable

    printf("What item do you want to remove? ");
    scanf("%s", name);


    for (i = 0; i < *InventoryCounter; i++)
    {
        if (strcmp(name, Inventory[i].itemName) == 0) //if strcmp returns 0 then item will be removed from inventory
        {
            for (j = i; j < *InventoryCounter - 1; j++)
                Inventory[j] = Inventory[j + 1];
            (*InventoryCounter)--;
            printf(" Item Succesfully Removed ");
            found = 'Y';
            PAUSE;
        }// end if statement
    }// end for loop

    if (found == 'N') // if strcmp does not return 0 and flag stays 'N' then item will not be in inventory
    {
        printf("Item not found ");
        PAUSE;
    }// end else statement
}// end removeSale function

Explanation / Answer

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define PAUSE system("pause")
#define CLS system("cls");
//#define FLUSH fflush(stdin);


//////////////// SRUCTS BELOW //////////////////////////


typedef struct SALESINFO
{
char itemName[40];
float purchasePrice;
float soldPrice;
float shippingPrice;
float ebayFee;
float payPalFee;
float profit;


}SALES;

////////// FUNCTION PROTOTYPE BELOW /////////////
void addSale(SALES *Inventory, int *InventoryCounter);
void displayMenu(int *InventoryCounter);
void displayProfit(int *InventoryCounter, SALES *Inventory);
void displaySales(int *InventoryCounter, SALES *Inventory);
int getChoice(int *InventoryCounter);
void searchSale(SALES* Inventory, int InventoryCounter);
void sortProfits(SALES* Inventory, int InventoryCounter);
void removeSale(SALES* Inventory, int *InventoryCounter);


main() {

int SIZE;
int userChoice;
int InventoryCounter = 0;

//printf("********************* ");
//printf("* Welcome Reseller! * ");
//printf("********************* ");
printf("Enter the size of todays inventory:");
scanf("%i", &SIZE);

//SALES *Inventory;
struct SALESINFO *Inventory;
Inventory=(struct SALESINFO *) malloc (SIZE * sizeof(struct SALESINFO));
//Inventory = calloc(SIZE, sizeof(SALES));//Using dynamic memory to allocate space on the heap

do {
userChoice = getChoice(&InventoryCounter);
switch (userChoice) {
case 1:// Add a Sale
if (InventoryCounter == SIZE) // statement checks so we dont exceed the size of the inventory user wants to enter.
{
printf("You Have Reached Max Inventory ");
PAUSE;
}
else {
addSale(&(Inventory[InventoryCounter]), &InventoryCounter);
}
break;
case 2: // Display all sales records
displaySales(&InventoryCounter, Inventory);
PAUSE;
break;
case 3: // Display all profits
sortProfits(Inventory, InventoryCounter);
displayProfit(&InventoryCounter, Inventory);
PAUSE;
break;
case 4: // Display Inventory
searchSale(Inventory, InventoryCounter);
break;
case 5:// remove a sale
removeSale(Inventory, &InventoryCounter);
break;
case 6: // Quit
break;
default:
printf("Please choose a valid option ");
PAUSE;
break;
}// end of switch
} while (userChoice != 6);
free(Inventory);

} // End of main


//////////////////////////////////////////// FUNCTIONS BELOW //////////////////////////////////////////////////

void addSale(SALES *Inventory, int *InventoryCounter)// Function that adds sale and calculates the fees in the background.
{
float payPalFee = 0; // variable to store the paypal fee from 1 sale
float ebayFee = 0; // variable to store ebay fee for 1 sale

/////////////// GETTING INVENTORY DETAILS ////////////////

printf("Enter the Item name: ");
scanf(" %[A-Z a-z]", Inventory->itemName);

printf(" Enter the item's purchase price: ");
scanf(" %f", &(Inventory->purchasePrice));

printf(" Enter the items Sold Price : ");
scanf(" %f", &(Inventory->soldPrice));

printf(" Enter the item Shipping Price: ");
scanf(" %f", &(Inventory->shippingPrice));

///////////////////////////////////////////////////////////

/////////////// Fees Calculated below ////////////////


payPalFee = (Inventory->soldPrice) * (.03) + .30; // Calculating Paypals fee Which is 3% of sold price plus .30 cents
ebayFee = (Inventory->soldPrice)* (.10); // Caluculating Ebays fee which is 10% of sold price
Inventory->payPalFee = payPalFee; // Saving the paypal fee for this specific item in struct
Inventory->ebayFee = ebayFee; // Saving the ebay fee for this specific item in struct

// calculating the profit for this 1 secific item based on details provided (line below) //
Inventory->profit = Inventory->soldPrice - Inventory->payPalFee - Inventory->ebayFee - Inventory->purchasePrice - Inventory->shippingPrice;

(*InventoryCounter)++; // Incrementing Inventory counter ( amount of sales)

} // End of addSale

void searchSale(SALES* Inventory, int InventoryCounter) // function that searches for a sale by name
{
char name[100]; // array to hold item searched name
char found = 'N'; // Flag variable
int i; // loop control variable

////// Ask User for item name //////
printf("What item are you searching for? ");
scanf("%s", name);


for (i = 0; i < InventoryCounter; i++)
{
if (strcmp(name, Inventory[i].itemName) == 0)// returns 0 if item was found, then we can display the item details.
{
printf(" |Name| |Purchase Price| |soldPrice| |Profit| ");
printf(" %.6s $%.2f $%.2f $%.2f ", Inventory[i].itemName, Inventory[i].purchasePrice, Inventory[i].soldPrice, Inventory[i].profit);
found = 'Y';//flag changed to illustrate item was found
PAUSE;
}// end if statment
}// end for loop
if (found == 'N') // if strcmp does not return 0 and flag stays 'N' then item will not be in inventory
{
printf("Item not found ");
PAUSE;
}
}// end search Sales function

void displayProfit(int *InventoryCounter, SALES *Inventory) // Function that displays the profit details of each item.
{
int i; // loop control variable
CLS;
printf("|Name| | Profit| ");
for (i = 0; i < *InventoryCounter; i++)// loops through all the items and displays their name and profit detials.
{
printf("%s %.2f ", Inventory[i].itemName, Inventory[i].profit);
} // End of for loop
} // End of displayProfit

void displayMenu(int *InventoryCounter) // Fucntion that displays the Main Menu
{
CLS;
printf("******** Main Menu ********* ");
printf("1. Add a sale. ");
printf("2. Display all Inventory records. ");
printf("3. Display all Inventory profits (high to low). ");
printf("4. Search inventory for a specific item . ");
printf("5. Remove a Sale from Inventory ");
printf("6. Quit. ");
printf("Current Sales: %i ", *InventoryCounter);
printf("Enter your choice: ");
} // End of displayMenu

void displaySales(int *InventoryCounter, SALES *Inventory) // Fucntion that Displays all sales records
{
int i; // loop control variable
CLS;
printf(" |Name| |Purchase Price| |soldPrice| |shippingPrice| ");

for (i = 0; i < *InventoryCounter; i++) // loops through all the inventory and displays details
{
printf(" %.6s $%.2f $%.2f $%.2f ", Inventory[i].itemName, Inventory[i].purchasePrice, Inventory[i].soldPrice, Inventory[i].shippingPrice);
} // End of for loop
} // End of displaySales

int getChoice(int *InventoryCounter)// Function that returns the user Choice from menu
{
int result;
displayMenu(InventoryCounter);
while (getchar() != ' ') {};
scanf(" %i", &result);
return result;
} // End of getChoice

void sortProfits(SALES *Inventory, int InventoryCounter)// Function that sorts all the profits from highest to lowest
{
int i, j;// loop control variable
SALES temp;// temp place holder.

for (i = 0; i < (InventoryCounter - 1); i++)
{
for (j = 0; j < InventoryCounter - i - 1; j++)
{
if (Inventory[j].profit < Inventory[j + 1].profit)
{
temp = Inventory[j];
Inventory[j] = Inventory[j + 1];
Inventory[j + 1] = temp;
}
}
}// end for loop through all inventory
}// End Sort Profits function


void removeSale(SALES* Inventory, int *InventoryCounter) {
char name[100];// char array to hold item searched name
int i, j; // loop control variable
char found = 'N';// Flag variable

printf("What item do you want to remove? ");
scanf("%s", name);


for (i = 0; i < *InventoryCounter; i++)
{
if (strcmp(name, Inventory[i].itemName) == 0) //if strcmp returns 0 then item will be removed from inventory
{
for (j = i; j < *InventoryCounter - 1; j++)
Inventory[j] = Inventory[j + 1];
(*InventoryCounter)--;
printf(" Item Succesfully Removed ");
found = 'Y';
PAUSE;
}// end if statement
}// end for loop

if (found == 'N') // if strcmp does not return 0 and flag stays 'N' then item will not be in inventory
{
printf("Item not found ");
PAUSE;
}// end else statement
}// end removeSale function

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

output sample:-

******** Main Menu *********
1. Add a sale.
2. Display all Inventory records.
3. Display all Inventory profits (high to low).
4. Search inventory for a specific item .
5. Remove a Sale from Inventory
6. Quit.
Current Sales: 0
Enter your choice: 1
Enter the Item name: XYZ

Enter the item's purchase price: 4

Enter the items Sold Price : 6

Enter the item Shipping Price:2

******** Main Menu *********
1. Add a sale.
2. Display all Inventory records.
3. Display all Inventory profits (high to low).
4. Search inventory for a specific item .
5. Remove a Sale from Inventory
6. Quit.
Current Sales: 1
Enter your choice: 2

|Name| |Purchase Price| |soldPrice| |shippingPrice|
XYZ $4.00 $6.00 $2.00

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

If you have any query, please feel free to ask.

Thanks a lot.