Posts

Showing posts from July 3, 2022

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