answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Is a linked list a physical data structure?

Physical data structures are how data is organized on a hardware storage device, and therefore how they appear to the computer. Logical or virtual data structures are software-based objects, and how the user or program sees it.

Although many file systems use a type of linked list format for storing information, a linked list is used for both hardware and software purposes, and therefore it cannot fall under either the physical or virtual data structure classification.

What is return in programming?

The return statement is used in functions to return control to the caller. If the function is declared non-void, the return statement also allows the programmer to return a value to the caller.

How would you describe the fetch execute cycle?

Fetch Execute Cycle

A more complete form of the Instruction Fetch Execute Cycle can be broken

down into the following steps:

1. Fetch Cycle

2. Decode Cycle

3. Execute Cycle

4. Interrupt Cycle

1. Fetch Cycle

The fetch cycle begins with retrieving the address stored in the Program

Counter (PC). The address stored in the PC is some valid address in the

memory holding the instruction to be executed. (In case this address does

not exist we would end up causing an interrupt or exception).The Central

Processing Unit completes this step by fetching the instruction stored at

this address from the memory and transferring this instruction to a special

register - Instruction Register (IR) to hold the instruction to be executed.

The program counter is incremented to point to the next address from

which the new instruction is to be fetched.

2. Decode Cycle

The decode cycle is used for interpreting the instruction that was fetched in the Fetch Cycle. The operands are retrieved from the addresses if the need be.

3. Execute Cycle

This cycle as the name suggests, simply executes the instruction that was fetched and decoded

What is the most easiest way to write a GUI programs?

Typically, language is not the determining factor in developing or programming a GUI. More importantly is the application that determines the development platform you are using. At that point, language is not so much of a factor. Depending on the application and development platform, certain languages are better suited though, so once you choose your application, then you pick from the languages that are available. Still though, ease should not be a factor, but servicibility and reliability are way more important than ease of programming. If you can eliminate all other factors, than you can pick the simplest platform available, usually the highest level programming language that has everything you need is a good choice.

That said, some programmers will just go for the simplest platforms every time and try to shoehorn it into the problem they are trying to solve. Java's as popular choice for some apps, WinForms on .NET are also really simple for Windows apps, you can use various languages for that though, C# is pretty easy if you have the tools. Really depends on what you want to do for your GUI though, so there's not really one answer. You have to answer questions like, Is it a Web app, Phone app, Windows app, Apple App, or what? That really determines your programming language.

How can you determine the address of a two dimensional array element in c?

If the array is a local fixed-length array, use the address-of operator (&).

Example:

int x[3][4]; // local fixed-length array

int* p = &x[2][1]; // obtain the address of element x[2][1]

//...

For variable-length arrays we need to use a pointer and separate variables to keep track of the dimensions. C uses row-major semantics so the first dimension specifies the number of rows. However, the type of element in each row is not int, it is int[4], as per the second dimension. In other words, it is one-dimensional array of 3 one-dimensional arrays each of length 4. However, we cannot have a pointer of type int[4] because int[4] also decays to a pointer. We could use a pointer to pointer to int (int**), however in order to allocate the array contiguously without having to maintain a separate array of pointers into the second dimension, it is simpler to treat the array as if it were one-dimensional using a pointer to int (int*). After all, the actual elements we wish to access are integers, so that's what we should really be referring to, rather than to rows of arrays of integers which adds an unnecessary level of indirection. This then means we must use pointer-arithmetic to determine the address of each element. The following demonstrates how to use pointer arithmetic to access each element of a two-dimensional array through a pointer given only the row and column of the element we wish to access:

const size_t rows = 3;

const size_t cols = 4;

int* array2d = malloc (rows * columns * sizeof (int)); // allocate memory to the array

for (int row=0; row<rows; ++row) {

for (int col=0; col<cols; ++col) {

int* p = array2d + (row * cols) + col;

// ...

}

}

free (array2d); // release allocated memory

Here, row * cols determines the offset address of the start of the given row. array2d refers to the start address, so adding array2d gives us the actual address of the row. Adding col then gives us the address of the element within the given row. Note that array2d, &array2d and &array2d[0] are equivalent; they all refer to the start address of the array.

The same technique can also be used when passing a two-dimensional array to a function:

void f (int* array2d, const size_t rows, const size_t cols) {

for (size_t row=0; row<rows; ++row) {

for (size_t col=0; col<cols; ++col) {

int* p = array2d + (row * cols) + col;

// ...

}

}

}

What is a compiler and an interpreter?

a compiler translates an entire program and then executes it while an interpreter translates and executes one line of a program at time

What are the Advantages of binary search on linear search in c?

(i) Binary search can interact poorly with the

memory hierarchy (i.e. caching), because of its

random-access nature. For in-memory

searching, if the interval to be searching is

small, a linear search may have superior

performance simply because it exhibits better

locality of reference.

(ii) Binary search algorithm employs recursive

approach and this approach requires more stack

space.

(iii) Programming binary search algorithm is

very difficult and error prone (Kruse, 1999).

What is the first in first out data structure?

In computer programming, first-in first-out (short FIFO) describes a data structure which implements a chronological order, such that when multiple elements are added to the data structure, the normal retrieval method returns the elements in the order in which they were added.

FIFO structures are often used to implement queues and buffers. The alternative commonly used chronological sorting container is LIFO, short for last-in first-out.

Write a c plus plus program using queue operation?

A queue is a first-in-first-out (FIFO) list of elements. In C++, you could implement a queue by designing a queue class that provided a constructor, destructor, add, and remove methods. The private implementation could use an array, with size specified in the constructor. It would have pointers or indicies that "follow each other" around the array. At boundary state, the queue::add() method would either throw a queue full exception, or it could dynamically adjust the array size, and the queue:remove() method would return null or throw a queue empty exception. Other implementations could be used, such as a singly or doubly linked list, but the "cost" of constantly allocating and deallocating elements would seem to overwhelm the simplicity of the array approach, even considering the possibility of (intermittantly) increasing the allocated size. No matter what approach is used, the public interface should be stable and well defined, so that various internal approachs could be tried and evaluated.

Discuss features of Java Language?

# interpreted and compiled

# simple # robust and secure # platform independent and portable # multithreaded and interactive# object oriented # dynamic and extensible

Print 1 to 100 prime numbers using for loop?

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

How do you draw a flowchart using case statement?

double discount; // Usually code would be read in char code = 'B' ; switch ( code ) { case 'A': discount = 0.0; break; case 'B': discount = 0.1; break; case 'C': discount = 0.2; break; default: discount = 0.3; } System.out.println ( "discount is: " + discount );

C program to for traveling salesman problem?

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

#define N 50

//Travelling sales man problem , travelling between 6 cities.

//initializing the path lengths between cities and the paths to be included

//in population

void initialize(int pathlen[][6],int path[][6])

{

int i,j,k;

//obtaining pathlengths

for(i=0;i<6;i++)

{

for(j=0;j<6;j++)

{

if(j<i) //path length from a to b will be same as b to a

{

pathlen[i][j]=pathlen[j][i];

}

if(j==i) // path length from a to a will be 0

{

pathlen[i][j]=0;

}

if(j>i) // rest initialized

{

do{

pathlen[i][j]= random(20);

}while(!pathlen[i][j]);

}

}

}

// display the path lengths

printf("\n\tThe PATH LENGTHS ARE: \n" );

for(i=0;i<6;i++)

{

for(j=0;j<6;j++)

{

printf(" %5d ",pathlen[i][j]);

}

printf("\n\n");

}

// generating the population

for(i=0;i<20;i++)

{

for(j=0;j<6;j++)

{

path[i][j]=random(6);

for(k=j-1;k>=0;k--)

{

if(path[i][j]==path[i][k]) //checking to avoid repeatition

{

path[i][j] = random(6);

k=j;

}

}

}

}

}

// evaluating the fitness function or total distance

void evaluate(int pathlen[6][6],int path[20][6],int fx[20])

{

int sum =0,i,j,a,b;

//obtaing the sum of the path taken

for(i=0;i<20;i++)

{

sum=0;

for(j=0;j<5;j++)

{

a=path[i][j];

b=path[i][j+1];

sum=sum+pathlen[a][b];

}

fx[i]=sum;

}

//display the paths generated

printf("\n");

printf("\n\tPATH \t\tf(x) \n\n");

for(i=0;i<20;i++)

{

printf("\t");

for(j=0;j<6;j++)

{

printf(" %d",path[i][j]);

}

printf("\t%d",fx[i]);

printf("\n");

}

}

//selecting the two points for cross over and then performing partial Crossover

void selection(int fx[20],int pos[2],int posmax[2])

{

int min1=fx[0],min2=fx[0],i,max1=fx[0],max2=fx[0];

pos[0]=0;

pos[1]=0;

posmax[0]=0;

posmax[1]=0;

//calculating the minimum postion

for(i=1;i<20;i++)

{

if(fx[i]<min1)

{

min1=fx[i];

pos[0]=i;

}

}

//calaculating the second minimum position

for(i=1;i<20;i++)

{

if(fx[i]<min2&&i!=pos[0])

{

min2=fx[i];

pos[1]=i;

}

}

//calculating the max position

for(i=1;i<20;i++)

{

if(fx[i]>max1)

{

max1=fx[i];

posmax[0]=i;

}

}

//calculating the second max position

for(i=1;i<20;i++)

{

if(fx[i]>max2&&i!=posmax[0])

{

max2=fx[i];

posmax[1]=i;

}

}

printf("\n\tFIRST MINIMUM=%4d \tPOSITION=%4d\n\tSECOND MINIMUN=%4d \tPOSITION=%4d\n\tFIRST MAXIMUM=%4d \tPOSITION=%4d\n\tSECOND MAXIMUM=%4d \tPOSITION=%4d\n",min1,pos[0],min2,pos[1],max1,posmax[0],max2,posmax[1]);

}

//PERFORMING PARTIAL CROSSOVER

void crossover(int pos[2],int path[][6],int child[2][6])

{

int crosspt1,crosspt2,j,i,temp,temp1[2][6],temp2;

//TAKING 2 CROSS POINTS

do

{

crosspt1=random(5);

}while(crosspt1>2) ;

do

{

crosspt2=random(5);

}while(crosspt2<=3);

clrscr();

printf("\n\n\t The CROSSOVER POINTS ARE : %d , %d ",crosspt1,crosspt2);

printf("\n\n\tTHE PATHS FOR CROSSOVER ARE");

printf("\n\n\t\t");

for(j=0;j<6;j++)

{

child[0][j]=path[pos[0]][j];

printf(" %d",child[0][j]);

}

printf("\n\t\t");

for(j=0;j<6;j++)

{

child[1][j]=path[pos[1]][j];

printf(" %d",child[1][j]);

}

int cnt=0;

//swapping the paths between two crosspoints

for(j=crosspt1+1;j<=crosspt2;j++)

{

temp1[1][cnt]=child[0][j];

temp1[0][cnt]=child[1][j];

temp=child[0][j];

child[0][j]=child[1][j];

child[1][j]=temp;

cnt++;

}

//performing partial crossover

int k,m;

for(m=0;m<2;m++)

{

for(i=0;i<crosspt1+1;i++) //taking the path before crosspoint

{

for(j=0;j<cnt;j++) //comparing the path within crossover point

{

if(child[m][i]==temp1[m][j]) //if found then

{

if(m==0) //for child 1

{

temp2=temp1[1][j]; //take the path from child2 crossover

for(k=0;k<6;k++)

{

if(child[m][k]==temp2) //if still the path repeats then repeat the process again

{ temp2=child[1][k];

k=0;

}

}

child[m][i]=temp2; //finally putting the value in child

}

else //for child 2

{

temp2=temp1[0][j];

for(k=0;k<6;k++)

{

if(child[m][k]==temp2)

{temp2=child[0][k];

k=0;

}

}

child[m][i]=temp2;

}

}

}

}

}

for(m=0;m<2;m++)

{

for(i=crosspt2+1;i<6;i++) //now chehcking the path after the second cross point

{

for(j=0;j<cnt;j++) //comparing the path within crossover point

{

if(child[m][i]==temp1[m][j]) //if found then

{

if(m==0) //for child 1

{

temp2=temp1[1][j]; //take the path from child2 crossove

for(k=0;k<6;k++)

{

if(child[m][k]==temp2) //if still the path repeats then repeat the process again

{temp2=child[1][k];

k=0;

}

}

child[m][i]=temp2; //finally assigning the value

}

else //for child 2

{

temp2=temp1[0][j];

for(k=0;k<cnt;k++)

{

if(child[m][k]==temp2)

{temp2=child[0][k];

k=0;

}

}

child[m][i]=temp2;

}

}

}

}

}

//display AfTER CROSSOVER

printf("\n\tAFTER CROSSOVER\n\t\t");

for(j=0;j<6;j++)

{

printf(" %d",child[0][j]);

}

printf("\n\t\t");

for(j=0;j<6;j++)

{

printf(" %d",child[1][j]);

}

}

//insering the paths in population removing those having maximum populaiton

void insert(int child[2][6],int posmax[2],int path[20][6])

{

for(int j=0;j<6;j++)

{

path[posmax[0]][j]=child[0][j];

path[posmax[1]][j]=child[1][j];

}

}

// performing mutation

void mutation(int child[2][6])

{

int sel=random(2);

int pos1=random(6);

int pos2=random(6);

int temp=child[sel][pos1];

child[sel][pos1]=child[sel][pos2];

child[sel][pos2]=temp;

}

void main()

{

clrscr();

randomize();

int pathlen[6][6],path[20][6],fx[20],pos[2],posmax[2],child[2][6];

printf("\n\t\t\t TRAVELLING SALESMAN PROBLEM ");

printf("\n\t\t\t_____________________________");

printf("\n\n\n\t\tTHE TRAVELLING SALES MAN PROBLEM DEALS WITH THE FACT");

printf("\n\n\t\tTHAT A SALESMAN TRAVELS BETWEEN CITIES TAKING THE PATH");

printf("\n\n\t\tTHAT IS OF MINIMUN DISTANCE.");

printf("\n\n\n\t\tTO OBTAIN THE MINIMUM DISTANCE WE USE GENETIC ALGO");

printf("\n\n\t\tWHERE WE TAKE AN INITIAL POPULATION OF 20 PATHS AND ");

printf("\n\n\t\tAND THEN THROUGH FITNESS FUNCTION WE OBTAIN THE PATHS ");

printf("\n\n\t\tWITH MINIMUN DISTANCE AND REPLACE THEM WITH THE CHILDS ");

printf("\n\n\t\tAFTER PARTIAL CROSSOVER WITH MAXIMUM DISTANCE.");

getch();

clrscr();

initialize(pathlen,path);

evaluate(pathlen,path,fx);

getch();

selection(fx,pos,posmax);

crossover(pos,path,child);

mutation(child);

getch();

insert(child,posmax,path);

for(int i=1;i<N;i++)

{

evaluate(pathlen,path,fx);

selection(fx,pos,posmax);

crossover(pos,path,child);

mutation(child);

insert(child,posmax,path);

}

evaluate(pathlen,path,fx);

selection(fx,pos,posmax);

crossover(pos,path,child);

insert(child,posmax,path);

evaluate(pathlen,path,fx);

getch();

}

Check whether a character is alphabaticdigital or special character using C program?

To solve this you need to remember that chars in C are represented as ints. Because of this property, you can use int comparison operators:

// to test if character c is a letter

int is_alpha(const char c) {

if( c >= 'a' && c <= 'z' )

return 1;

if( c >= 'A' && c <= 'Z' )

return 1;

return 0;

}

// to test if character c is a digit

int is_digit(const char c) {

if( c >= '0' && c <= '9' )

return 1;

return 0;

}

To test for a "special character" you would probably do best writing a similar function which checks for other ASCII value ranges.

What do you mean by identifier and keywords?

Keywords in a programming language are reserved for a special use. Words like if, while, for, etc. are common keywords among different languages.

Identifiers are the names given to variables, functions, structures, and classes (in order to identify them). Identifiers cannot be keywords.

What programming language does YouTube use?

If your are thinking of youtube it uses a variety of languages. It defiantly uses flash and JavaScript for its front end and some sort of SQL for its data base. It uses several other languages too but those listed above are the only ones I am sure of.

What is const pointer?

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages. The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what he or she can do. In addition, a class method can be declared as const, indicating that calling that method does not change the object. Such const methods can only call other const methods but cannot assign member variables. (In C++, a member variable can be declared as mutable, indicating that a const method can change its value. Mutable member variables can be used for caching and reference counting, where the logical meaning of the object is unchanged, but the object is not physically constant since its bitwise representation may change.) In C++, all data types, including those defined by the user, can be declared const, and all objects should be unless they need to be modified. Such proactive use of const makes values "easier to understand, track, and reason about," and thus, it increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates something about a value's intended use. For simple data types, applying the const qualifier is straightforward. It can go on either side of the type for historical reasons (that is, const char foo = 'a'; is equivalent to char const foo = 'a';). On some implementations, using const on both sides of the type (for instance, const char const) generates a warning but not an error. For pointer and reference types, the syntax is slightly more subtle. A pointer object can be declared as a const pointer or a pointer to a const object (or both). A const pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the object that it points to (called the "pointee"). (Reference variables are thus an alternate syntax for const pointers.) A pointer to a const object, on the other hand, can be reassigned to point to another object of the same type or of a convertible type, but it cannot be used to modify any object. A const pointer to a const object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object. The following code illustrates these subtleties: void Foo( int * ptr, int const * ptrToConst, int * const constPtr, int const * const constPtrToConst ) { *ptr = 0; // OK: modifies the pointee ptr = 0; // OK: modifies the pointer *ptrToConst = 0; // Error! Cannot modify the pointee ptrToConst = 0; // OK: modifies the pointer *constPtr = 0; // OK: modifies the pointee constPtr = 0; // Error! Cannot modify the pointer *constPtrToConst = 0; // Error! Cannot modify the pointee constPtrToConst = 0; // Error! Cannot modify the pointer To render the syntax for pointers more comprehensible, a rule of thumb is to read the declaration from right to left. Thus, everything before the star can be identified as the pointee type and everything to after are the pointer properties. (For instance, in our example above, constPtrToConst can be read as a const pointer that refers to a const int.) References follow similar rules. A declaration of a const reference is redundant since references can never be made to refer to another object: int i = 42; int const & refToConst = i; // OK int & const constRef = i; // Error the "const" is redundant Even more complicated declarations can result when using multidimensional arrays and references (or pointers) to pointers. Generally speaking, these should be avoided or replaced with higher level structures because they are confusing and prone to error.

Explain different parts of an instruction format?

Instruction FormatThe information encoded in an 80386 instruction includes a specification of the operation to be performed, the type of the operands to be manipulated, and the location of these operands. If an operand is located in memory, the instruction must also select, explicitly or implicitly, which of the currently addressable segments contains the operand.

80386 instructions are composed of various elements and have various formats. The exact format of instructions is shown in Appendix B; the elements of instructions are described below. Of these instruction elements, only one, the opcode, is always present. The other elements may or may not be present, depending on the particular operation involved and on the location and type of the operands. The elements of an instruction, in order of occurrence are as follows:

  • Prefixes -- one or more bytes preceding an instruction that modify the operation of the instruction. The following types of prefixes can be used by applications programs:
    1. Segment override -- explicitly specifies which segment register an instruction should use, thereby overriding the default segment-register selection used by the 80386 for that instruction.
    2. Address size -- switches between 32-bit and 16-bit address generation.
    3. Operand size -- switches between 32-bit and 16-bit operands.
    4. Repeat -- used with a string instruction to cause the instruction to act on each element of the string.
  • Opcode -- specifies the operation performed by the instruction. Some operations have several different opcodes, each specifying a different variant of the operation.
  • Register specifier -- an instruction may specify one or two register operands. Register specifiers may occur either in the same byte as the opcode or in the same byte as the addressing-mode specifier.
  • Addressing-mode specifier -- when present, specifies whether an operand is a register or memory location; if in memory, specifies whether a displacement, a base register, an index register, and scaling are to be used.
  • SIB (scale, index, base) byte -- when the addressing-mode specifier indicates that an index register will be used to compute the address of an operand, an SIB byte is included in the instruction to encode the base register, the index register, and a scaling factor.
  • Displacement -- when the addressing-mode specifier indicates that a displacement will be used to compute the address of an operand, the displacement is encoded in the instruction. A displacement is a signed integer of 32, 16, or eight bits. The eight-bit form is used in the common case when the displacement is sufficiently small. The processor extends an eight-bit displacement to 16 or 32 bits, taking into account the sign.
  • Immediate operand -- when present, directly provides the value of an operand of the instruction. Immediate operands may be 8, 16, or 32 bits wide. In cases where an eight-bit immediate operand is combined in some way with a 16- or 32-bit operand, the processor automatically extends the size of the eight-bit operand, taking into account the sign.

What are the binary and decimal values of the ASCII letter K?

ASCII symbol 'K' has the binary code 01001011.

Reading right-to-left, each bit has the following value:

bit 0 = 1 = 2^0 * 1 = 1 * 1 = 1

bit 1 = 1 = 2^1 * 1 = 2 * 1 = 2

bit 2 = 0 = 2^2 * 0 = 4 * 0 = 0

bit 3 = 1 = 2^4 * 1 = 8 * 1 = 8

bit 4 = 0 = 2^8 * 0 = 16 * 0 = 0

bit 5 = 0 = 2^16 * 0 = 32 * 0 = 0

bit 6 = 1 = 2^32 * 1 = 64 * 1 = 64

bit 7 = 0 = 2^64 * 0 = 128 * 0 = 0

1 + 2 + 0 + 8 + 0 + 0 + 64 + 0 = 75

Therefore 01001011 is the binary encoding for the decimal value 75.

Thus ASCII symbol 'K' has the decimal value 75

Working the other way, we divide the decimal value by 2. The remainder can only be 0 or 1 and we write this remainder down. We continue dividing by 2, writing a 0 or a 1, until the decimal value is 0. We then pad any remaining bits with 0s. Like so:

75 / 2 = 37 r 1

37 / 2 = 18 r 1

18 / 2 = 9 r 0

9 / 2 = 4 r 1

4 / 2 = 2 r 0

2 / 2 = 1 r 0

1 / 2 = 0 r 1

Reading from the bottom up, the remainders are 1001011, which we pad with a leading zero to create the binary value 01001011. Note that we typically use leading zeroes when the number of bits is not an exact multiple of 8, thus creating a binary value of one or more 8-bit bytes.

Another way to do the conversion is to use hexadecimal notation. Hexadecimal makes it much easier for humans to interpret binary values because there are 16 digits (0-9 then A-F) and each digit represents a unique 4-bit binary pattern:

0x0 = 0000

0x1 = 0001

0x2 = 0010 0x3 = 0011

0x4 = 0100

0x5 = 0101

0x6 = 0110

0x7 = 0111

0x8 = 1000

0x9 = 1001

0xA = 1010

0xB = 1011

0xC = 1100

0xD = 1101

0xE = 1110

0xF = 1111

From this we can see that 01001011 is formed from 4-bit patterns 0100 and 1011, which from the table above equates to 0x4 and 0xB respectively. Thus binary 01001011 is equivalent to 0x4B in hexadecimal.

0xB has the decimal value 11 while 0x4 has the decimal value 4. However, as with decimal, the position of the digit is significant. Instead of increasing powers of 10 we are dealing with increasing powers of 16, thus 0x4B really means 11 x 16^0 + 4 x 16^1, which reduces to 11 x 1 + 4 x 16 or simply 11 + 64 = 75.

Working the other way, we divide the decimal value by 16 and use the remainder to determine the hex digit:

75 / 16 = 4 r 11

4 / 16 = 0 r 4

11 in hexadecimal is B, thus 75 in hexadecimal is 0x4B.

From the table above, we see that 0x4 is 0100 and 0xB is 1011, thus 75 = 0x4B = 01001011.

From this we can see that converting decimal to any base is simply a matter of repeatedly dividing by the base and taking the remainder. If the base is greater than 10, we convert that remainder to its corresponding digit.

Converting the other way (to decimal) we need to know the positional value of each significant digit and that is always an increasing power of the base, starting from 0 in the least-significant position.

Write a Java program to find occurrences of given word in a text?

/*this block of code fineds any given string in tree different file

displays the string and prints the number of occurences in these file....thats all:)*/

package useToken;

import java.io.*;

class readFile extends StreamTokenizer {

readFile(InputStream in ){

super(in);

}

public static void main (String argv[]){

try {

int ret;

String string;

BufferedReader in =

new BufferedReader (newInputStreamReader(System.in));

System.out.print("Enter some text: ");

string = in.readLine();

String[] file = {"file1.txt","file2.txt","file3.txt"};

for(int i=0;i<3;i++){

FileInputStream fin = new FileInputStream(file[i]);

readFile rt = new readFile(fin);

int counter =0;

while((ret = rt.nextToken())!= 0 && rt.sval != null){

if(rt.sval.equals(string)){

System.out.println("Found Text :" + rt.sval );

counter++;

}

}

System.out.println("The String Found :" + counter + " " + "times");

System.out.println(file[i] + " " + "Complete");

}

} catch (Exception e ){

System.err.println("Exception :" + e);

}

}

}

Write a program to find the frequency of words in a sentence in java programing?

//program to find occurence of a word in a sentence

import java.io.*;

public class cnt2

{

public static void main(String args[])throws IOException

{

int times=0,count=0,x=0,no=0;

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

String s,w;

System.out.println("Enter the sentence:");

s=br.readLine();

System.out.println("Enter the word:");

w=br.readLine();

try{

for(int i=0;i

{

if(w.charAt(0)==s.charAt(i))

{

for(int j=0;j

{

if(s.charAt(i)==w.charAt(j))

{ count=count+1;}

if(count==w.length())

{no=no+1;count=0;};

}

}

}

catch(Exception e){}

if(no==0)

{

System.out.println("word is not present");

}

else

{

System.out.println("word is present "+no+" times");

}

}

}

Write a program to check whether a given number is in array or not?

/**

* Searches for the number n in ns.

*

* Note: Will always return false if the Number type of n is different from ns.

* For example, if n is an Integer and ns is an array of Long objects, then

* contains will always return false. This can be circumvented with some

* generics hacking, but this results in overly-ugly code.

*

* @return true iff n is an element of ns

*/

private static final boolean contains(final Number n, final Number[] ns) {

for(int i = 0; i < ns.length; ++i) {

if(ns[i] == n) {

return true;

}

}

return false;

}

What is the difference between function declaration and function definition?

// function declaration

void functionName (int, double);

// function definition

void functionName (int x, double y) {

// implementation...

}

A function declaration informs the compiler of the function's type, its name and the number and type of its arguments. A declaration is required so the compiler knows what the name represents and how it may be used, but it is not necessary to know what it does or how it does what it does.

A definition is also a declaration, but also includes the names of its formal arguments and provides the implementation details of the function itself. A declaration without a definition is not required, however declarations do help programmers to see the interface without being distracted by the implementation details. By placing declarations in a header file, multiple translation units can make use of the same declaration regardless of which translation unit defines the implementation. This also makes it possible to create functions that depend on each other because it would be impossible to define a function that used another function that hadn't yet been declared.