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

C++ Problem a. Create a text file of 15+ products including product Names, unit

ID: 3733357 • Letter: C

Question

C++ Problem

a. Create a text file of 15+ products including product Names, unit prices, quantities sold, and sales of each product in your computer file. Then input the file into an array of structure ADT to your program.

b. You need to print out the unsorted file.

c. Sort the above data by sorting on the product Names. You may use either bubble sort or selection sort in ascending or descending order on product Names. The arguments of call function must include arrays of structure.

d. You may use C-String function Strcmp (,) to compare two product Names and use Strcpy (,) together to do swapping for sorting.

e. In the program algorithm, you must use “->” structure pointer operator.

f. Print out the sorted array: product Names, unit prices, quantities sold, and the sales of each product including the total units sold and the total sales at the bottom of the sorted data..

g. The file path in the desktop can be located by file property: “C:\Users\yourUsersName\Desktop\filename.txt”

Explanation / Answer

/ print these values in the Produt.txt file and put it some directory then change the path in programme

// Like D:\Project\Product.txt

JetSki 60 100 6000
BubbleWrap 70 90 6300
Onesies 80 100 80000
Jacuzzi 50 70 3500
CrockPot 65 100 6500
Flunutter 40 80 3200
Breathal 55 70 3850
Zamboni 60 80 4800
Chapstick 70 90 6300
Kleenex 30 80 2400
PingPong 35 100 3500
Popsicle 20 50 1000
QTips 80 80 6400
Rollerblad 90 50 4500
ScotchTape 15 50 7500

// Code

/******************************************************************************

Online C++ Compiler.

Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

#include <fstream>

#include <string.h>

using namespace std;

struct Product_data{ // Structure to retrive data  

char pro_name[10]; // from txt file

int pro_unit;

int pro_quat_sold;

int pro_sales;

};

int main()

{

  

Product_data prodStructureArray[15];

  

ifstream txtdata_in;

//change the file path to your directory path

txtdata_in.open ("Product.txt");

  

if(!txtdata_in)

{

cerr << "File could not be opened" << endl;

return 1;

}

for (int counter = 0; counter < 15; counter++) {

Product_data prodStructure;   

txtdata_in >> prodStructure.pro_name;

txtdata_in >> prodStructure.pro_unit;

  

txtdata_in >> prodStructure.pro_quat_sold;

txtdata_in >> prodStructure.pro_sales;

prodStructureArray[counter] = prodStructure;

  

}

  

//closing the file

txtdata_in.close();

  

//Printing the unsorted file

printf("%-12s%-10s%-10s%s ","Name","U. Price","Q. Sold","Sales");

printf("-------------------------------------- ");

for(int i = 0; i<15; i++)

{

printf("%-12s%-10d%-10d%d",prodStructureArray[i].pro_name,

prodStructureArray[i].pro_unit,prodStructureArray[i].pro_quat_sold,

prodStructureArray[i].pro_sales);

printf(" ");

}

//Sorting by product Name assending order

int flag = 1;

for(int j= 1; j< 15 && flag; j++)

{

flag = 0;

for(int k= 0; k<14; k++)

{

//comparing there name

int res = strcmp(prodStructureArray[k].pro_name, prodStructureArray[k+1].pro_name);

if(res>0)

{

cout<<"enter";

Product_data tempStruc = prodStructureArray[k];

prodStructureArray[k] = prodStructureArray[k+1];

prodStructureArray[k+1] = tempStruc;

flag = 1;

}

}

}

int totalSale = 0;

int totalQSold = 0;

//Printing the sorted file

printf("%-12s%-10s%-10s%s ","Name","U. Price","Q. Sold","Sales");

printf("-------------------------------------- ");

for(int i = 0; i<15; i++)

{

printf("%-12s%-10d%-10d%d",prodStructureArray[i].pro_name,

prodStructureArray[i].pro_unit,prodStructureArray[i].pro_quat_sold,

prodStructureArray[i].pro_sales);

totalSale = totalSale+ prodStructureArray[i].pro_sales;

totalQSold = totalQSold +prodStructureArray[i].pro_quat_sold;

printf(" ");

}

cout<<"Total Quantity Sold = "<< totalQSold<<endl;

cout<<"Total Sales = "<<totalSale<<"$"<<endl;

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote