In: Computer Science
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
//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 :