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;
}

No comments:

Post a Comment