Saturday, April 18, 2009

program of queues

/*Program of queue using array*/
# include
# define MAX 5

int queue_arr[MAX];
int rear = -1;
int front = -1;

main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
}/*End of insert()*/

del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
}/*End of del() */

display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("Queue is :\n");
for(i=front;i<= rear;i++)
printf("%d ",queue_arr[i]);
printf("\n");
}
}/*End of display() */

Queue's

#include
#include
#define NIL -1
#define MAX 10
typedef struct
{
int terms[MAX];
int front,rear;
}que;
class queue
{
que *q;
public:
queue()
{
q->front=NIL;
q->rear=NIL;
}
void create(int);
int del(void);
void display(void);
};
void queue::create(int x)
{
int i;
que *t=q;
if((t->front==0) && (t->rear==MAX-1))
{
cout<<"OVERFLOW";
getch();
return;
}
if(t->front==NIL)
t->front=t->rear=0;
else if(t->rear==MAX-1)
{
for(i=t->front;i<=t->rear;i++)
t->terms[i-t->front]=t->terms[i];
t->rear=t->rear-t->front+1;
t->front=0;
}
else
t->rear++;
t->terms[t->rear]=x;
return;
}
void queue::display(void)
{
if((q->front==NIL)&&(q->rear==NIL))
{
cout<<"UNDERFLOW";
return;
}
for(int i=q->front;i<=q->rear;i++)
cout <<>
return;
}
int queue::del(void)
{
int n;
que *t=q;
if((q->front==NIL)&&(q->rear==NIL))
{
cout<<"UNDERFLOW";
return(-1);
}
n=t->terms[t->front];
if(t->front==t->rear)
t->front=t->rear=NIL;
else
t->front++;
return(n);
}
int main()
{
int ch,n,x,i;
queue qt;
do
{
clrscr();
cout<<"1->Insert \n";
cout<<"2->Delete \n";
cout<<"3->Display \n";
cout<<"4->Exit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case '1':
cout<<"Enter the term to insert:";
cin>>x;
qt.create(x);
getch();
break;
case '2':
x=qt.del();
if(x!=-1)
cout<<"Deleted term is " << x;
getch();
break;
case '3':
cout<<"Queue" << endl;
qt.display();
getch();
break;
case '4':
cout<<"Exiting";
getch();
break;
default:
cout<<"Not a valid choice";
getch();
break;
}
}while(ch!=4);
getch();
return(0);
}

Some important questions with answers

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

long double a=55555;

clrscr();

printf("%.2LE,%.2Le",a);

getch();

}

output:5.56E+04,5.56e+04

%e or %E represent print the number in exponential format.

%e means output has small letter e.

%E means output has capital letter e.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

signed a=5;

unsigned b=5;

clrscr();

if(a==b)

printf("EQUAL");

else

printf("UNEQUAL");

getch();

}

output:Equal

Explanation: before any arithmetic operation small data type convert into higher data type i.e signed to unsigned.

Floating point data type questions in c programming language:

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

float a=5.7e2;

clrscr();

printf("%.2f",a);

getch();

}

output:570.0

Explanation:

In 5.7e2 ,here e reperesent 10 i.e 5.7*10^2

%.2f means print only two number after decimal

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

float a=55.7E-2;

clrscr();

printf("%.2g",a);

getch();

}

output:0.56

Explanation:

In 55.7E-22 ,here E reperesent 10 i.e 55.7*10^-2

%.2f means print only two number after decimal

C best language questions and answer

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\377';

clrscr();

printf(2+"%d%o",a);

getch();

}

output:177777

Explanation:

‘\377’ is octal character constant.

%o is used to print octal number system.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\378';

clrscr();

printf("%o",a);

getch();

}

output:error ,character constant too much long

Expalnation: Heigest possible character constant is ‘\377’ which is

Equivlalent to decimal 255.


C best programming languages questions with answer

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\12';

char *str1="cquestion",*str2="bank";

clrscr();

printf("%s%c%s",str1,a,str2);

getch();

}

output:cquestion

bank

Explanation :

‘\12’ represent octal 12 i.e. decimal 10 which is ASCII code of new line character i.e. send the cursor to next line

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\15';

char *str1="cquestion",*str2="bank";

clrscr();

printf("%s%c%s",str1,a,str2);

getch();

}

output:bankstion

Explanation :

‘\11’ represent octal 11 i.e. decimal 9 which is ASCII code of carriage return (return to first position of that line)


C best programming questions and answer

Escape sequence questions in c programming.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\10';

char *str1="cquestion",*str2="bank";

clrscr();

printf("%s%c%s",str1,a,str2);

getch();

}

output:cquestiobank

Explanation:

‘\10’ represent octal 10 i.e. decimal 8 which is ASCII code of backspaces (only one character)

(q) What will be output of the following program?

#include"stdio.h"

void main(){

char a='\11';

char *str1="cquestion",*str2="bank";

clrscr();

printf("%d",printf("%s%c%s",str1,a,str2));

getch();

}

output: cquestion bank14

Explanation :

‘\11’ represent octal 11 i.e. decimal 9 which is ASCII code of blank space.

C best questions with answer

(q) What will be output of the following program?

#include"stdio.h"

void main(){

char a='\7',b='\8';

clrscr();

printf("%d %d",a,b);

getch();

}

output:7 56

Expalnation:

8 is not octal digit. octal digits are(0,1,2,3,4,5,6,7).

So ‘\7’ is octal 7

‘\8’ some special character constant.

C coding good questions and answer

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\\';

clrscr();

a=a-'/';

printf("%d",a);

getch();

}

output:error

Explanation:

Character is \ has special meaning in c programming.

e.g

‘\0’ represent octal character.

‘\n’ represent new line character. So we cannot use ‘\’ directly.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

char a='\5';

clrscr();

a=a<<2;

printf("%x",a);

getch();

}

output:14

Expanation: ‘\5’ means octal 5.

C language good questions with answer

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

//clrscr();

fputs("STANDARD PRINTER DEVICE",stdprn);

getch();

}

output: STANDARD PRINTER DEVICE (on the paper)

Explanation:

PRINT IN CONNECTED PRINTER DEVICE, OTHERWISE

TURBO C WILL BE HANGED FOR SEARCHING OF PRINTER IN THE SYSTEM.

Character constant questions in c programming language.

(q) What will be output of the following program?

#include"stdio.h"

void main(){

char a='\';

clrscr();

printf("%c",a);

getch();

}

output: error,unterminated character constant

Explanation:

Character is \ has special meaning in c programming.

e.g

‘\0’ represent octal character.

‘\n’ represent new line character. So we cannot use ‘\’ directly.

C programming good languages questions and answer

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

clrscr();

fputs("STANDARD OUTPUT PAGE",stdout);

getch();

}

output: STANDARD OUTPUT PAGE

it will display the output of program on standard output page or stream(by default for text)

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

clrscr();

fputs("STANDARD INPUT PAGE",stdin);

getch();

}

output: nothing

stdin is used as standard input device stream.

C programming good questions with answer

Standard predefined stream question in c programming language.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

clrscr();

fputs("ERROR page",stderr);

getch();

}

output: ERROR PAGE

explanation:

it will display the output of program on standard error page

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

clrscr();

fputs("STANDARD AUXILIARY PAGE",stdaux);

getch();

}

output: It will show a system message :

BAB MEMORY IMAGE OF FAT DETECTED

Note: stdaux is standard auxiliary device in c.

C good questions and answer

(q) What will be output of the following program ?

void main(){

enum data1{a,c,e}p;

enum data2{b,d,f}q;

p=q;

clrscr();

printf("%i",p);

getch();

}

output: 0

Standard predefined stream question in c programming language.

(q) What will be output of the following program ?

#include"stdio.h"

void main(){

clrscr();

fputs("ERROR page",stderr);

getch();

}

output: ERROR PAGE

explanation:

it will display the output of program on standard error page

best questions of c coding and answer

(q) What will be output of the following program ?

void main(){

enum data{a=5,b=a,c=a+b,d=+a+b+c};

clrscr();

printf("%#o",d);

getch();

}

output: 024

Explanations:

We can initialize enum constant with other enum constant.

(q) What will be output of the following program ?

void main(){

enum {a=5,b=a,c=a^b,d=a^b^c};

clrscr();

printf(2+"p=%#o",d);

getch();

}

output: 0

Explanations:

We can initialize enum constant with other enum constant and perform arithmetic ,logical operation etc.

^ is bitwise XOR operator.

%o is used for octal number.

best questions of c language and answer

(q) What will be output of the following program ?

void main(){

enum data{a,b=-1,c};

clrscr();

printf("%i %i %i",c,c++,++c);

getch();

}

output: error lvalue required

Explanation:

You cannot modify enum constant after initialization.

(q) What will be output of the following program ?

void main(){

enum data{a,b=-1,c=300000,d=0,e};

clrscr();

printf("%#x",c);

getch();

}

output: error

value of enum constant must be within the range of signed int.

best questions of c programming and answer

enum data type questions in c programming language.

(q) What will be output of the following program ?

void main(){

enum data{a,b,c};

clrscr();

printf("%i %i %i",a,b,c);

getch();

}

output: 0 1 2

Explanation:

By default initial value of first enum constant is zero.

Value of next enum constant will be :

Next_enum_constant_value= previous_enum_constant_value+1

(q) What will be output of the following program ?

void main(){

enum data{a,b=5,c};

clrscr();

printf("%i %i %i",a,b,c);

getch();

}

Output: 0 5 6

Explanation:

By default initial value of first enum constant is zero.

Value of next enum constant will be:

Next_enum_constant_value= previous_enum_constant_value+1

This formal is valid if enum constant is not explicitly initialized.

Otherwise it will store initialized value.

best questions of c with answer

(q) What will be output of the following program ?

void main(){
int x=012,y;
clrscr();
y=++x- ~x+4?5:6;
printf("%#X %x",y,y);
getch();

}

Output: 0X5 5
Explanation:
012 is octal number because it is staring with number zero.
%x,%X is used for to print the number in hexadecimal format.
# indicates output should in the format: 0x number.


(q) What will be output of the following program ?

void main(){
int x=0x12,y;
clrscr();
y=(x,1,10)+x,1,10;
printf("%#o, %o",y,y);
getch();

}

output: 034 34
Explanation:
0x12 is hexadecimal number because it is staring with 0x.
%o is used for to print the number in octal format.
# indicates output should in the format: 0 numbers.

good questions of c coding with answer

number system like octal, hexadecimal in questions c programming language.

(q) What will be output of the following program ?

void main(){

int x=0x25,y;

clrscr();

y=++x -x+printf("x");

printf("\n%d",y);

getch();

}

output: x

1

Explanation:

Ox represents hexadecimal number system.

Note. Printf function returns an integer which is number of character.

(q) What will be output of the following program?

void main(){

int x=12,y;

clrscr();

y=++x <

printf("%#X",y);

getch();

}

output: 0xA000

Explanation:

%x,%X is used for to print the number in hexadecimal format.

# indicate output should in the format 0x number

good questions of C programming languages and answer

(q) What will be output of the following program ?

void main(){

clrscr();

printf("%d",__LINE__);

getch();

}

output: 3

Explanation:

__LINE__ is global identifier in c programming language. It return an integer which is equal to total number of line in c source code program

upto __LINE__ including that line.

e.g

void main(){ //line 1

clrscr(); //line 2

printf("%d",__LINE__); //line 3

(q) What will be output of the following program ?

//save this program as test.c

void main(){

clrscr();

printf("%s",__FILE__);

getch();

}

output: test.c

Expalnation:

__FILE__ is global identifier in c programming language. It returns

a string containing name of current source file in which this program

has been saved.


good questions of c programming with answer

Global identifier questions c programming language.

(q) What will be output of the following program ?

void main()

{

clrscr();

printf("%s",__TIME__);

getch();

}

output: 22:45:19

Explanation:

__TIME__ is global indetifer in c. It will display current time (h:m:s)of system in which you are working.

(q) What will be output of the following program ?

void main()

{

clrscr();

printf("%s",__DATE__);

getch();

}

output: sept 12 2008

_TIME__ is global identifier in c. It will display current date (month date year)of system in which you are working

good questions of c with answer

volatile modifier keyword questions in c programming language:

(q) What will be output of the following program ?

void main()

{

volatile a=5;

a=~a+ ++a;

clrscr();

printf("%d",a);

getch();

}

output: -1

Explanation:

volatile variable can be modify by interrupt or other process in preprocessor. So in normal output will not affect.

Note. ~ is one’s complement operator

(q) What will be output of the following program ?

void main()

{

volatile a=5;

a=~a+++a;

clrscr();

printf("%d",a);

getch();

}

output: 0

Explanation:

volatile variable can be modify by interrupt or other process in preprocessor. So in normal output will not affect.

a+++a means a++ + a.

Note. ~ is one’s complement operator.

questions of c coding and answer

(q) What will be output of the following program ?

void main()

{

int * const a=(int * const)12;

a=25;

clrscr();

printf("%d",a);

getch();

}

output: error

Explanation:

we Cannot modify a const object.

Here address of a is not constant but variable a is constant. So we

Cannot modify variable a.

questions of c language with answer

(q) What will be output of the following program ?

void main()

{

const int *a=12;

a++;

clrscr();

printf("%d",a);

getch();

}

output: garbage value

Explanation: Here address of variable a is constant not variable a. So we can modify variable a. Initial value of auto type data is garbage.

(q) What will be output of the following program ?

void main()

{

const int *a=(const int * )12;

*a=(const int *)25;

clrscr();

printf("%d",a);

getch();

}

output: error

Explanation:

we Cannot modify a const object.

Here address of a is constant not variable a.

questions of C programming languages and answer

const modifier keyword questions in c programming language:

(q) What will be output of the following program ?

void main()

{

const a=10;

a=~a;

printf("%d",a);

getch();

}

output: error

Explanation:

we Cannot modify a const object.

(q) What will be output of the following program ?

void main()

{

const _=10;

int *p=&_;

printf("%d",*p);

getch();

}

output: 10

Explanation:

We can assign address of const object to its pointer.

underscore is valid variable name.

questions of c programming with answer

(q) What will be output of the following program ?

void main()

{

int x=5;

int y=10;

&x=y;

printf("%d %d",x,y);

getch();

}

output: error ,L value required

Explanation:

After applying any operator in variable name it always give a value.

(type): urinary type casting operator is not exception for this.

It is similar to write

3456=5

It is invalid c statement. Because left side of assignment operator must be a variable not any constant.

questions of c and answer

(q) What will be output of the following program ?

void main()

{

long int a,b=5;;

~a=++b + ++b + ++b;

printf("%d %d",++a,++b);

getch();

}

output: error ,L value required

Explanation:

After applying any operator in variable name it always give a value.

(type): urinary type casting operator is not exception for this.

It is similar to write

3456=5

It is invalid c statement. Because left side of assignment operator must be a variable not any constant.

(q) What will be output of the following program ?

void main()

{

int x;

int y;

x+y=10;

x=3;

printf("%d",y);

getch();

}

output: error ,L value required

Explanation:

After applying any operator in variable name it always give a value.

(type): urinary type casting operator is not exception for this.

It is similar to write

3456=5

It is invalid c statement. Because left side of assignment operator must be a variable not any constant.

C coding questions with answer

L value required error in c programming language:

(q) What will be output of the following program ?

void main()

{

long int a;

(float)a=6.5;

printf("%f",a);

getch();

}

output: error ,L value required

Explanation:

After applying any operator in variable name it always give a value.

(type): urinary type casting operator is not exception for this.

It is similar to write

3456=5

It is invalid c statement. Because left side of assignment operator must

Be a variable not any constant.

(q) What will be output of the following program ?

void main()

{

long int a,b=10;

++a=b++;

printf("%d %d",a,b);

getch();

}

output: error ,L value required

Explanation:

After applying any operator in variable name it always give a value.

(type): urinary type casting operator is not exception for this.

It is similar to write

3456=5

It is invalid c statement. Because left side of assignment operator must

Be a variable not any constant.

C language questions and answer

(q) What will be output of the following program ?

void main(){

char * __TIME__="world";

clrscr();

printf("%s ",__TIME__);

getch();

}

output:error

Explanation:

__TIME__ is valid identifier in c programming language but it is

Predefine global identifier .So a variable not should not be global

Identifier like __TIME__,__DATE___,__FILE__ etc.

C programming languages questions with answer

(q) What will be output of the following program ?

void main()

{

long int _=5l;

printf("%ld",_);

getch();

}

output: 5

Explanation:

Under score is valid keyword in c.

(q) What will be output of the following program ?

void main(){

char * __WORLD__="world";

clrscr();

printf("%s ",__WORLD__);

getch();

}

Output:world

Explanation:

__WORLD__ is valid identifier in c programming language.

But we should not write variable name in the forma like __xyx__,

__TIME__. Why ?

C programming questions and answer

Variable naming rule in c programming language:

(q) What will be output of the following program ?

void main()

{

Char * emp name=”raja”;

printf("%s",emp name);

getch();

}

output:error

Explanation:

invalid variable name.Except under score there should not be any special character in name of variable event blank space.

(q) What will be output of the following program ?

void main()

{

long int new=5l;

printf("%ld",new);

getch();

}

output:5

Explanation:

We can use c++ keyword in variable name in c programming.(But should not use ,why ?)

C questions with answer

Variable naming rule in c programming language:

(q) What will be output of the following program ?

void main()

{

int goto=5;

printf("%d",goto);

getch()

}

Output:error

Explanation:

invalid variable name. goto is keyword in c.

(q) What will be output of the following program ?

void main()

{

long int 1a=5l;

printf("%ld",1a);

getch();

}

Output:error

Explanation:

invalid variable name. Variable name must star from either alphabet or

under score.

solution of your query

Welcome to ask query

(q)

date 9/4/08

Hi!
I hope u will be fine.plz help in following program.I need source code.

The question is:
A string (eg: "I am writing an email") is entered through the keyboard, write a programme in C to get its reverse in a column as output ie:
email
an
writing
am

Answer:

code for such program is:

void main()

{

char str[20];

char *ptr=str,*temp;

int i=0,j;

clrscr();

scanf("%[^\n]",str);

while(*ptr){

i++;

ptr++;

}

for(j=0;j

if(*ptr==' ')

{

temp=ptr;ptr--;temp++;

while((*temp!=' ')&&(*temp!='\0')) {

printf("%c",*temp);

temp++;

}

printf("\n");

}

else

{

ptr--;

}

}

while(*ptr!=' ') {

printf("%c",*ptr);

ptr++;

}

getch();

}

(q)

I want a C program to check whether a string is a palindrome or not where the string to be checked is passed as command line argument during execution.

(pahal ray

8/25/08

Answer:

#include"string.h"

void main(int counter,char**string)

{

char *rev;

char str[15];

int i,j;

clrscr();

strcpy(str,string[1]);

printf("%s",str);

for(i=strlen(str)-1,j=0;i>=0;i--,j++)

rev[j]=str[i];

rev[j]='\0';

if(strcmp(rev,str))

printf("\nThe string is not a palindrome");

else

printf("\nThe string is apalindrome");

getch();

}

(q)

how to write a c program to display the source code of the program.

(ramya)

8/23/08

answer:

If source code is available

#include"stdio.h"

void main()

{

char str[70];

FILE *p;

clrscr();

if((p=fopen("mod.c","r"))==NULL)

{

printf("\nUnable t open file string.txt");

exit(1);

}

while(fgets(str,70,p)!=NULL)

puts(str);

fclose(p);

getch();

}

(q) Swapping of two number without using third variable

answer

void main()

{

int a=5,b=10;

clrscr();

//process one

a=b+a;

b=a-b;

a=a-b;

printf("a= %d b= %d",a,b);

//process two

a=5;b=10;

a=a+b-(b=a);

printf("\na= %d b= %d",a,b);

//process three

a=5;b=10;

a=a^b;

b=a^b;

a=b^a;

printf("\na= %d b= %d",a,b);

//process four

a=5;b=10;

a=b-~a-1;

b=a+~b+1;

a=a+~b+1;

printf("\na= %d b= %d",a,b);

//process five

a=5,b=10;

a=b+a,b=a-b,a=a-b;

printf("\na= %d b= %d",a,b);

getch();

}

(q) How to convert Decimal to binary in c programme?

answer

void main()

{

long int m,no=0,a=1;

int n,rem;

clrscr();

printf("Enter any decimal number->");

scanf("%d",&n);

m=n;

while(n!=0)

{

rem=n%2;

no=no+rem*a;

n=n/2;

a=a*10;

}

printf("The value %ld in binary is->",m);

printf("%ld",no);

getch();

}