Every FortnightTimes of India News paper releases a summary of the weather report .
It prints the date and the temperature on that date in three categories.
If the temperature is below or equal to 20 Celsius it comes under cool category, if the temperature is between 20 and 30 it falls under the warm and if the temperature is greater than 30 celsius then it comes under Dry
Write a program to display the date and its corresponding temperature for each category .
Create a two dimensional array to store the date and its temperature for each category.
Prompt the user to enter the temperature sequentially for 15 days.
Answers
Program in C++:
#include<iostream>
using namespace std;
int main()
{
int dd, mm, yyyy, arr[15][2];
cout<<"Enter date in dd/mm/yyyy format"<<endl;
cout<<"Enter day(dd): ";
cin>>dd;
if(dd < 1 || dd > 31)
{
cout<<"Invalid input";
return 0;
}
cout<<"Enter month(mm): ";
cin>>mm;
if(mm < 1 || mm > 12)
{
cout<<"Invalid input";
return 0;
}
if(mm == 2 && dd > 28)
{
cout<<"Invalid Input";
return 0;
}
if(dd == 31 && (mm != 1 && mm != 3 && mm != 5 && mm != 7 && mm != 8 && mm != 10 && mm != 12))
{
cout<<"Invalid Input";
return 0;
}
cout<<"Enter year(yyyy): ";
cin>>yyyy;
if(yyyy < 1500 || yyyy > 2500)
{
cout<<"Invalid input";
return 0;
}
cout<<endl;
for(int i = 0; i < 15; i++)
{
if((dd == 28 && mm == 2) || (dd == 30 && (mm == 4 || mm == 6 || mm == 9 || mm == 11)) || (dd == 31 && (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)))
{
arr[i][0] = dd;
dd = 1;
}
else
{
arr[i][0] = dd++;
}
}
int m = mm;
int y = yyyy;
for(int i = 0; i < 15; i++)
{
if((arr[i][0] == 1 && arr[i-1][0] == 31) || (arr[i][0] == 1 && arr[i-1][0] == 28) || (arr[i][0] == 1 && arr[i-1][0] == 30))
{
if(mm == 12)
{
m = 1;
y = yyyy + 1;
cout<<"Enter temperature on "<<arr[i][0]<<"/"<<m<<"/"<<y<<" : ";
}
else
{
m = mm + 1;
cout<<"Enter temperature on "<<arr[i][0]<<"/"<<m<<"/"<<y<<" : ";
}
cin>>arr[i][1];
}
else
{
cout<<"Enter temperature on "<<arr[i][0]<<"/"<<m<<"/"<<y<<" : ";
cin>>arr[i][1];
}
}
cout<<endl;
m = mm;
y = yyyy;
for(int i = 0; i < 15; i++)
{
if(arr[i][1] <= 20)
{
if((arr[i][0] == 1 && arr[i-1][0] == 31) || (arr[i][0] == 1 && arr[i-1][0] == 28) || (arr[i][0] == 1 && arr[i-1][0] == 30))
{
if(mm == 12)
{
m = 1;
y = yyyy + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Cool"<<endl;
}
else
{
m = mm + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Cool"<<endl;
}
}
else
{
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Cool"<<endl;
}
}
else if(arr[i][1] > 20 && arr[i][1] <= 30)
{
if((arr[i][0] == 1 && arr[i-1][0] == 31) || (arr[i][0] == 1 && arr[i-1][0] == 28) || (arr[i][0] == 1 && arr[i-1][0] == 30))
{
if(mm == 12)
{
m = 1;
y = yyyy + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Warm"<<endl;
}
else
{
m = mm + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Warm"<<endl;
}
}
else
{
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Warm"<<endl;
}
}
else
{
if((arr[i][0] == 1 && arr[i-1][0] == 31) || (arr[i][0] == 1 && arr[i-1][0] == 28) || (arr[i][0] == 1 && arr[i-1][0] == 30))
{
if(mm == 12)
{
m = 1;
y = yyyy + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Dry"<<endl;
}
else
{
m = mm + 1;
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Dry"<<endl;
}
}
else
{
cout<<arr[i][0]<<"/"<<m<<"/"<<y<<" : "<<"Dry"<<endl;
}
}
}
return 0;
}
Output:
Enter date in dd/mm/yyyy format
Enter day(dd): 28
Enter month(mm): 12
Enter year(yyyy): 2020
Enter temperature on 28/12/2020 : 12
Enter temperature on 29/12/2020 : 23
Enter temperature on 30/12/2020 : 34
Enter temperature on 31/12/2020 : 23
Enter temperature on 1/1/2021 : 11
Enter temperature on 2/1/2021 : 33
Enter temperature on 3/1/2021 : 2
Enter temperature on 4/1/2021 : 12
Enter temperature on 5/1/2021 : 34
Enter temperature on 6/1/2021 : 22
Enter temperature on 7/1/2021 : 21
Enter temperature on 8/1/2021 : 19
Enter temperature on 9/1/2021 : 17
Enter temperature on 10/1/2021 : 23
Enter temperature on 11/1/2021 : 21
28/12/2020 : Cool
29/12/2020 : Warm
30/12/2020 : Dry
31/12/2020 : Warm
1/1/2021 : Cool
2/1/2021 : Dry
3/1/2021 : Cool
4/1/2021 : Cool
5/1/2021 : Dry
6/1/2021 : Warm
7/1/2021 : Warm
8/1/2021 : Cool
9/1/2021 : Cool
10/1/2021 : Warm
11/1/2021 : Warm