100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COS1512 ASSIGNMENT 2, SEMESTER 1, 2021, Unique number: 777008 R55,00
Add to cart

Other

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

 4 purchases

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

Preview 3 out of 17  pages

  • May 22, 2021
  • 17
  • 2020/2021
  • Other
  • Unknown
  • 777008
All documents for this subject (51)
avatar-seller
reagantarbots-hepomarlpayne
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller reagantarbots-hepomarlpayne. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R55,00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

68175 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 15 years now

Start selling
R55,00  4x  sold
  • (0)
Add to cart
Added