Implementation of the getproductid function

Assignment Help C/C++ Programming
Reference no: EM13542597

The following code uses two arrays, one to store products and another to store product IDs. The function getProductID takes as input the two arrays, the length of the arrays, and a target product to search for. It then loops through the product name array; if a match is found, it returns the corresponding product ID:

intgetProductID(int ids[], string names [],
intnumProducts, string target)
{
for (inti=0; i<numProducts; i++)
{
if (names[i] == target)
return ids[i];
}
return -1; // Not found

int main() // Sample code to test the getProductID function
{
intproductIds[] = { 4, 5, 8, 10, 13 };
string products[] = { "computer", "flash drive",
"mouse", "printer", "camera" };
cout<<getProductID(productIds, products, 5, "mouse") <<endl;
cout<<getProductID(productIds, products, 5, "camera") <<endl;
cout<<getProductID(productIds, products, 5, "laptop") <<endl;
return 0;
}

One problem with the implementation of the getProductID function is that it returns the special error code of -1 if the target name is not found. The caller might ignore the -1, or later we might actually want to have -1 as a valid product ID number. Rewrite the program so that it throws an appropriate exception when a product is not found instead of returning -1.

Reference no: EM13542597

Questions Cloud

Explain oxygen is composed of only one kind of atom : Oxygen is composed of only one kind of atom and can't be separated into simpler substances. What type of matter is it
Explain what is the percent by mass concentration : 75.0 g of ammonium sulfate is placed in a 500.0 ml flask and diluted volume. What is the percent by mass concentration of this solution? (assume the solution density is 1.00 g/ml)
Explain line spectra as opposed to continuous spectra : Atoms, such as sodium, typically give line spectra, as opposed to continuous spectra. Given that such spectra are due to changes in the energies of electrons, explain, using a suitable diagram, what this tells you about the electronic structure of..
Estimate the maximum stress that this bone can withstand : The femur is a bone in the leg whose minimum cross-sectional area is about 4.3 x 10-4 m2. Find the maximum stress that this bone can withstand
Implementation of the getproductid function : Implementation of the getProductID function is that it returns the special error code of -1 if the target name is not found
Estimate by how much would the spring stretch : A small ball is attached to one end of a spring that has an unstrained length of 0.152 m. By how much would the spring stretch if it were attached to the ceiling and the ball allowed to hang straight down, motionless
Explain the substance with the weakest interparticle forces : The substance with the weakest interparticle forces (intermolecular) is the one with a boiling point of:
Evaluate how far the cars skid before coming to rest : An automobile has a mass of 1840 kg and a velocity of 15.0 m/s. It makes a rear-end collision with a stationary car whose mass is 2110 kg. If the coefficient of kinetic friction between the wheels of the cars and the pavement is ?k = 0.565, determi..
Explain ethyl acetate liquid was the vapor really ideal : Ethyl acetate liquid (flask with ethyl acetate with the top cover with aluminium paper with small whole in the top and inserted inside hot water container) Was the vapor really "ideal". If not, how were calculations affected

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd