working on Drills from chapter 4 of Programming: Principles and Practice using C++. Bjarne Stroustrup

programming principles and practice using c++ chapter 4 exercise
programming principles and practice using c++ code
c++ principles and practice drills
programming principles and practice using c++ pdf github
github stroustrup

I've been bashing my head against these drills for days and I can't grasp them. I have to add a unit to my numbers (i.e m,ft,in,cm.) and reject invalid units, then convert the numbers and units into metres. I have my code that shows how to choose and keep the highest and lowest numbers etc, but i'm at a complete loss as to how to add the unit part, i've tried if statements and they seem to look like they'll work, but then they don't and i get frustrated and it honestly bugs me that i've missed some fundamental point. here is my code so far

int main(){
double val1=0;
string unit;
double large=0;
double small=0;

cout<<"please Enter 1 number and a unit (cm, M, Ft, In):\n";
while (cin>>val1>>unit){

    if(val1<small) {small=val1; cout<<"smallest so far\n";}//find smallest number
    else if(val1>large) {large=val1; cout<<"largest so far\n";}//Find largest    number



else cout<<"Neither largest nor smallest\n";
}
cout<<"The smaller value so far is (in metres): "<<small<<"\n";
cout<<"The larger value so far is (in metres): "<<large<<"\n";

keep_window_open("~");
return 0;

}

It's not homework and I'm only doing this for my own interest. any help would be appreciated.

1 .By chapter 4 you already know about constants and "magic constants or numbers" (What is a magic number, and why is it bad?).

2 . cout << "please Enter 1 number and a unit (cm, M, Ft, In):" << endl; this is wrong becouse you use upper case and you test lower case letters.

3 .if (small == 0 && val1 > large) if i enter 0 m it says its "its neither largest nor smallest" like those are already set, better use

  if(small=0 && large==0)

4 .Personaly i dont like that conversion into meters i would rather use

    if(unit=="m")
    {
        if( (val1*=m_in_cm) <= small)
        {
            std::cout<<"\nThe smallest value so far "<<val1<<" "<<unit<<" \n";
            small=val1;
        }
        if( (val1*=m_in_cm) >= large)
        {
            std::cout<<"\nThe largest value so far "<<val1<<" "<<unit<<" \n";
            large=val1;
        }
    }
    else if(unit=="in"){....}

where "m_in_cm" is const double m_in_cm=100; It keeps original value and unit used.

working on Drills from chapter 4 of Programming: Principles , working on Drills from chapter 4 of Programming: Principles and Practice using C ++. Bjarne Stroustrup. I've been bashing my head against these drills for days� working on Drills from chapter 4 of Programming: Principles and Practice using C++. Bjarne Stroustrup I've been bashing my head against these drills for days and I can't grasp them.

Here is my solution. I would prefer to just check the vector for the highest and lowest values, but that isn't what the drill asks for? Seems like a pretty janky program Bjarne wanted us to make with this drill.

#include "../../std_lib_facilities.h"
void inMeters(double n) { //just seeing if i can call a function here, not needed.
    cout << "that is " << n << " in meters!\n";
}

int main() {
    double small_val; //declaring variables.
    double large_val;
    double val1;
    double val1_meter = 0;
    char unit = 'z';
    vector <double> all_vals;
    double total_meters = 0;

    constexpr double meter_per_cm = 0.01;
    constexpr double meter_per_inch = 0.03;
    constexpr double meter_per_ft = 0.3;



    cout << "Choose a starting number\n"; //make user choose starting number because while loops wont accept null variables >.>
    cin >> val1;
    small_val = val1;
    large_val = val1;

    cout << "Now pick a number! and a unit (c, m, i, f)\n";     //user inputs number and it is assigned to highest lowest or no variable.
    while (cin >> val1 >> unit) {
       if (val1 >= small_val && val1 <= large_val)
           cout << val1 << " is not a record.\n";
       if (val1 < small_val) {
           small_val = val1;
           cout << val1 << " Smallest number yet.\n";
       }
       if (val1 > large_val) {
           large_val = val1;
           cout << val1 << "Largest number yet.\n";
       }

       switch (unit) {
          case 'c' :
           val1_meter = val1 * meter_per_cm;
           inMeters(val1_meter);
           all_vals.push_back(val1_meter);
           break;
          case 'm' :
           val1_meter = val1;
           inMeters(val1_meter);
           all_vals.push_back(val1_meter);
           break;
          case 'i' :
           val1_meter = val1 * meter_per_inch;
           inMeters(val1_meter);
           all_vals.push_back(val1_meter);
           break;
          case 'f' :
           val1_meter = val1 * meter_per_ft;
           inMeters(val1_meter);
           all_vals.push_back(val1_meter);
           break;
          default :
           cout << "Invalid unit!\n";
           break;
       }



   }
   for (int i = 0; i < all_vals.size(); i++) {
    total_meters += all_vals[i];
   }
   cout << "here's your stats!\n" <<
    "Smallest number: " << small_val << '\n' <<
    "Largest number: " << large_val << '\n' <<
    "Total numbers accepted: " << all_vals.size() << '\n' <<
    "Total length in meters: " << total_meters << '\n';
   cout << "All valid entries this play:\n";
   sort(all_vals);
   for (int i = 0; i < all_vals.size(); i++)
       cout << all_vals[i] << '\n'; 
}

Chapter 4, 1 2 3 4 5 6 7 8 9 10 11 12 13 14. #include The next drill handles the case if the two values are equal. Augment A vector is similar to an array in C and other languages. The function body is the block (�4.4.2.2) that actually does the work . I have come at a stand still on chapter 6 of Bjarne Stroustrup's book Practice and Principles using c++. Basically, I have read this chapter twice and tried to understand the examples about grammars and tokens related to making a calculator.

this how i solved it

 #include "../../std_lib_facilities.h"
double unitconv(double x, string y) // a function to convert units. 
{
    if (y == "in")
    {
        x = x / 39.37;
        y = "m";
    }
    else if (y == "ft")
    {
        x = x / 3.28;
        y = "m";
    }
    else if (y == "cm")
    {
        x = x / 100;
        y = "m";
    }
    else if (y == "m")
    {
        x = x;
        y = y;
    }
    else
        cout << "unrecognised unit";
    return x;

}

int main()
{
    double value = 0;
    string unit = " ";
    double sum = 0;
    int iter = 0;
    vector<double>values;
    cin >> value >> unit;
    value = unitconv(value, unit); 
    values.push_back(value);
    sum = sum + value;
    ++iter;
    double largest = value;
    double smallest = value;
    while (cin >> value >> unit)
    {
        value = unitconv(value, unit); 
        if (value > largest)
        {
            largest = value;
        }
        if (value < smallest)
        {
            smallest = value;
        }
        values.push_back(value);
        sum = sum + value;
        ++iter;
        cout << "\nLargest Value = " << largest <<  "\nSmallest Value = " << smallest << "\nSum of Values = " << sum << "\nNumber of Values = " << iter << "\n";
    }
    sort(values.begin(),values.end());
    for (int i = 0; i < values.size(); ++i)
        cout << values[i] << '\n';
    keep_window_open();
}

Chrinkus/stroustrup-ppp: Chapter work from Bjarne , Bjarne Stroustrup - c++ all values. This program works for most, but not all values. Programming Principles and Practice Using C++ Chapter 4 Drill, Step 7 . Hello, I am an extreme beginner at C++ and am trying to learn this language through one of the creators books. I'm working on the drill in Chapter 4 and am having trouble with number 9 on the drill list. it asks me to keep track of the sum of values entered(as well as the smallest and the largest) and the number of values entered.

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
    double a = 0.0;
    vector<double> double_vec;

    int iterator = 0;
    double temp_smallest = 0;
    double temp_largest = 0;
    int count = 0;

    while (cin >> a)
    {
        // add i to the vector;
        double_vec.push_back(a);

        for (int i = 0; i < count; i++)
        {
            if (double_vec[i] > temp_largest)
            {
                temp_largest = double_vec[i];
            }

            else if (double_vec[i] < temp_smallest)
            {
                temp_smallest = double_vec[i];
            }
        }

        if (temp_smallest > a)
            cout << "smallest so far" << endl;
        else if (temp_largest < a)
            cout << "largest so far";
        count++;
    }

    system("PAUSE");

    return 0;
}

Chapter 4 does discuss vectors, there are a few modifications that can be added to perform tests on all values. This program works for most, but not all values.

0p3r4t4/PPPUCPP2nd: Exercises (Try these and Drills , Chapter work from Bjarne Stroustrup's Programming Principles and Practice text - Chrinkus/stroustrup-ppp. being covered through other resources, namely the study of K & R for learning C and daily use of Catch2 for testing. I'm busy working through the 2nd edition of Programming: Principles and Practice using C++ (Stroustrup), and getting a problem with reading in values with and without a space between them, then displaying them again.

Here is my solution. What I find concerning about all the other solutions is how they are handling the invalid inputs (i.e., those with invalid units). Most of them just add the value to the vector anyway. Some just break out of the while loop without giving the user the chance to keep adding values.

I am also using conventions suggested by Stroustrup such as constexpr constants, and a ranged for loop.

The additional complexity in my version is a valid_entry flag that is set on each loop. If the input is not valid, the program does not stop, it simply loops around again.

#include "std_lib_facilities.h"

int main()
{
  constexpr double METERS_PER_CM = 1/100.0;
  constexpr double METERS_PER_IN = 1/39.37; //avoid magic #s!
  constexpr double METERS_PER_FT = 1/3.28;

  double smallest_so_far = 0;
  double largest_so_far = 0;
  double current_val = 0;
  double current_val_meters = 0;
  vector<double> all_distances;
  double values_sum = 0;
  string units;
  bool valid_entry = true; //if they enter some bs, set to false

  cout << fixed << showpoint << setprecision(3);
  cout << "Please enter length and unit. Enter single char if done.\n"
      << "Assume 0 was first. Units are cm, m, in, ft.\n\n";
  while ( cin >> current_val >> units)
  {
    cout << "You entered " << current_val << units << ".\n";
    //process units: convert to meters
    if (units == "m")
      current_val_meters = current_val;
    else if (units == "cm")
      current_val_meters = current_val * METERS_PER_CM;
    else if (units == "in")
      current_val_meters = current_val * METERS_PER_IN;
    else if (units == "ft")
      current_val_meters = current_val * METERS_PER_FT;
    else
    {
      cout << "You done messed up A-aron!\n";
      valid_entry = false;
    }

    if (valid_entry)
    {
      if (units != "m")
        cout << "\nThat is " << current_val_meters << " meter(s)!\n";

      //add to vector
      all_distances.push_back(current_val_meters);
      //deal with relative sizes
      if (current_val_meters < smallest_so_far)
      {
        cout << "Smallest so far!\n";
        smallest_so_far = current_val;
      }
      if (current_val_meters > largest_so_far)
      {
        cout << "Largest so far!\n";
        largest_so_far = current_val_meters;
      }
  } // valid_entry
    valid_entry = true;
    cout << "\nAnother...?\n";
  }  // while cin

  //When done, present summary statistics
  cout << "You entered " << all_distances.size() << " values.\n";
  //Get sum
  sort(all_distances);
  cout << "All the values you entered: \n";
  for (double x: all_distances)
  {
    values_sum += x;
    cout << x << "\n";
  }
  cout << "Sum of all meters: " << values_sum << "\n";

  //show min and max
  cout << "The max was " << largest_so_far << ", and the min was "
       << smallest_so_far << ".\n";

  cout << "\nHave a good day!\n";
  return 0;
};

Chapter 04 – Principles and Practice Using C++ – Page 4 – bumpy , GitHub is home to over 50 million developers working together to host and I hope that Programming, Principles and Practice Using C++ could cope with those. In summary, I changed my mind and admit error() and a program is allowed to� Storage for exercises & drills from Programming: Principles & Practice using C++ - l-paz91/principles-practice

Help Needed with Chapter 4 Drill in PPP2, Category: Chapter 04 – Principles and Practice Using C++. simple while loop. Bjarne Stroustrup “Programming Principles and Practice Using C++” Chapter 4 Drill� Find helpful customer reviews and review ratings for Programming: Principles and Practice Using C++ (2nd Edition) at Amazon.com. Read honest and unbiased product reviews from our users.

Drill Chapter 4 Programming Principles a - C++ Forum, Help Needed with Chapter 4 Drill in PPP2 Osman Zakir // 12 / 19 / 2016 // Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition // Chapter 4 Drill When I first did the "larger" and "smaller" stuff, it was working fine. C++ � Information � Tutorials � Reference � Articles � Forum� This is the support site for Stroustrup: "Programming: Principles and Practice using C++ (Second Edition)" Addison-Wesley 2014, ISBN 978-0-321-99278-9. support for the first edition of PPP This book is aimed at beginners taking a programming course and people learning C++ as self study.

Drill, Drill Chapter 4 Programming Principles and Practice I'm working on the drill in Chapter 4 and am having trouble with number 9 on the drill Or better, use a range-based loop http://www.stroustrup.com/C++11FAQ.html#for� Principles & Practice Using C++ - Chapter 4 Drill // 4.5. In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:

Comments
  • Bjarne walks through some code involving user defined literals for units in a recent presentation channel9.msdn.com/Events/GoingNative/GoingNative-2012/… starting at about minute 23. That might help you :-)
  • then convert the numbers and units into metres, Where? I don't see you converting.
  • You don't seem to be retaining the smallest distance. You should read in the first distance, make it the largest and smallest, then proceed with the loop.
  • Yup - once you have val1 and unit, you ignore unit and just assume val1 is in metres.
  • This line from Bjarne really bugs me: "You may consider 12 m (with a space between the number and the unit) equivalent to 12m (without a space)". Reading units separated from numbers with a space was easy with std::cin, but I just can't get it to "eat" stuff like "12ft"
  • At first I did not understand your answer for #2 because that does not match the code in the original post. However after I found the answer from the same person who wrote the question it makes sense.
  • This is a very good analysis of the OP's code. I would just suggest use constexpr instead of const because you know that m_in_cm is going to be set at compile time.
  • and yes, the conversions aren't accurate, i just put in random numbers
  • I like the attempt to use a function, but if the unit is invalid, this just continues on as if nothing happened. My answer adds the validation layer needed to make sure this doesn't happen, without kicking out of the while loop with break.
  • This doesn't handle units, which is what makes this problem a challenge to deal with in the first place.
  • Maybe it's just me but i think you should indent your code better (e.g. everything in the scope of the main function should be indented and everything in the scope of the while loop should be indented). You should also use std::endl instead of placing the newline manually (e.g. 'cout << "bla" << endl;').
  • yes, my indenting needs attention for sure, I didn't know about the endl thing, thanks
  • For everything wrong with your code, see @user3446166's answer. Also, I initinally tried using break with this problem, but don't l ike it because it breaks free of the while loop altogether, rather than just moving on to the next number.