Consider the products in the Departmental Store. Each product has prodduct Id, product Name, product Unit price etc. Perform the following operations with Item details in the Departmental Store
#include <iostream>
#include <string>
using namespace std;
struct Product{
int pid;
float price;
string name;
};
void search(struct Product p[], int id, int size){
for(int i = 0; i < size; i++){
if(p[i].pid == id){
cout<<"Product id: "<<p[i].pid<<endl;
cout<<"Product name: "<<p[i].name<<endl;
cout<<"Product price: "<<p[i].price<<endl<<endl;
return;
}
else if(i + 1 == size) cout<<"Not found"<<endl;
}
}
void swap(struct Product& p1, struct Product& p2){
struct Product temp;
temp = p1;
p1 = p2;
p2 = temp;
}
void sortprice(struct Product p[], int size){
for(int i = 0; i < size; i++){
for(int j = i + 1; j < size; j++){
if(p[j].price < p[i].price) swap(p[i], p[j]);
}
}
for(int i = 0; i < size; i++){
cout<<"Product id: "<<p[i].pid<<endl;
cout<<"Product name: "<<p[i].name<<endl;
cout<<"Product price: "<<p[i].price<<endl;
}
}
void listLessthan100(struct Product p[], int size){
for(int i = 0; i < size; i++){
if(p[i].price < 100){
cout<<"Product id: "<<p[i].pid<<endl;
cout<<"Product name: "<<p[i].name<<endl;
cout<<"Product price: "<<p[i].price<<endl<<endl;
}
}
}
int main(){
struct Product product[5];
product[0] = {1, 300, "Console"};
product[1] = {2, 299, "Screen"};
product[2] = {3, 305, "RTX 3080"};
product[3] = {4, 100, "CPU"};
product[4] = {5, 84, "Cooler"};
cout<<"Products available\n";
for(int i = 0; i < 5; i++){
cout<<"Product id: "<<product[i].pid<<endl;
cout<<"Product name: "<<product[i].name<<endl;
cout<<"Product price: "<<product[i].price<<endl;
}
cout<<"\nEnter product id to search: ";
int pid; cin>>pid;
search(product, pid, 5);
cout<<"Products price less than 100:\n";
listLessthan100(product, 5);
cout<<"Products sorted by price: \n";
sortprice(product, 5);
return 0;
}
Comments