Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m looking for the Image Component Program in c++ language. Note : I already f

ID: 673513 • Letter: I

Question

I'm looking for the Image Component Program in c++ language.

Note : I already found similar c++ files that have the same funtion as the files below. The only one I need is the ArrayQueue File but in c++ language.

These are the Image Component files from JAVA :

TEXT

-------------------------------------------------------------------------------------------------------------------------------------

//ArrayQueue


/** a queue class that uses a one-dimensional array */


public class ArrayQueue implements Queue
{
// data members
int front; // one counterclockwise from first element
int rear; // position of rear element of queue
Object [] queue; // element array

// constructors
/** create a queue with the given initial capacity */
public ArrayQueue(int initialCapacity)
{
if (initialCapacity < 1)
throw new IllegalArgumentException
("initialCapacity must be >= 1");
queue = new Object [initialCapacity + 1];
// default front = rear = 0
}

/** create a queue with initial capacity 10 */
public ArrayQueue()
{// use default capacity of 10
this(10);
}

// methods
/** @return true iff queue is empty */
public boolean isEmpty()
{return front == rear;}


/** @return front element of queue
* @return null if queue is empty */
public Object getFrontElement()
{
if (isEmpty())
return null;
else
return queue[(front + 1) % queue.length];
}

/** @return rear element of queue
* @return null if the queue is empty */
public Object getRearElement()
{
if (isEmpty())
return null;
else
return queue[rear];
}

/** insert theElement at the rear of the queue */
public void put(Object theElement)
{
// increase array length if necessary
if ((rear + 1) % queue.length == front)
{// double array size
// allocate a new array
Object [] newQueue = new Object [2 * queue.length];

// copy elements into new array
int start = (front + 1) % queue.length;
if (start < 2)
// no wrap around
System.arraycopy(queue, start, newQueue, 0,
queue.length - 1);
else
{ // queue wraps around
System.arraycopy(queue, start, newQueue, 0,
queue.length - start);
System.arraycopy(queue, 0, newQueue,
queue.length - start, rear + 1);
}

// switch to newQueue and set front and rear
front = newQueue.length - 1;
rear = queue.length - 2; // queue size is queue.length - 1
queue = newQueue;
}

// put theElement at the rear of the queue
rear = (rear + 1) % queue.length;
queue[rear] = theElement;
}

/** remove an element from the front of the queue
* @return removed element
* @return null if the queue is empty */
public Object remove()
{
if (isEmpty())
return null;
front = (front + 1) % queue.length;
Object frontElement = queue[front];
queue[front] = null; // enable garbage collection
return frontElement;
}

/** test program */
public static void main(String [] args)
{
int x;
ArrayQueue q = new ArrayQueue(3);
// add a few elements
q.put(new Integer(1));
q.put(new Integer(2));
q.put(new Integer(3));
q.put(new Integer(4));

// remove and add to test wraparound array doubling
q.remove();
q.remove();
q.put(new Integer(5));
q.put(new Integer(6));
q.put(new Integer(7));
q.put(new Integer(8));
q.put(new Integer(9));
q.put(new Integer(10));
q.put(new Integer(11));
q.put(new Integer(12));

// delete all elements
while (!q.isEmpty())
{
System.out.println("Rear element is " + q.getRearElement());
System.out.println("Front element is " + q.getFrontElement());
System.out.println("Removed the element " + q.remove());
}
}
}

-----------------------------------------------------------------------------------------------------

//Image Components

/** image component labeling */

public class ImageComponents
{
// top-level nested class
private static class Position
{
// data members
private int row; // row number of the position
private int col; // column number of the position

// constructor
private Position(int theRow, int theCol)
{
row = theRow;
col = theCol;
}

// convert to string suitable for output
public String toString()
{
return new String(row + " " + col);
}
}

// data members
private static int [][] pixel;
private static int size; // number of rows and columns in the image

// methods
/* not yet implemented */
private static void welcome() {};

/** input the image */
private static void inputImage()
{
// define the input stream to be the standard input stream
MyInputStream keyboard = new MyInputStream();

System.out.println("Enter image size");
size = keyboard.readInteger();

// create and input the pixel array
pixel = new int [size + 2][size + 2];
System.out.println("Enter the pixel array in row-major order");
for (int i = 1; i <= size; i++)
for (int j = 1; j <= size; j++)
pixel[i][j] = keyboard.readInteger();
}

/** label the components */
private static void labelComponents()
{
// initialize offsets
Position [] offset = new Position [4];
offset[0] = new Position(0, 1); // right
offset[1] = new Position(1, 0); // down
offset[2] = new Position(0, -1); // left
offset[3] = new Position(-1, 0); // up
  
// initialize wall of 0 pixels
for (int i = 0; i <= size + 1; i++)
{
pixel[0][i] = pixel[size + 1][i] = 0; // bottom and top
pixel[i][0] = pixel[i][size + 1] = 0; // left and right
}

int numOfNbrs = 4; // neighbors of a pixel position
ArrayQueue q = new ArrayQueue();
Position nbr = new Position(0, 0);
int id = 1; // component id

// scan all pixels labeling components
for (int r = 1; r <= size; r++) // row r of image
for (int c = 1; c <= size; c++) // column c of image
if (pixel[r][c] == 1)
{// new component
pixel[r][c] = ++id; // get next id
Position here = new Position(r, c);

do
{// find rest of component
for (int i = 0; i < numOfNbrs; i++)
{// check all neighbors of here
nbr.row = here.row + offset[i].row;
nbr.col = here.col + offset[i].col;
if (pixel[nbr.row][nbr.col] == 1)
{// pixel is part of current component
pixel[nbr.row][nbr.col] = id;
q.put(new Position(nbr.row, nbr.col));
}
}

// any unexplored pixels in component?
here = (Position) q.remove(); // a component pixel
} while(here != null);

} // end of if, for c, and for r
}

/** output labeled image */
private static void outputImage()
{
System.out.println("The labeled image is");
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
System.out.print(pixel[i][j] + " ");
System.out.println();
}
}

/** entry point for component labeling program */
public static void main(String [] args)
{
welcome();
inputImage();
labelComponents();
outputImage();
}
}

-----------------------------------------------------------------------------------------------------------

//MyInputException


/** This exception is thrown when an input
* exception occurs. It is used by MyInputStream
* to convert checked exceptions such as
* FileNotFoundException and EOFException
* into unchecked runtime exceptions. */

public class MyInputException
extends RuntimeException
{
public MyInputException()
{super();}
public MyInputException(String message)
{super(message);}
}

--------------------------------------------------------------------------------------------------------

//Queue

public interface Queue
{
public boolean isEmpty();
public Object getFrontElement();
public Object getRearElement();
public void put(Object theObject);
public Object remove();
}

If its not possible to look for the codes in c++ language, can some one please traduce the following Java language code put function to c++.

TEXT BELOW

---------------------------------------------------------

public void put(Object theElement)
{
// increase array length if necessary
if ((rear + 1) % queue.length == front)
{// double array size
// allocate a new array
Object [] newQueue = new Object [2 * queue.length];

// copy elements into new array
int start = (front + 1) % queue.length;
if (start < 2)
// no wrap around
System.arraycopy(queue, start, newQueue, 0,
queue.length - 1);
else
{ // queue wraps around
System.arraycopy(queue, start, newQueue, 0,
queue.length - start);
System.arraycopy(queue, 0, newQueue,
queue.length - start, rear + 1);
}

// switch to newQueue and set front and rear
front = newQueue.length - 1;
rear = queue.length - 2; // queue size is queue.length - 1
queue = newQueue;
}

Thanks in advance.

ArrayQueue ImageComponents MylnputException ] MylnputStream Queue 10/1/2015 10:09 PM 10/1/2015 10:07 PM 10/1/2015 10:11 PM 10/1/2015 10:09 PM 10/4/2015 1:13 AM JAVA File JAVA File JAVA File JAVA File JAVA File

Explanation / Answer

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};

main()
{
int ch;
queue qu;
while(1)
{
cout <<" 1.insert 2.delet 3.display 4.exit Enter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
    cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21

1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22

1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16

1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16

1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21

1.insert 2.delet 3.display 4.exit
Enter ur choice3
22 16

1.insert 2.delet 3.display 4.exit
Enter ur choice

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote