Challenge

Project 1

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

// Structure to hold vendor information

struct Vendor {

char name[20];

int *prices;

};

// Structure to hold commodity information

struct Commodity {

char name[20];

int *quantities;

};

// Function to find the best vendor for each commodity

void findBestVendor(Commodity* commodities, Vendor* vendors, int numCommodities, int numVendors) {

for (int i = 0; i < numCommodities; ++i) {

int minPrice = INT_MAX;

int bestVendorIndex = -1;

for (int j = 0; j < numVendors; ++j) {

int totalPrice = 0;

for (int k = 0; k < 5; ++k) {

totalPrice += commodities[i].quantities[k] * vendors[j].prices[k];

}

if (totalPrice < minPrice) {

minPrice = totalPrice;

bestVendorIndex = j;

}

}

cout << "Best vendor for " << commodities[i].name << " is: " << vendors[bestVendorIndex].name << endl;

}

}

// Function to prompt user to enter commodity data

void enterCommodities(Commodity* commodities, int numCommodities) {

cout << "Enter details for each commodity:" << endl;

for (int i = 0; i < numCommodities; ++i) {

cout << "Commodity " << i + 1 << " name: ";

cin >> commodities[i].name;

commodities[i].quantities = new int[5];

for (int j = 0; j < 5; ++j) {

cout << "Enter quantity for " << commodities[i].name << " from vendor " << j + 1 << ": ";

cin >> commodities[i].quantities[j];

}

}

}

// Function to prompt user to enter vendor data

void enterVendors(Vendor* vendors, int numVendors) {

cout << "Enter details for each vendor:" << endl;

for (int i = 0; i < numVendors; ++i) {

cout << "Vendor " << i + 1 << " name: ";

cin >> vendors[i].name;

vendors[i].prices = new int[5];

cout << "Enter prices for each commodity from vendor " << vendors[i].name << ":" << endl;

for (int j = 0; j < 5; ++j) {

cout << "Price for commodity " << j + 1 << ": ";

cin >> vendors[i].prices[j];

}

}

}

// Function to save commodity and vendor data to a file

void saveToFile(Commodity* commodities, Vendor* vendors, int numCommodities, int numVendors) {

ofstream file("data.txt");

if (file.is_open()) {

file << "Commodities:" << endl;

for (int i = 0; i < numCommodities; ++i) {

file << commodities[i].name << ": ";

for (int j = 0; j < 5; ++j) {

file << commodities[i].quantities[j] << " ";

}

file << endl;

}

file << "Vendors:" << endl;

for (int i = 0; i < numVendors; ++i) {

file << vendors[i].name << ": ";

for (int j = 0; j < 5; ++j) {

file << vendors[i].prices[j] << " ";

}

file << endl;

}

file.close();

cout << "Data saved to file successfully." << endl;

} else {

cout << "Unable to open file." << endl;

}

}

int main() {

int numCommodities, numVendors;

cout << "Enter the number of commodities: ";

cin >> numCommodities;

cout << "Enter the number of vendors: ";

cin >> numVendors;

Commodity *commodities = new Commodity[numCommodities];

Vendor *vendors = new Vendor[numVendors];

enterCommodities(commodities, numCommodities);

enterVendors(vendors, numVendors);

findBestVendor(commodities, vendors, numCommodities, numVendors);

saveToFile(commodities, vendors, numCommodities, numVendors);

// Free dynamically allocated memory

for (int i = 0; i < numCommodities; ++i) {

delete[] commodities[i].quantities;

}

delete[] commodities;

for (int i = 0; i < numVendors; ++i) {

delete[] vendors[i].prices;

}

delete[] vendors;

return 0;

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.