Challenge

Project 1

Live Batch

Submitted to: Essaki Vigneswaran

PROGRAM:

#include <iostream> // header file
#include <fstream> //file stream library for file operations
#include <string> //string library for string operations


using namespace std; //Use standard namespace

const int MAX_ROWS = 3; // Define a constant integer for maximum number of rows
const int MAX_COLS = 5; // Define a constant integer for maximum number of columns


void displayMenu() // Define function to display menu
{
cout << "Enter Commodities to be purchased"<<endl; //// Display menu option for entering commodities
cout << "Enter Vendor quotations"<<endl;
cout << "Exit"<<endl;
}

void inputCommodities(string *commodities) // Define function to input commodity data
{

for (int i = 0;i<MAX_COLS;i++) // Loop for through each column
{
cout<<"Enter commodity #"<<i + 1<< ": ";
getline(cin, commodities[i]); //Read commodity name from standard input and store in array
}

}

void inputVendorData(int **vendorData) // Define function to input vendor data
{
for (int i = 0;i< MAX_ROWS;i++) // Loop through each row
{
for (int j = 0;j<MAX_COLS;j++) // Loop through each column
{
cout<< "Enter vendor #"<<i + 1<<" price for commodity #"<<j + 1<< ": ";
cin>>vendorData[i][j]; // Read price from standard input and store in array

}
}
}

void displayResult(int **vendorData) // Define function to display best vendor
{
int total[MAX_ROWS] = {0}; // Define array for total prices for each vendor
for (int i = 0; i < MAX_ROWS; i++) // Loop through each row
{
for (int j = 0; j < MAX_COLS; j++) // Loop through each column
{
total[i] += vendorData[i][j]; // Add price to total for the corresponding vendor
}
}
int bestVendorIndex = 0; // Initialize best vendor index to first vendor
for (int i = 1; i < MAX_ROWS; i++) // Loop through each vendor starting from second
{
if (total[i] < total[bestVendorIndex]) // If total for current vendor is less than total for best vendor
{
bestVendorIndex = i; // Update best vendor index to current vendor
}
}
cout << "Best vendor: #" << bestVendorIndex + 1 << endl; // Display best vendor index
}

int main() // Define main function
{
string commodities[MAX_COLS]; // Define array for commodity data
inputCommodities(commodities); // Call function to input commodity data

int **vendorData; // Define 2D array for vendor data
vendorData = new int*[MAX_ROWS]; // Allocate memory for array
for (int i = 0; i < MAX_ROWS; i++) // Loop through each row
{
vendorData[i] = new int[MAX_COLS]; // Allocate memory for array
}

inputVendorData(vendorData); // Call function to input vendor data
displayResult(vendorData); // Call function to display best vendor

// Save data to
ofstream outfile("vendor_data.txt"); // create an output file stream object with file name "vendor_data.txt"
if (outfile.is_open()) // check if file is opened successfullyfile
{
for (int i = 0; i < MAX_COLS; i++) // loop through the commodities array
{
outfile << commodities[i] << " "; // write each commodity followed by a space to the file
}
outfile << endl; // write a new line to the file
for (int i = 0; i < MAX_ROWS; i++) // loop through the vendorData array
{
for (int j = 0; j < MAX_COLS; j++) // loop through the columns of each row of vendorData array
{
outfile << vendorData[i][j] << " "; // write each element followed by a space to the file
}
outfile << endl; // write a new line to the file
}
outfile.close(); // close the file
cout << "Data saved to file vendor_data.txt" << endl; // display a message that data has been saved to file
}
else // if file could not be opened
{
cout << "Error: could not open file" << endl; // display an error message
}

// Deallocate memory
for (int i = 0; i < MAX_ROWS; i++) // loop through the rows of vendorData array
{
delete[] vendorData[i]; // delete the memory allocated for each row of vendorData array
}
delete[] vendorData; // delete the memory allocated for the vendorData array

return 0; // exit the program with success status

}

OUTPUT:


Enter commodity #1: Laptop
Enter commodity #2: Mouse
Enter commodity #3: Headset
Enter commodity #4: Laptop Bag
Enter commodity #5: Adaptor
Enter vendor #1 price for commodity #1: 45000
Enter vendor #1 price for commodity #2: 1500
Enter vendor #1 price for commodity #3: 2500
Enter vendor #1 price for commodity #4: 1600
Enter vendor #1 price for commodity #5: 1200
Enter vendor #2 price for commodity #1: 43000
Enter vendor #2 price for commodity #2: 1200
Enter vendor #2 price for commodity #3: 3000
Enter vendor #2 price for commodity #4: 1000
Enter vendor #2 price for commodity #5: 1500
Enter vendor #3 price for commodity #1: 50000
Enter vendor #3 price for commodity #2: 2000
Enter vendor #3 price for commodity #3: 30000
Enter vendor #3 price for commodity #4: 1000
Enter vendor #3 price for commodity #5: 900
Best vendor: #2
Data saved to file vendor_data.txt

vendor_data.txt

Laptop Mouse Headset Laptop Bag Adaptor
45000 1500 2500 1600 1200
43000 1200 3000 1000 1500
50000 2000 3000 1000 900

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.