Question
C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating
C++ Programming - Design Process
I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code:
//list of all the header files #include
using namespace std;
//Here are the Function Prototypes unsigned int put_block(char block, unsigned int position, char array[]); char get_block(void); unsigned int shift_right(unsigned int position); unsigned int shift_left(unsigned int position); bool compare_blocks(char robot, char in_slot); char switch_blocks(char robot, unsigned int position, char array[]);
int main() {//Start of main
//List of variables
unsigned int position, counter = 0, i, z, flag1 = 0, flag2 = 0, flag3 =0; char block, yn, robot, in_slot, slots[20];
while (flag3 == 0) { for (z = 0; z < 20; z++)//This for loop is to set the initial array to NULL { slots[z] = '#'; }
cout << "Welcome to the intial loading screen for the Baby Block Robot." << endl; cout << "This robot is designed to take an assortment of blocks and place them in alphabetcial order in the slots provided." << endl; cout << endl << endl; cout << "Please enter the intial slot (1-20) where you would like the robot to start." << endl; while (flag1 == 0) { cin >> position; if (position >= 1 && position <= 20)//this if statement is used to determine the position of the robot entered by the user { flag1 = 1; cout << "You have chosen to start at the spot marked \"" << position << "\"." << endl; position -= 1; } else { cout << "Invalid intial starting point.Please try again." << endl; } }
block = get_block(); //Fuction call fot the get_block function.
while (position != 0) { position = shift_left(position);//Function call for the shift_left function. }
put_block(block, position, slots); //Funtion call for the put_block function.
for (i = 0; i < 19; i++) { flag2 = 0; while (position != 0) { position = shift_left(position);//Function call for the shift_left function. } block = get_block();//Fuction call fot the get_block function. robot = block; in_slot = slots[position]; while (flag2 == 0) { robot = block; in_slot = slots[position];//this is what places the character in the slots.
if (slots[position] == '#')//this is to test if the slot is empty or not { cout << "Slot " << position + 1 << " empty. " << endl; put_block(block, position, slots); //Funtion call for the put_block function. flag2 = 1; } else { cout << "Slot " << position + 1 << " contains a block " << endl;
if (compare_blocks(robot, in_slot))//Function call for the compare_blocks function. { block = switch_blocks(block, position, slots);//Function call for the shift_block function. counter++;//used to count the number of switchs position = shift_right(position);//Function call for the shift_right function. }
else { position = shift_right(position);//Function call for the shift_right function. } } } } cout << endl << endl; cout << "Your blocks have been sorted! In order they are as follows: " << endl; //this is used to print all the charcters enterd by the user after being sorted by the robot. for (z = 0; z < 20; z++) { cout << "\"" << slots[z] << "\" "; } cout << endl << endl;
cout << endl << "This program switched approximately " << counter << " blocks to complete it's goal." << endl;
cout << endl << endl;
cout << "Would you like to sort another set? Y/N" << endl; //this while loop is used to ask the user if they want to run the program again or quit after the first run. int flag4 = 0; while (flag4 == 0) { cin >> yn; if (yn == 'Y' || yn == 'y' || yn == 'N' || yn == 'n') { if (yn == 'Y' || yn == 'y') { cout << "Rebooting..." << endl; flag4 = 1; cout << endl << endl; } else { cout << "Quiting..." << endl; cout << "Have a great day..." << endl; flag4 = 1; flag3 = 1; system("pause"); } } else { cout << " Invalid character input.Enter \"Y\" or \"N\"" << endl; } } } return 0; }//end of main
//////////////////////////////////////////////////////////////////////////// // // // List of Functions // // // ////////////////////////////////////////////////////////////////////////////
// Function get_block // Reads in a single character value from the keyboard // This is the input from the chute // Returns: a single character // // Example function call: block = get_block();
char get_block(void) { cout << "Enter the current character in the block chute." << endl; char block; cin >> block; return block; }
// Function put_block // This function stores a character into the character array representing the slots // // Inputs: // block - type char - The character to be inerted 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[]) { array[position] = block; cout << endl << endl; cout << "Block " << block << " inserted into slot " << position + 1 << endl; return position; }
// 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) { position++; cout << "Position right shifted to " << position + 1 << 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) { position--; cout << "Position left shifted to " << position + 1 << endl; return position; }
// Function compare_blocks // 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 compare_blocks(char robot, char in_slot) {
cout << endl << "Comparing robot block \"" << robot << "\" with the block in the slot \"" << in_slot <<"\"" << endl; if (robot <= in_slot) { cout << "returning true. Robot block LESS than or EQUAL to block in slot. " << endl; return true; } else { 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;
cout << "Switching blocks " << robot<< " with " << array[position] << endl; temp_hold = robot; robot = array[position]; array[position] = temp_hold; return robot; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started