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

  Develop a basic temperature class

You have to develop a basic temperature class

  Catch clauses are listed is important

write a program that can be used to demonstrate that the order in which catch clauses are listed is important

  Function that returns the height of a binary tree

Write a function called getHeight() that returns the height of a given binary tree - CptS 122 Write a function called countSmallerThan() that returns the number of nodes whose values are smaller than the supplied input paramter

  Implementation of classes

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

  You have in your program an arraylist which includes

you have in your program an arraylist that contains employee salaries double type in arbitrary order. you need to

  Rationalnumber class a rational number is a number that can

rationalnumber class a rational number is a number that can be represented as the quotient of two nbspintegers. for

  Make a c program that will calculate the gross pay

Write a C program that will calculate the gross pay of a set of employees. For each employee the program should prompt the user to enter the clock number, wage rate, and number of hours as shown below.

  Compare and contrast the object-oriented approach to design

compare and contrast the object-oriented approach to design and the traditional approach.do you believe that there are

  Performs a binary search instead of a linear search.

Modify the following program so it performs a binary search instead of a linear search. Use the selection sort algorithm to sort the array before the binary search is performed.

  Write a program that accepts as input, without prompts

Write a program that accepts as input, without prompts, a series of short values, terminated by end of file. The program outputs one line:

  Main program should instantiate an instance

The main program should instantiate an instance of the class Rectangle and then make calls to the member functions to input the data and output the results

  Program to generate a star-based histogram for a quiz

Write a C++ program to generate a star-based histogram for a quiz (maximum score possible is 5) given to a class of N students, where N is an input from the user. Use arrays for computation.

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