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

How to print prime numbers in C language program?

#include <stdio.h> void main() { int n,i=1,j,c; clrscr(); printf("Enter Number Of Terms"); scanf("%d",&n);

printf("Prime Numbers Are Following");

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

{ c=0; for(j=1;j<=i;j++) { if(i%j==0) c++; } if(c==2) printf("%d",i); } getch(); }

Which language has the simpliest syntactic structure?

I believe the correct answer is Hawaiian. The Hawaiian alphabet has only 12 letters in it, and therefore will contain the lowest number of syntactic combinations.

What is a heap?

Answer:- A sorting algorithm that works by first organizing the data to be sorted into a special type of binary tree called a heap. The heap itself has, by definition, the largest value at the top of the tree, so the heap sort algorithm must also reverse the order. It does this with the following steps:

1. Remove the topmost item (the largest) and replace it with the rightmost leaf. The topmost item is stored in an array.

2. Re-establish the heap.

3. Repeat steps 1 and 2 until there are no more items left in the heap.

The sorted elements are now stored in an array.

A heap sort is especially efficient for data that is already stored in a binary tree. In most cases, however, the quick sort algorithm is more efficient.

GOURAV KHARE (CHANDIGARH)

gouravsonu89@gmail.com

What are the differences between Macros and Functions in C Language?

Subroutine:

1.When a task is to be done repeatedly then it is written as subroutine and this subroutine will be called each time to perform that task.

2.Subroutine program will be stored in some memory location and program control will be transfered to that location each time.

3.where as in macro the number of instructions will be less than subroutine.Here each time u call a macro that set of instructions will be inserted in that location.

4.macro doesn't have return statement while subroutine has.

5.memory requirement for macro is higher.

6.execution time of macro is lesser than subroutine.

Programs to implement operations on doubly linked list in C?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack.

You can see the whole source code implementing the stack in related links, below.

What are the benefits of using Abstract Data Types?

  • The implementor of the class can change the implementation for maintenance, bug fixes or optimization reasons, without disturbing the client code.
  • The data type is defined as a set of high-level services with a semantic contract defining both the output that is provided and the input that is required from the client. This actually correspond to the general definition of a type in programming: a type is defined as a set of possible values and a set of operations available on these values.

Why should an object hide its data?

As my AP Computer Science teacher eloquently said:

"You can't let strangers touch your private parts"

Therefore, if data was not hidden, a client class can modify all the variables(given that it is not a final constant) by simply calling:

className.variableName = newWrongData;

By using special methods to access the variables, the class can change its internal implementation, without breaking compatibility with other classes that are already using it.

What is inbound coach operator do?

someone who sorts out coaches for tourists

What are the Characteristics of primitive mode of production?

Primitive mode of production was characterized by the following features or characteristics.

Ownership of the means of production was collective

It was a classless society

Tools used in production were crude

Firstly people lived by hunting and gathering

No exploitation

People started living in caves

Write a c programm of insertion of element in the array?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],i,j,n;

clrscr();

printf("Enter the no of element you want to insert in the array : ");

scanf("%d",&n);

printf(" Enter the Array element : ");

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

{

scanf("%d",&a[i]);

}

printf("The array is : ");

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

{

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

}

printf(" Enter the element which you want to insert");

scanf("%d",&j);

printf("The array is : ");

for(i=0;i<n+1;i++)

{

if(i==n)

a[i]=j;

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

}

getch();

}

Does every line in C program end with a semicolon?

Yes, but it will be difficult, because you will need to explicitly declare any library functions and constants that you will need, and often there are many such declarations. If you are talking about a header file specific to your program, the answer is still yes; simply declare your functions and constants before you use them. This is often done in single file program that are provided on the Internet - the file will contain includes for "standard" headers, but none for specific program related headers - and the order of functions will simply be backwards, with main() at the end - or the function will be declared at the top, with main() immediately after the declarations.

What processes are needed to transform a C program to a machine language program that is ready for execution?

  • 1st create source file using text editor for the compiler
  • 2nd check source file for syntax errors by the compiler
  • 3rd save it as object file by the compiler,
  • 4th combine object files and resolve external library references to create executable file by the linker.

You can get the detailed answer on P29-31 ,Problem_Solving & program_design_in_c.

First the source files must be pre-processed. This handles all the precompiler directives (such as #include which pulls in headers) and macro definitions (such as #define statements which perform text replacements). This creates intermediate files known as translation units, which are then passed to the compiler. Each translation unit is compiled into machine code to create an object file. Finally, all the object files are linked together along with any precompiled static library functions to create the executable.

There are three stages to the translation process: preprocessing; compiling and linking

Write an algorithm for the implementation of a circular doubly linked list?

  1. Create a new node, making sure it is not allocated locally in the function and thus will not be destroyed when the function execution finishes
  2. Fill in data
  3. Use the "last node" pointer in the list and copy the "next" pointer location (pointing to the first node) into the new nodes "next" pointer
  4. Set the "last node" "next" pointer to point to the new node
  5. Change the list's "last node" pointer to point to the new node

For an example of implementation see: How_you_insert_a_newnode_in_singly_circular_link_list

Sine series program using c language?

#include<stdio.h>

#include<math.h>

void main()

{

int i = 2, n, s = 1, x, pwr = 1, dr;

float nr = 1, x1, sum;

clrscr();

printf("\n\n\t ENTER THE ANGLE...: ");

scanf("%d", &x);

x1 = 3.142 * (x / 180.0);

sum = x1;

printf("\n\t ENTER THE NUMBER OF TERMS...: ");

scanf("%d", &n);

while(i <= n)

{

pwr = pwr + 2;

dr = dr * pwr * (pwr - 1);

sum = sum + (nr / dr) * s;

s = s * (-1);

nr = nr * x1 * x1;

i+= 2;

}

printf("\n\t THE SUM OF THE SINE SERIES IS..: %0.3f",sum);

getch();

}

To get all source codes for calculating sine,cose and tan through Taylor series in C++, visit:

http://bitsbyta.blogspot.com/2011/02/calculating-sine-in-radians-using_16.HTML

http://bitsbyta.blogspot.com/2011/02/calculating-cosine-in-radians-using.HTML

http://bitsbyta.blogspot.com/2011/02/calculating-tan-in-radians-using-Taylor.HTML

Program that implements breadth first search algorithm?

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#define TRUE 1

#define FALSE 0

#define MAX 8

struct node

{

int data ;

struct node *next ;

} ;

int visited[MAX] ;

int q[8] ;

int front, rear ;

void bfs ( int, struct node ** ) ;

struct node * getnode_write ( int ) ;

void addqueue ( int ) ;

int deletequeue( ) ;

int isempty( ) ;

void del ( struct node * ) ;

int main( )

{

struct node *arr[MAX] ;

struct node *v1, *v2, *v3, *v4 ;

int i ;

v1 = getnode_write ( 2 ) ;

arr[0] = v1 ;

v1 -> next = v2 = getnode_write ( 3 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 1 ) ;

arr[1] = v1 ;

v1 -> next = v2 = getnode_write ( 4 ) ;

v2 -> next = v3 = getnode_write ( 5 ) ;

v3 -> next = NULL ;

v1 = getnode_write ( 1 ) ;

arr[2] = v1 ;

v1 -> next = v2 = getnode_write ( 6 ) ;

v2 -> next = v3 = getnode_write ( 7 ) ;

v3 -> next = NULL ;

v1 = getnode_write ( 2 ) ;

arr[3] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 2 ) ;

arr[4] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 3 ) ;

arr[5] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 3 ) ;

arr[6] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 4 ) ;

arr[7] = v1 ;

v1 -> next = v2 = getnode_write ( 5 ) ;

v2 -> next = v3 = getnode_write ( 6 ) ;

v3 -> next = v4 = getnode_write ( 7 ) ;

v4 -> next = NULL ;

front = rear = -1 ;

bfs ( 1, arr ) ;

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

del ( arr[i] ) ;

getch( ) ;

return 0;

}

void bfs ( int v, struct node **p )

{

struct node *u ;

visited[v - 1] = TRUE ;

printf ( "%d\t", v ) ;

addqueue ( v ) ;

while ( isempty( ) -1 )

return TRUE ;

return FALSE ;

}

void del ( struct node *n )

{

struct node *temp ;

while ( n != NULL )

{

temp = n -> next ;

free ( n ) ;

n = temp ;

}

}

What errors are caught only when a program executes?

The only thing that can actually be "caught" at runtime is an exception. An exception is not a run-time error as such; it only becomes a runtime error if it is not caught in which case it becomes an unhandled exception. An exception is handled the moment it is caught, and therefore isn't an error. Depending on the nature of the exception, there may be no need to alert the user that an exception has even occurred.

Runtime errors include logic errors and system errors. Logic errors are bugs that can be identified at runtime. System errors are a bit more problematic because system errors do not throw exceptions so we cannot catch them. However, many can be resolved at runtime before they become system errors. Divide by zero is an example; always check your divisors are non-zero before executing any division operation. Hardware malfunction's, on the other hand, are often beyond our control, but they are not really run time errors unless they occurred because of our code rather than in spite of our code.

What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.

Write a program to merge two linked list and find out the running time?

#include<stdio.h>

#include<conio.h>

#define null 0

void create();

void display();

void insert();

void delet();

void erase();

void create2();

void display2();

void insert2();

void delet2();

void erase2();

void link1();

void link2();

void merge();

struct node

{

int info;

struct node *next;

}*start,*ptr,*temp,*prev;

struct node2

{

int info;

struct node2 *nex;

}*start2,*ptr2,*temp2,*prev2;

void main()

{

int op;

clrscr();

do

{

printf("\n1.link1\n2.link2\n3.merge\n4.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

link1();

break;

case 2:

link2();

break;

case 3:

merge();

break;

case 4:

break;

default:

printf("\nEnter correct option");

}

}while(op!=4);

getch();

}

void create()

{

int data;

printf("\nEnter the data");

scanf("%d",&data);

do

{

ptr=malloc(sizeof(struct node));

ptr->info=data;

ptr->next=NULL;

if(start==NULL)

start=ptr;

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=ptr;

}

printf("\nEnter the data & Press 0 to terminate");

scanf("%d",&data);

}while(data!=0);

}

void display()

{

temp=start;

if(start==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp->next!=NULL)

{

printf("%d\t",temp->info);

temp=temp->next;

}

printf("%d\t",temp->info);

}

}

void insert()

{

int data,pos;

printf("\nEnter data & position to insert the element");

scanf("d",&data,&pos);

temp=start;

ptr=malloc(sizeof(struct node));

ptr->info=data;

while(temp->next!=NULL)

{

if(temp->info==pos)

{

ptr->next=temp->next;

temp->next=ptr;

break;

}

else

temp=temp->next;

}

if(temp->next==NULL)

{

temp->next=ptr;

ptr->next=NULL;

}

}

void delet()

{

int pos,flag=0;

if(start==NULL)

printf("List is empty");

else

{

printf("Enter element to be deleted");

scanf("%d",&pos);

temp=start;

if(start->info==pos)

{

flag=1;

start=start->next;

free(temp);

}

else

{

while(temp->next!=NULL)

{

prev=temp;

temp=temp->next;

if(temp->info==pos)

{

flag=1;

prev->next=temp->next;

free(temp);

break;

}

}

}

if(flag==0)

printf("\nElement is not present in list");

}

}

void erase()

{

if(start==NULL)

printf("\nList is empty");

else

{

while(start->next!=NULL)

{

temp=start;

start=start->next;

free(temp);

}

temp=start;

start=start->next;

free(temp);

printf("list is erased");

}

}

void create2()

{

int data;

printf("\nEnter the data");

scanf("%d",&data);

do

{

ptr2=malloc(sizeof(struct node2));

ptr2->info=data;

ptr2->nex=NULL;

if(start2==NULL)

start2=ptr2;

else

{

temp2=start2;

while(temp2->nex!=NULL)

{

temp2=temp2->nex;

}

temp2->nex=ptr2;

}

printf("\nEnter the data & Press 0 to terminate");

scanf("%d",&data);

}while(data!=0);

}

void display2()

{

temp2=start2;

if(start2==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp2->nex!=NULL)

{

printf("%d\t",temp2->info);

temp2=temp2->nex;

}

printf("%d\t",temp2->info);

}

}

void insert2()

{

int data,pos;

printf("\nEnter data & position to insert the element");

scanf("d",&data,&pos);

temp2=start2;

ptr2=malloc(sizeof(struct node2));

ptr2->info=data;

while(temp2->nex!=NULL)

{

if(temp2->info==pos)

{

ptr2->nex=temp2->nex;

temp2->nex=ptr2;

break;

}

else

temp2=temp2->nex;

}

if(temp2->nex==NULL)

{

temp2->nex=ptr2;

ptr2->nex=NULL;

}

}

void delet2()

{

int pos,flag=0;

if(start2==NULL)

printf("List is empty");

else

{

printf("Enter element to be deleted");

scanf("%d",&pos);

temp2=start2;

if(start2->info==pos)

{

flag=1;

start2=start2->nex;

free(temp2);

}

else

{

while(temp2->nex!=NULL)

{

prev2=temp2;

temp2=temp2->nex;

if(temp2->info==pos)

{

flag=1;

prev2->nex=temp2->nex;

free(temp2);

break;

}

}

}

if(flag==0)

printf("\nElement is not present in list");

}

}

void erase2()

{

if(start2==NULL)

printf("\nList is empty");

else

{

while(start2->nex!=NULL)

{

temp2=start2;

start2=start2->nex;

free(temp2);

}

temp2=start2;

start2=start2->nex;

free(temp2);

printf("list is erased");

}

}

void link1()

{

int op;

start=NULL;

do

{

printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

create();

break;

case 2:

display();

break;

case 3:

insert();

break;

case 4:

delet();

break;

case 5:

erase();

break;

case 6:

break;

default:

printf("\nEnter correct option");

}

}while(op!=6);

getch();

}

void link2()

{

int op;

start2=NULL;

do

{

printf("\n1.create\n2.display\n3.insert\n4.delete\n5.erase\n6.exit");

printf("\nEnter your option");

scanf("%d",&op);

switch(op)

{

case 1:

create2();

break;

case 2:

display2();

break;

case 3:

insert2();

break;

case 4:

delet2();

break;

case 5:

erase2();

break;

case 6:

break;

default:

printf("\nEnter correct option");

}

}while(op!=6);

getch();

}

void merge()

{

if(start==NULL)

{

start=start2;

}

else

{

temp=start;

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=start2;

}

temp=start;

if(start==NULL)

printf("\nList is empty");

else

{

printf("\nElements of list:\n");

while(temp!=NULL)

{

printf("%d\t",temp->info);

temp=temp->next;

}

}

getch();

}

the above listed program is definitely good one, here is a program which just does merging job, http://www.refcode.net/2013/02/merging-linked-lists.html

What is an object oriented program?

The term "object-oriented program" is essentially meaningless. Object-oriented programming applies to high-level source code, not to programs.

High-level source code does not create a program as such because because the high-level instructions are far too abstract for the machine to understand. In order to create an actual program, the high-level symbolic code must be converted to native machine code, the only language the machine actually understands. This is achieved through compilation or interpretation or a combination of the two. Machine code is an entirely procedural language; there is no such thing as object-oriented machine code.

Machine code is non-portable. To overcome this, a compiler may convert the source code to an intermediate form such as byte code. Byte code is usually portable but it still needs to be converted to native machine in order to execute. This is achieved by an interpreter. The interpreter handles the execution, converting byte code to machine code while the byte code is executing within the interpreter. Although portable, byte code has to be interpreted every time it is executed, so it is much slower to execute than native machine code. However, like machine code, byte code is entirely procedural.

Although it is (theoretically) possible to execute high-level object-oriented instructions through interpretation, it makes no sense to do so as this would only reduce performance even further. The whole point of compiling to byte code is to produce code that can be efficiently interpreted in order to keep performance as high as possible.

Write four different c statement that each add 1 to ineger variable?

i++

i=i+1

++i

i+=1

but remember the answers of i++ and ++i are not be same..

use the following program to take closer look of the values of integer 'i'.

thanks,

Happy Garcha

UFV, Abbotsford, Canada

#include<stdio.h>

main()

{

int i;

printf(" i =>%d",i);

i++;

printf(" i++=> %d",i);

++i;

printf(" ++i => %d",i);

i=i+1;

printf(" i=i+1 =>%d",i);

i+=1;

printf(" i=+1 =>%d",i);

getch();

}

How do you do calculate ovality parameter?

It is about 14 days after your last period ended. Here's a good site: http://www.fertilityfriend.com/p/163/index14.html?gclid=CNDdwsGw3JACFQqwQwodKhvUWA It will help you to calculate when you ovulate because everyone is different

C program to construct an expression tree for a valid input arithmetic expression display the expression in infix prefix and postfix forms?

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<string.h>

#include<stdlib.h>

struct treenode

{

char c;

treenode * rlink;

treenode * llink;

}*stc[30],*temp,*root;

char prefix[20],ch;

int topt=-1,max=50,len; //global declaration

void pusht(struct treenode * p);

struct treenode* popt();

void tredis(struct treenode *ptr,int level);

void exptree();

void post(struct treenode* p);

void main()

{

clrscr();

printf("

Enter a prefix expression :");

scanf("%s",prefix);

exptree();

tredis(root,1);

printf("

The postfix expression is :");

post(root);

getch();

}

void post(struct treenode* p)

{

if(p!=NULL)

{

post(p->llink);

post(p->rlink);

printf("%c",p->c);

}

}

void exptree()

{

len=strlen(prefix);

int i=len-1;

while(i>=0)

{

switch(prefix[i])

{

case '+':

case '-':

case '*':

case '/':

case '^':

temp=(struct treenode*)malloc(sizeof(struct treenode));

temp->c=prefix[i];

temp->llink=popt();

temp->rlink=popt();

pusht(temp);

break;

default :

temp=(struct treenode*)malloc(sizeof(struct treenode));

temp->c=prefix[i];

temp->rlink=NULL;

temp->llink=NULL;

pusht(temp);

}

i--;

}

root=stc[topt];

}

void pusht(struct treenode * p)

{

if(topt==max)

{

printf("

/**Beyond Capacity**/");

}

else

{

stc[++topt]=p;

}

}

struct treenode* popt()

{

if(topt==-1)

printf("

/**No Expression**/");

else

return(stc[topt--]);

}

void tredis(struct treenode *ptr,int level)

{

int i;

if ( ptr!=NULL )

{

tredis(ptr->rlink, level+1);

printf("

");

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

printf(" ");

printf("%c", ptr->c);

tredis(ptr->llink, level+1);

}

}

Is the double a modifier in c plus plus?

A double is a floating point type, greater than or equal in size to a float.