Challenge

Project 1

Project 1 [C++ Programming]

  • Using C++ to write/ store and dynamically allocate the data to find the best vendor.

Aim and task:

  • Using an array of pointers or dynamically using double pointers allocate memory for every row.
  • The menu should be listed for prompting the user input to enter the commodities and vendors.
  • Find the best vendor from whom the purchases can be made based on the number of commodities and respective prices to pay a reasonable price.
  • The data input provided by the user during runtime should be saved to a file(.dat or .txt) using stream classes.

Overview of the project:

Ramu, Raju and Shyam are working in an IT support team in an Organisation. An organisation has several departments which work on different projects. Due to covid organisations had to restructure their finances as work from home should be implemented across the organisation. There would be several workstations to be purchased to support employees' work without any hindrance and achieve the project deliverables.

As a part of their duty, each of them (Ramu, Raju, Shyam) is given responsibilities to maintain the project budget and reduce the stationary expenses made by various departments. Each department's expenses depends on the number of people working on the project, cubicles desk and cubicle chairs are allocated for each person and Workstation allocation includes Desktop/Laptop, headset and stationery items such as Notepad, Pen, Pencil, Stapler, A4 Sheets for printouts of project material etc. One or two printers would be allocated to departments based on the project size. Ramu, Raju and Shyam are required to maintain these commodities and bring down the project expense cost as the stationary items would not be required as employees would be working from home. Hence, the expenses on stationery should be transferred to the workstation expenses. Ramu is responsible for Department ADAS, Raju is the IT support contact for Department Electro, and Shyam is the IT SPOC(single point of contact ) for Department Connexion. The manager of each department has shared the number of commodities required by the IT SPOC and they had approached two vendors who had given their reasonable quotations for the commodities. However, the finance department had asked the IT support team to purchase within the given budget and reduce the expenses as discussed above.

Say there are two vendors from whom the commodities/workstations are ordered, the individual prices and desired quantities of commodities are given in the following tables.

Person/Number of Commodities to be brought

Laptop

Mouse

Headset

Laptop Bag

Adapter

Ramu

5

15

10

5

20

Raju

10

10

25

10

15

Shyam

3

5

6

3

10



Commodity/Commodity cost

Vendor1

Vendor2

Laptop

45000

43000

Mouse

1500

1200

Headset

2500

3000

Laptop Bag

1200

1000

Adapter

1600

1500

Taking the above-mentioned data as a sample, Find the best vendor from whom the purchases can be made based on the number of commodities and respective prices to pay a reasonable price.

Now, using an array of pointers or dynamically using double pointers allocate memory for every row in the following arrays:

  1. Array one ->commodities
  2. Array two ->Vendors
  3. The Result will also be a two-dimensional array or a double pointer.

Also, have a menu listed for prompting the user input to enter the commodities and vendors. The menu looks like

  1. Enter Commodities to be purchased
  2. Enter Vendor quotations
  3. Exit

The data input provided by the user during runtime should be saved to a file(.dat or .txt) using stream classes. Note that this file contains the commodities list and vendor data.

Ans->

Source code & Output :

//CODE:
#include <iostream>//header file
#include <fstream>//uses the H file extension and is considered a Developer file.
#include <string> //string header file

// `M × N` matrix for storing data
#define M 5
#define N 3
using namespace std; //declare namespace

int main()
{

string* A = new string[M * N]; // dynamically allocate memory of size `M × N`

// declarations
int Laptop[3], Mouse[3], Headset[3], Laptop_Bag[3], Adapter[3]; //Array declaration
int s;
int vendor1_total, vendor2_total;
int num[2][2];
stream outdated;
outdated.open("Project_1_sample.txt");

// Menu
start:
cout<<"Select from below menu: \n";
cout<<"1. Select to Enter Commodities to be purchased \n";
cout<<"2. Select Enter Vendor quotations \n";
cout<<"3. Select to find the best vendor (Only select when you have entered commodities and vendors data)\n";
cout<<"0. Exit\n";
cout<<endl;
cin>>s;
outdated << "Selected option" << endl;
outdated << s << endl;

// operations for selected choice from the menu

switch(s) //switch block for switching one case to another case as per the condition.
{

case 1:
cout<<"Please enter the name of commodities:";
cout<<endl;
outdated << "\nName of commodities" << endl;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < 1; j++)
{
cin>> *(A + i*N + j); // assign values to the allocated memory for commodities only
outdata << *(A + i*N + j) << endl;
}
}
outdata<<endl;
cout<<endl;
goto start; //jump statement to go start
break; //the loop iterations stop there and control returns from the loop immediately to the first statement after the loop.

case 2:
cout<<"\nPlease enter the Vendor 1 quotations (Strictly follow the sequence as per commodities entered): \n";
cout<<endl;
outdated << "\nVendor 1 quotations (Sequence: As per commodities entered)" << endl;
for (int i = 0; i < M; i++)
{
for (int j = 1; j < 2; j++)
{
cin>> *(A + i*N + j); // assign values to the allocated memory for vendor 1 quotations
outdata << *(A + i*N + j) << endl;
}
}
cout<<"\nPlease enter the Vendor 2 quotations (Strictly follow the sequence as per commodities entered): \n";
cout<<endl;
outdated << "\nVendor 2 quotations (Sequence: As per commodities entered)" << endl;
for (int i = 0; i < M; i++)
{
for (int j = 2; j < 3; j++)
{
cin>> *(A + i*N + j); // assign values to the allocated memory for vendor 2 quotations
outdata << *(A + i*N + j) << endl;
}
}
outdata<<endl;
cout<<endl;
goto start; //jump statement to go start
break; //the loop iterations stop there and control returns from the loop immediately to the first statement after the loop.

case 3:
cout<<"\nEnter the no. of laptops 'Ramu', 'Raju' and 'Shyam' want to be bought (Strictly follow the sequence as Ramu, Raju and Shyam):\n";
cout<<endl;
outdata << "\nNo. of laptops 'Ramu', 'Raju' and 'Shyam' want to be bought (Sequence: Ramu, Raju and Shyam):"<<endl;
for(int i=0; i<3; i++)
{cin>>Laptop[i]; outdated << Laptop[i] << endl;} // Taking input and at the same time writing input data into a file.

cout<<"\nEnter the no. of Mouse 'Ramu', 'Raju' and 'Shyam' want to be bought (Strictly follow the sequence as Ramu, Raju and Shyam):\n";
cout<<endl;
outdata << "\nNo. of Mouse 'Ramu', 'Raju' and 'Shyam' want to be bought (Sequence: Ramu, Raju and Shyam):"<<endl;
for(int i=0; i<3; i++)
{cin>>Mouse[i]; outdated << Mouse[i] << endl;} // Taking input and at the same time writing input data into a file.


cout<<"\nEnter the no. of Headset 'Ramu', 'Raju' and 'Shyam' want to be bought (Strictly follow the sequence as Ramu, Raju and Shyam):\n";
cout<<endl;
outdata << "\nNo. of Headset 'Ramu', 'Raju' and 'Shyam' want to be bought (Sequence: Ramu, Raju and Shyam):"<<endl;
for(int i=0; i<3; i++)
{cin>>Headset[i]; outdated << Headset[i] << endl;} // Taking input and at the same time writing input data into a file.

cout<<"\nEnter the no. of Laptop_Bag 'Ramu', 'Raju' and 'Shyam' want to be bought (Strictly follow the sequence as Ramu, Raju and Shyam):\n";
cout<<endl;
outdata << "\nNo. of Laptop_Bag 'Ramu', 'Raju' and 'Shyam' want to be bought (Sequence: Ramu, Raju and Shyam):"<<endl;
for(int i=0; i<3; i++)
{cin>>Laptop_Bag[i]; outdated << Laptop_Bag[i] << endl;} // Taking input and at the same time writing input data into a file.


cout<<"\nEnter the no. of Adapter 'Ramu', 'Raju' and 'Shyam' want to be bought (Strictly follow the sequence as Ramu, Raju and Shyam):\n";
cout<<endl;
outdata << "\nNo. of Adapter 'Ramu', 'Raju' and 'Shyam' want to be bought (Sequence: Ramu, Raju and Shyam):"<<endl;
for(int i=0; i<3; i++)
{cin>>Adapter[i]; outdated << Adapter[i] << endl;} // Taking input and at the same time writing input data into a file.

//converting strings to integers. As we have created an array of strings while taking commodity and vendor inputs
for (int i = 0; i < M; i++)
{
for (int j = 1; j < 3; j++)
{num[i][j] = stoi(*(A + i*N + j));}
}


for(int k=0; k<3; k++)
{
//0 indicates correspond to Ramu, 1 to Raju and 2 to Shyam.
if (k==0)
{
cout << "\nThe best vendor for Ramu is: ";
}
else if (k==1)
{
cout << "\nThe best vendor for Raju is: ";
}
else
{
cout << "\nThe best vendor for Shyam is: ";
}

vendor1_total= Laptop[k]*num[0][1] + Mouse[k]*num[1][1] + Headset[k]*num[2][1] + Laptop_Bag[k]*num[3][1] + Adapter[k]*num[4][1];
vendor2_total= Laptop[k]*num[0][2] + Mouse[k]*num[1][2] + Headset[k]*num[2][2] + Laptop_Bag[k]*num[3][2] + Adapter[k]*num[4][2];

if(vendor1_total<vendor2_total)
{
cout<<" Vendor 1 "<<endl; //if less than value of vendor1_total as compare to vendor2_total,its printing
}
else
{
cout<<" Vendor 2 "<<endl; // else its printing because greater value of vendor1_total as compare to vendor2_total
}

}

break; //the loop iterations stop there and control returns from the loop immediately to the first statement after the loop.

case 0:
exit(0); //if we select option 0 it accesses the exit action.

default:
cout<<"Invalid selection!\n\n"; //default other option its showing invalid selection
goto start; //jump statement to go start

}

return 0;
}

output-

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.