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

Insert a node into a doubly linked list at nth position where n is user defined in data structure in c source code?

# include < stdio.h >

# include < stdlib.h >

struct list

{

char info[20];

struct list *next;

struct list *prev;

};

struct list *new1,*node;

void create(struct list *s,struct list *e)

{

char ch;

node=s;

printf("\nWant to create a node(y/n):");

ch=getche();

while (ch != 'n')

{

node->next = (struct list *) malloc(sizeof(struct list));

node->next->prev= node;

node = node->next;

printf("\n Enter the string value:- ");

gets(node->info);

node->next = e;

e->prev=node;

printf("\n Enter choice--'n' for break: ");

ch = getche();

}

}

void displayL (struct list *s,struct list *e)

{

node = s->next;

while (node!=e)

{

printf(" 0x%x--%s", node,node->info);

node = node->next;

}

printf("\n");

}

void displayR (struct list *e,struct list *s)

{

node = e->prev;

while (node!=s)

{

printf(" 0x%x--%s", node,node->info);

node = node->prev;

}

printf("\n");

}

void insertA(struct list *s)

{

struct list *new1;

int c=1,count;

printf("\nEnter the location:");

scanf("%d",&count);

fflush(stdin);

new1 = (struct list *) malloc(sizeof(struct list));

printf("\nEnter the new value:");

gets(new1->info);

node=s->next;

while(node)

{

if(c==count)

break;

node=node->next;

c++;

}

node->prev->next=new1;

new1->prev=node->prev;

new1->next=node;

node->prev=new1;

}

void main()

{

struct list *start,*end;

clrscr();

start=(struct list *) malloc(sizeof(struct list));

end=(struct list *) malloc(sizeof(struct list));

create(start,end);

printf("\n Created list is as follows(L ->R)\n");

displayL(start,end);

printf("\n Created list displayed from R->L\n");

displayR(end,start);

printf("\nInserting a new location at user specified location\n");

insertA(start);

printf("\n now the listfrom L ->R\n");

displayL(start,end);

printf("\n list from R to L after insertion\n");

displayR(end,start);

getch();

}

How do you write the programme that display truth table of AND gate by using C or C plus plus programming language?

#include <iostream>

int main (void)

{

std::cout << "\nBitwise AND (&):\n";

for (int a=0; a<=1; ++a)

for (int b=0; b<=1; ++b)

std::cout << a << " & " << b << " is " << a & b << std::endl; std::cout << "\nLogical AND (&&):\n";

for (int a=0; a<=1; ++a)

for (int b=0; b<=1; ++b)

std::cout << a << " && " << b << " is " << a && b << std::endl;

return 0;

}

A new programmer whom you are training is writing a C program and wants to place comment lines in the program what characters should be used at the beginning and end of the comments?

if single line comment just place // before for single line comment else if multiple line denote as like this /*.............................

..............................*/

Strictly speaking, // is non.standard in C only in C++

Insert algorithm for circular queue in data structures?

In a standard queue data structure re-buffering problem occurs for each dequeue operation. To solve this problem by joining the front and rear ends of a queue to make the queue as a circular queue

Circular queue is a linear data structure. It follows FIFO principle.

  • In circular queue the last node is connected back to the first node to make a circle.
  • Circular linked list fallow the First In First Out principle
  • Elements are added at the rear end and the elements are deleted at front end of the queue
  • Both the front and the rear pointers points to the beginning of the array.
  • It is also called as "Ring buffer".
  • Items can inserted and deleted from a queue in O(1) time.

Circular Queue can be created in three ways they are

· Using single linked list

· Using double linked list

· Using arrays

Using single linked list:

It is an extension for the basic single linked list. In circular linked list Instead of storing a Null value in the last node of a single linked list, store the address of the 1st node (root) forms a circular linked list. Using circular linked list it is possible to directly traverse to the first node after reaching the last node.

The following figure shows circular single linked list:

Using double linked list

In double linked list the right side pointer points to the next node address or the address of first node and left side pointer points to the previous node address or the address of last node of a list. Hence the above list is known as circular double linked list.

The following figure shows Circular Double linked list :-

Algorithm for creating circular linked list :-

Step 1) start

Step 2) create anode with the following fields to store information and the address of the next node.

Structure node

begin

int info

pointer to structure node called next

end

Step 3) create a class called clist with the member variables of pointer to structure nodes called root, prev, next and the member functions create ( ) to create the circular linked list and display ( ) to display the circular linked list.

Step 4) create an object called 'C' of clist type

Step 5) call C. create ( ) member function

Step 6) call C. display ( ) member function

Step 7) stop

Algorithm for create ( ) function:-

Step 1) allocate the memory for newnode

newnode = new (node )

Step 2) newnode->next=newnode. // circular

Step 3) Repeat the steps from 4 to 5 until choice = 'n'

Step 4) if (root=NULL)

root = prev=newnode // prev is a running pointer which points last node of a list

else

newnode->next = root

prev->next = newnode

prev = newnode

Step 5) Read the choice

Step 6) return

Algorithm for display ( ) function :-

Step 1) start

Step 2) declare a variable of pointer to structure node called temp, assign root to temp

temp = root

Step 3) display temp->info

Step 4) temp = temp->next

Step 5) repeat the steps 6 until temp = root

Step 6) display temp info

Step 7) temp=temp->next

Step 8) return

Using array

In arrays the range of a subscript is 0 to n-1 where n is the maximum size. To make the array as a circular array by making the subscript 0 as the next address of the subscript n-1 by using the formula subscript = (subscript +1) % maximum size. In circular queue the front and rear pointer are updated by using the above formula.

The following figure shows circular array:

Algorithm for Enqueue operation using array

Step 1. start

Step 2. if (front == (rear+1)%max)

Print error "circular queue overflow "

Step 3. else

{ rear = (rear+1)%max

Q[rear] = element;

If (front == -1 ) f = 0;

}

Step 4. stop

Algorithm for Dequeue operation using array

Step 1. start

Step 2. if ((front == rear) && (rear == -1))

Print error "circular queue underflow "

Step 3. else

{ element = Q[front]

If (front == rear) front=rear = -1

Else

Front = (front + 1) % max

}

Step 4. stop

List of not object-oriented programming languages?

Languages like C, Pascal, Basic do not use classes, polymorphism, etc, they promote functional style of programming.

Languages like PHP, Perl, Python, Ruby provide you with the opportunity to choose whether you like to program using classes, in other words in the object-oriented way, or adhere to functional programming - depending on what is optimal for the project you are working on.

Program for upper triangular matrix in arrays?

#include<stdio.h>

int main(){

int a[3][3],i,j;

float determinant=0;

printf("Enter the 9 elements of matrix: ");

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

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

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

printf("\nThe matrix is\n");

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

printf("\n");

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

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

}

printf("\nSetting zero in upper triangular matrix\n");

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

printf("\n");

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

if(i>=j)

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

else

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

}

return 0;

}

What is syntax of memcpy?

memcpy function is used to copy memory area. Syntax ------ void *memcpy(void *dest, const void *src, size_t n); *dest is a destination string. *src is a source string. n is a number of characters to be copied from source string. Example: #include #include main() { char src[]="Hello World"; char des[10]; memcpy(des,src,5); printf("des:%s\n",des); //It will contain the string "Hello". }

What are Semantic errors in a programming language?

Semantic or Syntax errors are errors in the way a programmer has written his code. The code does not conform to language standards and is incorrect.

Ex:

for(int i = 0, i++, i<10) {

}

The above is a syntactically incorrect declaration of a for loop in Java. The compiler would not let you compile this code successfully.

Why do most compilers do not directly produce machine languages but rather they produce assembly languages instead?

They don't. All compilers produce machine code by default, with the exception of the Java compiler which emits Java byte code suitable for interpretation by the Java virtual machine at runtime.

What you call assembly is actually the disassembled machine code. That is, the machine code produced by the compiler is translated into the equivalent disassembly. This is the code you see when you hit a breakpoint and examine the disassembled machine code. You will also see disassembly when debugging code for which no source code exists (as is the case with most 3rd party libraries).

If you wish to view the actual assembly, along with your comments and source code, then you have to configure your compiler to emit assembly rather than the default machine code. You would do this if you wished to tweak the assembled code prior to final assembly, for instance, to take advantage of manual optimisations. However, modern compilers are capable of optimising code more efficiently and more easily than any human can so there's rarely a need to manually optimise.

Can a variable name can be passed to a value parameter in c plus plus?

No. Pass by value always receives a copy of the value being passed. Even if it were possible to physically pass a user-defined identifier into a function by value, the compiled code would not recognise the name since all identifiers are stripped out by the compiler and replaced with memory addresses.

Strictly speaking, even pass by reference does not pass the variable name, as the function argument is simply an alias, an alternate but informal name, for the formal name you actually pass. In essence you are passing the memory address of the variable, rather the value of the memory address as you would with pass by value.

How do I find my compiler on my computer?

Most likely you haven't installed any compilers, otherwise you would know.

What is a declaration of value?

Maybe you misinterpret something? There doesnt seem to be a declaration of value, but maybe declaration of variable?

Palindrome program by using array C?

#include<stdio.h>

#include<math.h>

int main()

{

int num,a,b,c,d,y,k=0;

printf("Enter no.\n");

scanf("%d",&num);

y=num;

for(a=0;a<=num;a++)

{ c=pow(10,a);

d=num/c;

if (d>=1)

k=k+1;

else

break;}

printf(" an aaray of dimension %d" ,k);

int v1[k],v2[k];

int l=0,i,e,f;

for(i=k-1;i>=0;i--)

{

e=pow(10,i);

f=num/e;

v1[l]=f;

l++;

num=num%e;

}

int q,r,t=0,s=0;

q=y;

for(i=k-1;i>=0;i--)

{while(q!=0)

{

r=q%10;

v2[t]=r;

t++;

q=q/10;

}}

for (i=0;i<=k-1;i++)

{if(v1[i]==v2[i])

s++;}

if(s==k)

printf("the no.is palindrome\n");

else

printf("not a palindrome");

return 0;

}

Program for marksheet in c language?

The std::cout and std::cin streams are peculiar to the C++ standard library. They are not available in the C standard library, but are analogous to stdin and stdout which is in the C standard library.

Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl

print factorial($ARGV[11]);

sub factorial {

my($num) = @_;

if($num == 1) {

return 1; # stop at 1, factorial doesn't multiply times zero

} else {

return $num * factorial($num - 1); # call factorial function recursively

}

}

What are the primitive operation perform on a stack and queue?

Primitive Operations

:

enqueue(o)

Put object o on the end of the queue.

dequeue()

Remove and return the object at the front

of the queue.

head()

Reveal the front object in the queue, but

do not remove it.

size()

Return the number of objects currently in

the queue.

capacity()

Return the maximum number of objects

the queue can hold.

Other operations can be defined in terms of these primitive on

es. For

example:

isEmpty()

can be defined by testing whether size() is 0.

isFull()

can be defined by testing whether size() equals the capac-

ity().

These last two could also be considered primitive operation

s. There is

a balance between minimizing the set of primitive operation

s, versus

providing many convenient operations for users.

Program that prints every integer value from 1 to 20 along with its squared in java?

public class PrintValues {

public static void main(String[] args) {

for (int i = 1; i <=20; i++) {

System.out.println("Value is: " + i);

int sq = i * i;

System.out.println(i + " Squared is: " + sq);

}

}

}

C program to delete n characters from a given position from a given string?

void strsectdel (char *str, size_t offs, size_t ndel)

{

size_t len= strlen (str);

if (offs>len) return;

if (ndel>len-offs) ndel= len-offs;

if (ndel==0) return;

memmove (str+offs, str+offs+ndel, ndel);

len -= ndel;

str [len]= '\0';

}