Author Archives: ivanna lases

Mastery27


Mastery27

Mastery27

Validated user input in C++

WSQ10


WSQ10

WSQ10

WSQ10 – Lists

WSQO9


WSQO9

WSQO9

WSQ09 – Factorial Calculator

MASTERY26

Creation and use of matrixes in C++
Note that also you can use boost.ublas for matrix creation and manipulation and also boost.graph to represent and manipulate graphs in a number of ways, as well as using algorithms on them, etc.

Edit: Anyway, doing a range-check version of a vector for your purposes is not a hard thing:

template
class BoundsMatrix
{
std::vector inner;
unsigned int dimx
, dimy_;

public:
BoundsMatrix (unsigned int dimx, unsigned int dimy)
: dimx (dimx), dimy (dimy)
{
inner.resize (dimx*dimy_);
}

    T& operator()(unsigned int x, unsigned int y)
    {
            if (x >= dimx_ || y>= dimy_)
                    throw 0; // ouch
            return inner_[dimx_*y + x];
    }

};
Note that you would also need to add the const version of the operators, and/or iterators, and the strange use of exceptions, but you get the idea.

LINK:
http://stackoverflow.com/questions/618511/a-proper-way-to-create-a-matrix-in-c

TC1017

Mastery25


Mastery25

Mastery25

Creation and use of strings in C++

C++ provides following two types of string representations:

The C-style character string.

The string class type introduced with Standard C++.

The C-Style Character String:
The C-style character string originated within the C language and continues to be supported within C++. This string is actually a one-dimensional array of characters which is terminated by a null character ‘’. Thus a null-terminated string contains the characters that comprise the string followed by a null.

The following declaration and initialization create a string consisting of the word “Hello”. To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word “Hello.”

char greeting[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘’};
If you follow the rule of array initialization, then you can write the above statement as follows:

char greeting[] = “Hello”;

Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the ‘’ at the end of the string when it initializes the array.
link:http://www.tutorialspoint.com/cplusplus/cpp_strings.htm

Mastery24

Creation and use of arrays in C++:

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:

type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:

double balance[10];

link:

http://www.tutorialspoint.com/cplusplus/cpp_arrays.htm

TC1017

Mastery 23

Creation and use of vectors in C++:
with the new C++ standard (may need special flags to be enabled on your compiler) you can simply do:

std::vector v { 34,23 };
// or
// std::vector v = { 34,23 };
Or even:

std::vector v(2);
v = { 34,23 };
On compilers that don’t support this feature (initializer lists) yet you can emulate this with an array:

int vv[2] = { 12,43 };
std::vector v(&vv[0], &vv[0]+2);
Or, for the case of assignment to an existing vector:

int vv[2] = { 12,43 };
v.assign(&vv[0], &vv[0]+2);
Like James Kanze suggested, it’s more robust to have functions that give you the beginning and end of an array:

template <typename T, size_t N>
T begin(T(&arr)[N]) { return &arr[0]; }
template <typename T, size_t N>
T
end(T(&arr)[N]) { return &arr[0]+N; }
And then you can do this without having to repeat the size all over:

int vv[] = { 12,43 };
std::vector v(begin(vv), end(vv));

link:
http://stackoverflow.com/questions/8906545/how-to-initialize-a-vector-in-c

TC1017

Mastery21

Use of recursion for repetitive algorithms:
What is recursion? The simple answer is, it’s when a function calls itself. But how does this happen? Why would this happen, and what are its uses?

When we talk about recursion, we are really talking about creating a loop. Let’s start by looking at a basic loop.

for(int i=0; i<10; i++) {
cout << "The number is: " << i << endl;
}

For those who don’t yet know, this basic loop displays the sentence, "The number is: " followed by the value of ‘i’
Inside the ‘for loop’ declaration we have the integer variable ‘i’ and have its starting value of 0. So the first time the sentence is displayed it reads, "The number is: 0". The part of the ‘for loop’ declaration that is ‘i++’ tells the program that each time the loop repeats, the value of ‘i’ should be increased by 1. So, the next time the sentence is displayed it reads, "The number is: 1".
This cycle will continue to repeat for as long as the value of ‘i’ is less than 10. The last sentence displayed would read, "The number is: 9". As you can see the basic ‘for loop’ has three parts to its declaration, a starting value, what must remain true in order to continue repeating, and a modifying expression. Everything that is contained within the {braces} is what the program performs. Cout stands for console out, and prints words or characters to the screen.
So what does this have to do with recursion? Remember recursion is a loop. What if I did not want to just print a message to the screen? A loop can be used to perform other tasks as well.
I have declared a void function, which means it returns nothing, and takes a parameter of ‘int i’. The function is named ‘numberFunction’ and as you can see, all it does is display the sentence, "The number is: " followed by the current value of ‘i’. The function is called into use by the ‘for loop’, which continually calls the function as long as the value of ‘i’ is less than 10.

Now with recursion, we won’t need to use a ‘for loop’ because we will set it up so that our function calls itself. Let’s recreate this same program one more time, only this time we will do it without a ‘for loop’. We will use a recursion loop instead.

links:
https://www.youtube.com/watch?v=vCDoLbs1HEU
http://www.cplusplus.com/articles/D2N36Up4/

TC1017

Mastery20


Mastery20

Mastery20

Use of loops with “for”:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:
The syntax of a for loop in C++ is:

for ( init; condition; increment )
{
statement(s);
}
Here is the flow of control in a for loop:

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

link:
http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

Mastery 18

Nesting of conditional statements:

is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax:
The syntax for a nested if statement is as follows:

if( boolean_expression 1)
{
// Executes when the boolean expression 1 is true
if(boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
}
You can nest else if…else in the similar way as you have nested if statement.

Example:

include

using namespace std;

int main ()
{
// local variable declaration:
int a = 100;
int b = 200;

// check the boolean condition
if( a == 100 )
{
// if condition is true then check the following
if( b == 200 )
{
// if condition is true then print the following
cout << "Value of a is 100 and b is 200" << endl;
}
}
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;

return 0;
}
When the above code is compiled and executed, it produces the following result:

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

link: http://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm

TC1017