Write a program that will take 10 integer values from the user. a. If the entered value is Even then enqueue it into a queue at even index and if the entered value is Odd then enqueue it into a queue at odd index. (Note: Even index will start from 0 & odd will start from 1 ). b. Dequeue the values and display the sum of the even values and the sum of the odd values.
Write a program that will take 10 integer values from the user.
a. If the entered value is Even then enqueue or link-list it into a queue at even index and if the entered value is Odd then enqueue it into a queue at odd index. (Note: Even index will start from 0 & odd will start from 1 ).
b. Dequeue the values and display the sum of the even values and the sum of the odd values.
Answer:
C++ code:
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
struct queue
{
int front, rear, size, data[10];
public:
int even_sum = 0;
int odd_sum = 0;
queue()
{
front
= 0;
rear
= -1;
size
= 10;
}
void enqueue(int x)
{
if (rear == 9)
{
cout << "Queue is full no more
elements can be insert" << endl;
}
else
{
++rear;
if (rear%2==0)
{
data[rear]
= x;
}
else
{
data[rear]
= x;
}
}
}
void dequeue()
{
if (front ==size)
{
cout
<< "Queue is
already Empty"
<< endl;
}
front++;
}
void show()
{
int i = front;
while (i <= rear)
{
cout
<< data[i++] << endl;
}
}
void even_oddsum()
{
for (int i = front; i < size; i++)
{
if (data[i] % 2 == 0)
{
even_sum
= even_sum + data[i];
}
else
{
odd_sum
= odd_sum + data[i];
}
}
cout
<< "Sum of Even
Integers = "
<< even_sum << endl;
cout
<< "Sum of odd
Integers = "
<<odd_sum << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
queue q1;
q1.enqueue(0);
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.enqueue(6);
q1.enqueue(7);
q1.enqueue(8);
q1.enqueue(9);
cout
<< "After
Inserting all values in Queue" << endl;
q1.show();
cout
<< "---------------------------" << endl;
q1.dequeue();
q1.dequeue();
q1.show();
cout
<< "After
deleting values from Queue" << endl;
q1.show();
q1.even_oddsum();
_getch();
return 0;
}
Thank
You!!!........)
great wor
ReplyDeleteimpressive coding
ReplyDelete