C++ motivational thread

He he, there are NGOs scraping the net and running similar code I’d bet. :face_with_spiral_eyes: :slight_smile:

Practicing does make these alien things sink in, just playing about with stuff you’ve learned like that.

They are definitely having something done along that line. And lots of online discussion sites seem fine with abiding, so as to keep their ad revenue coming in.

Still working on end of chapter exercises this morning, still slogging in the mud. This is reminding me again why I have really disliked all the Python beginner books I looked at for other people (with maybe one exception). Those books skated right over this stuff, not pushing the reader to get reasonably competent at working with basics (variables, constants, types, vectors / arrays, arithmetic, comparisons, control statements, nesting, selection statements, loops, iteration, functions, and using this stuff to effectively express problems as computations), before moving on to higher level stuff. Not meaning to sound preachy about those books. But I don’t feel like they are really teaching programming.

Paraphrasing here. The end of chapter postscript says: In theory, you now have all the language tools you need to express any problem as computation. But doing so isn’t only about the tools.

Seems to be something of a min / max function. The book makes you figure these things out along the way, later telling you what it is, examples of using it, and pointing out the equivalent standard library tools. This seems to be a pattern of teaching in this book. And it seems good to me to get some practice in thinking about how these things might work first, instead of just being handed the tools right away. Any thoughts on that?

#include "../std_lib_facilities.h"
// 

int main()
{
	double num_current;
	double smallest;
	double largest;
	
	cout << "\nEnter a number ('q' to quit): ";
	
	// Get input
	bool i = 0; // Used to flag first run
	while(cin >> num_current){
		cout << "\nYou entered: " << num_current << "\n";
		
		// First run
		if(i == 0){
			smallest = num_current;
			largest = num_current;
			i = 1;
		}
		// After first run
		else if(i  == 1){
			// Set smallest and largest so far
			if(num_current < smallest)
				smallest = num_current;
			else if(num_current > largest)
				largest = num_current;
		}

		cout << "The largest value so far is: " << largest << '\n';
		cout << "The smallest value so far is: " << smallest << '\n';

		cout << "\nEnter a number ('q' to quit): ";
	}

	return 0;

After wrapping up this chapter, it might be good to revisit the game ‘Human Resources Machine’ and implement the given problems in C++. I think all the needed tools for it are covered in this book so far. It would be a means to practice the C++ language features up to this point by thinking about solving some fun problems.

Edit: Sorry for all the edits.

Yes, it’s a very good idea. Programming is all about the problem solving after all and it’s also better to have a handle on how roughly/precisely at least some algorithms we call work under the hood.

That’s a good idea doing that in HRM, it’s sort of another programming language so would be good practice in thinking in the problem first and what thing is easier to solve it in second.

Edit: Oops. Screwed up the level number. Fixed.

Generating random numbers hasn’t been covered yet in the book. Hopefully that is the only additional tool needed to implement the HRM levels.

#include "std_lib_facilities.h"
// Human Resources Machine - Level 1
// Level objective:  Move 3 numbers from the inbox to the outbox

int main()
{
	vector<int>inbox;
	vector<int>outbox;
	
	// Generate random numbers between 1 and 10 in the inbox
	cout << "Inbox: ";
	srand(time(NULL)); // Seed for unique random numbers each time
	for(int i = 0; i < 3; ++i){
		inbox.push_back(1 + rand()%10);
		cout << inbox[i] << ' ';
	}

	// Copy numbers from inbox to outbox
	cout << "\nOutbox: ";
	for(int i = 0; i < outbox.size(); ++i){
		outbox.push_back(inbox[i]);
		cout << outbox[i] << ' ';
	}

	cout << '\n';

	return 0;	
}

Edit: Oops. Screwed up the level number. Fixed.

I needed random letters too for the HRM stuff. Implementing the HRM levels in C++ is time consuming so far. And I haven’t made it to the good stuff yet.

#include "std_lib_facilities.h"
// Human Resources Machine Level 2
// Level objective:  Move 7 letters from inbox to outbox

int main()
{
	vector<int>inbox;
	char character;
	
	// Generate random letters for the inbox
	cout << "Inbox: ";
	srand(time(NULL)); // Seed rand() for unique random numbers each time
	for(int i = 0; i < 7; ++i){
		inbox.push_back(65 + rand()%26); // Random starting at  65 (uppercase) with range of 26
		char character = inbox[i];  // Convert random numbers to characters
		cout << character << ' ';
	}

	vector<char>outbox(inbox.size()); // Outbox created with same size as inbox

	// Copy letters from inbox to outbox
	cout << "\nOutbox: ";
	for(int i = 0; i < (outbox.size()); ++i){
		outbox[i] = inbox[i];
		cout << outbox[i] << ' ';
	}

	cout << '\n';

	return 0;
}

Another trivial level. I suppose I could skip the trival level’s, but it’s probably good to do anything at this point. I should probably make a ‘copyfrom’ function, but I don’t know that it would be useful here over using assignments.

#include "std_lib_facilities.h"
// Human Resources Machine Level 3
// Level objective:  Move letters, B U G, from memory to outbox

int main()
{
	vector<char>memory = {'U','J','X','G','B','E'};
	vector<char>outbox(3);

	// Print what is in memory
	cout << "\nMemory: ";
	for(int i = 0; i < memory.size(); ++i){
		cout << memory[i];
	}

	// Copy letters, B U G, from memory to outbox
	outbox[0] = memory[4];
	outbox[1] = memory[0];
	outbox[2] = memory[3];

	// Print outbox
	cout << "\nOutbox: ";
	for(int i = 0; i < outbox.size(); ++i){
		cout << outbox[i];
	}

	cout << '\n';

	return 0;	
}

It just hit me that I should probably be coding these as game levels (commandline versions of HRM levels), rather than just reproducing what is happening in the levels. But I think that I don’t have a big enough hat for that yet. It does sound like fun though.

I guess this will be the book to go to when it’s time to see what has changed in c++ 20: A Tour of C++, Third Edition. To be released in July. By the time I get to it the fourth edition might be available.

Creating functions that do specific things is a great idea. Even small tasks can be put in functions that are given descriptive names that make code read gooderer. It’s something to get very, very used to doing!

Even in your posted code, you can use a function for all of that and call it from main. And/or a function to print the vector (or return a std::string containing a pretty printed vector etc etc).

It should be a stonker. Scott Meyer’s Effective Modern C++ is a good book, but stuck at C++14 and he’s said that he doesn’t want to do another.

Seems pretty common that good books don’t see continued updates. It’s a shame. Hopefully Stroustrup updates his other c++ books. I really like the one that I’m using, and it will be too bad if it slides into the ether as new versions of c++ roll on.

I barely got any time in for practicing programming yesterday. Back at it today. I thought I had a handle on generating random numbers for the HRM levels, but this isn’t doing what I expect after dropping it into a function. It gives the same number for each call to rand_num() but a unique number at each execution of the program.

#include "std_lib_facilities.h"
// Generates random numbers within given range, starting at given min

int rand_num(int range, int lowest)
{
	int num;
	srand(time(NULL));	// Seeds rand() so as to produce unique random numbers on each execution
	num = rand() % range + lowest;	// range is max relative to min; lowest_num sets where range begins

	return num;
}

int main()
{
	for(int i = 0; i < 10; ++i){
		cout << rand_num(10,1) << '\n';
	}

	return 0;
}

This previous code using rand() works as expected.

#include "std_lib_facilities.h"

// Human Resources Machine Level 1
// Game objective:  Move 3 numbers from inbox to outbox

int main()
{
	vector<int>inbox;
	vector<int>outbox(3);
	
	// Generate random numbers between 1 and 10 in the inbox
	cout << "Inbox: ";
	srand(time(NULL)); // Seed random number for unique random numbers each time
	for(int i = 0; i < 3; ++i){
		inbox.push_back(1 + rand()%10);
		cout << inbox[i] << ' ';
	}

	// Copy numbers from inbox to outbox
	cout << "\nOutbox: ";
	for(int i = 0; i < outbox.size(); ++i){
		outbox[i] = inbox[i];
		cout << outbox[i] << ' ';
	}

	cout << '\n';

	return 0;	
}

The function version is seeding every time while the main version correctly seeds once.

1 Like

Thanks Snookoda. I should probably just leave the details of that alone for now and get on with more immediate things. All in due time I guess.

I am questioning why seeding at every call of my function matters. I guess it is because srand() provides a kickoff point for generating a sequence of random numbers. When calling it one time in main(), it starts a single sequence of random numbers. When calling srand() multiple times from my function, multiple sequences are generated. And the times of call are so close (where time is given as a parameter of srand()) that each sequence is the same. Something like that, I think.

Yeah, that’s it… the rand just produces the same range of pseudo-random numbers for each seed. So kind of random, but also predictable. rand doesn’t produce an even distribution of random numbers either with people who care choosing different ways of getting randomish numbers.

Funny story… there’s a tech company in the \US who have a “wall of entropy” where they have lots of different coloured lava lamps in a grid on a wall where there’s a bunch of office foot traffic. A camera is pointed at it which produces randomly different pictures and they generate their random numbers from those!

Interesting on the lava lamps. I guess if you want truly random numbers, you need truly random input.

Seems like I remember reading something about system electrical noise being used as an input for generating random numbers, but it’s all foggy at this point.

Yeah, it’s a subject that’s taken very seriously by security peeps etc. The true C++ way of generating random numbers is more involved than rand but is apparently trusted more.

Talking of stuff that’s frowned upon, this is the C++ reference guide you should use:

https://en.cppreference.com/w/

There’s cplusplus.com which apparently has buggy examples and isn’t as up to date, according to some folks with opinions on these things.

Adding it to my notes for later. thanks. It’s mostly over my head at this point.

I have to rehearse writing my own name just to sign a postal vote