I was given a problem. In the problem I am suppose to take input current month, current year and current water level of a dam from user. There is a scale that from month march to august the water level increase by 150 feet each month and from sep to feb it decreases by 200 feet. Now I am supposed to tell in which month and year the Dam will have no water or 0 feet water level. I have made the below program using loops but I have to it with out loops/recursive function. I got the year by dividing the water level with avg_decrease in water level.You cans see the program that does what I want with loop.
#include<iostream>
using namespace std;
int main(){
int c_month, c_year, wlevel, avg_decrease;
cout<<"Enter current month number: ";
cin>>c_month;
cout<<"Enter current water level: ";
cin>>wlevel;
cout<<"Enter current year: ";
cin>>c_year;
avg_decrease = 300; //-25 each month, -300 each year
cout<<wlevel/avg_decrease<<endl;
int m = c_month, level = wlevel, y = c_year;
while(true){
if(x)
break;
for(int i =0;i<=12;i ){
if(level < 0){
x = true;
break;
}
else if(m >= 3 && m<=8){
level = level 150;
m ;
}
else{
level = level -200;
if(m == 12)
m=1;
else
m ;
}
}
y ;
}
cout<<y<<"\t"<<m<<endl;
}
I want to get the month and year in which the water level is 0 feet which is being printed in the last line without using the loops. I dont know how to implement the above program without using loops. If any one can help, it would be great. Thanks in advance
CodePudding user response:
test this and change code if my logic is wrong
#include<iostream>
using namespace std;
int main(){
int m, y, lev;
cout << "Enter current month number: ";
cin >> m;
cout << "Enter current year: ";
cin >> y;
cout << "Enter current water level: ";
cin >> lev;
int avg_year = 300; // average year decrease
int year_count = lev/avg_year; // how many entire years we will be decreasing for sure
lev = lev % avg_year; // how much level we still have after entire years have pass
if ((lev > 0) && (m <= 8) && (m >=2)) // march - aug we are adding 150 each month
{
int delta = (6 - m 2); // how much times we should add 150
lev = lev 150*delta;
m = m delta;
}
if((lev > 0) && (m == 8)) // end of aug (sep = -200)
{
m ;
lev = lev - 200;
}
if((lev > 0) && (m == 9)) // end of sep (oct = -200)
{
m ;
lev = lev - 200;
}
if((lev > 0) && (m == 10)) // end of oct (nov = -200)
{
m ;
lev = lev - 200;
}
if((lev > 0) && (m == 11)) // end of nov (dec = -200)
{
m ;
lev = lev - 200;
}
if((lev > 0) && (m == 12)) // end of dec (jan = -200)
{
m = 1;
year_count ;
lev = lev - 200;
}
/*
in case at the beginning of the program m== 1 AND Level == (801 - 899)
2 years past and we are at m == 1 with level == (201 - 299)
februarry gives -200. So at the end of m==2, level == (1 - 99)
when we do march-december we gain 100 level
so in the beginning of the next year jan level will be 101-199
and since jan takes -200 from level. It is definitely next years jan
so we are just increasing year count
*/
if((lev > 0) && (m == 1)) // enf of jan (feb = -200)
{
lev = lev - 200;
if(lev <= 0)
m ;
else
year_count ;
}
cout << (y year_count) << "\t" << m << endl;
}
UPD this is bellow I think the right solution as I would do it with all the loops. I doubt it can be achieved just by plain code with no loops or recursion
#include<iostream>
using namespace std;
int level_func(int m, int y, int lev)
{
int ar[] = { -200, -200, 150, 150, 150, 150, 150, 150, -200, -200, -200, -200 };
int m_count = 0;
while (lev > 0)
{
m ;
m_count ;
if (m > 12)
m = 1;
lev = lev ar[m - 1];
}
return m_count;
}
int main() {
int m, y, lev;
cout << "Enter current month number: ";
cin >> m;
cout << "Enter current year: ";
cin >> y;
cout << "Enter current water level: ";
cin >> lev;
int month_count = level_func(m, y, lev);
y = y month_count / 12;
m = m month_count % 12;
if (m > 12)
{
y ;
m = m - 12;
}
cout << y << "\t" << m << endl;
}
CodePudding user response:
I worked on the solution myself and found a very efficient way to do it. I calculated total months required and got the total years from the month.The code is below
#include<iostream>
using namespace std;
int main(){
int exact_months,year = 0;
int c_month, c_year, wlevel, avg_decrease;
cout<<"Enter current month number: ";
cin>>c_month;
cout<<"Enter current water level: ";
cin>>wlevel;
cout<<"Enter current year: ";
cin>>c_year;
int mn;
exact_months = wlevel/25;
mn = (c_month exact_months);
if ((c_month exact_months) !=0 && c_month != 1)
year = 1;
year = exact_months/12 year;
cout<<"Date: "<<year c_year<<":"<<mn;
}