Posts

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

Software and Its Types

Image
Software Software is a collection of sets of programs, which are used to execute all kinds of specific instruction. It consists of a number of machine instructions, and arrays in a specific order to perform a particular task. Software is used to describe all the programs and their associated documents which run on a computer. So, a computer needs both software and hardware for its proper functioning. Software means computer instructions or data. Anything that can be stored electronically is software. Firmware is software (programs or data) that has been permanently written onto read-only memory (ROM) All software falls into two general types or categories: System Software and Application Software. System software consists of low-level programs that interact with the computer at a very basic level. This includes operating systems, compilers, and utilities for managing resources. On the other hand, application software includes database programs, word processors, and spreadsheets. Typ