COS1512 ASSIGNMENT 2 SEMESTER 1 2021
UNIQUE NEMBER: 777008
Question 1
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int calcDogsAge(int age) { //conventional method
return age * 7;
}
double calcDogsAge(double age) { //empirical equation
return 16 * log(age) + 31;
}
int main() {
cout << "Using the conventional method : " << calcDogsAge(10) << endl;
cout << "Using the empirical equation : "<< calcDogsAge(10.0) << endl;
return 0;
}
Output:
, Question 2
// C++ program to validate the user birth date and display how old he will be this year, and whether he
has been born in a leap year or not.
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;
int main()
{
int day, month, year;
// input the day, month and year of user birth date
cout<<"Enter your birth date(day month year): ";
cin>>day>>month>>year;
// validate year > 0
assert(year > 0);
// validate month is between [1, 12]
assert(month >= 1 && month <= 12);
// validate day is valid
// for months 1, 3, 5, 7, 8, 10, 12, day should be between [1, 31]
// for months 4, 6, 9, 11, day should be between [1, 30]
// for month 2, day should be between [1, 28] if year is not leap and between [1, 29] if year is leap
assert(((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||
month == 12) && (day >= 1 && day <= 31))
|| ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
|| ((year%400 == 0 || (year%4 ==0 && year%100 != 0) && (day >= 1 && day <= 29)) || (day >= 1 && day
<= 28)));
// get the current time and date as number of seconds passed since Jan 1, 1970
time_t now = time(0);
// get the pointer to object of struct tm to get the individual components of time
tm *today = localtime(&now);
// get the current year
int current_year = today->tm_year + 1900; // today->year returns number of years since 1900
// display the age the user will turn in this year
cout<<"You will be "<<(current_year - year)<<" years old this year"<<endl;
// display if the user was born in leap year or not
, if((year%400 == 0) || (year%4 == 0 && year%100 != 0))
cout<<"You were born in leap year"<<endl;
else
cout<<"You were not born in leap year"<<endl;
return 0;
}
// end of program
Output:
Question 3
UNIQUE NEMBER: 777008
Question 1
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int calcDogsAge(int age) { //conventional method
return age * 7;
}
double calcDogsAge(double age) { //empirical equation
return 16 * log(age) + 31;
}
int main() {
cout << "Using the conventional method : " << calcDogsAge(10) << endl;
cout << "Using the empirical equation : "<< calcDogsAge(10.0) << endl;
return 0;
}
Output:
, Question 2
// C++ program to validate the user birth date and display how old he will be this year, and whether he
has been born in a leap year or not.
#include <iostream>
#include <ctime>
#include <cassert>
using namespace std;
int main()
{
int day, month, year;
// input the day, month and year of user birth date
cout<<"Enter your birth date(day month year): ";
cin>>day>>month>>year;
// validate year > 0
assert(year > 0);
// validate month is between [1, 12]
assert(month >= 1 && month <= 12);
// validate day is valid
// for months 1, 3, 5, 7, 8, 10, 12, day should be between [1, 31]
// for months 4, 6, 9, 11, day should be between [1, 30]
// for month 2, day should be between [1, 28] if year is not leap and between [1, 29] if year is leap
assert(((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||
month == 12) && (day >= 1 && day <= 31))
|| ((month == 4 || month == 6 || month == 9 || month == 11) && (day >= 1 && day <= 30))
|| ((year%400 == 0 || (year%4 ==0 && year%100 != 0) && (day >= 1 && day <= 29)) || (day >= 1 && day
<= 28)));
// get the current time and date as number of seconds passed since Jan 1, 1970
time_t now = time(0);
// get the pointer to object of struct tm to get the individual components of time
tm *today = localtime(&now);
// get the current year
int current_year = today->tm_year + 1900; // today->year returns number of years since 1900
// display the age the user will turn in this year
cout<<"You will be "<<(current_year - year)<<" years old this year"<<endl;
// display if the user was born in leap year or not
, if((year%400 == 0) || (year%4 == 0 && year%100 != 0))
cout<<"You were born in leap year"<<endl;
else
cout<<"You were not born in leap year"<<endl;
return 0;
}
// end of program
Output:
Question 3