What is the major difference between c and c plus plus?
C is an imperative (procedural), structured paradigm language whereas C++ is multi-paradigm: procedural, functional, object-oriented and generic. Both are high-level, abstract languages. While C's design provides constructs that map efficiently to machine code instructions, C++ is more abstract, relying heavily upon object-oriented principals. However, both are equally capable of producing highly-efficient machine code programs. C++ derives almost directly from C thus everything you can do in C you can do in C++ with relatively minor alterations to the source. C++ was originally called C with Classes and that pretty much sums up the main difference between the two languages. However, there are many subtle differences.
One key difference between C and C++ is in the struct data type. In C, a struct can only contain public data members (with no methods). In C++, a struct is similar to a class, combining data and the methods that operate upon that data into a single entity (an object). The only difference between a C++ struct and a C++ class is that class members are private by default whereas struct members are public by default.
Another key difference is that because C++ is object oriented, there is much less reliance upon the programmer to manage memory. Each object takes care of its own memory allocations (including embedded objects), thus the programmer simply creates and destroys objects as needed. Thus C++ is much easier to work with, especially with regards to highly-complex hierarchical structures, but is every bit as efficient as C.
Both languages are highly popular and there are few architectures that do not implement suitable compilers for both. Thus they are both highly portable. However, the object oriented approach to programming gives C++ a major advantage over C in terms of code re-usability, scalability and robustness.
What is mean by selection sort?
Selection sort works by looking for the largest value in a set and swapping it with the last value. The last value can now be ignored since it is now in place. The process repeats with the remainder of the set. After repeated passes, the remainder of the set will have only one item, the smallest value, at which point all the values will be in sorted order.
The algorithm is similar to that of bubblesort, but is generally more efficient because there can only be one swap at most for each iteration of the algorithm. With bubble sort, there may be multiple swaps per iteration. However, while the number of comparisons is the same for both algorithms, bubblesort can be optimised to minimise the number of iterations required and thus minimise the number of comparisons. Nevertheless, swapping is a more expensive operation than comparing, thus selection sort is generally faster.
Neither algorithm is suitable for sorting large sets of data.
What is the formula to find sum of n even numbers?
Sum = n/2[2Xa1+(n-1)d] where n is last number, a1 is the first number & d is the common difference between the numbers, here d=2 for the even /odd numbers. Sum = n/2 [2Xa1+(n-1)2]
Why should all of the elements in an array have the same data type?
Yes all of the elements in array must be the same type.
Because when you define an array you specify the type of data it will hold.
Examples in C:
int IntArray[10]; // an array of 10 integers
double FloatArray[20]; // array of 20 double floating point numbers
1
Is it true or false that a dynamically linked list can be accessed both sequentially and randomly?
No. Linked lists require traversal, and are therefore accessed sequentially. For random access you need an array. An array of pointers to the data in your list would do, but you will incur an overhead in creating the array on top of the list.
what is void data type
Void is an empty data type normally used as a return type in C/C++, C#, Java functions/methods to declare that no value will be return by the function.
The another use of void is to declare the pointer in C/C++ whe It is not sure that what data type will be addressed by the pointer.
eg:
void *p;
Here p can hold the address of int or float or char or long int or double.
Can you use pointers in c language?
Pointers in C allow you to transfer references data around without transferring the data itself.
Think of it like a parcel. Instead of sending the entire package which might cost a lot in postage, you send a slip of paper with the location of the package and the other person goes and collects it from that location. The "cost" of sending the slip of paper is comparable to the memory and time "cost" of transferring data in a program.
To take the analogy further, you can send slips of paper to lots of people who can all go and reference the same package, which saves you making copies of the package which would take up more space (memory).
Write a java program to print the last digit in Fibonacci series?
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
What are the advantages of functionalism?
Functionalism is defined as the theory that all aspects of a society serve a function and are necessary for the survival of that society, the theory that mental states can be sufficiently defined by their cause, their effect on other mental states, and their effect on behavior. The advantages are a reassurance of our values, boundary formation, social change, and social affirmation.
Why do more programmers prefer to write programs using high level language than low level language?
A high level language like Java is easier for programmers (Us) to understand. The machine language will be in binary & byte codes which is very difficult for the normal man to decipher and understand. Hence we prefer writing the code in HLL and then have a compiler or interpreter convert it into machine language for the machine to understand.
When should a for loop be used instead of a while loop?
The golden rule in iteration: everything done with a for loop can be done with a while loop, BUT not all while loops can be implemented with a for loop. for-loops are just a short-cut way for writing a while loop, while an initialization statement, control statement (when to stop), and a iteration statement (what to do with the controlling factor after each iteration). = Examples of for-loops = The most basic use for using for-loops is to do something a set number of times: for(int k = 0; k < 10; k++); // this loops runs for 10 times another less common use of the for-loop is traversing raw listNodes, since it does contain an initialization(finding the first node), control (as long as there is a next node), and a iteration statement (get my next node). i.e.: for(ListNode temp = startingNode; temp != null; temp = temp.getNext); // this traverses the entire ListNode list and stops when it has exhausted the list = How to implement for-loops using while loop = Basically for loops are just short hand for while loops, any for loop can be converted from: for([initialize]; [control statement]; [iteration]); to [initialize]; while([control statement]) { //Do something [iteration]; } These two does the exact same thing. = For When Only while Loop can be used = while-loops are used when the exiting condition has nothing to do with the number of loops or a controll variable, maybe you just want to keep prompting the user for an input until the given input is valid, like the following example which demands a positive number: int x = [grab input]; while(x < 0) { // Do code
x = [grab input];
} It is true that, when used as intended, a for loop cannot do everything a while loop can, however, in reality, for loops are just as versatile. For example, the above while loop can easily be rewritten to be a for loop as so:
for(int x = [grab input]; x < 0; x = [grab input]){
// Do Code
}
The above for loop behaves exactly like the while loop in the previous heading. A better example of a while loop that should not be a for loop might be:
while(true){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}
Here, the checking of the condition comes in the middle of the processing for the while loop, whereas the condition checked in a for loop is always done at the beginning of the loop. Also, the "iteration" statement is non-existant and is a factor of processing done somewhere else in the while loop. Finally, there was no initialization for this while loop. However, this while loop can still be written as a for loop:
for(;true;){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}
As you can see, a for loop is exactly like a while loop if you leave out the initialization and iteration sections (you still needs the semicolons, to signify those parts of the for loop are still there, they just do nothing). However, it is clear that when you do not need the extra portions of the for loop, why not just use a while loop?
The basic for loop was extended in Java 5 to make iterating over arrays and other collections more convenient. See this website for further explanation:
(http://www.leepoint.net/notes-java/flow/loops/foreach.html)
Java programming to check a digit number is palindrome or not?
import java.io.*;
public class chuva
{
public static void main(String[] args) throws Exception
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
int rem, quo, rev=0;
System.out.println("Enter a number: ");
int a = Integer.parseInt(x.readLine());
int b=a;
for(int ctr=0; ctr<=a; ctr++)
{
rem = a%10;
a = a/10;
rev = rev*10 +rem;
ctr=0;
}
System.out.println(+rev);
if(b==rev)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
What is the difference between a programming language and an Application Programming Interface?
By what I think you asked yes but I can't give you a definite answer because your question does not make sense.
By what I can gather I think you accidently put that is after language.
Application-oriented languages are specialized languages which may be specified and implemented based on general-purpose languages and their implementations. The model used to introduce the specialized languages is based on translation. A simple model supports modifications and extensions of the general language only. An alternative model has an initial phase for defining a semantic basis for the specialized language in the form of a set of abstractions to model the concepts and notions of the application area. The use of specialized languages can be seen as an abstraction process, where several levels of languages (or language parts) are defined.
How do you get a recursive pattern?
A recursive pattern is a pattern that goes like this 2,4,6,8 and on. A pattern rule which is used to find the next term.
What is storage qualifiers in c?
A C++ qualifier is a keyword that contains semantic information related to a type. In other words, they are used to qualify a type.
The C++ qualifiers are const, mutable, restrict and volatile. Qualifiers do not alter the type's storage capacity nor how that storage is interpreted in any way.
The const qualifier is used to qualify that a type cannot be modified once it is initialised. As such, constants must be initialised at the point of instantiation, like so:
const int meaning_of_life = 42;
The const qualifier can also be used to qualify function arguments, like so:
void foo(const int x) { ... }
In this case, x is instantiated and initialised whenever the function is called.
The const qualifier can also be used with class instance methods (non-static member functions):
void foo::method() const { ... }
In this case, the const qualifier applies to the hidden this pointer, and thus ensures that the instance's non-mutable member attributes cannot be modified. Constant member methods may only access other other constant member methods of the same instance.
The mutable qualifier applies to class instance member data only. Data that is qualified as mutable may be modified by any method, including constant methods. You will typically use this feature for internal class data that does not alter the outward appearance of an object in any way. For instance, you might use internal caches or counters that do not alter the outward appearance of an object. If these members were not qualified as mutable, you wouldn't be able to modify them from within any constant methods. Note that the compiler uses a bitwise constant check to ensure all members remain unchanged within a constant method. The mutable keyword simply excludes the specified members from this check.
The restrict qualifier is not strictly a qualifier as it is not part of the C++ standard (it was introduced in C99) but some compilers support it. The restrict qualifier applies to pointers and references and merely provides a promise to the compiler that the object being referenced will only be access through the restricted reference or through copies of that reference.
The volatile qualifier denotes that a memory location may be modified by hardware or by external processes.
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
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.