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

Where you have to put semicolon in c programming?

For clasesses it defines from which class to inherit. :: means area of visibility in certain name space.

How can you use pointers as a function arguments?

You would use a pointer as a parameter when you want to pass the parameter by reference, meaning your function has a reference to the object being passed in, rather than simply a copy of that object. This means that changes to that object made in the function will persist once that function has completed.

The below example should print:

x = 3

x = 4

Note: I just coded this from memory so you will probably have to tweak it slightly to get it to compile

int main()

{

int x = 3;

passByVal(3);

cout << "x = " + x;

passByRef(3);

cout << "x = " + x;

return 0;

}

void passByVal(int a)

{

a++; // a is a copy of x, a different object

}

void passByRef(int &b)

{

b++; // b is a reference to the same object that x points to

}

How do you print rectangle in c?

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 #include #include #include #include void draw (int r[][2]) { int i; setlinestyle (DOTTED_LINE, 0, 1); line (320, 0, 320, 480); line (0, 240, 640, 240); setlinestyle (SOLID_LINE, 0, 1); line (320+r[0][0], 240-r[0][1], 320+r[1][0], 240-r[1][1]); line (320+r[0][0], 240-r[0][1], 320+r[3][0], 240-r[3][1]); line (320+r[1][0], 240-r[1][1], 320+r[2][0], 240-r[2][1]); line (320+r[2][0], 240-r[2][1], 320+r[3][0], 240-r[3][1]); } void reset (int r[][2]) { int i; int val[4][2] = { { 0, 0 },{ 100, 0 },{ 100, 50 },{ 0, 50 } }; for (i=0; i<4; i++) { r[i][0] = val[i][0]; r[i][1] = val[i][1]; } } void rotate (int r[][2], int angle) { int i; double ang_rad = (angle * M_PI) / 180; for (i=0; i<4; i++) { double xnew, ynew; xnew = r[i][0] * cos (ang_rad) - r[i][1] * sin (ang_rad); ynew = r[i][0] * sin (ang_rad) + r[i][1] * cos (ang_rad); r[i][0] = xnew; r[i][1] = ynew; } } void shear (int r[][2], int sx, int sy) { int i; for (i=0; i<4; i++) { int xnew, ynew; xnew = r[i][0] + r[i][1] * sx; ynew = r[i][1] + r[i][0] * sy; r[i][0] = xnew; r[i][1] = ynew; } } void translate (int r[][2], int dx, int dy) { int i; for (i=0; i<4; i++) { r[i][0] += dx; r[i][1] += dy; } } void ini() { int gd=DETECT,gm; initgraph(&gd,&gm,"..//bgi"); } void main() { int r[4][2],angle,dx,dy,x, y,choice; do { clrscr(); printf("1.Rotation about the origin followed by translation\n"); printf("2.Rotation about an arbitrary point\n"); printf("3.Shear about the origin\n"); printf("4.Exit\n\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the rotation angle: "); scanf("%d", &angle); printf("Enter the x- and y-coordinates for translation: "); scanf("%d%d",&dx,&dy); ini(); cleardevice(); reset(r); draw(r);getch(); rotate(r, angle); cleardevice(); draw(r);getch(); translate(r,dx,dy); cleardevice(); draw(r);getch(); closegraph(); break; case 2: printf("Enter the rotation angle: "); scanf("%d",&angle); printf("Enter the x- and y-coordinates of the point: "); scanf("%d%d",&x,&y); ini(); cleardevice(); reset(r); translate(r,x,y); draw(r); putpixel(320+x,240-y,WHITE); getch(); translate(r,-x,-y); draw(r);getch(); rotate(r,angle); draw(r);getch(); translate(r,x,y); cleardevice(); draw(r); putpixel(320+x,240-y,WHITE); getch(); closegraph(); break; case 3: printf("Enter the x- and y-shears: "); scanf("%d%d",&x,&y); ini(); reset(r); draw(r);getch(); shear(r, x, y); cleardevice(); draw (r);getch(); closegraph(); break; case 4: closegraph(); } }while(choice!=4); }

How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.

Usually one element at a time. If you want to process all elements of an array, you write a loop.

Usually one element at a time. If you want to process all elements of an array, you write a loop.

Usually one element at a time. If you want to process all elements of an array, you write a loop.

What is stack define the operations of stack?

In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top[1] (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. record Node {

data // The data being stored in the node

next // A reference to the next node; null for last node

}

record Stack {

Node stackPointer // points to the 'top' node; null for an empty stack

}

function push(Stack stack, Element element) { // push element onto stack

new(newNode) // Allocate memory to hold new node

newNode.data := element

newNode.next := stack.stackPointer

stack.stackPointer := newNode

}

function pop(Stack stack) { // increase the stack pointer and return 'top' node

// You could check if stack.stackPointer is null here.

// If so, you may wish to error, citing the stack underflow.

node := stack.stackPointer

stack.stackPointer := node.next

element := node.data

return element

}

function top(Stack stack) { // return 'top' node

return stack.stackPointer.data

}

function length(Stack stack) { // return the amount of nodes in the stack

length := 0

node := stack.stackPointer

while node not null {

length := length + 1

node := node.next

}

return length

}

As you can see, these functions pass the stack and the data elements as parameters and return values, not the data nodes that, in this implementation, include pointers. A stack may also be implemented as a linear section of memory (i.e. an array), in which case the function headers would not change, just the internals of the functions. Implementation

A typical storage requirement for a stack of n elements is O(n). The typical time requirement of O(1) operations is also easy to satisfy with a dynamic array or (singly) linked list implementation. C++'s Standard Template Library provides a "stack" templated class which is restricted to only push/pop operations. Java's library contains a Stack class that is a specialization of Vector. This could be considered a design flaw because the inherited get() method from Vector ignores the LIFO constraint of the Stack. Here is a simple example of a stack with the operations described above (but no error checking) in Python. class Stack(object):

def __init__(self):

self.stack_pointer = None

def push(self, element):

self.stack_pointer = Node(element, self.stack_pointer)

def pop(self):

e = self.stack_pointer.element

self.stack_pointer = self.stack_pointer.next

return e

def peek(self):

return self.stack_pointer.element

def __len__(self):

i = 0

sp = self.stack_pointer

while sp:

i += 1

sp = sp.next

return i

class Node(object):

def __init__(self, element=None, next=None):

self.element = element

self.next = next

if __name__ == '__main__':

# small use example

s = Stack()

[s.push(i) for i in xrange(10)]

print [s.pop() for i in xrange(len(s))]

The above is admittedly redundant as Python supports the 'pop' and 'append' functions to lists.

Write a code to implement the insertion sort?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[5]={5,2,8,9,4};

int i, k,temp;

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

{

for(k=i+1;k<5;k++)

{

if(a[i]>a[k])

{

temp=a[i];

a[i]=a[k];

a[k]=temp;

}

}

}

printf("\n sorted list=");

for(k=o;k<5;k++)

printf("%d",a[k]);

}

C program for the two dimensional array repesentation of priority queue?

//implement double ended queue using array.

#include<stdio.h>

#include<conio.h>

#define SIZE 20

typedef struct dq_t

{

int front,rear;

int item[SIZE];

}deque;

/********** Function Declaration begins **********/

void create(deque *);

void display(deque *);

void insert_rear(deque *, int);

void insert_front(deque *, int);

int delete_front(deque *, int);

int delete_rear(deque *, int);

/********** Function Declaration ends **********/

void main()

{

int data,ch,x;

deque DQ;

clrscr();

create(&DQ);

printf("\n\t\t Program shows working of double ended queue");

do

{

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

printf("\n\t\t 1: insert at rear end");

printf("\n\t\t 2: insert at front end");

printf("\n\t\t 3: delete from front end");

printf("\n\t\t 4: delete from rear end");

printf("\n\t\t 5: exit. ");

printf("\n\t\t Enter choice :");

scanf("%d",&ch);

switch(ch)

{

case 1:

if (DQ.rear >= SIZE)

{

printf("\n Deque is full at rear end");

continue;

}

else

{

printf("\n Enter element to be added at rear end :");

scanf("%d",&data);

insert_rear(&DQ,data);

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 2:

if (DQ.front <=0)

{

printf("\n Deque is full at front end");

continue;

}

else

{

printf("\n Enter element to be added at front end :");

scanf("%d",&data);

insert_front(&DQ,data);

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 3:

x = delete_front(&DQ,data);

if (DQ.front==0)

{

continue;

}

else

{

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 4:

x = delete_rear(&DQ,data);

if (DQ.rear==0)

{

continue;

}

else

{

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 5: printf("\n finish");

return;

}

}

while(ch!=5);

getch();

}

/********** Creating an empty double ended queue **********/

/********** Function Definition begins **********/

void create(deque *DQ)

{

DQ->front=0;

DQ->rear =0;

}

/********** Function Definition ends **********/

/********** Inserting element at rear end **********/

/********** Function Definition begins **********/

void insert_rear(deque *DQ, int data)

{

if ((DQ->front 0)

{

printf("\n Underflow");

return(0);

}

else

{

DQ->rear = DQ->rear -1;

data = DQ->item[DQ->rear];

printf("\n Element %d is deleted from rear:",data);

}

if (DQ->front==DQ->rear)

{

DQ->front =0;

DQ->rear = 0;

printf("\n Deque is empty(rear end)");

}

return data;

}

/********** Function Definition ends **********/

/********** Displaying elements of DEQUE **********/

/********** Function Definition begins **********/

void display(deque *DQ)

{

int x;

for(x=DQ->front;x<DQ->rear;x++)

{

printf("%d\t",DQ->item[x]);

}

printf("\n\n");

}

/********** Function Definition ends **********/

Concatenate two singly linked lists?

If you mean how to add all the elements of one list to another, then the answer is simple: link the last element of one to the first element of the other and you will have one large linked list.

What is a plus equals plus plus a - minus minus a?

You mean

a += ++a - --a

?

Well, it can be anything, depending on the compiler's decision. Never ever write expressions like this.

Flowchart for Fibonacci series in c language?

#include<stdio.h>

void printFibonacci(int);

int main(){

int k,n;

long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");

scanf("%d",&n);

printf("Fibonacci Series: ");

printf("%d %d ",0,1);

printFibonacci(n);

return 0;

}

void printFibonacci(int n){

static long int first=0,second=1,sum;

if(n>0){

sum = first + second;

first = second;

second = sum;

printf("%ld ",sum);

printFibonacci(n-1);

}

}

What is the different between loop and do while loop?

no difference in logic in both of them you put the key to enter :while( ....),for(;....;)

but infor loop you have option to initialize value and make operation on it

for(int i=0;...;i++){} same int i=0; while(..){ i++;}

Purpose of printf?

print means print, f means formatting

or

printf is a output statement function in the C run-time library

example:

printf ("the value of A is %d\n", A);

Where can you download a free turbo c plus plus tutorial?

You can download Turbo C++ from the Embarcadero website for free. Embarcadero now own and develop all CodeGear products previously owned and developed by Borland, including the latest version of C++ Builder.

What is the relationship between compiler interpreter loader and linker?

Compiler - Translates High level language (C, C++ etc.) and generates Object code (machine readable but not directly executable)

Interpreter - Same as compiler but do that interactively (simply saying line by line). Interpreted languages don't usually require a linker, and employ a loading process different from that of a compiled program: the interpreter itself is linked and loaded like a compiled program (and in all probability is a compiled program), but the program interpreted by the interpreter is typically loaded through interpreter-specific means.

Linker - Connects the compiler generated object code with library code to generated independent executable (like in C, you dont write how printf() works, linker add the code for printf() function in your program)

Loader - Load the machine readable codes in memory to be executed. This step typically includes relocating the code output by the linker to a specific memory location into which the program was being loaded.

Algorithm for finding min element in a linked list?

In a linked list, there is no way to find the "min element" or any other element with a specific property except by walking through the entire list. Of course, you could store a pointer to the min element and update that each time the list changes (add ing or deleting an element), which makes the list less generic and a tiny bit slower but is worth it if you need this min element often.

Pseudocode for finding the min element (I'm assuming you're storing values and you want the element with the lowest value):

ptr_minelement = null;
ptr_element = ptr_firstelement;
while (ptr_element != null) {
if (ptr_minelement == null) or (ptr_minelement->value > ptr_element->value)
ptr_minelement = ptr_element;
ptr_element = ptr_element->ptr_next;

}

What are the Main differences between object oriented programming and generations 1-4 programming language?

Object oriented computer programming and design methodology has become popular as a way of modeling and solving many programming problems. Traditionally, the implementation of such systems has been performed using an object oriented programming language such as C++. Those skilled in the art know that object oriented programming languages share at least five unique and defining concepts or notions. These concepts, which are discussed fully in the relevant literature pertaining to object oriented programming, are known as: CLASSES, OBJECTS; INHERITANCE; POLYMORPHISM, and ENCAPSULATION. Objects communicate with one another via "message passing." A "message" is texts string such as "redraw you". When an object receives a message, a corresponding class method is executed. It is well-known that in object oriented programming, different objects of an object oriented programming language will respond to messages differently.

Shift from top-down to OOP could be called going from fourth to fifth generation, in that what it enables programmers to do better or more conveniently than they could with the more primitive languages resembles what the higher generation languages enabled them to do over the lower generation ones. OOP, however, is a paradigm shift as significant as that from first to second or, arguably, from second to third -- but far more radical than the from third to fourth. Of course, it is impossible to quantify this, but in terms of ease of programming and what OOP enables, it might be fair to say the leap from fourth-generation languages to OOP, especially what OOP has now become, can be likened to the span between binary code and BASIC. OOP supercharges the program environment.

At each generational jump from binary to assembly language, to third-generation, to fourth-generation, the leap made programming easier and enabled more complex tasks by adding layers around the central core of binary code and its next outer layer, assembly. All programming languages including the advanced OOP iterations that are out now, are parsed down to binary to be executed by the computer. It's all fundamentally the same.

OOP packaged the laborious and error-prone systems of calling subroutines with variable parameters we used in the old days with a very slick interface, an envelope of error-checking and ease of use.

Object Oriented Programming organizes programming logic around objects instead of processes (as is the case with non-OOP). Some widely used third generation, object-oriented programming languages include C++, Java, and Smalltalk. In OOP, data, and the processes that can be performed on the data, are combined into an object. In addition, objects with similar characteristics may be combined into something called a class. So when an OOP programmer creates a class and wants to categorize certain files they are able to create a sub-class. Sub-classes inherit all the characteristics and processes from the original class file that it is derived from. Inheritance is one of the most powerful features of OOP. Once a programmer creates the subclass, he can add to or change the characteristics and processes to meet the precise needs of the subclass. An example of an OOP is Microsoft Office Suite products (Word, Excel, PowerPoint, Access). First Generation Programming is a machine language. It only understands zeros and ones, so we say machine languages are binary. Second Generation Programming is also called assembly languages; it uses simple words in place of zeroes and ones. The programmer associates each assembly language statement with a specific machine language command. Third Generation Programming uses source codes that could then convert into machine language. A special computer program, called a compiler, would handle the conversion. A compiler is a computer program that translates a specific third generation language (3GL) into machine language. Forth Generation Programming languages are closer to natural language. People who have little or no programming skills can use them to write simple programs. One example of a 4GL is structured query language (SQL). Structured query language is a standard language for manipulating databases. Users can write simple SQL programs to create a database, enter data, retrieve data, and delete data. How are they similar? All programming languages still go back to the First Generation Programming language, Each generation has become more advance, with better tools and features. But each language resorts to machine language.

Where linked list is used in computer?

Linked lists are dynamic linear data containers.

They are linear because each item has one adjacent item in each direction (there is a predecessor, or none, and a successor, or none, to each item, but never more than one in each direction).

They are dynamic because the size is not generally fixed and potentially infinite. To manage such a dynamic structure, memory for new items is allocated at runtime, and the different items are connected, or linked, through pointers.

In a single linked list, a single pointer points from one item to the next item.

In a double linked list, one pointer points from one item to the next item, and a second pointer points to the previous item in the list.

What is the difference between LinkedList and Hashtable?

A linked list is made up of a sequence of connected (linked) nodes. A hashtable is usually backed by an array, and is an association of keys and values. When an object is added to the array it becomes a value; the object is hashed to get a key (an index into the array).

What are the various advantages and disadvantages of pointer in c?

The purpose of a pointer is to store a memory address and to allow indirect access to the value stored in that memory address. The act of indirectly accessing memory through a pointer is known as dereferencing. Thus a pointer is said to hold a reference; it "points" to the reference. However, a reference and a pointer are not the same thing. A reference is an alias, an alternate name by which we can refer to an existing object. An object can have many aliases, but aliases does not require any memory over and above that of the object itself, unlike a pointer which does require memory.


The address of a reference is always the address of the object being referred to (just as Bill and Billy are different ways of referencing someone with the name William). However the address of a pointer is always the address of the pointer itself; it is a variable just like any other. The value of a pointer is the address that is stored in the pointer; the address of the object being referenced by the pointer. The dereferenced value of the pointer is the value stored at that memory address; the value of the referenced object itself.


In C++, we can enumerate all the properties of a variable, a reference to that variable and a pointer to that variable like so:


int i = 42; // instantiate an integer with the value 42

std::cout<<"Address of integer: 0x"<<&i<

std::cout<<"Value of integer: "<

int& r = i; // refer to i

std::cout<<"Address of reference: 0x"<<&r<

std::cout<<"Value of reference: "<

int* p = &i; // point to i

std::cout<<"Address of pointer: 0x"<<&p<

std::cout<<"Value of pointer: 0x"<

std::cout<<"Dereferenced value of pointer: "<<*p<


The output of this code would be something like this:


Address of integer: 0x00FBFBAC

Value of integer: 42

Address of reference: 0x00FBFBAC

Value of reference: 42

Address of pointer: 0x00FBFB94

Value of pointer: 0x00FBFBAC

Dereferenced value of pointer: 42


The actual memory addresses may be different on your system. However, note that the address of the reference is the same as the address of the integer, the object being referenced. Thus r and i are aliases, different names that refer to the same object. As such they have the same value. After all, the are one and the same object. The address of the pointer, however, is a different address. This proves that pointers are variables like any other; they have an address all of their own. Note also that the value of the pointer is the address of i (and, by extension, the address of r). The third property, the dereferenced value, is unique to pointer types. This returns the value of the object that is referenced by the pointer value.


This is obviously a trivial example and doesn't cover the full range of a pointer's potential, however it's important to understand the difference between a pointer and a reference. Not all languages that support pointers also support references like C++ does, thus you will often see the terms pointer and reference being used interchangeably. C is a typical example because it has no concept of a dedicated reference types but it does have pointer types. To be crystal clear, a reference is the value stored in a pointer (the address being referred to), it is not the pointer itself.


Aside from the semantic difference, another difference between a pointer and a reference is that a reference must always refer to something whereas a pointer need not. When a pointer doesn't refer to anything in particular, it is assigned address zero. Memory address zero is a reserved address which cannot be dereferenced and a pointer that holds the value zero is said to be a null pointer. The importance of the null pointer will become clear.


Now that we have an understanding of what pointers can actually do, what are the advantages and disadvantages?


The main advantage is that we can use pointers to pass objects into functions by reference. Languages like C and C++ use pass by value semantics by default which means that when we pass an object to a function, the object is copied; only the value of the object is passed, not the object itself. If we want the function to operate upon the object itself, we must pass the object by reference, not by value. C has no concept of a reference, let alone pass by reference, but if we pass a pointer to the object, the pointer is passed by value, and since the value is a memory address, it is the same as passing the object itself, by reference.


This is both an advantage and a disadvantage. It is advantageous in that we can pass objects by reference, but it is disadvantageous because we must be certain the pointer is non-null before we can actually operate upon it. This means that every function that accepts a pointer as an argument will have to perform the exact same test upon that pointer; does the pointer actually point at something valid? In C++ we don't have this problem because we can simply uses a reference instead of a pointer argument. References can never be null; they must always refer to a valid object thus there's no need to test the reference.


However, sometimes we want to pass an object as an optional parameter rather than a required parameter, so passing a null pointer would be acceptable. We still have to perform the test for non-null, but there's no way to pass an object as an optional parameter other than by instantiating a default object which can be considerably less efficient than simply testing if a pointer is zero or not.


Another advantage of a pointer is that when we wish to allocate memory dynamically, we must keep track of the start address of the allocation. The operating system's memory manager knows exactly how much memory is allocated to each address, so when we're finished with a block of memory, all we really need is the start address and the memory manager takes care of the rest, making that block of memory available to any thread or process that requires it. However, sometimes the memory manager may report that there is insufficient memory to meet an allocation, so no memory can be allocated. That's not a problem because if an allocation cannot be met, the operating system returns address zero as the start address. so each time we make an allocation, we simply store the value returned by the allocator in a pointer. If it is null, there is no memory available. We cannot use references for this purpose because references cannot be null, thus a pointer is clearly advantageous here.


In languages that do not support pointers, such as Java, memory is allocated to the Java virtual machine (using internal pointers of course) and the JVM provides the memory management required by its programs. There's still the chance of running out of memory, but the onus of responsibility shifts to the JVM's memory manager, away from the programmer, which leads us to one of the biggest disadvantages of pointers.


If the programmer fails to keep track of memory allocations, the program will inevitably leak memory (known as a resource leak). That is, if he allows all pointers to a memory resource to fall from scope, there's no way to recover the start address and thus no way to release the memory to the system. The problem with pointers is that there is no concept of resource ownership. A pointer simply holds a memory address, but there's no way to determine if that pointer actually owns the memory it is pointing at or if it simply referring to memory that is shared. In truth, all memory is shared and no pointer can physically own the memory it points at. We can easily end up with hundreds of pointers to the same object. Therefore the programmer needs to take extra care to ensure that whenever memory is released back to the system, all pointers to that memory must be immediately nullified. This is easier said than done, because not all pointers may be in scope at the point the memory is released, thus they will still be pointing at what they believe is valid memory. Any attempt to dereference memory that has already been released will result in undefined behaviour because that memory may have already been reallocated so it may not even belong to our program let alone still be in a valid state. And if we attempt to release the same memory twice, undefined behaviour is the least of our worries. So it's important that the programmer be aware of memory ownership, even though there's no way to actually define that ownership with a pointer.


In C++ this is less of a problem because we can encapsulate resources in special classes known as resource handles. Technically, a resource handle is a smart pointer. It behaves exactly like an ordinary pointer would, except that when the smart pointer falls from scope it automatically releases its resource. Thus ownership of the resource falls to the resource handle itself. A resource handle can also pass ownership to another resource, thus ownership of a resource becomes more explicit. If we wish to share ownership, we can use specialised resources handles that deal with shared resources through reference counting. The count is increment each time we add a new handle to a resource and decremented each time we release a handle to that resource. When the count reaches zero, the resource is released when the one and only remaining handle falls from scope. We can also use weak references to shared memory. Weak references are more like ordinary pointers except they cannot release resources, but when a shared resource falls from scope, the weak references are automatically nullified. In languages that do not support resource handles, the onus is entirely upon the programmer to manage their own resources through "raw" pointers, which is a distinct disadvantage and is often less efficient because of the complexity of defining resource ownership in a system where ownership is not a native concept.


When used properly and in an organised manner, pointers have many advantages and can lead to more efficient code. For instance, being able to navigate your way through consecutive memory addresses is more efficient than navigating your way through a series of named variables. This is precisely how arrays work, even in languages that do not support native pointers. An array is simply a contiguous block of allocated memory divided into one or more elements of equal size. Knowing the start address of the allocation and the size of each element, we can refer to any one element in the array as an offset address from the start of the array. Of course most languages will provide a more convenient array index operator (technically a suffix operator) that allows use to access individual elements using a zero-based index value ([0], [1], [2], [3], etc). However, behind the scenes, we're actually doing pointer arithmetic. The 3rd element in an array can be found at index [2], but what this really means is that the element resides at the 2nd offset from the start of the array. Thus if an element is 4 bytes in length, the 3rd element will be found at the address that is 2 * 4 bytes from the start of the array. Simple pointer arithmetic like this is a constant time operation, thus we gain constant time random access to any element in the array, whether it is the first element, the last element, or any element in between.


Arrays are by far the most compact and efficient method of storing data elements of the same length and type. However arrays are not ideally suited to data that is variable length. This is because every time we add a new element to an array we must reallocate the array, which may mean copying the array to new memory. There are ways to minimise the need for reallocations, such as allocating more memory than is actually required, however this undermines the efficiency by allocating memory that might not be needed. Moreover, removing elements is problematic because we can only efficiently remove the last element in an array. In order to keep track of unused elements, the unused elements must reside at the end of the array so we only need to maintain a count of those elements, not where each one is. If we remove any other element besides the last, we must move all the elements that follow it in order to close the gap and move the gap into the unused block.


To maintain efficiency we can use other structures that do not require contiguous memory. For instance, rather than allocating a single contiguous block of memory we can divide the memory into separate blocks of equal size. Thus when we run out of space, we simply allocate a new block, leaving the original blocks in place; no need to reallocate them. This is achieved by creating an array of pointers, where each pointer refers to a separate block. We use additional memory to maintain this array, but we now have the advantage that an array can grow and shrink more efficiently without having to reallocate the existing blocks. In effect, we're actually creating an array of arrays, otherwise known as a multi-dimensional array. However, since the controlling array contains pointers and the value of a pointer is a memory address, we still have constant-time random access to any element, because when we refer to a pointer by its index within the pointer array, the return value is another array, thus we can use a two-dimensional suffix to access an individual element, just as if we'd allocated the array as a single contiguous block. This would not be possible without pointers.

Moreover, we can also more easily remove and insert elements from either end of the array, but not from the middle.


If we need to add or remove lots of elements in the middle of a structure, then we need a more complex structure known as a linked list. Each element in a linked list is a node and each node points to the next node. The last node, the tail node, simply points to null and we only need to keep track of the first node, known as the head node. We lose constant time random access as well as bidirectional access because, to locate any node, we must traverse the pointers from the head node. However, since the structure is now encapsulated by the nodes themselves, the nodes can physically reside anywhere in memory and we can insert and extract nodes simply by adjusting the internal pointers between the affected nodes. For instance, if we have nodes x, y and z where x points to y and y points to z, in order to remove y we first navigate our way from the head until we reach x (because it points to y, the node we wish to delete). We then copy the value of y's pointer (which holds the address of z). We can then release y from memory and finally replace x's pointer with the value we copied. So x now points to z.


With a linked list, all insertions must be done at the head because that's the only node we have constant time access to. However, if we point the tail back at the head instead of null we create a circular linked list. Since the tail now points at the head, if we keep track of the tail rather than the head then we get constant time access to both the head and the tail. Now we can insert at either end of the list in constant time. Although lists do not allow constant time access to any element in the lists, if we need to perform a lot of insertions in the middle of the list, it is more efficient than using an array because we only need to maintain the internal links of the affected nodes, we don't have to reallocate or move elements around in order to make room for insertions or cater for extractions. The pointers consume additional memory of course (one word per element) however it is more efficient overall. If we require bidirectional traversal, then we can add a second pointer to each node so that they point to the previous element as well as the next.


The concept of a bidirectional node can be extended so that rather than simply pointing forwards and backwards through a list, we can point left and right to construct binary trees, where every node has up to 2 child nodes. These are particularly useful when we wish to sort data. Each node represents the root of its own subtree such that the left node points to a subtree with values that are less than this node's value while the right node points to a subtree with values that are not less than this node's value. Thus when we insert values, we simply navigate from the root of the tree, traversing left or right according to the value in the current node until we reach a null pointer in the required direction. We then insert a new node at that position. If there is no root, the value simply becomes the root node. New nodes always have null child pointers.


Of course a binary tree can easily end up unbalanced such that there are more nodes on one side of the root than the other. To improve efficiency, we can adjust the internal pointers upon each insertion to maintain balance, making it more efficient to locate a value regardless of which side of the subtree it is situated. To enable this balancing, we need to insert sentinel nodes, which uses additional memory, but improves the overall efficiency of the structure. These trees are typically known as self-balancing trees or red/black trees.


All these non-contiguous structures (and more besides) share a common aspect in that they all use pointers to iterate through the sequence. In C++ we can take advantage of this and create an iterator class. This behaves much like a pointer would (and is not unlike a smart pointer in that respect), but we can overload the increment and decrement operators to make traversal of these structures more intuitive. Normally, when we increment a pointer, the value of a pointer is incremented or decremented by the number of bytes dictated by its type. But an iterator can be forced to replace its value with that of another pointer. Thus if we have an iterator to the start of a sequence and we increment that iterator, we automatically traverse to the next element in the sequence.


Pointers clearly have many advantages, but not all languages implement them. This is primarily because pointers are low-level and require a lot of knowledge and skill to use them properly and safely. High-level languages do implement pointers but they hide the low-level implementation details within objects including resource handles, smart pointers, iterators and so on. Thus complex structures like lists and binary trees are still possible in high level languages without using raw pointers but, behind the scenes, there's still a lot of raw pointer arithmetic going on, it's simply hidden from view and that much harder to reach them (if at all). But knowing what's going on behind the scenes inevitably leads to more efficient code and who knows...? It may even spark an interest in getting to grips with low-level coding. If you want speed and efficiency, pointers are the fundamental means of achieving it.

Why you use structure in c language?

Structures are handy if you have a certain set of types of information that will be used repeatedly. For example, a structure called 'address' might consist of the street number, the city, the zip code, the state, and the country. You could then create several instances of this structure, maybe for several employees or something. Each instance would have its own zip code, state, country variable etc.

What is a local variable and What statements are able to access a local variable?

A local variable is a variable that can only be called on by the module. Where as a global variable can be called upon by any module. Only statements made inside the same module can call on a local variable.

How do you write a graphics program of Indian flag using class?

// INDIAN FLAG : SIMPLE PROGRAM //

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int gdriver=DETECT, gmode, xc=195, yc=175;

double deg, max, x, y, newx, newy, i, move;

initgraph (&gdriver, &gmode, "--path--");

rectangle (100, 100, 300, 250);

rectangle (90, 100, 100, 400);

line (100, 150, 300, 150);

line (100, 200, 300, 200);

setfillstyle (SOLID_FILL, LIGHTRED);

floodfill (110, 110, WHITE);

setfillstyle (SOLID_FILL, WHITE);

floodfill (110, 160, WHITE);

setfillstyle (SOLID_FILL, GREEN);

floodfill (110, 210, WHITE);

setfillstyle (SOLID_FILL, BROWN);

floodfill (91, 101, WHITE);

setcolor (BLUE);

circle (xc, yc, 20);

deg= (double)(15 * 3.14 / 180);

max= (double)(360 * 3.14 / 180);

move= deg;

x= xc;

y= 155;

line (xc, yc, x, y);

for (i= deg; i<= max; i+= move)

{

newx= xc + (x-xc)*cos(i) + (y-yc)*sin(i);

newy= yc - (x-xc)*sin(i) + (y-yc)*cos(i);

line (xc, yc, newx, newy);

}

putpixel (xc, yc, 15);

delay (500);

getch ();

closegraph ();

}

What is the algorithm for swapping two numbers?

In order to swap two values in computer memory you need to temporarily store one of those values during the exchange. That is, to swap the values of object's A and B we must perform the following steps where T is a temporary object:

  1. Copy the value of A to T
  2. Copy the value of B to A
  3. Copy the value of T to B

Note that this algorithm does not implement a "perfect swap". That is, when two people (the objects) swap their house keys (the values), we don't require a third person (the temporary object) to hold a copy of one of those keys, we simply hand the actual keys over (perhaps even simultaneously). No copies are made or indeed required -- we simply "move" values from one place to another.

But a computer cannot move values from one place to another, it has to copy them via assignment. If we assign the value of B to A then both A and B would hold the same value. The original value of A is lost, hence we require a temporary object to hold the value of A.

Note that although languages like C++ support a native move operation, the move operation applies to resource handles only. When we swap a resource, the resources don't actually move, the only thing that changes is the "owner" of the resource. That is, it is the handle's that swap, not the resources. Even so, with move semantics we still need a temporary:

  1. Move ownership of A's resource to T
  2. Move ownership of B's resource to A
  3. Move ownership of T's resource to B

This is as close to the "perfect swap" as we can get, but it does not apply to primitive data types like numbers because the notion of "ownership" only applies to resource handles. The most efficient way to "move" a primitive data type is simply to copy it.

How linker works?

There is no such thing as a Java "linker". There is, however, the concept of a classloader which - given an array of java byte codes from "somewhere" - can create an internal representation of a Class which can then be used with new etc...................