Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/jennifer/basic recruit training #323

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Recruit-Training/Basic-Recruit-Training/person/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ void compareAnswers(const std::string myAnswer, const std::string correctAnswer)
// Swaps a & b.
void mySwap(int& a, int& b)
{
int temp = a; // Create a temporary variable to hold the value of 'a'.
a = b; // Assign the value of 'b' to 'a'.
b = temp; // Assign the value of the temporary variable (initial 'a') to 'b'.
}

// Increment the int that x is pointing to by one.
void myIncrement(int* x)
{
(*x)++; // Increment the value pointed to by 'x' by one.
}

// Uncomment this when person.cpp has been created.
/* void personFunctions()
void personFunctions()
{
Person Bill;
Bill.setName("Bill");
Expand Down Expand Up @@ -58,16 +63,17 @@ void myIncrement(int* x)
<< myBirthdayAnswer.str() << std::endl;
compareAnswers(myBirthdayAnswer.str(), correctBirthdayAnswer);
delete Joe;
} */
}

int main()
{
int a = 5;
int b = 6;

int *pointer = &a;
// Pass a & b into mySwap here
mySwap(a,b);
// Pass in a to myIncrement here

myIncrement(pointer);
std::cout << "Checking Swap and Increment: " << std::endl;

std::stringstream swapIncrementAnswer;
Expand All @@ -79,7 +85,8 @@ int main()
// Pass a and b into mySwap
// Pass in a to myIncrement

//personFunctions(); // Uncomment this once you have completed the definitions of the Person class.
personFunctions(); // Uncomment this once you have completed the definitions of the Person class.

return 0;
}

59 changes: 59 additions & 0 deletions Recruit-Training/Basic-Recruit-Training/person/person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "person.h"
#include <iostream>

// Default constructor
Person::Person() {
name_ = "";
age_ = new int(0);
}

// Destructor
Person::~Person() {
delete age_;
}

// Parameterized constructor
Person::Person(const std::string& name, int age) {
name_ = name;
age_ = new int(age);
}

// Getter for age
int Person::getAge() const {
return *age_;
}

// Getter for name
const std::string& Person::getName() const {
return name_;
}

// Setter for name
void Person::setName(const std::string& newName) {
name_ = newName;
}

// Setter for age
void Person::setAge(int newAge) {
*age_ = newAge;
}

// Print out the name and age of the person
void Person::printInfo() const {
std::cout << "Name: " << name_ << ", Age: " << *age_ << std::endl;
}

// Print the combined age of everyone pointed to by personArray
int Person::combinedAge(Person** personArray, int size) {
int totalAge = 0;
for (int i = 0; i < size; i++) {
totalAge += personArray[i]->getAge();
}
return totalAge;
}

// Increases the Person's Age by 1
void Person::birthday(Person& x) {
int currentAge = x.getAge();
x.setAge(currentAge + 1);
}
25 changes: 18 additions & 7 deletions Recruit-Training/Basic-Recruit-Training/pointers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,36 @@ using namespace std;

void foo(int *a, int b);
void bar(int *a, int b);
void mySwap(int *p1, int *p2);

int main()
{
int x = 3;
int y = 4;
int *p = &x;
int *p2 = &y;

mySwap(p, p2);

//TODO in the line below predict what what is going to be output
cout << "Predicted value of p: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of p: " /* << before mySwap: 0x61ff04 (address of X) after mySwap: 0x61ff04 (address of X) */ << endl;
cout << "Actual value of p: " << p << endl;
cout << "Predicted value &x: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value &x: " /* << before mySwap: 0x61ff04 (address of X) after mySwap: 0x61ff04 (address of X) */ << endl;
cout << "Actual value &x: " << &x << endl;
cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of *p: " /* << before mySwap: 3 after mySwap: 4*/ << endl;
cout << "Actual value of *p: " << *p << endl;

foo(p, x);

cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of *p: " /* << before mySwap: 42 after mySwap: 42 */ << endl;
cout << "Actual value of *p: " << *p << endl;
cout << "Predicted value of x: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of x: " /* << before mySwap: 42 after mySwap: 42*/ << endl;
cout << "Actual value of x: " << x << endl;
foo(p, y);

cout << "Predicted value of *p: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of *p: " /* << before mySwap: 42 after mySwap: 42 */ << endl;
cout << "Actual value of *p: " << *p << endl;
cout << "Predicted value of y: " /* << PLACE YOUR PREDICTION HERE*/ << endl;
cout << "Predicted value of y: " /* << before mySwap: 4 after mySwap: 3*/ << endl;
cout << "Actual value of y: " << y << endl;

//start writing mySwap here
Expand All @@ -55,3 +59,10 @@ void var(int *a, int b)
*a = 365;
b = 912;
}

void mySwap(int *p1, int *p2)
{
int temp = *p1; // Store the value pointed to by p1 in a temporary variable
*p1 = *p2; // Update the value pointed to by p1 with the value pointed to by p2
*p2 = temp; // Update the value pointed to by p2 with the value stored in the temporary variable
}
1 change: 1 addition & 0 deletions jennifer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Solar Car!