•Write a program which control value of the robot. The charging
min value is zero and max value is 100. When the charger is not
plugged, the robot is in discharging state and the battery
decreases. When charger is attached, the state is changed to
charging and the value increase. Once it reaches the max
limit, the state changes to battery full.
EXPLANATION:-
First checking if robot's charging plug is plugged or not if charging is plugged a function is called that will increase the charging by 1 and at each step it will also check if the charger is plugged or not , if charger is unplugged then another function that is made for discharging of battery is called and it will discharge the battery by one and if in between of discharging , charger is plugged then again charging function will be called like this it will go on.
CODE:-
#include<bits/stdc++.h>
using namespace std;
int current_charging_value = 0;
void charging_state(int charging);
void discharging_state(int charging)
{
//this function is invoked when robot is not
charging
while(charging==0)
{
if(current_charging_value==0)//we'll stop discharging when it's
charging value reaches to min
{
cout<<"Battery discharged";
break;
}
current_charging_value-=1;//decrease in charging value
}
if(charging==1)
charging_state(charging); // if charger is plugged
then charging is started
}
void charging_state(int charging)
{
//this function is invoked when robot is
charging
while(charging==1)
{
if(current_charging_value==100)//we'll stop charging
when it's charging value reaches to max
{
cout<<"Fully charged";
break;
}
current_charging_value+=1;//increase in charging value
}
if(charging == 0)
discharging_state(charging); // if charger is
unplugged then discharge is started
}
int main(){
int charging = 0;
char flg;
cout<<"Is the charger plugged?";
cout<<"Answer in Y/N ";
cin>>flg;
if(flg =='Y')
{
charging = 1;//change in charging value if charger is
pluged
}
else
{
charging = 0;
}
discharging_state(charging);//if charging is 0 than the robot is in
discharge state
charging_state(charging);//if charging is 1 than the robot is in
charging state
}
OUTPUT:-
Get Answers For Free
Most questions answered within 1 hours.