Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Algorithms Code Examples

Byte alignment in a struct

/* Byte alignment in a struct */ #include <iostream.h> #define ALIGN 8 #pragma pack(ALIGN) /* Algorithm Byte offset between currentType and nextType = MIN( multiple of Byte Alignment specified , multiple of size of nextType) **/ int ALIGNOFFSET(int currRelativeAddess,int currrentTypeSize,int nextTypeSize) { int nOffset = currrentTypeSize ;//this is size of the current type.Offset will be minimum of this size. currRelativeAddess += currrentTypeSize ; //cuurent address is increased by size of current type. if( nextTypeSize < ALIGN) // now alignment depens on the next type and also alignment defined by #pragam pack(#) { // if the size of next type is less than byte alignment size specified , then offset will be increased to the muliple of // size of next type. int nRemainder = currRelativeAddess % nextTypeSize ; if(nRemainder != 0) { nOffset += nextTypeSize - nRemainder ;//this will align boundary to next multiple of nextTypeSize } } else { // if the byte alignment size specified is less than size of next type , then offset will be increased to the next muliple of // size of byte alignment. int nRemainder = currRelativeAddess % ALIGN ; if(nRemainder != 0) { nOffset += ALIGN - nRemainder ;//this will align the boundary to next multiple of ALIGN } } return nOffset ; } struct S { char a[2]; int b; double c; char d[1]; int e; double f; }; void main() { S sz; cout << "size of struct :" << sizeof(sz) << endl; cout << "address of size::a "<< &sz.a <<" Actual offset = " << int(0) << " Calculated offset = " << 0 << endl; int offset = ((int)&sz.b)- (int)(&sz.a); cout << "address of size::b " << &sz.b <<" Actual offset = " << offset << " Calculated offset = " << ALIGNOFFSET((int)(&sz.a)- (int)(&sz),sizeof(sz.a),sizeof(sz.b)) << endl; offset = ((int)&sz.c)- (int)(&sz.b); cout << "address of size::c " << &sz.c <<" Actual offset = " << offset << " Calculated offset = " << ALIGNOFFSET((int)(&sz.b) - (int)(&sz),sizeof(sz.b),sizeof(sz.c)) << endl; offset = ((int)&sz.d)- (int)(&sz.c); cout << "address of size::d " << &sz.d <<" Actual offset = " << offset << " Calculated offset = " << ALIGNOFFSET((int)(&sz.c)- (int)(&sz),sizeof(sz.c),sizeof(sz.d)) << endl; offset = ((int)&sz.e)- (int)(&sz.d); cout << "address of size::e " << &sz.e <<" Actual offset = " << offset << " Calculated offset = " << ALIGNOFFSET((int)(&sz.d)- (int)(&sz),sizeof(sz.d),sizeof(sz.e)) << endl; offset = ((int)&sz.f)- (int)(&sz.e); cout << "address of size::f " << &sz.f <<" Actual offset = " << offset << " Calculated offset = " << ALIGNOFFSET((int)(&sz.e)- (int)(&sz),sizeof(sz.e),sizeof(sz.f)) << endl; }

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

Return the smallest. Returns the smallest of a and b. If both are equivalent, a is returned. min() function is a library function of algorithm header, it is used to find the smallest value from given two values, it accepts two values and returns the smallest value and if both the values are the same it returns the first value. The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. The function uses operator< (or comp, if provided) to compare the values.

Check whether eofbit is set. Returns true if the eofbit error state flag is set for the stream. This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream. Note that the value returned by this function depends on the last operation performed on the stream (and not on the next). Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The sizeof() is an operator that evaluates the size of data type, constants, variable. It is a compile-time operator as it returns the size of any variable or a constant at the compilation time. The size, which is calculated by the sizeof() operator, is the amount of RAM occupied in the computer. The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type. The data_type can be the data type of the data, variables, constants, unions, structures, or any other user-defined data type.

In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:


Split the Data into two equal half until we get at most one element in both half. Merge Both into one making sure the resulting 'Sequence' is sorted. Then 'recursively' split them, merge

Program to solve a matching problem. Given N men and N women, where each person has ranked all members of the opposite gender in order of preference, marry the men & women

Program to print the kind of rotation that is performed when an 'Element is inserted' or deleted from tree. In Discrete Mathematics, tree rotation is an operation on a binary tree



Program 'takes the input' of a set of integers or characters. It firstly generates the random partition of the "Length of the Set". Starting from the beginning, it 'prints the number' of