Answer:
#include <iostream>
using namespace std;
void swap(int *a,int *b){ //function to interchange values of 2 variables
int temp=*a;
*a=*b;
*b=temp;
}
void sort(int queue[],int n)
{
int i,j;
for(i=0;i<n;i++) //to implement bubble sort
{
for(j=0;j<n-i-1;j++)
{
if(queue[j]>queue[j+1])
swap(queue[j],queue[j+1]); //to swap values of these 2 variables
}
}
}
int main()
{
int queue[]={6,4,2,9,5,1};
int n=sizeof(queue)/4; //to find length of array
sort(queue,n);
for(int i=0;i<n;i++)
cout<<queue[i]<<" ";
return 0;
}
OUTPUT :
1 2 4 5 6 9
Explanation:
In the above code, Queue is implemented using an array and then passed to a function sort, so that the queue can be sorted in ascending order. In the sort function, in each pass 2 adjacent values are compared and if lower index value is greater than the higher one they are swapped using a swap function that is created to interchange the values of 2 variables.
Try restarting your laptop
Option + 1
-----------------------------
Answer:
Following are the output of the given question
3 4 6 8
Explanation:
In the following code, firstly, we have declare two integer data type "X" and "Y".
- Then, set two for loop in which first one is outer loop and the second one is the inner loop, inner loop starts from 1 and end at 2 and outer loop starts from 3 and end at 4.
- Then, when the variable X=1 then the condition of the outer loop become true and the check the condition of the inner loop, when Y=3, than the multiplication of X and Y occur and output should be 1*3=3.
- Then, the value of the X will remain same at time when the value of Y become false. Thrn, second time X=1 and Y=4 and output should be 4.
- Then, X=2 because Y turns 5 so the inner loop will terminate and than outer loop will execute. Then again Y=3 but X=2 and output should be 6.
- Then, the value of the X will remain same at time when the value of Y become false. Thrn, second time X=2 and Y=4 and output should be 8.
Finally, all the conditions of the loop become false and the program will terminate.