Professional Documents
Culture Documents
C++ Mooc Answers
C++ Mooc Answers
Week 1
QUE1
bool my_bool = true;
cout << my_bool << endl;
my_bool = test;
cout << my_bool << endl;
QUE2
int my_int = first_num;
cout << my_int << endl;
my_int = second_num;
cout << my_int << endl;
my_int = third_num;
cout << my_int << endl;
QUE3
cout << line1 << endl;
cout << line2 << endl;
QUE4
double my_double = 3.14;
cout << my_double << endl;
my_double = number;
cout << my_double << endl;
QUE5
cout << "Okay, it is time to ";
cout << "learn about operators." << endl;
Week 2
QUE1
int a = 4;
int b = 9;
QUE2
cout << boolalpha << ( 5 == 4 ) << endl;
cout << boolalpha << ( ! (5 < 8 && 6 >= 2) ) << endl;
cout << boolalpha << ( 5 > 8 || 6 < 2 );
QUE3
string a = "Hello ";
string b = "world";
cout << (a + b) << endl;
QUE4
double seven = 7.0;
double two = 2.0;
cout << ( seven / two ) << endl;
QUE5
int sum = stoi(num1) + stoi(num2);
cout << ( num1 + " + " + num2 + " = " + to_string(sum) ) << endl;
Week 3
Q1
if ((x >= 0) && (x <= 25)) {
cout << to_string(x) + " is between 0 and 25 or 75 and 100";
}
if ((x >= 75) && (x <= 100)) {
cout << to_string(x) + " is between 0 and 25 or 75 and 100";
}
Q2
if (x % 5 == 0) {
cout << to_string(x) + " is divisible by 5";
}
else {
cout << to_string(x) + " is not divisible by 5";
}
Q3
Q4
Q5
Q1
Q2
Q3
int sum = 0;
if (a == b) {
sum = a;
}
Q4
Q5
WEEK 1
Q1
Q2
oceans.push_back("Pacific");
oceans.push_back("Atlantic");
oceans.push_back("Indian");
oceans.push_back("Arctic");
oceans.push_back("Southern");
oceans.push_back("Place Holder");
oceans.at(5) = "Delete";
oceans.pop_back();
Q3
reverse.push_back(argv[3]);
reverse.push_back(argv[2]);
reverse.push_back(argv[1]);
reverse.erase(reverse.begin());
reverse.erase(reverse.begin());
reverse.erase(reverse.begin());
Q4
nums[0][0] = a;
nums[0][1] = b;
nums[0][2] = a + b;
nums[1][0] = c;
nums[1][1] = d;
nums[1][2] = c + d;
nums[2][0] = a + c;
nums[2][1] = b + d;
nums[2][2] = ((a+b) + (c+d) + (a+c) + (b+d)); //or 2(a+b+c+d)
Q5
numList.push_back(input);
WEEK 2
Q1
else {
cout << "Neither number is larger" << endl;
}
Q2
p1 = &a;
p2 = &b;
WEEK 3
Q1
char first = my_string.at(0);
char last = my_string.at(my_string.length()-1);
cout << first << " is the first character and " << last << " is the last character"
<< endl;
Q2
Q3
Q4
WEEK 4
Q1
try {
ifstream file;
string read;
int lines = 0;
int chars = 0;
file.open(path);
if (!file) {
throw runtime_error("File failed to open.");
}
while (getline(file, read)) {
lines++;
chars += read.length();
}
file.close();
cout << lines << " line(s)" << endl;
cout << chars << " character(s)";
}
catch (exception& e) {
cerr << e.what() << endl;
}
Q2
vector<string> data;
try {
ifstream file;
string read;
file.open(path);
if (!file) {
throw runtime_error("File failed to open.");
}
while (getline(file, read)) {
stringstream ss(read);
while (getline(ss, read, ',')) {
data.push_back(read);
}
}
file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
int col1 = 0;
int col2 = 0;
int col3 = 0;
int col4 = 0;
for (int i = 0; i < data.size(); i++) {
if (i == 0 || i == 4 | i == 8) {
col1 += stoi(data.at(i));
}
if (i == 1 || i == 5 | i == 9) {
col2 += stoi(data.at(i));
}
if (i == 2 || i == 6 | i == 10) {
col3 += stoi(data.at(i));
}
if (i == 3 || i == 7 | i == 11) {
col4 += stoi(data.at(i));
}
}
Q3
vector<string> data;
try {
ifstream file;
string read;
file.open(path);
if (!file) {
throw runtime_error("File failed to open.");
}
while (getline(file, read)) {
stringstream ss(read);
while (getline(ss, read)) {
data.push_back(read);
}
}
file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
Q4
vector<string> data;
try {
ifstream file;
string read;
file.open(path);
if (!file) {
throw runtime_error("File failed to open.");
}
while (getline(file, read)) {
stringstream ss(read);
while (getline(ss, read, '\t')) {
data.push_back(read);
}
}
file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
int max = 0;
string person;
cout << "The oldest person is " << person << ".";
Q5
vector<string> data;
try {
ifstream file;
string read;
file.open(path);
if (!file) {
throw runtime_error("File failed to open.");
}
while (getline(file, read)) {
stringstream ss(read);
while (getline(ss, read, ',')) {
data.push_back(read);
}
}
file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
string cities;
cout << "The following cities are in the Southern Hemisphere: ";
cities.pop_back();
cities.pop_back();
cities += ".";
WEEK 1
Q1
Q3
Q4
bool IsPalindrome(string x) {
bool palindrome = false;
string y;
for (int i = x.length() - 1; i >= 0; i--) {
y += x.at(i);
}
if (x == y) {
palindrome = true;
}
return palindrome;
}
Q5
bool IsPalindrome(string x) {
bool palindrome = false;
if (x == (Reverse(x))) {
palindrome = true;
}
return palindrome;
}
WEEK 2
Q1
int RecursiveSum(int n) {
if (n == 0) {
return 0;
}
else {
return n + RecursiveSum(n - 1);
}
}
Q2
Q3
int BunnyEars(int n) {
if (n == 0) {
return 0;
}
else {
return 2 + BunnyEars(n - 1);
}
}
Q4
string ReverseString(string s) {
if (s.length() == 1) {
return s;
}
else {
return s.substr(s.length() - 1) + ReverseString(s.substr(0, s.length() - 1));
}
}
Q5
WEEK 3
Q1
class PracticeClass {
public:
string name;
};
Q2
class Cat {
public:
string breed;
string color;
string name;
Cat() {
breed = "American Shorthair";
color = "black";
name = "Kiwi";
}
};
Q3
class Superhero {
public:
string name;
string identity;
vector<string> powers;
Q4
class Observation {
public:
string date;
double temperature;
double elevation;
int penguins;
double precipitation = 0;
Q5
WEEK 4
Q1
#include <iostream>
using namespace std;
class CelestialBody {
public:
CelestialBody(string n, double diam, double dist, int m) {
name = n;
diameter = diam;
distance = dist;
moons = m;
}
double ComparedToEarth() {
double earth = 12756.3;
double relative_size = diameter / earth;
return relative_size;
}
private:
string name;
double diameter;
double distance;
int moons;
};
int main() {
return 0;
Q2
#include <iostream>
using namespace std;
class CelestialBody {
public:
CelestialBody(string n, double diam, double dist, int m) {
name = n;
diameter = diam;
distance = dist;
moons = m;
}
string CloserToSun(CelestialBody cb) {
if (distance < cb.distance) {
return name;
}
else {
return cb.name;
}
}
private:
string name;
double diameter;
double distance;
int moons;
};
int main() {
//DO NOT EDIT the code below
return 0;
Q3
#include <iostream>
using namespace std;
class Mass {
public:
Mass(double mg, double g, double kg) {
milligrams = mg;
grams = g;
kilograms = kg;
}
double TotalWeight() {
return ((milligrams / 1000) + grams + (kilograms * 1000));
}
string CompareWeight(Mass m) {
double mass1 = TotalWeight();
double mass2 = m.TotalWeight();
if (mass1 > mass2) {
return "The first object is heavier.";
}
else if (mass2 > mass1) {
return "The second object is heavier.";
}
else {
return "The two objects weigh the same.";
}
}
private:
double milligrams;
double grams;
double kilograms;
};
int main() {
return 0;
Q4
#include <iostream>
using namespace std;
class MarbleBag {
public:
MarbleBag(int r, int b, int y) {
red = r;
blue = b;
yellow = y;
}
int AddRed(int amount) {
return red += amount;
}
int AddBlue(int amount) {
return blue += amount;
}
int AddYellow(int amount) {
return yellow += amount;
}
int RedTotal() {
return red;
}
int BlueTotal() {
return blue;
}
int YellowTotal() {
return yellow;
}
int BagTotal() {
return red + blue + yellow;
}
private:
int red;
int blue;
int yellow;
};
int main() {
return 0;
Q5
#include <iostream>
using namespace std;
class Subway {
public:
Subway() {
passengers = 0;
total_fares = 0;
}
int GetPassengers() {
return passengers;
}
void Board(int p) {
if (p >= 0) {
passengers += p;
CalculateFares(p);
}
}
void Disembark(int p) {
if (p >= 0) {
if (passengers - p < 0) {
passengers = 0;
}
else {
passengers -= p;
}
}
}
double GetFares() {
return total_fares;
}
private:
const double fare = 2.40; //variable cannot be reassigned
int passengers;
double total_fares;
int main() {
Subway s;
cout << s.GetPassengers() << endl;
s.Board(23);
s.Disembark(12);
cout << s.GetPassengers() << endl;
cout << s.GetFares() << endl;
return 0;
WEEK 1
Q1
#include <iostream>
using namespace std;
class Fruit {
public:
Fruit(string n, string c) {
name = n;
color = c;
}
string GetName() {
return name;
}
string GetColor() {
return color;
}
void SetColor(string new_color) {
color = new_color;
}
private:
string name;
string color;
};
int main() {
return 0;
Q2
#include <iostream>
using namespace std;
class Watch {
public:
Watch(string mf, string mo, int d, int w, string mt) {
manufacturer = mf;
model = mo;
diameter = d;
water_resistance = w;
material = mt;
}
string GetManufacturer() {
return manufacturer;
}
string GetModel() {
return model;
}
int GetDiameter() {
return diameter;
}
int GetWaterResistance() {
return water_resistance;
}
string GetMaterial() {
return material;
}
void Summary() {
cout << "Manufacturer: " << manufacturer << endl;
cout << "Model: " << model << endl;
cout << "Diameter: " << diameter << " mm" << endl;
cout << "Water Resistance: " << water_resistance << " m" << endl;
cout << "Material: " << material << endl;
}
private:
string manufacturer;
string model;
int diameter;
int water_resistance;
string material;
};
int main() {
return 0;
Q3
#include <iostream>
using namespace std;
class Song {
public:
Song(string ar, string t, string al) {
artist = ar;
title = t;
album = al;
}
string GetArtist() {
return artist;
}
string GetTitle() {
return title;
}
string GetAlbum() {
return album;
}
int GetPlayCount() {
return play_count;
}
double GetMoneyEarned() {
return money_earned;
}
double GetPayRate() {
return pay_rate;
}
void Stats() {
cout << artist << endl;
cout << title << endl;
cout << album << endl;
cout << play_count << endl;
cout << pay_rate << endl;
cout << money_earned << endl;
}
private:
string artist;
string title;
string album;
int play_count = 0;
const double pay_rate = 0.001;
double money_earned = 0;
void UpdatePlayCount() {
play_count++;
}
void UpdateMoneyEarned() {
money_earned = play_count * pay_rate;
}
};
int main() {
return 0;
#include <iostream>
#include <iomanip>
using namespace std;
class Atm {
public:
Atm() {}
double GetMoney() {
return money;
}
private:
double money = 0;
};
int main() {
Atm my_atm;
my_atm.Deposit(-10);
my_atm.Deposit(100.02);
my_atm.Withdraw(-20);
my_atm.Withdraw(200);
my_atm.Withdraw(50.22);
cout << fixed; //prevents value from being truncated or rounded
cout << setprecision(2); //sets value to 2 decimal places
cout << my_atm.GetMoney() << endl;
return 0;
}
Q5
#include <iostream>
using namespace std;
class CardBinder {
public:
CardBinder() {}
int GetGold() {
return gold_card;
}
int GetSilver() {
return silver_card;
}
int GetTotal() {
return gold_card + silver_card;
}
private:
int gold_card = 0;
int silver_card = 0;
};
int main() {
CardBinder cb;
cb.AddGold(21);
cb.AddGold(11);
cb.AddSilver(-4);
cb.AddSilver(8);
cb.RemoveGold(12);
cb.RemoveGold(4);
cb.RemoveSilver(-2);
cb.RemoveSilver(6);
cout << cb.GetGold() << endl;
cout << cb.GetSilver() << endl;
cout << cb.GetTotal() << endl;
return 0;
WEEK 2
Q1
#include <iostream>
using namespace std;
double GetSize() {
return size;
}
double GetMass() {
return mass;
}
string GetComposition() {
return atm_composition;
}
string GetName() {
return name;
}
private:
double size;
double mass;
string atm_composition;
string name;
};
string GetHostPlanet() {
return host_planet;
}
private:
string host_planet;
};
string GetHostStar() {
return host_star;
}
private:
string host_star;
};
int main() {
return 0;
Q2
#include <iostream>
using namespace std;
class Book {
public:
Book(string t, string a, string g) {
title = t;
author = a;
genre = g;
}
string GetTitle() {
return title;
}
string GetAuthor() {
return author;
}
string GetGenre() {
return genre;
}
private:
string title;
string author;
string genre;
};
string GetWebsite() {
return website;
}
int GetWordCount() {
return word_count;
}
int GetPageViews() {
return page_views;
}
private:
string website;
int word_count;
int page_views;
};
int main() {
return 0;
Q3
#include <iostream>
using namespace std;
class Parent1 {
public:
string Identify() {
return "This function is called from Parent1";
}
};
string Identify2() {
return Parent2::Identify();
}
string Identify3() {
return Parent1::Identify();
}
};
int main() {
Child c;
cout << c.Identify() << endl;
cout << c.Identify2() << endl;
cout << c.Identify3() << endl;
return 0;
Q4
#include <iostream>
using namespace std;
class PiggyBank {
public:
PiggyBank(int o, int f, int tn, int tw) {
ones = o;
fives = f;
tens = tn;
twenties = tw;
}
int GetOnes() {
return ones;
}
int GetFives() {
return fives;
}
int GetTens() {
return tens;
}
int GetTwenties() {
return twenties;
}
private:
int ones;
int fives;
int tens;
int twenties;
};
void DisplayBills() {
cout << "One-dollar bill: " << GetOnes() << endl;
cout << "Five-dollar bill: " << GetFives() << endl;
cout << "Ten-dollar bill: " << GetTens() << endl;
cout << "Twenty-dollar bill: " << GetTwenties() << endl;
}
void DisplayCashTotal() {
cout << "Total amount: ";
cout << GetOnes() + GetFives() * 5 + GetTens() * 10 + GetTwenties() * 20;
cout << endl;
}
};
int main() {
return 0;
Q5
#include <iostream>
using namespace std;
class Person {
public:
Person(string n, string a) {
name = n;
address = a;
}
string GetName() {
return name;
}
string GetAddress() {
return address;
}
string Info() {
return name + " lives at " + address + ".\n";
}
private:
string name;
string address;
};
//DO NOT EDIT code above this line
int GetAccountNumber() {
return account_number;
}
double GetBalance() {
return balance;
}
int GetCreditLimit() {
return credit_limit;
}
private:
int account_number;
double balance;
int credit_limit;
};
double GetRewards() {
return rewards;
}
private:
double cash_back;
double rewards;
};
int main() {
return 0;
WEEK 3
Q1
#include <iostream>
#include <vector>
using namespace std;
//DO NOT EDIT/////////////////
class VectorAddThree { //
public: //
vector<int> Add() { //
nums.push_back(num1); //
nums.push_back(num2); //
nums.push_back(num3); //
return nums; //
} //
//
protected: //
int num1 = 1; //
int num2 = 2; //
int num3 = 3; //
//
private: //
vector<int> nums; //
}; //
//////////////////////////////
protected:
int num4 = 4;
int num5 = 5;
int num6 = 6;
private:
vector<int> nums;
};
int main() {
VectorAddSix v6;
for (int i : v6.Add()) {
cout << i << endl;
}
return 0;
Q2
#include <iostream>
#include <vector>
using namespace std;
private:
double speed;
};
int main() {
Airplane a(550.0);
a.Distance(2);
a.Distance(3.15);
a.Distance(10.63);
Airplane a2(228.5);
a2.Distance(2);
a2.Distance(3.15);
a2.Distance(10.63);
return 0;
Q3
#include <iostream>
#include <vector>
using namespace std;
private:
double Celsius(double temp) {
return (temp - 32) / 1.8;
}
int main() {
Conversion c;
cout << c.ConvertTo('c', 212.0) << endl;
cout << c.ConvertTo('C', 78.0) << endl;
cout << c.ConvertTo('h', 23.0) << endl;
cout << c.ConvertTo('F', 0.0) << endl;
cout << c.ConvertTo('f', 29.0) <<endl;
cout << c.ConvertTo('3', 112.0) << endl;
return 0;
Q4
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
class Median {
public:
double CalculateMedian(int n1, int n2) {
return (double) (n1 + n2) / 2;
}
double CalculateMedian(int n1, int n2, int n3, int n4, int n5) {
vector<int> nums = {n1, n2, n3, n4, n5};
sort(nums.begin(), nums.end());
return nums.at(2);
}
};
int main() {
Median m;
cout << m.CalculateMedian(3, 5, 1, 4, 2) << endl;
cout << m.CalculateMedian(8, 6, 4, 2) << endl;
cout << m.CalculateMedian(9, 3, 7) << endl;
cout << m.CalculateMedian(5, 2) << endl;
Median m2;
cout << m2.CalculateMedian(24, 22, 21, 23, 20) << endl;
cout << m2.CalculateMedian(12, 18, 9, 3) << endl;
cout << m2.CalculateMedian(0, 1, 0) << endl;
cout << m2.CalculateMedian(5, 3) << endl;
return 0;
Q5
#include <iostream>
#include <vector>
using namespace std;
string SubstituteChar() {
string sub;
if (input_string.length() < 5) {
return "Not enough characters";
}
else {
for (int i = 0; i < input_string.length(); i++) {
if (i == 4) {
sub += "HELLO";
}
else {
sub += input_string.at(i);
}
}
}
return sub;
}
private:
string input_string;
};
int main() {
Substitute s1("dog");
cout << s1.SubstituteChar() << endl;
Substitute s2("string");
cout << s2.SubstituteChar() << endl;
Words w("string");
cout << w.SubstituteChar() << endl;
return 0;
}
WEEK 4
Q1
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
using namespace std;
class Hello {
public:
void PrintHello() {
cout << "Hello" << endl;
}
};
#endif
Q2
#include <iostream>
using namespace std;
int main() {
cout << "Winter is the " << season1 << "st season of the year." << endl;
cout << "Spring is the " << season2 << "nd season of the year." << endl;
cout << "Summer is the " << season3 << "rd season of the year." << endl;
cout << "Fall is the " << season4 << "th season of the year." << endl;
return 0;
Q3
#include <iostream>
using namespace std;
struct Student {
string name = "Amy";
int age = 18;
string major = "Physics";
};
int main() {
Student amy;
cout << amy.name << endl;
cout << amy.age << endl;
cout << amy.major << endl;
Student adam;
adam.name = "Adam";
adam.age = 19;
adam.major = "Computer Science";
cout << adam.name << endl;
cout << adam.age << endl;
cout << adam.major << endl;
return 0;
Q4
#include <iostream>
using namespace std;
class Person {
public:
static string name;
int main() {
return 0;
}
//DO NOT EDIT code above this line
Q5
#include <iostream>
using namespace std;
int main() {
return 0;