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

C++ show work Assume that you have the following definition of a struct: struct

ID: 3673629 • Letter: C

Question

C++ show work

Assume that you have the following definition of a struct: struct partsType {string partName; int partNum; double price; int quantitiesInStock; }; Declare an array, inventory, of 100 components of type partsType. Write a C++ code to initialize each component of inventory as follows: partName to null string, partNum to -1, price to 0.0, and quantitiesInStockto 0. Write a C++ code that uses a loop to output the data stored in inventory. Assume that the variable length indicates the number of elements in inventory.

Explanation / Answer

C++ program with desired output is given, comments are given in bold letters

#include<iostream>
using namespace std;

struct partsType //structure of partsType
{
   string partName;
   int partNum;
   double price;
   int quantitiesInStock;
};

int main() //main method
{
   partsType inventory[100]; // array of partsType
  
   for(int i = 0; i<100; i++) // for loop for setting the initial values
   {
       inventory[i].partName = ""; // In c++ you can not assign std::string to null because its not pointer type, it can be made empty
       inventory[i].partNum = -1;
       inventory[i].price = 0.0;
       inventory[i].quantitiesInStock = 0;
   }
      
   for(int i = 0; i<100; i++) // for loop to display data
   {
       cout<<"Component: "<<i+1<<endl;
       cout<<"Part Name: "<<inventory[i].partName<<endl;
       cout<<"Part Num: "<<inventory[i].partNum<<endl;
       cout<<"Price: "<<inventory[i].price<<endl;
       cout<<"Quantities In Stock: "<<inventory[i].quantitiesInStock<<endl;
       cout<<"*********************************"<<endl;
   }
}

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