Posts

Showing posts from November, 2015

Sorting in Vectors C++

We will use the builtin function sort() to sort the vector. Header File: #include <algorithm> Synatax: sort(first, last); first -> first position in the sequence. last  -> last position in the sequence Sample Code: #include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <algorithm> using namespace std; void createVector(vector<int> &vect) {     srand(time(NULL));     for (int i = 0; i < 10; i++)         vect.push_back(rand()%100 + 1); } void displayVector(vector<int> vect) {     for (int i = 0 ; i < vect.size(); i++)     {         cout << vect[i] << endl;     } } int main() {     vector<int> int_vector;     createVector(int_vector);     displayVector(int_vector);     cout << endl;     sor...

Searching in a vectors

Sample Code : #include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; void  createVect(vector<int> &vect) {     srand(time(NULL));     for(int i = 0; i < 100; i++)     {         vect.push_back(rand()%100 + 1);     } } int searchVect(vector<int> vect, int searchValue) {     int found = -1;     //Manual Search     for(int i=0;i<vect.size();i++)     {         if (vect[i]  == searchValue)                     return i;     }     //Using Vector function     found = vect.at(searchValue);     return found; } int main() {     vector<int> int_vect;     int num, found;     createVect(int_vect);     cout << "...

Generating Random Numbers in C++

To generate random numbers c++ provides a function rand(), which is present in the header file #include <cstdlib> rand() function returns an integer in the range 0 to RAND_MAX(2147483647). If you want the rand() function to generate random numbers only in the range between 0 to 10. you can write rand()%10 + 1 The problem with using only rand() function is that you will start observing the same sequence of numbers after execution. If you want to get a different sequence of numbers ,you have to perform a operation called seeding . The function which performs seed is srand() and takes integer as an argument. Keep in mind that you should call the srand() function only once.  A commonly used technique is to seed the random number generator using clock. The time() function will return the number of seconds elapsed since Jan1, 1970. You can pass this as an argument to srand(). Sample Code: #include <iostream> #include <cstdlib> #include <ctime...

Introduction to vectors in C++

What is a vector? Vector in C++ is similar to arrays, You can say vectors are more than arrays. It performs dynamic array functionality with taking care of memory management(freeing,allocating) Vector combines the advantage of both static and dynamic arrays. It will automatically deletes the memory allocated for the vector like static array and like dynamic array you can have variable size, there is no need of specifying the size at the beginning. In order to use vectors in your programs, you have to include vector header file #include <vector> As vectors are present in std namespace, you have to declare using namespace std; To declare a vector of integers vector<int> var_name; If you have not specified using namespace std; std::vector<int> var_name; Now to check the size of the vector, you can use size() member function of the vector class. var_name.size(); Initially it will return 0. #include <iostream> #include <vector> u...

Predicate Function in C++

Definition: Function that return a boolean(true or false) value is called predicate function. There are two variations of predication functions: 1. Unary Predicate : Function takes one argument and returns true or false 2. Binary Predicate : Function takes two arguments and returns true or false Lets take some example to understand this Code1: Check whether number is even or not #include <iostream> using namespace std; bool isEven(int number) {     return ((number%2) == 0); } int main() {     int num;     cout << "Enter a number";     cin >> num;     if (isEven(num))     {         cout << num << " is Even" << endl;     }     else{         cout << num << " is Odd" << endl;     }     return 0; } Code2: Check whether character is vowel or not #include <iostr...

Difference between Pointers and References in C++

1. You can assign a pointer variable multiple times, but the same cannot be done with a reference variable. It should be assigned only during initialization. Eg: Re Assigning Pointer variables can be possible       int num1 = 10;       int num2  = 20;       int *ptr = &num1;       ptr = &num2; Re Assigning Reference variables is not possible       int num1 = 10;       int num2 = 20;       int &ref;      ref = num2;  //Error 2. You have to use * operator to dereference a pointer variable, but there is no need of any operator to dereference a reference variable(Automatic Indirection), you can directly use it as a normal variable. 3. You can have multiple levels of indirections in pointers(i.e., pointer to pointer to pointer) but the same cannot be done with references. with references there is only one level of indirection. ...

Reference Parameters in C++ Tutorial

With Reference parameters you are actually creating an alias(another name) to the existing variable. Use case where Reference Parameters are required: 1. Return More than one Value: A function can return only one value, what if you are having a situation where you function has to modify more than one value and return it to the caller. By using reference parameters you can modify more than one value in the function. What happens when a reference parameter is declared? When you pass a reference parameters , instead of copying the actual value, the memory address is copied into the formal parameter. How to differentiate between a simple parameter and a reference parameter? We use an ampersand(&) operator to indicate the parameter is a reference parameter.  Eg: int &a; //Reference parameter       int a; // Not a reference parameter Swap Example Using Reference Parameters: #include <iostream> using namespace std; ...

How do I replace tabs with spaces in Visual Studio

Image
If you have a requirement of adding spaces while pressing tabs in visual studio. Go to Tools->Options->Text Editor -> [Language] -> Tabs Select Insert spaces and click OK

Corflags tool Tutorial

Corflags tools is used to know whether your .exe or .dll is meant to run only on a specific platform or under WOW64. This tool will be very useful if you are planning to run the application on 32 and 64 bit machines. This tool is automatically installed with Visual Studio. Open Visual Studio Command Prompt. Syntax: corflags AssemblyName Eg: Corflags.exe AssemblyName.dll Some of the fields present in the output are: Version : .NET Version that the binary was built with CLR Header: 2.0 means either 1.1 or 1.1 while 2.5 means 2.0 PE &32 Bit: The platform this binary was compiled to run with: Any CPU: PE = PE32 and 32BIT = 0 X86 : PE = PE32 and 32BIT = 1 x64: PE = PE32+ and 32BIT = 0 ILONLY: If the assembly contains unmanaged code the value is 0 else 1. Signed: Whether this assembly was signed using a stronger key. References: 1. http://blogs.msdn.com/b/gauravseth/archive/2006/03/07/545104.aspx

Diskpart Utility Tutorial

Diskpart:  Diskpart utility is a command line utility that allows you to manage and configure your hard disks, volumes, or partitions How to format the pendrive using diskpart utility if it is not getting listed in the Windows Explorer? Steps to be followed Open command prompt in administrator mode type "diskpart" type "list disk" which displays all disks present on the computer type "select disk 1", if you want to the select the second disk, you can choose any number type "clean", which will remove all the partition or volume formatting from the disk with focus type "create partition primary", which creates a primary partition on the current basic disk. after you create the partition, the focus automatically shifts to the new partition type "active", which marks the partition as active type "format FS=NTFS LABEL="ANY NAME" QUICK", performs a quick format type "assign" which ...

MFC Tutorial Part 1

Simple.h : #include <afxwin.h> class CSimpleApp:public CWinApp { public: BOOL InitInstance(void); }; Simple.cpp : #include "Simple.h" BOOL CSimpleApp::InitInstance(void) { return TRUE; } CSimpleApp Application;