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

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.

How do you copy and paste in turbo c compiler?

Press key F2, or select command Save from menu File.

Write a program in c language to accept 10 strings as input and printthem in lexicographic order?

#include <stdio.h>

#include <string.h>

void main()

{

char a[4][25],temp[25];

int i,j;

clrscr();

printf("Enter the names\n");

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

gets(a[i]);

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

for (j=i+1;j<4;j++)

{

if (strcmp(a[i],a[j])>0)

{

strcpy(temp,a[i]);

strcpy(a[i],a[j]);

strcpy(a[j],temp);

}

}

printf("Sorted strings are \n");

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

puts (a[i]);

getch();

}

How do you calculate cgpa using c plus plus program?

#include <stdio.h>

void main()

{

int num,num1,num2, cal;

num=cal=0;

char grade1=cal=0,grade2=cal=0;

printf("\n Enter the number of subjects taken in Spring Semester:");

scanf("%d", &num);

fflush(stdin);//

if(grade1==4){

printf("\n\nEnter the Math Grade(A,B,C): %c",grade1);

do{

printf("\ngrade1=");

scanf("%d",&cal);

}

else if(

printf("\nError!\n\n");

}while(1);

printf("\nEnter the Math Credit hours(1~3):");

num1 = getchar();

grade1=4;

}

else if(grade2==3){

grade2=3;

}

printf("\nEnter the Math Grade(A,B,C):\n");

scanf("%c",&grade1);

printf("Enter the Physics Grade(A,B,C):");

grade2 = getchar();

printf("\nEnter the Physics Credit hours(1~3):");

num2 = getchar();

printf("\nMath Credit hours: %d",num1);

printf("\nPhysics Grade: %c",grade2);

printf("\nPhysics Credit hours:%d\n",num2);

printf("\n <Math Credit hours> \n");

do{

printf("\n 1 + 1 = ");

scanf("%d", &cal);

}while(cal != 3);

printf("\n Error!\n\n");

}

printf("\n <Physics Credit hours> \n");

do{

printf("\n 4 - 1 = ");

scanf("%d", &cal);

}while(cal != 3);

printf("\n Error!\n");

}

printf("\n The End.\n");

system("pause");

}

What is priority queue?

A priority queue is a collection of elements that each element has been assigned a priority and such that order in which elements are deleted and processed comes from the following riles: 1) An element of higher priority is processed before any element of lower priority. 2) Two element with the same priority are processed according to the order in which they were added to the queue.

What do you mean by garbage collection in data structure?

all classes and variables created in a program are put on something called the Heap, which is stored in main memory (RAM).

The Garbage collector gets rid of any class or variable that becomes impossible to reference ever again in the program.

For example, say you have a main method and from there you call another method, any local variables created in that other method will be put on the heap while they are in use, ie that function is running. As soon as the method ends, the Garbage collector will come and "release" the memory where those variables were for use in other parts, because you can never access them again when the method finishes.

Write program in c to show the pattern 1 23 456?

hi.... char *arr = argv[1];

if (arr!=NULL)

{

int len = strlen(argv[1]);

len--;

arr = argv[1];

while(len>=0)

printf("%c",arr[len--]);

}

How are string stored in c language?

Strings represented by the language character set (e.g., ASCII) are stored as null-terminated arrays of type char. Wide-character strings are stored as null-terminated arrays of type wchar_t. Other types are also available, such as char16 and char32 (for UTF16 and UTF32 encodings, respectively).

How do you download c programming software?

Visual C++ Express http://www.microsoft.com/exPress/ is the best place to get a full C++ IDE (Integrated Development Environment). There are many other IDEs around, but I find this is the best one.

Other IDEs:

Eclipse http://www.eclipse.org/cdt/

Code::Blocks http://www.codeblocks.org/

Bloodshed Dev-C++ http://www.bloodshed.net/devcpp.html

Write a c program to print 1 232 34543 5678765 using for loop?

If you visualize 1123581321 as a set of numbers (without spaces in between) instead of one number, you'll see that it's a set of the first 8 Fibnocci nos. - 1 1 2 3 5 8 13 21.

The following program would print the required no. using a For looping structure.

int main (void)

{

int i=1;

int j=1;

int k, num=1;

for(k=0; k<7; k++){

if(k==0){

printf("%d", num);

}

printf("%d", num);

//next no. is the sum of previous two nos. in the series

i=j;

j=num;

num=i+j;

// 1 + 1 = 2 ... 1 + 2 = 3 ... 2 + 3 = 5 ... 3 + 5 = 8

// sum=i + j ... i takes value of j ... j takes value of sum ... repeat.

}

}