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

C program to read an array and print its reverse using pointers?

#include<stdio.h>

#include<conio.h>

// note: this code does not test for user-input errors!!

int main() {

int array[10],i,n;

printf("enter the number of elements in the array:");

scanf("%d",&n);

for(i=0;i<n;i++) scanf("%d",&array[i]);

printf("the reverse ordered array is :");

for(i=n-1;i>=0;i--) printf("%d",array[i]);

return 0;

}

Why java takes 4 byte of int while c has 2?

Java defined int as a 32-bit number because that is generally large enough to hold the information you need.

The size of an int in C may actually have either 16 or 32 bits, depending on the implementation. Basically, the specifications for any C implementation in UNIX must have 32-bit ints, while the ISO C standard only requires 16-bit ints. The stdint.h and limits.h files exist exactly because not all implementations are the same, and these files will define the min/max values of the integral types.

Do compiled programs usually run faster because they are already in machine code?

No it is because compiled programs are scared so they run like stink.

Plus, uncompiled programs, ie. source programs, do not run at all... neither slowly nor fast.

What are the different ways of constructing a multiway if-else structure?

If this is a one-off, use either SSI components from your junk-box, or one or more MUX chips, with some outputs wired back to inputs.

If you are making quantities, either use an eprom, burn it to ROM when it's debugged, or use a PLA.

Whichever you do, logical analysis first will probably save time and money in the long term.

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]

Why do you need structure?

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.

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.

Write a 'c' program to calculate the frequencies of different alphabets present in a given string the string of alphabets is to be taken as input from the keyboard?

/* 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*/

What are function pointers?

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.

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

Why do you use conio.h?

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 // also includes stdio.h

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.