Question

In: Computer Science

Design and write a function which will calculate and display the stock level of a product...

Design and write a function which will calculate and display the stock level of a product at the end of the day. The function will take as input the initial stock level, the amount we bought and the amount we sold.

Add lines to main which get 3 values from the user, as shown below, the pass them to the function.

Sample output:
Enter stock at start of day: 20
Enter amount of new stock received today: 10
Enter amount of stock sold: 25
Display stock amount at end of the day: 5

Solutions

Expert Solution

//Since the language is not specified , i have written the code in C++, if needed can write in java

Code in C++ :

#include <iostream>
using namespace std;

int stockLevelForCurrentDay(int initialStock,int amountBought,int amountSold) //function definition to calculate stock level
{
  
return initialStock+amountBought-amountSold; // remaining stock = current stock + stock bought - stock sold
}

int main() {
  
   int initialStock,amountBought,amountSold;// declare 3 variable to get the input from the user
  
   cout<<"Enter stock at start of day:\n";
   cin>>initialStock; //read input for initialStock
  
   cout<<"Enter amount of new stock received today:\n";
   cin>>amountBought; //read input for amountBought
  
   cout<<"Enter amount of stock sold:\n";
   cin>>amountSold; //read input for amountSold
  
   int stockLevel = stockLevelForCurrentDay(initialStock,amountBought,amountSold); //call function and store the result
  
   cout<<"Display stock amount at end of the day: "<<stockLevel; //print the result
  
   return 0;
}

Code screenshot :


Related Solutions

Design and write a function to calculate a person’s pay for a variable number of hours...
Design and write a function to calculate a person’s pay for a variable number of hours worked. ● Create a constant called HOURLY_RATE set to 24.95 ● To Consider - should this be local to the function or global? ● Ask the user to enter their number of hours worked (do this inside the function). Remember to always use meaningful variable names for variables! ● Then calculate and return their total pay Add code to main which will call this...
Write a program that will calculate and display the cost of a gasoline purchase based on...
Write a program that will calculate and display the cost of a gasoline purchase based on the brand of gasoline, the octane rating and the amount purchased. Your program should first display a menu prompting the user for the brand of gasoline.  Next prompt the user for the amount of gasoline in liters. Finally, your program should calculate and display the cost of the gasoline purchase using the formula below. Cost of Discount Gasoline = ($2.49 - (100 - Octane Rating)...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
Use the Design Recipe to write a function count_vowels, which consumes a string and returns the...
Use the Design Recipe to write a function count_vowels, which consumes a string and returns the number of vowels in that string. For these purposes, the vowels are a, e, i, o, and u, but never y. Include a Docstring! Note: Count both upper and lowercase vowels! Write 3 assert_equal statements to test your function.
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Write a program to calculate the area and circumference of a cuboid. Use printf to display...
Write a program to calculate the area and circumference of a cuboid. Use printf to display 2 digits after decimal point in all the results Program should be in java language.
Let's assume that, an application program calls a High Level Language library function to display a...
Let's assume that, an application program calls a High Level Language library function to display a string with red color in the monitor. Explain the steps executing the operation (from function call to low hardware level).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT