I am unable to solve this problem this is for 15 points Plea

I am unable to solve this problem, this is for 15 points Please help me resolve

The following problem is known as the Dutch Flag Problem. In this problem, the task is to rearrange an array of characters R, W, and B (for red, white, and blue, which are the colors of the Dutch national flag) so that all the R\'s come first, all the W\'s come next, and all the B\'s come last. Design a linear, in-place algorithm that solves this problem.

Solution

##Solution to Dutch Flag Problem##

Dear Student,

Following is the solution of the given problem.

This simple linear search algorithm print the given array of characters as in below format.

if character of array is:

char arr[21] = {\'W\',\'R\',\'B\',\'B\',\'R\',\'W\',\'B\',\'R\',\'B\',\'W\',\'W\',\'R\',\'B\',\'R\',\'W\',\'B\',\'W\',\'W\',\'W\',\'R\',\'B\'};

then output should be:

RRRRRRWWWWWWWWBBBBBBB


Following program illustarted this-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i,j,k;
char r,w,d;
char arr[100] = {\'W\',\'R\',\'B\',\'B\',\'R\',\'W\',\'B\',\'R\',\'B\',\'W\',\'W\',\'R\',\'B\',\'R\',\'W\',\'B\',\'W\',\'W\',\'W\',\'R\',\'B\'};
for(i=0; i<21; i++)
{
if(arr[i]==\'R\')
{
r=arr[i];
printf(\"%c\",r);
}
else{}
}
for(j=0;j<21;j++)
{
if(arr[j]==\'W\')
{
w=arr[j];
printf(\"%c\",w);
}
else{}
}
for(k=0;k<21;k++)
{
if (arr[k]==\'B\')
{
d=arr[k];
printf(\"%c\",d);
}
else{}
}
printf(\"\ \");
return 0;
}

Note:The above program is tested on ubuntu 14.04 system.Compilation has been done using \"gcc\" compiler.

I am unable to solve this problem, this is for 15 points Please help me resolve The following problem is known as the Dutch Flag Problem. In this problem, the t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site