Posts

Operator Overloading Types and Rules | C++ Programming

Operator Overloading It is the mechanism of giving special meanings to an operator. By overloading operators, we can give additional meanings to operators like +, *, -, <=, >=, etc. which by default are supposed to work only on standard data types like ints, and floats. In general, a = b + c; works only with basic types like ‘int’ and ‘float’, and attempting to apply it when a, b and c are objects of a user-defined class will cause complaints from the compiler. But, using overloading, we can make this statement legal even when a, b and c are user-defined types (objects). Even though the semantics of an operator can be expressed, we cannot change its syntax. When an operator is overloaded, its original meaning is not lost. The grammar rules defined by C++ that govern its use such as the number of operands, precedence, and associatively of the operator remain the same for overloaded operators.  We can overload (give additional meanings to) all the C++ operators except the follo

Friend Functions | C++ Programming

Image
Friend Functions Friend Function A function is said to be a friend function of a class if it can access the members (including private members) of the class even if it is not the member function of this class. In other words, a friend function is a non-member function that has access to the private members of the class. Characteristics of a friend function: A friend function can be either global or a member of some other class. Since the friend function is not a part of the class, it can be declared anywhere in the public, private, and protected sections of the class. It cannot be called by using the object of the class since it is not in the scope of the class. It is called a normal function without the help of any object. Unlike member functions, it cannot access member names directly. So, it has to use an object name and dot operator with each member name (like A.x) It, generally, takes objects as arguments. The concepts of data hiding and encapsulation dictate that private

Constant in C Programming | C Programming

Constants in C Programming Constants in C refer to fixed values that do not change during the execution of a program.  const  is the keyword and it’s used to declare constants. A constant value is one that does not change during the execution of a program. C supports several types of constants. Integer Constants Real Constants Single Character Constants String Constants A constant value is one that does not change during the execution of a program. C supports several types of constants. Integer Constants Real Constants Single Character Constants String Constants Integer Constants: An integer constant is a sequence of digits. There are 3 types of integers namely decimal integers, octal integers, and hexadecimal integers. Decimal Integers: Consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces, commas, and non-digit characters are not permitted between digits. Examples of valid decimal integer constant are: 123 -31 0 562321 + 78 Some examples

Data Representation in Computer

Image
How Computers Represent Data All symbols, pictures, or words must be reduced to a string of binary digits. A binary digit is called a bit and represents either a 0 or a 1. These are the only digits in the binary or base 2, number system used by computers. A string of eight bits used to store one number or character in a computer system is called a byte. One byte for character A 01000001 The computer representation in ASCII for the name Alice is 01000001 = A 01001100 = L 01001001 = I 01000011 = C 01000101 = E To represent the numbers 0 through 9 and the letters  a  through  z  and  A  through  Z , computer designers have created coding systems consisting of several hundred standard codes. In one code, for instance, the binary number 01000001 stands for the letter  A . Two common coding systems are Extended Binary Coded Decimal Interchange Code (EBCDIC) and American Standard Code for Information Interchange (ASCII). EBCDIC represents every number, alphabetic character, or spe

C++ Program to Represent the Graph Using Adjacency Matrix | C++ Programming

Image
 C++ Program to Represent the Graph Using Adjacency Matrix #include <iostream> #include <iomanip> using namespace std ; // A function to print the adjacency matrix. void PrintMat ( int mat [][ 20 ], int n ) {     int i , j ;     cout << " \n\n "          << setw ( 4 ) << "" ;     for ( i = 0 ; i < n ; i ++)         cout << setw ( 3 ) << "(" << i + 1 << ")" ;     cout << " \n\n " ;     // Print 1 if the corresponding vertexes are connected otherwise 0.     for ( i = 0 ; i < n ; i ++)     {         cout << setw ( 3 ) << "(" << i + 1 << ")" ;         for ( j = 0 ; j < n ; j ++)         {             cout << setw ( 4 ) << mat [ i ][ j ];         }         cout << " \n\n " ;     } } int main () {     int i , j , v ;     cout << "Enter the number of vertexes: " ;

Expression, Statements and Comments in C Programming | C Programming

Image
Expression, Statements, and Comments in C Programming  Arithmetic C Expressions in C Programming An expression is a combination of variables constants and operators written according to the syntax of the C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of C expressions are shown in the table given below. Algebraic C Expression in C programming Expression a x b - ca * b – c (m + n) (x + y) (m + n) * (x + y) (ab / c)a * b / c 3x2 +2x + 13*x*x+2*x+1 (x / y) + cx / y + c Evaluation of Expressions in C Programming Expressions are evaluated using an assignment statement of the form Variable = expression; Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first and then replaces the previous value of the variable on the left-hand side. All variables used in the expression must be assigned values before evaluation

Fractional Knapsack Problem | DAA

Image
Fractional Knapsack Problem A thief has a bag or knapsack that can contain the maximum weight W of his loot. There are n items and the weight of an ith item is wi and it worth vi. Any amount of item can be put into the bag i.e. xi fraction of item can be collected, where 0<=xi<=1. Here the objective is to collect the items that maximize the total profit earned. Here we arrange the items by ratio vi/wi. Algorithm GreedyFracKnapsack (W, n) {     for (i = 1 ; i <= n; i++)     {         x [i] = 0.0 ;     }     tempW = W;     for (i = 1 ; i <= n; i++)     {         if ( w [i] > tempW)             then break ;         x [i] = 1.0 ;         tempW -= w [i];     }     if (i <= n)         x [i] = tempW / w [i]; } We can see that the above algorithm just contains a single loop i.e. no nested loops the running time for the above algorithm is O(n). However our requirement is that v[1 ... n] and w[1 ... n] are sorted, so we can use the sorting method to sort it in O(n log n