8 Replies - 471 Views - Last Post: 07 November 2019 - 07:54 PM Rate Topic: -----

#1 ascesix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-November 19

Block Robot Program - C++

Posted 07 November 2019 - 11:09 AM

Now I'm having trouble with a block sorting robot, it is supposed to arrange the blocks in alphabetical order and the first block is supposed to be inserted into space 10 of the array. The error is that it sorts everything put ignores the block that was put in the 10th space of the array, I have tried removing the block at space 10 and reinserting it that didn't work. It is just confusing me because I can only use the given functions for the program.

#include <iomanip>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

unsigned int put_block(char block, unsigned int position, char array[]);
char get_block_testcase(unsigned int testcase, unsigned int index);
char get_block_testone(void);
bool test_empty(unsigned int position, char array[]);
unsigned int shift_right(unsigned int position);
unsigned int shift_left(unsigned int position);
bool robot_ltoreq_slot(char robot, char in_slot);
char switch_blocks(char robot, unsigned int position, char array[]);
void print_slots(char slots[]);

int main()
{
	//variables

	int position;
	int counts = 0;
	int reset = 0;
	int pos = 0;
	int blocksl = 0;
	int test;
	int index = 0;

	char block;
	char robot;
	char in_slot;
	char slots[20] = { NULL };

	while (reset == 0)
	{
		for (int k = 0; k < 20; k++)
		{
			test_empty(k, slots);
		}

		cout << "Hello! I am the baby block sorting robot!." << endl;
		cout << "I am desgined to take blocks and arrange them in alphabetical order!" << endl;

		//cout << "Please Enter the Block set you want to use." << endl;
		//cout << "1. AXFIUTRPQVWSEYJINYTB " << endl << "2. ABFGHIJKMOPRSTUVWXYZ" << endl << "3. ZYXWVUTSRPOKJIIHGFBA" << endl << "4. AAAAAYYYYYQQQQQXXXXX" << endl << "5. XXXAAAZZZAAYYVVVVVQQQ" << endl;
		//cin >> test; 

		position = 10;
		position -= 1;

		cout << "OH NO! BLOCK INSERTED INTO SPACE 10!" << endl;

		index = 0;
		//block = get_block_testcase(test,index);
		block = get_block_testone();
		put_block(block, position, slots);

		index++;

		print_slots(slots);

		while (position != 0)
		{
			position = shift_left(position);
		}

		for (int i = 0; i < 19; i++)
		{
			blocksl = 0;
			while (position != 0)
			{
				position = shift_left(position);
			}
			//block = get_block_testcase(test, index);
			block = get_block_testone();
			index++;
			robot = block;
			in_slot = slots[position];
			while (blocksl == 0)
			{
				robot = block;
				in_slot = slots[position];

				if (slots[position] == NULL)
				{
					put_block(block, position, slots);
					blocksl = 1;
				}
				else
				{
					if (robot_ltoreq_slot(robot, in_slot))
					{
						block = switch_blocks(block, position, slots);
						counts++;
						position = shift_right(position);
					}

					else
					{
						position = shift_right(position);
					}
				}
			}
		}

		cout << endl << endl;
		cout << "Your blocks have been sorted! They are order: " << endl;


		print_slots(slots);

		cout << endl << "I had to switch approximately: " << counts << " times until I reached my goal." << endl;

		cout << endl;

		cout << "HOPE YOU ENJOYED YOUR BABY BLOCK ROBOT! GOODBYE! :D/>/>" << endl;

		system("pause");
	}
}


// ------------------------------------------------------------------------ //
//																			//
//																			//
//				Functions for robot operations								//
//																			//
//																			//
// ------------------------------------------------------------------------ //

//
// Function get_block
// Reads in a single character value from the keyboard 
// This is the input from the chute
// Returns: a single character in uppercase
//
// Example function call: 	block = get_block();

char get_block(void)
{
	char block;
	cout << "Enter one block: ";
	cin >> block;
	return toupper(block);
}
//
// Function print_slots
// Prints the contents of the slots array in a well formatted form.
// Input: Array of slots
// Returns: Nothing (void)
//
// Example function call: print_slots(slot_array);

void print_slots(char slots[])
{
	unsigned int j = 1;
	for (j = 1; j <= 20; j++)
	{
		cout << setw(3) << j;
	}
	cout << endl;
	for (j = 0; j < 20; j++)
	{
		cout << setw(3) << slots[j];
	}
	cout << endl;
}

// Function put_block
// This function stores a character into the character array representing the slots
//
// Inputs: 
// block - type char - The character to be inserted into a slot
// position - type unsigned int - index of the slot where the block will go
// array - type char - array of slots containing the blocks
//
// Returns:
// position - type unsigned int - the index of the slot where the block was placed
//
// Example function call: 	put_block(block, position, slots);

unsigned int put_block(char block, unsigned int position, char array[])
{
	bool debug = true;
	array[position] = block;
	if (debug)
		cout << "Block " << block << " inserted into slot " << position << endl;
	return position;
}

// Function remove_block
// This function removes a block from the slot array
// The slot where the block is removed is then set to a space
//
// Inputs: 
// position - type unsigned int - index of the slot where block is located
// array - type char - array of slots containing the blocks
//
// Returns:
// block - type char - the block removed from the slot
//
// Example function call: 	remove_block(position, slots);

unsigned int remove_block(unsigned int position, char array[])
{
	bool debug = true;
	char block = ' ';
	block = array[position];
	array[position] = ' ';  // Reset slot to blank after block removed
	if (debug)
		cout << "Block " << block << " removed from slot " << position + 1 << endl;
	return block;
}


// Function shift_right
// This function increments the index simulating a movement of the robot 
// to the next higher slot (index) of the array
// 
// Inputs:
// position - type unsigned int - current slot position
//
// Returns:
// position - type unsigned int - The updated position which is input position + 1
//
// Example function call:  position = shift_right(position)
//

unsigned int shift_right(unsigned int position)
{
	bool debug = true;
	position++;
	if (debug)
		cout << "Position right shifted to " << position << endl;
	return position;
}

// Function shift_left
// This function decrements the index simulating a movement of the robot 
// to the next lower slot (index) of the array
// 
// Inputs:
// position - type unsigned int - current slot position
//
// Returns:
// position - type unsigned int - The updated position which is input position - 1
//
// Example function call: position = shift_left(position)
//

unsigned int shift_left(unsigned int position)
{
	bool debug = true;
	position--;
	if (debug)
		cout << "Position left shifted to " << position << endl;
	return position;
}

// Function robot_ltoreq_slot
// This function compares the value of the block held by the robot 
// with the value of the block in a slot 
// 
// Inputs:
// robot - type char - value of block held by robot
// in_slot - type char - value of block in the slot
//
// Returns:
// true or false
// TRUE if block held by robot is LESS than or equal to the block in slot
// FALSE if block held by robot is GREATER than block in slot
//
// Example function call: if ( compare_blocks(robot_block, slot_block) )
//
bool robot_ltoreq_slot(char robot, char in_slot)
{
	bool debug = true;
	if (debug)
		cout << endl << "Comparing robot block " << robot << " with block in slot " << in_slot << endl;
	if (robot <= in_slot)
	{
		if (debug)
			cout << "Returning true. Robot block LESS than or EQUAL to block in slot. " << endl;
		return true;
	}
	else
	{
		if (debug)
			cout << "Returning false. Robot block GREATER than block in slot. " << endl;
		return false;
	}
}
// Function switch_blocks
// This function switches the block held by the robot with a block in a slot. 
// After the switch the robot is holding the block removed from the slot.
// 
// Inputs: 
// robot - type char - The block to be inserted into a slot
// position - type unsigned int - index of the slot where the block will go
// array - type char - array of slots containing the blocks
//
// Returns:
// robot - type char. The value of the block removed from the slot.
//
// Example function call: block = switch_blocks(block,  position, array);
//

char switch_blocks(char robot, unsigned int position, char array[])
{
	char temp_hold;
	bool debug = true;
	if (debug)
		cout << "Switching blocks " << robot << " with " << array[position] << endl;
	temp_hold = robot;
	robot = array[position];
	array[position] = temp_hold;
	return robot;
}
// Function test_empty
// This function tests the array to determine if a slot is empty (NULL) 
// or if the slot contains a blank. The slot array must be intialized to 
// all NULL or all blanks (spaces) before any blocks are added.
// 
// Inputs:
// position - type unsigned int - index of slot to be tested
//
// Returns:
// true or false as value o function
// TRUE if slot is empty
// FALSE if the slot contains a block
//
// Example function call: if ( test_empty(index, array) )
//
bool test_empty(unsigned int position, char array[])
{
	char blank = ' '; // Blank space
	bool debug = true;
	if (array[position] == NULL || array[position] == blank)
	{
		if (debug)
			//cout << "Slot " << position << " empty. " << endl;
			return true;
	}
	else
	{
		if (debug)
			//cout << "Slot " << position << " contains a block " << endl;
			return false;
	}

}

// ------------------------------------------------------------------------ //
//																			//
//																			//
//				Functions for robot test cases								//
//																			//
//																			//
// ------------------------------------------------------------------------ //

//
// Function get_block_testcase
// Returns blocks for a given test case one at a time
// This function can be used for all five test cases.
//
// Input: Unsigned int case designating test case 1-5.
// Input: Index of letter desired 0 - 19.
// Returns: a single uppercase character
//
// Example function call: 	block = get_block_testcase(1, 0);
// Returns first letter of test case 1.

char get_block_testcase(unsigned int testcase, unsigned int index)
{
	string testcases[5] = { "AXFIUTRPQVWSEYJINYTB",
		"ABFGHIJKMOPRSTUVWXYZ",
		"ZYXWVUTSRPOKJIIHGFBA",
		"AAAAAYYYYYQQQQQXXXXX",
		"XXXAAAZZZAAYYVVVVQQQ" };

	return testcases[testcase - 1].at(index);
}


//
// Function get_block_testone
// Returns blocks for test case one one at a time
// Returns: a single character
//
// Example function call: 	block = get_block_testone();

char get_block_testone(void)
{
	static unsigned int index = 0;
	char test_case_one[21] = "AXFIUTRPQVWSEYJINYTB";
	return test_case_one[index++];
}



//
// Function get_block_testtwo
// Returns blocks for test case two one at a time
// Returns: a single character
//
// Example function call: 	block = get_block_testtwo();

char get_block_testtwo(void)
{
	static unsigned int index = 0;
	char test_case_two[21] = "ABFGHIJKMOPRSTUVWXYZ";
	return test_case_two[index++];
}

//
// Function get_block_testthree
// Returns blocks for test case three one at a time
// Returns: a single character
//
// Example function call: 	block = get_block_testthree();

char get_block_testthree(void)
{
	static unsigned int index = 0;
	char test_case_three[21] = "ZYXWVUTSRPOKJIIHGFBA";
	return test_case_three[index++];
}
//
// Function get_block_testfour
// Returns blocks for test case four one at a time
// Returns: a single character
//
// Example function call: 	block = get_block_testfour();

char get_block_testfour(void)
{
	static unsigned int index = 0;
	char test_case_four[21] = "AAAAAYYYYYQQQQQXXXXX";
	return test_case_four[index++];
}

//
// Function get_block_testfive
// Returns blocks for test case five one at a time
// Returns: a single character
//
// Example function call: 	block = get_block_testfive();

char get_block_testfive(void)
{
	static unsigned int index = 0;
	char test_case_five[21] = "XXXAAAZZZAAYYVVVVQQQ";
	return test_case_five[index++];
}



Is This A Good Question/Topic? 0
  • +

Replies To: Block Robot Program - C++

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Block Robot Program - C++

Posted 07 November 2019 - 12:45 PM

What's your input and what is it giving back to you?
Was This Post Helpful? 0
  • +
  • -

#3 ascesix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-November 19

Re: Block Robot Program - C++

Posted 07 November 2019 - 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  
B E F I I J N P Q  A  R  S  T  T  U V  W  X  Y  Y


This post has been edited by Skydiver: 07 November 2019 - 07:54 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Block Robot Program - C++

Posted 07 November 2019 - 01:03 PM

So that's your input and output, or what/
Was This Post Helpful? 0
  • +
  • -

#5 ascesix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-November 19

Re: Block Robot Program - C++

Posted 07 November 2019 - 01:05 PM

sorry this is my input
AXFIUTRPQVWSEYJINYTB

This post has been edited by Skydiver: 07 November 2019 - 07:53 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Block Robot Program - C++

Posted 07 November 2019 - 03:27 PM

I would treat it like a bubble sort.

Put a letter in the first slot of the array.
Get the next letter.
Compare with the letter in the first slot.
If the 2nd letter is greater than the first insert into the 2nd slot.
If not then move slot 0's letter to slot 1, and insert 2nd letter into slot 0.
Repeat and shift slotted numbers as needed.
Was This Post Helpful? 0
  • +
  • -

#7 ascesix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-November 19

Re: Block Robot Program - C++

Posted 07 November 2019 - 03:29 PM

That's what it does now, it starts from the left and compares everything to the right, but when it gets to the 10th space where the A is it just skips it.

This post has been edited by Skydiver: 07 November 2019 - 07:53 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,588
  • Joined: 12-June 08

Re: Block Robot Program - C++

Posted 07 November 2019 - 04:05 PM

I don't get why 'A' is put in spot 10. Why not 0 and bubble sort?

Also your output looks terribly butchered with missing letters and added others.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Block Robot Program - C++

Posted 07 November 2019 - 07:54 PM

ascesix: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1