Question:

Find whats wrong in the program

by  |  earlier

0 LIKES UnLike

if i giv 2 option before 1option in the first running of program, then it shud giv "no elements in queue" but its not giving. rather its showing

"value deleted from the queue = -1

the queue at this moment is as follows-

_"

nothing else. but the program works correctly wen 1 is chosen then 2 option. find out wats wrong, thanx. u better run the program first 2 better understand my program code

// program to display the functions of a queue

#include <iostream.h>

#include <conio.h>

#define max 15

void insert(int queue[],int&rear,int val);

int remove(int queue[], int&front);

void show(int queue[], int front, int rear);

int front=-1,rear=-1;

int queue[max];

void main()

{

int val;

int ch;

menu: // Label

clrscr();

cout<<"\n\n\n1> Insertion\n2> Deletion\n\n\n\t Enter ur choice: ";

cin>>ch;

switch(ch)

{

case 1: cout<<"\n Enter the value 2 be inserted: ";

cin>>val;

insert(queue,rear,val);

show(queue,front,rear);

break;

case 2: val=remove(queue,front);

if(val!=-9999)

cout<<"\n The value deleted from queue = "<<val;

getch();

show(queue,front,rear);

break;

}

char ans;

cout<<"\n\n\t Want 2 continue (Y/N)?";

cin>>ans;

if(ans=='Y' || ans=='y')

goto menu;

else

{

cout<<"\n Thanks User, cya...";

getch();

}

} // End of main()

void insert(int queue[], int&rear,int val)

{

if (rear==max-1)

{

cout<<"\n Queue is full!!!";

getch();

}

else

{

front=0;

rear=rear 1;

queue[rear]=val;

cout<<"\n The value has been inserted at the "<<rear<<"th position";

getch();

}

}

int remove(int queue[],int&front)

{

int val;

if(front==rear 1)

{

val=-9999;

cout<<"\n Queue is empty!!! So no value can be deleted";

getch();

}

else

{

val=queue[front];

front=front 1;

}

return val;

}

void show(int queue[],int front,int rear)

{

if(front==-1)

{

cout<<"\n\n Nothing in the Queue!!!";

getch();

}

else

{

cout<<"\n\n The Queue at this moment is as follows- \n";

for (int i=front;i<=rear;i )

cout<<queue[i]<<"\n";

getch();

}

}

 Tags:

   Report

6 ANSWERS


  1. There seems to be no mistake in it. But as we all know programming in C is always conceptual causing minor errors unknowingly.

    Sometimes there seems no mistake but still we come across errors and most of the times errors are as small as one&#039;s nails.

    Sit quietly and go through your code and I&#039;m dead sure you will come to know where you went wrong.

    Also visit this site as I have gone through this and it seems to be one of the best blog I have come across on C programming Tutorials.

    It contains all tutorials links in it at one place + its own explanation.

    This will clear or atleast brush up all your concepts thus solving out your errors too.

    http://c4tutorials.blogspot.com


  2. everythin correct


  3. Sql Question And Answer-http://sqlqa.blogspot.com/

  4. over my head

  5. hey wat i think...

    In the beginning u r assigning

    front=-1

    rear=-1

    Now when u choose 2nd option it goes to delete section...

    Here the &quot;if &quot; condition is not satisfied since both front and rear are -1 Therefore it goes in else section and deletes one value and updates the front and rear values.

    But when u choose option 1st front and rear values are uodated according to insertion values, hence it works fine...

    I have just analyzed the code and given the ans so may be i m wrong...

    Do update me wid any of ur findings ....tc

  6. project and programming help

    at

    http://oracletutorials.blogspot.com/

Question Stats

Latest activity: earlier.
This question has 6 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.