Pages

Wednesday, 17 August 2016

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

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


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


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 5 // Exercise 8

Write a program that reads and stores a series of integers and then computes the sum of the first N integers. First ask for N, then read the values into a vector, then calculate the sum of the first N values. For example:

"Please enter the number of values you want to um:"
3
"Please enter some integers (press | to stop):"
12 23 13 24 15 |
"The sum of the first 3 numbers (12 23 13 ) is 48."

Handle all inputs. For example, make sure to give an error message if the user asks fors the sum of more numbers than there are in the vector.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

double getSum();
double g_noValues;
vector<double> g_values;

int main()
try
{
cout << "Please enter the number of values you want to sum: \n";
cin >> g_noValues;

cout << "Please enter some integers (press enter then ctrl+z and enter again to stop): \n";
double n;
while (cin >> n)
{
g_values.push_back(n);
}

double sumN = getSum();

cout << "The sum of the first " << g_noValues << " numbers ( ";
for (int i = 0; i < g_noValues; ++i)
{
cout << g_values[i] << " ";
}
cout << " ) is " << sumN << '\n';

keep_window_open();

return 0;
}


catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}


double getSum()
{
double n = 0;
if (g_noValues > g_values.size())
{
error("You haven't entered enough numbers, sorry.\n");
}

for (int i = 0; i < g_noValues; ++i)
{
n += g_values[i];
}
return n;
}

Now, I know he says to use '|' to stop input but on my machine this just made it skip all the code and close the window immediately without showing the results, so I reverted to ctrl+z instead.

Chapter 5 // Exercise 9

Modify the program from exercise 8 to write out an error if the result cannot be represented in an int.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

int getSum();
double g_noValues;
vector<double> g_values;

int main()
try
{
cout << "Please enter the number of values you want to sum: \n";
cin >> g_noValues;

cout << "Please enter some integers (press enter then ctrl+z and enter again to stop): \n";
double n;
while (cin >> n)
{
g_values.push_back(n);
}

int sumN = getSum();

cout << "The sum of the first " << g_noValues << " numbers ( ";
for (int i = 0; i < g_noValues; ++i) //print out first n values
{
cout << g_values[i] << " ";
}
cout << " ) is " << sumN << '\n';

keep_window_open();

return 0;
}


catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}


int getSum()
{
int nTest = 0;
double n = 0;
if (g_noValues > g_values.size())
{
error("You haven't entered enough numbers, sorry.\n");
}

for (int i = 0; i < g_noValues; ++i)
{
n += g_values[i];
nTest += g_values[i]; //for comparison to see if it can fit
}
if (nTest != n)
{
error("Sorry, this number cannot be represented in an int.\n");
}
return nTest;
}

This one was quite simple. In order to find out if the sum couldn't fit into an int you just create an int and a double containing the same number and then compare them to see if they match. If they don't obviously the int has been truncated.


Chapter 5 // Exercise 10

Modify the program from exercise 8 to use double instead of int. Also, make a vector of doubles containing the N-1 differences between adjacent values and write out that vector of differences.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

void getSum();
void getDiffs();
double g_noValues;
vector<double> g_values;
vector<double> g_valDif;

int main()
try
{
cout << "Please enter the number of values you want to sum: \n";
cin >> g_noValues;

cout << "Please enter some integers (press enter then ctrl+z and enter again to stop): \n";
double n;
while (cin >> n)
{
g_values.push_back(n);
}

getSum();
getDiffs();

keep_window_open();

return 0;
}


catch (exception& e)
{
cerr << "Error: " << e.what() << '\n';
keep_window_open();
return 1;
}

//this returns the sum of the first n values
void getSum()
{
double n = 0;
if (g_noValues > g_values.size())
{
error("You haven't entered enough numbers, sorry.\n");
}

for (int i = 0; i < g_noValues; ++i)
{
n += g_values[i];
}

cout << "The sum of the first " << g_noValues << " numbers ( ";

for (int i = 0; i < g_noValues; ++i) //print out first n values
{
cout << g_values[i] << " ";
}
cout << " ) is " << n << '\n';
}

//this gets and prints the differences. Also it always prints positive numbers
void getDiffs()
{
double diff = 0;

for (int i = 1; i < g_values.size(); ++i)
{
if (g_values[i] < g_values[i - 1])
{
diff = g_values[i - 1] - g_values[i];
g_valDif.push_back(diff);
}
else
{
diff = g_values[i] - g_values[i - 1];
g_valDif.push_back(diff);
}
}

cout << "\nThe differences between each numbers are: \n";

for (int i = 0; i < g_valDif.size(); ++i)
{
cout << g_valDif[i] << " ";
}

cout << '\n';
}

I changed things up a little bit in this one and made getting the sum into a function as well. Getting the differences is straightforward enough, I just stuck an if statement in there so the number is always a positive.

1 comment:

  1. To whoever still uses this, you can have question 8 output "The sum of the first 3 numbers : 12, 23, and 13 is 48." by implementing an if-else statement in the for loop:

    int main(){
    vector numbers;
    int val = 0, x = 0;
    cout << "Please enter how many numbers you want to be summed.\n";
    cin >> x;
    cout << "Please enter a set of integers.\n";
    while(cin >> val){
    if(val < 0)
    error("Invalid integer");
    numbers.push_back(val);
    }
    int sum = 0;
    cout << "The sum of the first " << x << " numbers: ";
    for(int i = 0; i < x; ++i){
    sum += numbers[i];
    if(i < x-1)
    cout << numbers[i] << ", ";
    else if(i == x-1)
    cout << "and " << numbers[i] << " ";
    }
    cout << "is " << sum;
    }

    ReplyDelete