Project 1
Code-
#include
#include
#include
using namespace std;
// Function to find the best vendor
char* findBestVendor(int* quantities, int* pricesVendor1, int* pricesVendor2, int numCommodities) {
int totalCostVendor1 = 0;
int totalCostVendor2 = 0;
for (int i = 0; i < numCommodities>
totalCostVendor1 += quantities[i] * pricesVendor1[i];
totalCostVendor2 += quantities[i] * pricesVendor2[i];
}
if (totalCostVendor1 < totalCostVendor2>
return "Vendor1";
} else {
return "Vendor2";
}
}
// Function to display the menu and get user input
void displayMenu(int* quantities, int* pricesVendor1, int* pricesVendor2, int numCommodities) {
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Enter Commodities to be purchased\n";
cout << "2. Enter Vendor quotations\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter quantities for each commodity:\n";
for (int i = 0; i < numCommodities>
cout << "Quantity for commodity " << i>
cin >> quantities[i];
}
break;
case 2:
cout << "Enter prices for each commodity from Vendor1:\n";
for (int i = 0; i < numCommodities>
cout << "Price for commodity " << i>
cin >> pricesVendor1[i];
}
cout << "Enter prices for each commodity from Vendor2:\n";
for (int i = 0; i < numCommodities>
cout << "Price for commodity " << i>
cin >> pricesVendor2[i];
}
break;
case 3:
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 3);
}
// Function to save input data to a file
void saveToFile(int* quantities, int* pricesVendor1, int* pricesVendor2, int numCommodities) {
ofstream outFile("input_data.txt");
if (!outFile) {
cerr << "Error opening the file.\n";
return;
}
outFile << "Quantities:\n";
for (int i = 0; i < numCommodities>
outFile << quantities>
}
outFile << "\nPrices from Vendor1:\n";
for (int i = 0; i < numCommodities>
outFile << pricesVendor1>
}
outFile << "\nPrices from Vendor2:\n";
for (int i = 0; i < numCommodities>
outFile << pricesVendor2>
}
outFile.close();
}
int main() {
const int numCommodities = 5;
int* quantities = new int[numCommodities];
int* pricesVendor1 = new int[numCommodities];
int* pricesVendor2 = new int[numCommodities];
// Display menu and get user input
displayMenu(quantities, pricesVendor1, pricesVendor2, numCommodities);
// Find the best vendor
char* bestVendor = findBestVendor(quantities, pricesVendor1, pricesVendor2, numCommodities);
// Display the best vendor
cout << "\nThe best vendor is: " << bestVendor>
// Save input data to a file
saveToFile(quantities, pricesVendor1, pricesVendor2, numCommodities);
// Deallocate memory
delete[] quantities;
delete[] pricesVendor1;
delete[] pricesVendor2;
return 0;
}