Programming: Week 5

I haven’t made any posts for quite some time, mostly because I didn’t want to post about something incomplete. A lot has been happening recently and I have been working on several areas: doing exercises, analyzing Arkanoid game code, doing SDL 2 tutorials, and more. So this post won’t be strictly about what we did during the fifth week but rather a selection of various topics I covered in the two previous weeks.

Advanced topics: Recursion

We were introduced to the concept of recursion. Recursion was not new to me: in my post “Programming: Week 2 (cont.)”, you can find a solution to Ex. 11, calculate factorial recursively.

In the lecture, we were presented how to solve two other exercises, Ex. 18 and Ex. 23 of the first week, using recursion instead of loops. These exercises were not covered here, by the way.

Ex. 18

Write a program that outputs the following:

...
"2 bottles of juice on the wall, one fell down!"
"1 bottles of juice on the wall, one fell down!"
"No more bottles left on the wall!"

The user should be able to enter how many bottles to start with.


The first idea that pops up is to write a loop that counts down until the loop variable is less than one, then the last line is printed.

But you can also have main() call a function which would then repeatedly call itself as long as the number of bottles is greater than 0. This way you implement a recursion. Here’s the code:

#include <stdafx.h>
#include <iostream>

void func(int count)
{
	std::cout << count << " bottles of juice on the wall, one fell down!" << std::endl;
	if (count > 0) func(count - 1);
	else std::cout << "No more bottles left on the wall!\n";
}

void main(){

	int count = 0;
	std::cout << "How many bottles?";
	std::cin >> count;
	func(count);
}

Ex. 23

Write a program that loops from 0 to 7 and then back to 0. Print the number at each iteration. Print “Going down.” when the loop starts counting down.


Here is the code:

#include <stdafx.h>
#include <iostream>

void subfunc(int counter, int dir)
{
	for (int i = 0; i < counter; i++)
		std::cout << " ";
	std::cout << counter;

	if (counter != 7)
		std::cout << std::endl;
	else
	{
		std::cout << "\tGoing down." << std::endl;
		dir = -1;
	};
	if (counter == 0 && dir == -1)
		return;
	else
		subfunc(counter + dir, dir);
}

int main()
{
	subfunc(0, 1);
	return 0;
}

And here’s the fancy output:
Capture


Advanced topics: Reading and writing to files

Reading and writing to files is supposedly pretty important in game programming, so we were also introduced into how to read from and write to files (simple *.txt in this case).

First of all, to enable read/write file functionality in your program, you have to include the following:

#include <fstream>

Then you instantiate an object of type std::ofstream (to write to files) or std::ifstream (to read from files).

After that you use methods of instantiated objects to handle file contents. For an object of type std::ofstream,

*.open(), whose parameter is a file name followed by an extension, creates a new empty file in the working directory (in the case of Visual Studio 2013, that’s where the *.vcxproj of your project is located). If there already exists a file with such file name, it will be replaced by the new empty file.

If you add the parameter std::ios_base::app (app = append) when using the .open() method, existing file will not be replaced but just selected.

Once you have a file open, you can use << operator to add contents to file. After you’re done editing your file, you close it using the method .close().

This code creates a new file named test.txt and adds two lines of text: “Hello world!” and “This is a test.”

#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
	std::ofstream out;
	out.open("test.txt", std::ios_base::app);
	out << "Hello world!\nThis is a test.";
	out.close();
	out << "\nThis is another line of text.";
	return 0;
}

Note that no third line will be added because the file is closed before the << operator can modify it. If we were to comment out line 11, the line would be added.

Reading from files works in a similar fashion. You also use open and close methods. Here error handling becomes important – if you plan to do something with the data you read from a file, and the file does not exist, the user should know about it because all file content manipulations will fail.

Below is an example program that uses C++ file and string operations to read the file data.txt and print the contents, line by line:

#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{
	std::ifstream in;
	in.open("data.txt");
	std::string word;
	while (!in.eof())
	{
		std::getline(in,word);
		std::cout << word << std::endl;
	}
	in.close();
	return 0;
}

The .eof() method returns TRUE if end of file has been reached, FALSE otherwise. So this loop “scans” the whole file before exiting.

Introducing Source Control

In one lecture towards the end of the course, we were briefly introduced to how professionals manage large projects. And that’s using a source control system, such as Mercurial or, in our case, BitBucket.

Source Control systems facilitate developer collaboration in large scale projects. Source Control systems track code changes, allow to maintain multiple versions (branches) of the same code, merge and compare changes etc.

Capture

We created a repository and started using it to track our code changes at the beginning of the course project work.

This entry was posted in Studies at Campus Gotland and tagged . Bookmark the permalink.

Leave a comment