Page 1 of 1

BEGINNER STL FUNCTION POINTER TUTORIAL BEGINNER STL FUNCTION POINTER TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 15 July 2009 - 10:37 AM

BEGINNER STL FUNCTION POINTER TUTORIAL


CONTENTS
• I. ASSUMPTIONS
• II. INTRODUCTION
• III. VOCABULARY
• IV. DECLARING A FUNCTION POINTER
• V. INITIALIZING A FUNCTION POINTER
• VI. CALLING A FUNCTION POINTER
• VII. PASSING ARRAY AND VECTOR TO FUNCTION BY REFERENCE
• VIII. VECTOR FILE INPUT AND OUTPUT
• IX. PROGRAM ANALYSIS
• X. CONCLUSION
• XI. CONTACT ME

WHAT YOU WILL LEARN IN THIS TUTORIAL

1. You will learn the basic function pointer vocabulary.

2. You will learn how to declare a function pointer.

3. You will learn how to initialize a function pointer.

4. You will learn how to call a function pointer.

5. You will learn how to pass an array and a vector to a function by reference.

6. You will learn vector file input and output.

• I. ASSUMPTIONS

Hello; nice to meet you. I assume you are a beginner programmer, you have a compiler, and you want to learn how to use function pointers.

• II. INTRODUCTION

Welcome to the function pointer tutorial. A function pointer is one of the many exciting concepts that draw programmers into programming.

Discussing function pointers means entering the realm of mental abstractions. For example, given that anything that behaves like a function is a function; when you define an object that behaves like a function it can be used as a function. Wow! I drank a whole cup of coffee thinking about that one
.
• III. VOCABULARY

1. What is binding?

Binding refers to the process used to convert function names into machine language addresses.

The difference between a non-virtual function and a virtual function is the non-virtual functions are resolved at compile time. Resolving at compile time is called static binding. Virtual functions are resolved during run-time. Resolving at run time is known as dynamic binding.

Early binding, also called static binding, means the compiler is able to directly associate the function with a machine address.

Late binding or dynamic binding occurs when it is not possible to know which function will be called until runtime. One way to get late binding is to use function pointers.

2. What are casts?

Casts are used to convert the type of a function argument or function return value to another type.

3. What is a declaration?

A variable is declared by giving it a type and a name. A lvalue of a variable is the value of its address where it is stored in memory. The rvalue of a variable is the value stored in that variable at that address.

A pointer variable is declared by giving it a type and a name. The asterisk ‘*’ tells the compiler that the variable is a pointer variable. The type tells the compiler what type the pointer is to point to. Once a pointer variable is declared we can get its address by preceding its name with the unary ‘&’. We can dereference a pointer and refer to the value it points to by using the unary ‘*’ operator.

4. What is a function pointer?

A function pointer is a variable that stores the address of a function. When the function pointer is dereferenced, the function whose address is pointed to is called.

5. What is a functor?

A functor is an object that behaves like a function.

Once you understand function pointers you might want to read the Standard Template Library (STL). The STL discusses a special class of function pointers called functors, closures, or delegates. Functors are function objects or function pointers that contain the function call operator()(); a virtually overloaded operator() used to call a function. In the STL functors are used as predicates. Predicates can be unary or binary. A function that accepts one argument and returns a bool is a unary predicate. A function that accepts two arguments and returns a bool is a binary predicate.

6. What is an indirect function call?

An indirect function call is calling a function with a function pointer.

7. What is a listener or Windows callback function pointer?

Listener or Windows callback functions are functions that are event driven and invoked when a particular event happens.

• IV. DECLARING A FUNCTION POINTER

A function pointer is declared and initialized to NULL using the following syntax.

Return_Type (*Pointer_Name) (List_Of_Parameter_Types) = NULL;
The following is an example from the attached sample program.
void (*pfcnWriteFile)(std::vector<float> & myVector) = NULL;
The above function pointer declaration says the function pointed to by pfcnWriteFile returns a void and takes one argument.

The first parentheses is used to group the * with the pointer name pfcnWriteFile.

The second parentheses is used to enclose the function arguments.

When you write the declaration for a function pointer, write the declaration of a function with (*function_name) where you would normally write (function_name).

• V. INITIALIZING A FUNCTION POINTER

A function pointer is initialized to a function using the following syntax.
Pointer_Name = &Function_Name;

Always initialize a function pointer before you use it to call a function.
The following is an example from the attached sample program.

Given WriteFile(std::vector<float> & myVector) is a function; the pointer pfcnWriteFile pointing to the address of the function WriteFile (std::vector<float> & myVector) would be initialized as follows.

pfcnWriteFile = &WriteFile;

• VI. CALLING A FUNCTION POINTER

Calling a function with a function pointer is known as an indirect function call.

A function pointer is called using the following syntax.

Pointer_Name(List_Of_Parameters);

The following is an example from the attached sample program.
pfcnWriteFile(myVector);

• VII. PASSING ARRAY AND VECTOR TO FUNCTION BY REFERENCE

An array and vector are passed to a function by reference using the following syntax.

Function definition:

Return_Type Function_Name(List_Of_Parameters){…}

Function call:

Function_Name(List_Of_Parameters);

The following is an example from the attached sample program.
Function definition:

void Step1(float(&f)[8][8], std::vector<float> & myVector) {…}
Function call:

Step1(f, myVector);

• VIII. VECTOR FILE INPUT AND OUTPUT

Writing a vector to a file:

Step 1 Before you can use the member functions of a class in your program you have to create a stream by instantiating or declaring an object of that class in your program.

The class for output is ofstream. You can use any object name you want to use.

The syntax for instantiating an object of the ofstream class is as follows:
ofstream object_name;

The attached program syntax is as follows:

ofstream myfile;

Step 2 After you create a stream by instantiating an object of the class in your program you have to associate or link that object with a file. You can use the open() function to associate the instantiated object with a file.

The complete detailed syntax for the prototype of an output file is as follows:

void ofstream::open(const char *filename, ios::openmode mode = ios::out | ios::trunc);

Because of the defaults, you will not normally see the above detailed syntax. Normally you will see the following syntax:

ofstream instantiated_object_name(“filename”);

The attached program syntax is as follows:

myfile.open("FourDigits.dat");

Test to make sure the file opened before you write to the file.

The attached program uses the following code to check to see if the file is open:

if (myfile.is_open()){…}

After the stream object is associated with a file, the file can be used the same as any other stream object.

Examine the

void WriteFile(std::vector<float> & myVector){…}

function in the attached program very carefully and notice the way the records were output to myfile. The output is the same as if you were printing the records to the console screen, with the exception that instead of cout the code uses the instantiated_object_name myfile.

The attached program uses the following code to close the file after it has been written to:

myfile.close();

Reading to or initializing a vector from a file:

Step 1 Before you can use the member functions of a class in your program you have to create a stream by instantiating an object of that class in your program. The class for output is ifstream. You can use any object name you want to use.

The syntax for instantiating or declaring an object of the ifstream class is as follows:

ifstream object_name;

The syntax used in the attached program is as follows:

We have already instantiated or declared myfile to be an ofstream class object when we created the output file in the

void WriteFile(std::vector<float> & myVector){…}

function. Therefore, when you read from a file you have previously declared as an output file you can skip this step because the instantiated_object_name myfile has already been instantiated or declared.

Step 2 After you create a stream by instantiating an object of the class in your program you have to associate or link that object with a file. You can use the open() function to associate the instantiated object with a file.
The complete detailed syntax for the prototype of an input file is as follows:

void ifstream::open(const char *filename, ios::openmode mode = ios::in);

Because of the defaults, you will not normally see the above detailed syntax. Normally you will see the following syntax:

ifstream instantiated_object_name(“filename”);

The syntax used in the attached program is as follows:

myfile.open("FourDigits.dat");

Test to make sure the file opened before you read from the file.
The attached program uses the following code to check to see if the file is open:

if (myfile.is_open()){…}

After the stream object is associated with a file, the file can be used the same as any other stream object.

Examine the

void ReadFile(std::vector<float> & myVector)

function in the attached program very carefully and notice the way the records were output to myfile. The output is the same as if you were printing the records to the console screen, with the exception that instead of cout the code uses the instantiated_object_name myfile stream as follows:

myfile >> line;

and

myVector.push_back(myVector[line]);

The attached program uses the following code to close the file after it has been read from:

myfile.close();

• IX. PROGRAM ANALYSIS

Please copy and paste the attached program into a file your compiler can read. Then build, compile, and debug the program. Following along, back and forth, between the tutorial and your copy of the program will help you understand the program functionality.

[code]
//************************************
//Beginner STL Function Pointers.
//************************************
#include <algorithm> //Sort, lower_bound.
#include <cmath>
#include <cstdlib> //Random number generator; srand() and rand().
#include <cstring>
#include <ctime> //System time; srand(time(0));
#include <fstream> //File-based input and output.
#include <functional>
#include <iomanip> //Output formatting.
#include <iosfwd>
#include <iostream> //Input and output.
#include <istream>
#include <limits> //Limits.
#include <math.h>
#include <ostream>
#include <string> //String.
#include <vector> //Vector.


using namespace std;

//************************************
//Pause.
//************************************
void Pause()
{
cout << endl << endl << endl;
cout << " ";
system("PAUSE");
}

//************************************
//WRITE VECTOR TO FILE
//************************************
void WriteFile(std::vector<float> & myVector)
{
ofstream myfile; //ofstream class initialization of object myfile.

myfile.open("FourDigits.dat"); //Opens myfile.

if (myfile.is_open()) //Checks to see if file is open.
{
for (int i = 0; i <= 3; i++)
{
myfile << myVector[i];
}

myfile.close(); //Closes file.
}
else cout << " Unable to open file";
}

//************************************
//READING TO OR INITIALIZING A VECTOR
//FROM A FILE
//************************************
void ReadFile(std::vector<float> & myVector)
{
//We already declared myfile to be an ofstream class object
//when we created the output file in the
//void WriteFile() function.

ifstream myfile; //input file

myfile.open("FourDigits.dat"); //Opens myfile.

float line = 0.0; //Instantiates line as an object of class type float.

if (myfile.is_open()) //Checks to see if file is open.
{
while (! myfile.eof()) //eof(); returns true when end of file has been reached,
{ //otherwise it returns false.
myfile >> line;
myVector.push_back(myVector[line]);
}

myfile.close(); //Closes file.
}

else
{
if(!myfile)
{
cout << " Unable to open file.\n";
}
}
}

//************************************
//Initialize array to zero.
//************************************
void ff1(float(&f)[8][8])
{
for (int row = 0; row <= 7; row++)
{
for (int column = 0; column <= 7; column++)
{
f[row][column] = 0;
}
}
}

//************************************
//Generates one random number
//with a value range of
//0 to 3 for Encryption Step 2.
//************************************
void S2rnd0To3(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt1;
int left = 0; //Starting number for range of random numbers
int right = 3; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt1 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[1][4] = rndInt1;
}

//************************************
//Generates one random number
//with a value range of
//1 to 9 for Encryption Step 2.
//************************************
void S2rnd1To9(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt2;
int left = 1; //Starting number for range of random numbers
int right = 9; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt2 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[1][5] = rndInt2;
}

//************************************
//Generates one random number
//with a value range of
//0 to 3 for Encryption Step 3.
//************************************
void S3rnd0To3(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt1;
int left = 0; //Starting number for range of random numbers
int right = 3; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt1 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[2][4] = rndInt1;
}

//************************************
//Generates one random number
//with a value range of
//1 to 9 for Encryption Step 3.
//************************************
void S3rnd1To9(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt2;
int left = 1; //Starting number for range of random numbers
int right = 9; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt2 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[2][5] = rndInt2;
}

//************************************
//Generates one random number
//with a value range of
//0 to 3 for Encryption Step 4.
//************************************
void S4rnd0To3(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt1;
int left = 0; //Starting number for range of random numbers
int right = 3; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt1 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[3][4] = rndInt1;
}

//************************************
//Generates one random number
//with a value range of
//0 to 3 for Encryption Step 5.
//************************************
void S5rnd0To3(float(&f)[8][8])
{
srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function
int rndInt1;
int left = 0; //Starting number for range of random numbers
int right = 3; //Ending number for range of random numbers
int vc = (right - left) + 1;

for(int i = 0; i < 1; i++) //Number of random numbers generated
{
rndInt1 = left + int(vc * rand()/(RAND_MAX + 1.0));
}

f[4][4] = rndInt1;
}

//************************************
//Generates random number
//with a value range of
//0 to 9.
//************************************
void Digit(std::vector<float> & myVector)
{
//rand()/RAND_MAX gives you a random number between 0 and 1.
//Multiplying that by 9 gives you a random number between 0 and 9.

srand((unsigned)time(0)); //void srand ( unsigned int seed ); time() function.
int rndInt = 0; //Declares rndInt1 type int.

for(int i = 0; i <= 3; i++) //Loops selecting four random integers.
{
for(int j = 0; j < 1; j++) //Number of random numbers generated.
{
rndInt = ((double)rand() / (double)RAND_MAX) * 9; //Creates one random integer rndInt1.
}

myVector.push_back(rndInt); //Writes random integer to vector.
}

}

//************************************
//Encryption Step 1;
//start with four
//numbers; e.g., 9 8 7 6
//************************************
void Step1(float(&f)[8][8], std::vector<float> & myVector)
{
ifstream myfile; //input file

myfile.open("FourDigits.dat"); //Opens myfile.

float line = 0.0; //Instantiates line as an object of class type float.

if (myfile.is_open()) //Checks to see if file is open.
{
myfile >> line;
f[0][4] = line;

myfile.close(); //Closes file.
}
else
{
if(!myfile)
{
cout << " Unable to open file.\n";
}
}

system("CLS");
cout << endl << endl << endl;
cout << " Step 1: Start with four numbers; e.g., ";
cout << myVector[0] << " " << myVector[1] << " " << myVector[2] << " " << myVector[3] << endl << endl;
cout << " The vector size is " << myVector.size() << " elements." << endl << endl;
cout << " The elements in the vector are as follows: ";
cout << endl << endl << endl;
for (unsigned int i = 0; i < myVector.size(); i++)
{
cout << " Element " << i << ": " << myVector[i] << '\n';
}
cout << endl << endl << endl;
Pause();
}

//************************************
//Encryption Step 2;
//e.g., add 7 to each digit.
//************************************
void Step2(float(&f)[8][8], std::vector<float> & myVector)
{
S2rnd0To3(f); //Generates one random number from 0 to 3.
//Random number 0 will represent addition.
//Random number 1 will represent subtraction.
//Random number 2 will represent multiplication.
//Random number 3 three will represent division.
S2rnd0To3(f);
int rndInt1 = 0;
rndInt1 = f[1][4];


S2rnd1To9(f); //Generates one random number from 1 to 9.
int rndInt2 = 0;
string step2math = " ";
rndInt2 = f[1][5];

if (rndInt1 == 0)
{
myVector.push_back(myVector[0] + rndInt2); //myVector[4] = 16
myVector.push_back(myVector[1] + rndInt2); //myVector[5] = 15
myVector.push_back(myVector[2] + rndInt2); //myVector[6] = 14
myVector.push_back(myVector[3] + rndInt2); //myVector[7] = 13
step2math = "add";
}

if (rndInt1 == 1)
{
myVector.push_back(myVector[0] - rndInt2); //myVector[4] = 2
myVector.push_back(myVector[1] - rndInt2); //myVector[5] = 1
myVector.push_back(myVector[2] - rndInt2); //myVector[6] = 0
myVector.push_back(myVector[3] - rndInt2); //myVector[7] = -1
step2math = "subtract";
}

if (rndInt1 == 2)
{
myVector.push_back(myVector[0] * rndInt2); //myVector[4] = 63
myVector.push_back(myVector[1] * rndInt2); //myVector[5] = 56
myVector.push_back(myVector[2] * rndInt2); //myVector[6] = 49
myVector.push_back(myVector[3] * rndInt2); //myVector[7] = 42
step2math = "multiply";
}

if (rndInt1 == 3)
{
myVector.push_back(myVector[0] / rndInt2); //myVector[4] = 0
myVector.push_back(myVector[1] / rndInt2); //myVector[5] = .14285
myVector.push_back(myVector[2] / rndInt2); //myVector[6] = .28571
myVector.push_back(myVector[3] / rndInt2); //myVector[7] = .42857
step2math = "divide";
}

system("CLS");
cout << endl << endl << endl;
cout << " Step 2: Randomly " << step2math;
cout << " a number; e.g. " << rndInt2;
cout << " to each number." << endl << endl;
cout << " The vector size is " << myVector.size() << " elements." << endl << endl;
cout << " The elements in the vector are as follows: ";
cout << endl << endl << endl;
for (unsigned int i = 0; i < myVector.size(); i++)
{
cout << " Element " << i << ": " << myVector[i] << '\n';
}
cout << endl << endl << endl;
Pause();
}

//************************************
//Encryption Step 3;
//e.g., divide each digit of the
//number by 9.
//************************************
void Step3(float(&f)[8][8], std::vector<float> & myVector)
{
S3rnd0To3(f); //Generates one random number from 0 to 3.
//Random number 0 will represent addition.
//Random number 1 will represent subtraction.
//Random number 2 will represent multiplication.
//Random number 3 three will represent division.
S3rnd0To3(f);
int rndInt1 = 0;
rndInt1 = f[2][4];

int rndInt2 = 0;
S3rnd1To9(f); //Generates one random number from 1 to 9.
string step3math = " ";
rndInt2 = f[2][5];

if (rndInt1 == 0)
{
myVector.push_back(myVector[4] + rndInt2); //myVector[8] = 25
myVector.push_back(myVector[5] + rndInt2); //myVector[9] = 24
myVector.push_back(myVector[6] + rndInt2); //myVector[10] = 23
myVector.push_back(myVector[7] + rndInt2); //myVector[11] = 22
step3math = "add";
}

if (rndInt1 == 1)
{
myVector.push_back(myVector[4] - rndInt2); //myVector[8] = 7
myVector.push_back(myVector[5] - rndInt2); //myVector[9] = 6
myVector.push_back(myVector[6] - rndInt2); //myVector[10] = 5
myVector.push_back(myVector[7] - rndInt2); //myVector[11] = 4
step3math = "subtract";
}

if (rndInt1 == 2)
{
myVector.push_back(myVector[4] * rndInt2); //myVector[8] = 144
myVector.push_back(myVector[5] * rndInt2); //myVector[9] = 135
myVector.push_back(myVector[6] * rndInt2); //myVector[10] = 126
myVector.push_back(myVector[7] * rndInt2); //myVector[11] = 117
step3math = "multiply";
}

if (rndInt1 == 3)
{
myVector.push_back(myVector[4] / rndInt2); //myVector[8] = 1.77778
myVector.push_back(myVector[5] / rndInt2); //myVector[9] = 1.66667
myVector.push_back(myVector[6] / rndInt2); //myVector[10] = 1.55556
myVector.push_back(myVector[7] / rndInt2); //myVector[11] = 1.44444
step3math = "divide";
}

system("CLS");
cout << endl << endl << endl;
cout << " Step 3: Randomly " << step3math;
cout << " the result by a number; e.g., " << rndInt2 << " to each number." << endl << endl;
cout << " The vector size is " << myVector.size() << " elements." << endl << endl;
cout << " The elements in the vector are as follows: ";
cout << endl << endl << endl;
for (unsigned int i = 0; i < myVector.size(); i++)
{
cout << " Element " << i << ": " << myVector[i] << '\n';
}
cout << endl << endl << endl;
Pause();
}

//************************************
//Encryption Step 4;
//e.g.; swap 1st digit with
//3rd digit.
//************************************
void Step4(float(&f)[8][8], std::vector<float> & myVector)
{
S4rnd0To3(f);
float rndInt3;
rndInt3 = f[1][6];

S4rnd0To3(f);
float rndInt4;
rndInt4 = f[1][7];
string step4swap = " ";

if (rndInt3 == rndInt4)
{
myVector.push_back(myVector[8]); //myVector[12] = 1.66667
myVector.push_back(myVector[9]); //myVector[13] = 1.77778
myVector.push_back(myVector[10]); //myVector[14] = 1.55555
myVector.push_back(myVector[11]); //myVector[15] = 1.44444
step4swap = " random numbers equal; therefore, no swap. ";
}
else
{

if ((rndInt3 == 0) && (rndInt4 == 1)) //Swap 1st with 2nd.
{
myVector.push_back(myVector[9]); //myVector[12] = 1.66667
myVector.push_back(myVector[8]); //myVector[13] = 1.77778
myVector.push_back(myVector[10]); //myVector[14] = 1.55555
myVector.push_back(myVector[11]); //myVector[15] = 1.44444
step4swap = " 1st with 2nd. ";
}

if ((rndInt3 == 0) && (rndInt4 == 2)) //Swap 1st with 3rd.
{
myVector.push_back(myVector[10]); //myVector[12] = 1.55556
myVector.push_back(myVector[9]); //myVector[13] = 1.66667
myVector.push_back(myVector[8]); //myVector[14] = 1.77778
myVector.push_back(myVector[11]); //myVector[15] = 1.44444
step4swap = " 1st with 3rd. ";
}

if ((rndInt3 == 0) && (rndInt4 == 3)) //Swap 1st with 4th.
{
myVector.push_back(myVector[11]); //myVector[12] = 1.44444
myVector.push_back(myVector[9]); //myVector[13] = 1.66667
myVector.push_back(myVector[10]); //myVector[14] = 1.55556
myVector.push_back(myVector[8]); //myVector[15] = 1.44444
step4swap = " 1st with 4th. ";
}

if ((rndInt3 == 1) && (rndInt4 == 2)) //Swap 2nd with 3rd.
{
myVector.push_back(myVector[8]); //myVector[12] = 1.77778
myVector.push_back(myVector[10]); //myVector[13] = 1.55556
myVector.push_back(myVector[9]); //myVector[14] = 1.55556
myVector.push_back(myVector[11]); //myVector[15] = 1.44444
step4swap = " 2nd with 3rd. ";
}

if ((rndInt3 == 1) && (rndInt4 == 3)) //Swap 2nd with 4th.
{
myVector.push_back(myVector[8]); //myVector[12] = 1.77778
myVector.push_back(myVector[11]); //myVector[13] = 1.44444
myVector.push_back(myVector[10]); //myVector[14] = 1.55556
myVector.push_back(myVector[9]); //myVector[15] = 1.66667
step4swap = " 2nd with 4th. ";
}

if ((rndInt3 == 2) && (rndInt4 == 3)) //Swap 3rd with 4th.
{
myVector.push_back(myVector[8]); //myVector[12] = 1.77778
myVector.push_back(myVector[9]); //myVector[13] = 1.66667
myVector.push_back(myVector[11]); //myVector[14] = 1.44444
myVector.push_back(myVector[10]); //myVector[15] = 1.55556
step4swap = " 3rd with 4th. ";
}
}

system("CLS");
cout << endl << endl << endl;
cout << " Step 4: Randomly swap two numbers; e.g., " << step4swap << endl << endl;
cout << " The vector size is " << myVector.size() << " elements." << endl << endl;
cout << " The elements in the vector are as follows: ";
cout << endl << endl << endl;
for (unsigned int i = 0; i < myVector.size(); i++)
{
cout << " Element " << i << ": " << myVector[i] << '\n';
}
cout << endl << endl << endl;
Pause();
}

//************************************
//Encryption Step 5;
//e.g.; swap 2nd digit with
//4th digit.
//************************************
void Step5(float(&f)[8][8], std::vector<float> & myVector)
{
S5rnd0To3(f);
float rndInt3;
rndInt3 = f[2][6];

S5rnd0To3(f);
float rndInt4;
rndInt4 = f[2][7];
string step5swap = " ";

if (rndInt3 == rndInt4)
{
myVector.push_back(myVector[12]); //myVector[16] = 1.66667
myVector.push_back(myVector[13]); //myVector[17] = 1.77778
myVector.push_back(myVector[14]); //myVector[18] = 1.55555
myVector.push_back(myVector[15]); //myVector[19] = 1.44444
step5swap = " random numbers equal; therefore, no swap. ";
}
else
{
if ((rndInt3 == 0) && (rndInt4 == 1)) //Swap 1st with 2nd.
{
myVector.push_back(myVector[13]); //myVector[16] = 1.66667
myVector.push_back(myVector[12]); //myVector[17] = 1.55556

Is This A Good Question/Topic? 0
  • +

Page 1 of 1