Programming Course Begins

Programming Course began yesterday. I enjoyed it so far. Yesterday’s lecture was a general introduction to computers and today we were introduced to variables.

I’m not new to programming. Though I’m no expert but I do know the basics (see one of my old posts where I wrote a guessing game in C++). As such, I’m skipping the talk about what a variable is, so that you won’t die of boredom.

Instead, I’m going directly to the exercise list intended for this week. Let me cover some of the more interesting findings in these exercises.

Ex. 3

Define two int variables, assign the values of 5 and 3 to them. Define a third int variable, assign the sum of the previous two variables to it. Output the third variable.

#include "stdafx.h"
#include "iostream"


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 5;
	int b = 3;
	int c = a + b;
	std::cout << c << std::endl;
	return 0;
}

While technically violating the task, you can achieve the same result like this:

#include "stdafx.h"
#include "iostream"


int _tmain(int argc, _TCHAR* argv[])
{
	int a = 5;
	int b = 3;
	std::cout << a + b << std::endl;
	return 0;
}

There is no third variable and as a result, less memory space is reserved under run-time.

Ex. 5

Define a float variable, assign a value to it. Define an int variable, assign the value of the previously defined float variable to it. Output the int variable. What happens to the original float value?

#include "stdafx.h"
#include "iostream"


int _tmain(int argc, _TCHAR* argv[])
{
	float a = 5.47;
	int b = a;
	std::cout << b << std::endl;
	return 0;
}

The output is 5. If I set a = 5.67, I still get b = 5. This means the value is truncated, i.e. the fractional part is removed and no rounding occurs.

Ex. 6

Define two float variables and assign the values 0.1f and 0.2f to them. Print their sum. Then, insert the line std::cout.precision(8); before the printing statement. What changes in the output does the line introduce? Why?

#include "stdafx.h"
#include "iostream"


int _tmain(int argc, _TCHAR* argv[])
{
	float a = 0.01f;
	float b = 0.02f;
	//std::cout.precision(8);
	std::cout << a + b << std::endl;
	return 0;
}

With the line commented out, the output is 0.03, as expected. When the line is uncommented, the output becomes 0.029999999. Here’s what’s happening.

Literals like 0.01 and 0.02 are interpreted as double by default. When you tack on the f suffix, they are interpreted as float. Floats can hold 7 significant digits by default. When you ask for precision(8), you’ are asking for one extra significant digit and floats cannot accommodate that. So rounding errors creep in in the output.

Ex. 11

Define two int variables, n and m. Ask the user to enter the values for the variables. Define a float variable and assign to it the result of

dfrac{n^2}{m}

#include "stdafx.h"
#include "iostream"

int _tmain(int argc, _TCHAR* argv[])
{
	int n,m;
	float res;
	std::cout <> n;
	std::cin >> m;
	res = (float) n*n/m;
	std::cout << "The value of n^2/m is: " << res << std::endl;
	return 0;
}

Important to note here is since n and m are int, division in the expression dfrac{n^2}{m} is also “integer”. Therefore, the result can be unexpected, e.g. if you enter n=2,m=5, you get 0. You need to cast the entire expression to float, which is seen in line 11. Correct result is then assigned to the float variable, in our case 0.8.

Ex. 24

Write a program that computes n! for any entered n.

#include "stdafx.h"
#include "iostream"

int _tmain(int argc, _TCHAR* argv[])
{
	int n,temp = 1;
	std::cout << "Enter an integer n: " <> n;
	for (int i = 1; i <= n; i++)
		temp = temp * i;
	std::cout << n << "! = " << temp << std::endl;
	return 0;
}

Note that two special cases, namely 1!=1 and 0!=1 are also handled correctly by the program.

Ex. 26

Write a guessing game where the player has to guess a number 1 to 100. After each wrong guess the computer should say if the number is less or more and add one penalty point. When the player guesses the number, the number of penalty points should be displayed and the game stopped.

#include "stdafx.h"
#include "iostream"
#include 

int _tmain(int argc, _TCHAR* argv[])
{
	srand((unsigned int)time(0));
	int n,g,p = 0; //n - number to be guessed, g - number attempted by player, p - penalty
	std::cout << "Guess a number from 1 to 100!" <> g;
	while (n != g)
		{
			if (n > g) std::cout << "More!" << std::endl;
			else std::cout << "Less!" <> g;
		}
	std::cout << "You guessed right! You have " << p << " penalty points." << std::endl;
	return 0;
}

About Rokas Paulauskas

2014  Programming