Saturday, 28 April 2018

Chapter 8 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 10



10. Write a function maxv() that returns the largest element of a vector argument.


#include "stdafx.h"
#include "std_lib_facilities.h"

double maxv(const vector<double>& v)
{
 //if vector only has 1 value return that
 if (v.size() == 1)
  return v[0];

 //make first value in vector max value
 double largest = v[0];

 //go through every value, if the next value is bigger
 //than the last, make that the new max value
 for (int i = 0; i < v.size(); ++i)
 {
  if (largest < v[i])
   largest = v[i];
 }

 return largest;
}

int main()
{
 vector<double> numbers = { 1000, 6, -12, 700, 56, 89, -900, 1 };

 double max = maxv(numbers);

 cout << max << endl;

 keep_window_open();

 return 0;
}

At first I tried to use the max_element() function however it didn't seem to like working with vectors, so I used the template code to draft this function. It was a lot simpler than I initially thought. Since Bjarne didn't specify a data type I went with double however you can easily change the data type yourself.

Wednesday, 25 April 2018

Chapter 8 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 9



9. Write a function that given two vector<double>s price and weight computes a value (an "index") that is the sum of all price[i]*weight[i]. Make sure to have weight.size()==price.size().


#include "stdafx.h"
#include "std_lib_facilities.h"

//read numbers into weight
vector<double> getAmount(string label)
{
 vector<double> v_amount;
 int howMany;
 double amount;

 cout << "How many items are there for " << label << ": ";
 cin >> howMany;
 for (int i = 0; i < howMany; ++i)
 {
  cout << ">>";
  cin >> amount;
  v_amount.push_back(amount);
  cout << endl;
 }
 
 return v_amount;
}

//multiply weight by price if vectors are same size
double getSum(const vector<double>& price, const vector<double>& weight)
{
 double sum = 0;

 if (price.size() == weight.size())
 {
  for (int i = 0; i < price.size(); ++i)
  {
   sum += price[i] * weight[i];
  }
 }
 else
  cout << "Sorry those vectors are not the same size.\n";

 cout << "\nSum: " << sum << endl;

 return sum;
}


int main()
{
 vector<double> weight = getAmount("weight");
 vector<double> price = getAmount("price");

 double sum = getSum(price, weight);

 keep_window_open();

 return 0;
}

Saturday, 21 April 2018

Chapter 8 // Exercise 7, 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 7



7. Read five names into a vector<string> name, then prompt the user for the ages of the people names and store the ages in a vector<double> age. Then print out the five (name[i],age[i]) pairs. Sort the names (sort(name.begin(), name.end())) and print out the (name[i], age[i]) pairs. The tricky part here is to get the age vector in the correct order to match the sorted name vector. Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name.


#include "stdafx.h"
#include "std_lib_facilities.h"
//print vectors to the screen
void print(const vector<double>& ages, const vector<string>& names)
{
 for (int i = 0; i < ages.size(); ++i)
 {
  cout << names[i] << ": " << ages[i] << endl;
 }

 cout << "--------------------------------" << endl;
}

//read five names into a vector string
vector<string> getNames(vector<string>& v)
{
 string name;

 for (int i = 0; i < 5; ++i)
 {
  cout << "Name Please: ";
  cin >> name;
  v.push_back(name);
  cout << endl;
 }

 return v;
}

//read 5 ages for names
vector<double> getAges(const vector<string>& name, vector<double>& v_age)
{
 double age;

 for (int i = 0; i < name.size(); ++i)
 {
  cout << "Age for " << name[i] << ": ";
  cin >> age;
  v_age.push_back(age);
  cout << endl;
 }

 return v_age;
}

//compare copy of name vector to new to sort ages correctly
void sortNames(vector<string> name_copy, vector<string>& name, vector<double> age_copy, vector<double>& age)
{
 sort(name.begin(), name.end());  //sort actual names

 //go through each member of sorted name
 for (int i = 0; i < name.size(); ++i)
 {
  //go through each member of original copy name
  for (int j = 0; j < name.size(); ++j)
  {
   //if sorted name matches original
   if (name[i] == name_copy[j])
   {
    //assign original age to new position to match sorted vector
    age[i] = age_copy[j];
   }
  }
 }
}


int main()
{
 //get names
 vector<string> names;
 getNames(names);

 //get ages
 vector<double> ages;
 getAges(names, ages);

 //print original vectors
 print(ages, names);

 //sort the vectors using copies to compare
 sortNames(names, names, ages, ages);

 //print vectors again to see changes
 print(ages, names);

 keep_window_open();

 return 0;
}


Chapter 8 // Exercise 8


8. Then, do that exercise again but allowing an arbitrary number of names.

The only part of this I changed was the function to get the names:

//read five names into a vector string
vector<string> getNames(vector<string>& v)
{
 string name;

 cout << "Press 'q' to stop entering names" << endl;

 while(name != "q")
 {
  cout << "Name Please: ";
  cin >> name;
  if (name == "q")
   break;
  v.push_back(name);
  cout << endl;
 }

 return v;
}

Wednesday, 18 April 2018

Chapter 8 // Exercise 5, 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 5



5. Write two functions that reverse he order of elements in a vector<int>. For example 1,3,5,7,9 becomes 9,7,5,3,1. The first reverse function should produce a new vector with the reversed sequence, leaving its original vector unchanged. The other reverse function should reverse the elements of its vector without using any other vectors (hint: swap).


#include "stdafx.h"
#include "std_lib_facilities.h"

//prints a given vector to the screen with a label
void print(string& label, const vector<int>& v)
{
 cout << label << ": " << endl;

 for (int i = 0; i < v.size(); ++i)
 {
  //if incrementor is divisible by 10, start a new line to print
  cout << v[i] << '\t';
  if (i % 10 == 0 && i != 0)
   cout << '\n';
 }

 cout << '\n';
}

//calculates fibonnaci sequence for a given amount of numbers
void fibonnaci(int first, int second, vector<int>& v, int howMany)
{
 //is vector empty?
 if (v.size() == 0)
 {
  v.push_back(first);
  v.push_back(second);

  int temp;

  //pushback numbers from sequence depending on how many we want
  //start with 3rd number
  for (int i = 1; i < howMany; ++i)
  {
   temp = v[i] + v[i - 1];
   v.push_back(temp);
  }
 }
 else
  cout << "Sorry that vector is not empty.\n";
}

//swap vectors elements creating new vector leaving original unchanged
vector<int> swap1(const vector<int>& originalV)
{
 vector<int> newV;
 for (int i = originalV.size() - 1; i >= 0; --i)
 {
  newV.push_back(originalV[i]);
 }

 return newV;
}

//swap vectors elements using swap
void swap2(vector<int>& originalV)
{
 for (int i = 0; i < originalV.size()/2; ++i)
 {
  swap(originalV[i], originalV[originalV.size() - (i + 1)]);
 }
}

int main()
{
 vector<int> fibonacciNumbers;
 vector<int> newFibonacci;

 //populate vector with sequence
 fibonnaci(1, 2, fibonacciNumbers, 10);

 //swap creating a copy and assigning to new vector
 newFibonacci = swap1(fibonacciNumbers); 

 //print the vectors
 string label = "Fibonacci Numbers";
 print(label, fibonacciNumbers);
 label = "New Fibonacci";
 print(label, newFibonacci);

 //print the original vector
 label = "Fibonacci Numbers";
 print(label, fibonacciNumbers);

 //swap using swap function and no other vectors
 swap2(fibonacciNumbers);

 //print the original vector which has now been modified
 label = "Swapped Fibonocci Numbers";
 print(label, fibonacciNumbers);

 keep_window_open();

 return 0;
}

For this exercise I decided to build upon the previous ones instead of writing numbers to pushback into random vectors. The first swap, swap1() works by taking a const reference to the original vector (so it doesn't change it) and applies those values to a new vector. The for loop starts at the end of the original vector and pushes back a number into the new one until it gets to the first value. It then returns the new vector as value.

Chapter 8 // Exercise 


6. Write versions of the functions from exercise 5, but with a vector<string>.


#include "stdafx.h"
#include "std_lib_facilities.h"

//prints a given string vector to the screen with a label
void print(string& label, const vector<string>& v)
{
 cout << label << ": " << endl;

 for (int i = 0; i < v.size(); ++i)
 {
  //if incrementor is divisible by 10, start a new line to print
  cout << v[i] << '\t';
  if (i % 10 == 0 && i != 0)
   cout << '\n';
 }

 cout << '\n';
}

//swap string vectors using another vector
vector<string> swapString1(const vector<string>& originalV)
{
 vector<string> newV;
 for (int i = originalV.size() - 1; i >= 0; --i)
 {
  newV.push_back(originalV[i]);
 }

 return newV;
}

void swapString2(vector<string>& originalV)
{
 for (int i = 0; i < originalV.size() / 2; ++i)
 {
  swap(originalV[i], originalV[originalV.size() - (i + 1)]);
 }
}

int main()
{
 vector<string> myString = { "hello", "world", "this", "is", "a", "blog", "post" };
 vector<string> newString;

 //swap creating a copy
 newString = swapString1(myString);

 //print the vectors
 string label = "My String";
 print(label, myString);
 label = "New String";
 print(label, newString);

 //swap without creating a copy
 label = "My String";
 print(label, myString);

 swapString2(myString);

 label = "My String";
 print(label, myString);

 keep_window_open();

 return 0;
}

Sunday, 15 April 2018

Chapter 8 // Exercise 3, 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Exercise 3



3. Create a vector of Fibonacci numbers and print them using the function from exercise 2. To create the vector, write a function, fibonacci(x,y,v,n), where integers x and y are ints, v is an empty vector<int>, and n is the number of elements to put into v; v[0] will be x and v[1] will be y. A Fibonacci number is one that is part of a sequence where each element is the sum of the previous ones. For example, starting with 1 and 2, we get 1,2,3,4,8,13,21,....Your fibonacci() function should make such a sequence starting with its x and y arguments.

For this one I changed the name of few things to make them more descriptive. I also decided to not make the ints references and instead allow the user to pass numbers directly as arguments. The vector is just passed by reference in fibonacci() due to the need to modify it.

#include "stdafx.h"
#include "std_lib_facilities.h"

//prints a given vector to the screen with a label
void print(string& label, const vector<int>& v)
{
 cout << label << ": " << endl;

 for (int i = 0; i < v.size(); ++i)
  cout << v[i] << endl;

 cout << '\n';
}

//calculates fibonnaci sequence for a given amount of numbers
void fibonnaci(int first, int second, vector<int>& v, int howMany)
{
 //is vector empty?
 if (v.size() == 0)
 {
  v.push_back(first);
  v.push_back(second);

  int temp;

  //pushback numbers from sequence depending on how many we want
  //start with 3rd number
  for (int i = 1; i < howMany; ++i)
  {
   temp = v[i] + v[i - 1];
   v.push_back(temp);
  }
 }
 else
  cout << "Sorry that vector is not empty.\n";
}

int main()
{
 vector<int> fibonacciNumbers;

 //populate vector with sequence
 fibonnaci(1, 2, fibonacciNumbers, 10);

 //print the vector
 string label = "Fibonacci Numbers";
 print(label, fibonacciNumbers);

 keep_window_open();

 return 0;
}

Chapter 8 // Exercise 4
4. An int can hold integers only up to a maximum number. Find approximation of that maximum number by using fibonacci().
#include "stdafx.h"
#include "std_lib_facilities.h"

//prints a given vector to the screen with a label
void print(string& label, const vector<int>& v)
{
 cout << label << ": " << endl;

 for (int i = 0; i < v.size(); ++i)
 {
  //if incrementor is divisible by 10, start a new line to print
  cout << v[i] << '\t';
  if (i % 10 == 0 && i != 0)
   cout << '\n';
 }

 cout << '\n';
}

//calculates fibonnaci sequence for a given amount of numbers
void fibonnaci(int first, int second, vector<int>& v, int howMany)
{
 //is vector empty?
 if (v.size() == 0)
 {
  v.push_back(first);
  v.push_back(second);

  int temp;

  //pushback numbers from sequence depending on how many we want
  //start with 3rd number
  for (int i = 1; i < howMany; ++i)
  {
   temp = v[i] + v[i - 1];
   v.push_back(temp);
  }
 }
 else
  cout << "Sorry that vector is not empty.\n";
}

int main()
{
 vector<int> fibonacciNumbers;

 //populate vector with sequence
 fibonnaci(1, 2, fibonacciNumbers, 10);

 //print the vector
 string label = "Fibonacci Numbers";
 print(label, fibonacciNumbers);

 //after printing 100 numbers this is highest number it can go to
 int number = 1836311903;
 cout << '\n' << number;

 keep_window_open();

 return 0;
}
Here I added a couple of lines in print to print numbers in rows of 10. When told to find 100 numbers the number before everything goes weird is 1836311903.