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

  Design a cruiseship class that is derived from ship class

SEN 970 Object-Oriented Programming in Objective-C. Demonstrate the classes in a program that has an array of ship pointers. The array elements should be initialized with the addresses of dynamically allocated Ship, CruiseShip and CargoShip objects..

  Write a c matrix multiplication program which you can

write a c matrix multiplication program which you can instrument to monitor and measure execution time as a function of

  Design a deck plan space ship in eclipse

Develop a simple interactive two-dimensional spaceship deck plan editor using OpenGL and GLUT. Your program will enable a user to create and save a deck plan of a hypothetical spaceship comprising the hull walls.

  Struct definition as the page table entry

Struct definition as the page table entry and the array that follows it as the page table. Implement the software simulation of least recently used algorithm in two C++ functions. Should RUN on UNIX.

  Type two numbers from range 20-60

Ask the user to type two numbers from range 20-60. Keep on asking until he types in the range of 20-60. Display the numbers between those two numbers in ascending order.

  Write a program which has a base process which communicates

write a program which has a base process which communicates with child processes acting as bombers that it

  Properly store the number from variable value

1. Statement will properly store the number from variable  value  into the output file object AnsFile?

  Write in c++. read an inputfile.txt

write in C++.  read an inputFile.txt which contains integers that are virutal addresses, and I am suppossed to translate that into physical addresses using a page table and a transition lookaside buffer

  Program to store lists of names and ages in parallel arrays

Write a C++ program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names.

  Write a loop to total and average the contents of the array

Given the following code, write a loop to total and average the contents of the array.

  Test a function hydroxide that takes a chemical formula

Write and test a function hydroxide that takes a chemical formula as a parameter and returns True if the chemical is a hydroxide (i.e. ends in OH).

  State private portion of class to model the item

In c++ state the private portion of a class to model the given item: a class Date for dates consisting of a month, day and year. a class PhoneNumber for a telephone numbers consisting of area code, local exchange, and number.

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