100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

COS1512 ASSIGNMENT 2, SEMESTER 1, 2021, Unique number: 777008

Rating
-
Sold
4
Pages
17
Uploaded on
22-05-2021
Written in
2020/2021

This document contains fully detailed solutions for COS1512 assignment 2, semester 1, 2021, unique code :











Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
May 22, 2021
Number of pages
17
Written in
2020/2021
Type
Other
Person
Unknown

Subjects

  • 777008

Content preview

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

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
reagantarbots-hepomarlpayne University of South Africa (Unisa)
View profile
Follow You need to be logged in order to follow users or courses
Sold
86
Member since
5 year
Number of followers
73
Documents
1
Last sold
1 year ago

3,5

10 reviews

5
1
4
4
3
4
2
1
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions