What is singleton class it's implementation?
"A class containing a static variable that stores a unique, and inaccesible
to external classes (private), intance of itself. The static variable is
accessed by a static method, with public access, usually called getIntance.
The static variable is initiated by the static getInstance method that
validates wether or not the static variable already exits. If the static
variable has not being initiated, a new instance of the class is created
and assigned to the static variable which reference is then returned by the
method. If the static variable was previously created, the method will
return a reference to the static variable." 1
A Singleton class is used when you wish to restrict instantiation of a class to only one object.
"Simple Singleton Pattern Example in AS3
class Data
{
private static var dataInstance:Data;
public static function getInstance():Data
{
if(!dataInstance) dataInstance = new Data();
return dataInstance;
}
public function Data()
{
if(dataInstance) throw Error("instance exists, please use Data.getInstance()");
}
}
" 2
1 [Daniel Guzman - AS3 Object Oriented Programming]
2 [Daniel Guzman - AS3 Object Oriented Programming]
Structure is like a architects drawings for a building it is the basis and a plan we need for building anything solid upon,foundation and good plans for the structure show the way we wish things to be,these plans helps us if there are any blips along the way. Structure provides a plan and an idea for advancing an idea and letting it exist for as long as is necessary until other elements interfere with or change our original plans.
How do you write a program to find out largest number between two numbers using ternary operators?
// assume you want the find the largest of ints a,b,c:
int max = (a>b&&a>c?a:b>c?b:c);
C program for Sum of n number using if else statements?
// Why do you need if/else statements?
int main()
{
int numbers[] = {1, 2, 3, 4, 5, 6}; // and so on
int i;
int sum = 0;
for(i = 0; i < sizeof (numbers)/sizeof(int); i++)
sum += i;
return sum;
}
Where does global variables stored in C?
It depends entirely on what platform you are using.
In an embedded environment, for instance
global/static variables go into different RAM memory segments depending on whether or not they are initialised.
constants are often left in ROM
automatic variables are normally placed of the stack of the currently running task but not always.
Differences between Quicksort and selection sort?
Bubble sort is easy to program, slower, iterative. Compares neighboring numbers swaps it if required and continues this procedure until there are no more swaps
Quick Sort is little difficult to program, Fastest, Recursive. Pivot number is selected, other numbers are compared with it and shifted to the right of number or left depending upon criteria again this method is applied to the left and right list generated to the pivot point number. Select pivot point among that list.
/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/
#include<stdio.h>
#include<conio.h>
main()
{
int count,i,j;
char str[50];
printf("Enter string : ");
gets(str);
for(i=0;i<=strlen(str)-1;i++)
{
count=1;
if(str[i]==' ')
continue;
for(j=i+1;j<=strlen(str)-1;j++)
{
if(str[i]==str[j])
{
str[j]=' ';
count++;
}
}
printf("%c : %d \n",str[i],count);
}
getch();
}
/*Answered by Ankush Monga
Doing DOEACC B level*/
Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.
AnswerFunction pointers are the only way for "Interept programming". In UNIX all the Interepts are called using function pointers. This is mainly used in system programming. Answerits nothing but a pointer to function. which is similar to ptr to a variable, if we are saying ptr to a variable then it will hold address of the variable like that fn. ptr will have the address of the function..one of the major application of the function pointer is call back function.. i.e callback.
AnswerPointers to functions/methods/subroutines aka 'Delegates' are frequently used in .NET programming especially in EventHandling, MemberInvoking<><><>
A function pointer is used to pass a function as an argument to another function, or to store a function as a data item, for example a list of functions can be implemented as an array of pointers to functions. Function pointers are used to store interrupt handlers in tables.
What if a final keyword is applied to a function?
The final keyword in JAVA means that the class can no longer be derived, i.e. it cannot be used as a base class for a new child class.
If you declare a method as final, this method cannot be overridden in any of the child class that may extend this class.
What is principle the Murray Loop Test?
A Murray Loop Test is used to locate faults in networks of cables such as three-phase services, groups of underground cables, etc.
It works by using the principle equation on which the Wheatstone bridge is based: when the galvanometer is in a null condition, R1/R3=R2/R4.
The location of a cable fault within a network can be found by using a process of elimination: good connections are identified and are then excluded from further iterations of the Murray Loop Test.
What are global variables explain with examples?
Variables that the program can use everywhere in the program. Example:
int x = 5;
int main(void)
{
x = 6;
foo();
return 0;
}
void foo(void)
{
x = 5;
}
How to remove duplicate values in an array?
The simplest way of doing this is not very efficient, but provides a quick development time. You want to take all of the elements of the array, add them to a Set, then retrieve them as an array again. Note that this will probably not preserve the order of elements in the array.
{
Object[] originalArray; // let's pretend this contains the data that
// you want to delete duplicates from
Set newSet = new HashSet();
for (Object o : originalArray) {
newSet.add(o);
}
// originalArray is now equal to the array without duplicates
originalArray = newSet.toArray();
}
Now the efficient, and more correct, solution.
This one will create a new array of the correct size without duplicates.
{
Object[] originalArray; // again, pretend this contains our original data
// new temporary array to hold non-duplicate data
Object[] newArray = new Object[originalArray.length];
// current index in the new array (also the number of non-dup elements)
int currentIndex = 0;
// loop through the original array...
for (int i = 0; i < originalArray.length; ++i) {
// contains => true iff newArray contains originalArray[i]
boolean contains = false;
// search through newArray to see if it contains an element equal
// to the element in originalArray[i]
for(int j = 0; j <= currentIndex; ++j) {
// if the same element is found, don't add it to the new array
if(originalArray[i].equals(newArray[j])) {
contains = true;
break;
}
}
// if we didn't find a duplicate, add the new element to the new array
if(!contains) {
// note: you may want to use a copy constructor, or a .clone()
// here if the situation warrants more than a shallow copy
newArray[currentIndex] = originalArray[i];
++currentIndex;
}
}
// OPTIONAL
// resize newArray (using _newArray) so that we don't have any null references
String[] _newArray = new String[currentIndex];
for(int i = 0; i < currentIndex; ++i) {
_newArray[i] = newArray[i];
}
}
---------
The second version is correct in theory. However, if you deal with large two- or more- dimensional arrays, you are in trouble, as with each new element in the destination array you will have to search through a greater number of elements.
This is especially true if you look for duplicates in more than one element of the array, for example looking in columns 'a' and 'b' of array
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
Drop in performance is unbelievable if you go over approx 1,000 records with majority or records being unique.
I am trying to test a couple of different approaches for large arrays. If anyone is interested, let me know, and I will keep you posted.
What is ide in c plus plus .explain?
There is no single answer to this since everyone has their own opinion on what is the best IDE. For example, a Windows programmer will probably advocate Borland/Embarcadero C++ Builder or Microsoft VC++, while a Linux programmer might favour a more generic implementation such as GC++, which is better suited to cross-platform coding.
Can you declare a function in the body of another function in c language?
yes, we can not declare a function in the body of another function. but if we declare a function in the body of another function then we can call that very function only in that particular function in which it is declared; and that declared function is not known to other functions present in your programme. So if a function is required in almost all functions of your programme so you must declare it outside the main function i.e in the beginning of your programme.
What are five common utility programs that are normally included in the operating system?
1.disk cleaner
2.cc Cleaner.
3.any anti virus.
Write a C program to implement dequeue?
Write a C program for Dequeue
#include#include
struct dll
{
struct dll *llink;
int data;
struct dll *rlink;
};
typedef struct dll node;
node *first=NULL, *new;
void create(),insertbeg(),insertmid(),insertend(),insert();
void delbeg(),delmid(),delend(),delete();
void display();
void main()
{
char ch='y';
int c;
clrscr();
while(ch=='y')
{
printf("1. create 2. insert 3. delete 4. display 5.exit\n");
printf("enter u'r choice");
scanf("%d",&c);
switch(c)
{
case 1: create();break;
case 2: insert();break;
case 3: delete();break;
case 4: display();
}
printf("do u want to continue(y/n)");
fflush(stdin);
scanf("%c",&ch);
}
}
void create()
{
node *temp;
char ch='y';
temp=first;
do
{
printf("enter the data for new node\n");
new=(node *)malloc(sizeof(node));
scanf("%d",&new->data);
if(first==NULL)
{
first=new;
temp=new;
new->llink=NULL;
new->rlink=NULL;
}
else
{
temp->rlink=new;
new->llink=temp;
new->rlink=NULL;
temp=new;
}
printf("do u wnat to create another node(y/n)");
ch=getchar();
fflush(stdin);
}while(ch!='n');
}
void insert()
{
char ch='y';
int c;
while(ch=='y')
{
printf("1.insertbeg 2: insertmid 3. insertend 4. exit\n");
printf("enter u'r option");
scanf("%d",&c);
switch(c)
{
case 1:insertbeg();break;
case 2:insertmid();break;
case 3:insertend();
}
printf("do u want to continue(y/n)");
fflush(stdin);
ch=getchar();
}
}
void insertbeg()
{
printf("enter data for node to aded in the beginnning");
new=(node *)malloc(sizeof(node));
scanf("%d",&new->data);
if(first==NULL)
{
first=new;
new->llink=NULL;
new->rlink=NULL;
}
else
{
new->rlink=first;
first->llink=new;
new->llink=NULL;
first=new;
}
}
void insertmid()
{
int pos,i=1;
node *temp;
temp=first;
printf("enter the positon to insert a node");
scanf("%d",&pos);
printf("enter data for node to be added at a positon %d",pos);
new=(node *)malloc(sizeof(node));
scanf("%d",&new->data);
while(i {
temp=temp->rlink;
i++;
}
temp->rlink->llink=new;
new->rlink=temp->rlink;
temp->rlink=new;
new->llink=temp;
}
void insertend()
{
node *temp;
temp=first;
printf("enter data for node to be aded at end");
new=(node *)malloc(sizeof(node));
scanf("%d",&new->data);
while(temp->rlink!=NULL)
{
temp=temp->rlink;
}
temp->rlink=new;
new->llink=temp;
new->rlink=NULL;
}
void delete()
{
char ch='y';
int c;
while(ch=='y')
{
printf("1. delbeg 2. delmid 3. delend 4. exit\n");
printf("enter u'r option");
scanf("%D",&c);
switch(c)
{
case 1: delbeg();break;
case 2: delmid();break;
case 3: delend();break;
}
printf("do u want to continue(y/n)");
ch=getchar();
}
}
void delbeg()
{
node *temp;
if(first==NULL)
{
printf("deletion not possible");
exit();
}
else
{
first=first->rlink;
first->llink=NULL;
}
}
void delmid()
{
int pos,i=1;
node *temp;
temp=first;
printf("enter the position to delete a node");
scanf("%d",&pos);
while(i {
temp=temp->rlink;
i++;
}
temp->rlink->rlink->llink=temp;
temp->rlink=temp->rlink->rlink;
}
void delend()
{
node * temp;
temp=first;
while(temp->rlink->rlink!=NULL)
{
temp=temp->rlink;
}
temp->rlink->llink=NULL;
temp->rlink=NULL;
}
void display()
{
node *temp;
if(first==NULL)
{
printf("linked list is empty");
exit();
}
else
{
printf("the contents of dd are :\n");
temp=first;
while(temp->rlink!=NULL)
{
printf("%d",temp->data);
temp=temp->rlink;
}
}
}
What are the fields of a node in a linked list?
usually we have two fields they are data field and node i.e. pointer field.it also depends on type of linked list.the above said is for single linked list.And for double linked list it sontains three fields first pointer that pointes to previious node and data field and another pointer that point to next node
How do you write a c program that prints a box an oval an arrow and a diamond?
You first learn how to program in C.
You include stdio.h when writing C code. If you are writing C++ code then you must include cstdio instead. Both headers implement the C standard input and output library but cstdio is compliant with C++.
Consider the following :
#include
int main()
{
printf("Hello world\n");
std::printf("Hello world\n"); // ERROR!
}
The error occurs because there's no guarantee the printf symbol resides in the std namespace. There's a small possibility it does reside there, but it is only guranteeed to exist in the global namespace. Thus the risk of error is high.
Now consider the following:
#include
int main()
{
using std::printf;
printf("Hello world\n");
std::printf("Hello world\n");
}
The cstdio header guarantees all symbols defined in stdio.h now appear in the std namespace (where all standard library symbols rightly belong). Although there's no guarantee those same symbols also reside in the global namespace, the using keyword can be used to inject those symbols into the global namespace. In this case the using keyword is unnecessary; the code will compile with or without it.
It's important to recognise the difference between C++ standard library headers (those with no extension) and the equivalent C headers (those with a .h extension). C headers guarantee all symbols are imported to the global namespace and possiblythe std namespace. C++ headers guarantee all symbols are imported to the std namespace and possibly the global namespace. In most cases, the C++ header simply modifies the equivalent C header to render it compliant with C++. This isn't always the case, but ultimately it is much easier to inject symbols to the global namespace (with using) than it is to inject to the std namespace. The C++ headers take care of all the messy stuff for you and are less problematic in the long run.
Write a program to count the number of times the digit is present?
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[50],i,n,s,t=0;/* the digit 50 can be any integer, its a user defined function a[limit]*/
clrscr( );
printf("\n\nenter the number of elements u want to enter");
scanf("%d",&n);
printf("\n\nenter the numbers u desire");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the element to be counted");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s==a[i])
t++;
}
printf("the number u entered has occured %d number of times",t);
getch( );
}
Is java interpreter or compiler?
Java has both a compiled and an interpreted stage.
1) The programmer writes his source codes (.java extension); a compiler will compile this to bytecode (.class extension).
2) When the end-user runs the .class program, the JVM (Java Virtual Machine) will interpret this.
What are data structures and algorithms used for?
Data structures are a way of storing and organizing data on a computer so that it can be used in a way that is most efficient and uses least resources. Algorithms are step by step processes for calculations which are used for data structures.
Is the array size is fixed after it is created?
Generally, a array is fixed in size. With some libraries, however, they are extensible, either by reallocation/copying strategies (C/C++/STL), or by linking/referencing strategies (JAVA).
What is a variable that is used within a function?
If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns.
If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained.
Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument.
Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.