GithubHelp home page GithubHelp logo

bogcyg / bookcpp Goto Github PK

View Code? Open in Web Editor NEW
48.0 2.0 17.0 6.44 MB

Contains code of the book "Introduction to Programming with C++ for Engineers" by prof. Bogusław Cyganek, Wiley-IEEE, 2020

CMake 5.27% C++ 91.13% C 3.60%

bookcpp's Introduction

BookCpp

Contains code of the book "Introduction to Programming with C++ for Engineers" by prof. Bogusław Cyganek, Wiley-IEEE, 2020

// ==========================================================================
//
// Software written by Boguslaw Cyganek (C) to be used with the book:
// INTRODUCTION TO PROGRAMMING WITH C++ FOR ENGINEERS
// Published by Wiley, 2020
//
// The software is supplied as is and for educational purposes
// without any guarantees nor responsibility of its use in any application.
//
// ==========================================================================

bookcpp's People

Contributors

bogcyg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bookcpp's Issues

Problems of source code for chapter 3 , about lambdas

https://github.com/BogCyg/BookCpp/blob/master/CCppBookCode/src/Functions_Lambdas.cpp

I was trying the functions
LambdaTestFun_0() , LambdaTestFun_1() , LambdaTestFun_2()
So I constructed this file

//3.14.7
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include <array>

using std::string, std::vector, std::cout, std::endl, std::ostream_iterator, std::array, std::transform;

void LambdaTestFun_0( void )
{
	vector< int >	vec;
	// Is it ok?
	//auto rand = [] ( const int from = 1, const int to = 1000 ) { return (std::uniform_int_distribution<int>( from, to ) )( std::default_random_engine( (std::random_device())() ) ); };

	//auto gen = bind(normal_distribution<double>{15,4.0},default_random_engine{});		// from Stroustrup

	// Insert 10 random values to vec
	generate_n( back_inserter( vec ), 10, std::rand );  // or use rand or gen from the previous lambda, after you uncomment them?
    cout << "Here are all the " << vec.size() <<" random elements created and placed in the vector:"<< endl;
    for(size_t i=0; i<vec.size(); ++i) cout << vec[i] << " ";
    cout<<"\nFrom the above elements, the ones that are smaller than 1000 are: " <<endl; //maybe none will be <1000
	// Print only if element less than 1000
	copy_if( vec.begin(), vec.end(), ostream_iterator< int >( cout, "," ), [] ( auto x ) { return x < 1000; } );
	cout << endl;
}


void LambdaTestFun_1( void )
{
	// Fill array with odd values
	const int k_elems = 10;
	array< int, k_elems >	odd_vals {};	// fill all with 0

	// Fill odd_vals with odd values
	char counter {};
	transform( odd_vals.begin(), odd_vals.end(), odd_vals.begin(),
			// Caption [& counter] used to pass counter by reference
			// auto & is an empty placeholder for the argument passed by transform
			[& counter]( auto & ){ return 2 * (counter++) + 1; }
	);
	cout << "\n An array with " << k_elems << " odd values:" << endl;
	copy( odd_vals.begin(), odd_vals.end(), ostream_iterator< int >( cout, " " ) );
	cout << endl;
}


void LambdaTestFun_2( void )
{
	// Chars used to generate passwords, put them in 3 literal strings
	const vector< string > rand_strings {	"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz", "0123456789_@&#" };

	std::srand( (unsigned int) time( 0 ) );		// seed random generator
	// Generated value can be from 0 .. max_val-1. We could use default caption [=] to access RAND_MAX;
	// However, a better way is to create a local copy rm via the "init capture" (generalized captures).
	// -> int to explicitly convert return value from double to int
    cout << RAND_MAX << " to be used" << endl;
	auto rand_index = [ rm = RAND_MAX ] ( const int max_val ) //-> int
	{
		 return	static_cast<double>( std::rand() * max_val ) / static_cast<double>( rm + 1 );  // static_cast to make division on doubles
        //return	 std::rand() * max_val  % ( rm + 1 );
	};
	// Let's generate a few exemple passwords ...
	const int kPswdToGenerate { 10 };
	for( int cnt = kPswdToGenerate; cnt > 0; cnt-- )
	{
		string pwd;
		// Insert kPasswordLen random chars via lambda function
		const int kPasswordLen = 8;
		generate_n	(	back_inserter( pwd ), kPasswordLen,
			// Use lambda which calls another lambda passed by reference. However, avoid default [&]
			[ & rand_strings, & rand_index ] ()
			{
				const auto & s = rand_strings[ rand_index( rand_strings.size() ) ];
				return s[ rand_index( s.size() ) ];
			}
		);
		cout << "Proposed password: " << pwd << endl;
	}
}


int main()
{
      LambdaTestFun_0();
      LambdaTestFun_1();
      LambdaTestFun_2();
}

and building it with
g++ -std=c++20 -o lambda_passwd_generator lambda_passwd_generator.cpp
And the execution gives :
`Here are all the 10 random elements created and placed in the vector:
1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
From the above elements, the ones that are smaller than 1000 are:

An array with 10 odd values:
1 3 5 7 9 11 13 15 17 19
2147483647 to be used
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
Proposed password: AAAAAAAA
`
For the LambdaTestFun_0(), the problem is that the generated integer numbers are too big, so there would be no output in general, because there us nothing smaller than 100. That is why I wanted to see all the generated random numbers, before I attempt to print them. It will be better if the example produces numbers in the range of eg 0-200 (or 1-200) and we only print the numbers < 100 , ans the example intends, so we will see some of them (around half of them should be < 100)

For LambdaTestFun_2() it is obvious that the generated random index is always 0, so always the 'A' is picked. I tried to fix it , but could not. I did various changes in the lambda auto rand_index but I was still getting all A , or for some changes I got segmentation fault.

Errata in text book

page 66 ( a little after Listing 3.5)
Then, on lines [32–35], the sum of only the diagonal elements of m is computed.
correct should be
Then, on lines [32–35], the sum of only the non-diagonal elements of m is computed.

typos in source code of chapter 1 Variables.cpp

https://github.com/BogCyg/BookCpp/blob/master/CCppBookCode/src/Variables.cpp
inside function VariablesConstWithInitialization()

Current line :
//int x(); // z is int and set to 0?? No !!
Correct line should be :
//int x(); // x is int and set to 0?? No !!

Current line :
int y( 100 ); // Ok, var x with 100
Correct line should be :
int y( 100 ); // Ok, var y with 100

Also in the same area of code , it is better to have the line
std::cout << "Area = " << kPi * radius * radius;
as
std::cout << "Area = " << kPi * radius * radius << endl;
for better output.

Punctuation in the book

Literally every paragraph that starts with bullet does not have a dot at the end. It is really annoying. A second printing is necessary. It is not that I can not read the book with dots missing... but just leaves a bad impression, especially for 99$ book.
Please fix that in the new printings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.