简体   繁体   中英

A function to shift the elements of an array one space left or right

I have been trying to solve this problem for several days and googled a lot of different options. None of the code I've found has been able to help me solve my problem; and I'm sorry if this is something that's already been answered elsewhere on the web. I feel like this something I should be able to solve on my own but nothing seems to work. The program that I am designing is a program that is supposed to be for an imaginary robot. The robot is fed random characters from a chute and has to place them in a set of 20 slots. It has a hardware restriction that forces it to put the first block into slot 10. The issue that I'm having is I am trying to create a function that shifts every element within an array one space to the left or the right. I've tried a number of different things. The most success I had was with this bit of code.


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

using namespace std;

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;
    }
}
// This function checks if the blocks are less than or == to the block in the robots hand
// If the block is equal to it return TRUE if the block is < it return FALSE

bool robot_equal_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 EQUAL to block in slot. " << endl;
        return true;
    }
    else
    {
        if (debug)
            cout << "Returning false. Robot block LESS THAN 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;
    }

}
char * move_all_to_right(char slot[], char block){
    for(int i = 0; i < 20; i++){
        if((!(test_empty(i,slot))) && (i != 20)){
            block = switch_blocks(block,i,slot);
            put_block(block,i+1,slot);
            block = switch_blocks(block, i+1, slot);

        }
        else if(i == 20){
            break;
        }
    }
    return slot;

}

int main() {
    char block = get_block();
    char slot[20];
    for (int i = 0; i < 20; i++) {
        slot[i] = NULL;
    }
    int array_size;
    int position;
    char robot;
    int counter = 0;
    int dummy;
    int filled_right;
    bool swapped = false;
    int block_counter = 0;
    int switch_counter = 0;
    //Put the first block from the chute INTO SLOT 10(hardware restriction)
    put_block(block, 10, slot);
    do {
        block = get_block();
        position = counter;
        for(int i = 0; i <sizeof(slot); i++) {
            unsigned int back_to_front = sizeof(slot);
            unsigned int front_to_back = 0;
            if(!(test_empty(back_to_front, slot))){
                do{
                    back_to_front--;
                }while(test_empty(back_to_front, slot));
                do{
                    front_to_back++;
                }while(test_empty(front_to_back,slot));
                for(int i = 0; i <sizeof(slot); i++){
                    if(!(test_empty(i,slot))){
                        block_counter++;
                    }
                }
                if((block <= slot[front_to_back]) && (test_empty(0,slot))){
                    put_block(block, front_to_back - 1,slot);
                    break;
                }
                else if((block > slot[back_to_front]) && (test_empty(19,slot))){
                    put_block(block,back_to_front + 1,slot);
                    break;
                }
                if((block == slot[back_to_front]) && (test_empty(19,slot))){
                    put_block(block,back_to_front + 1,slot);
                    break;
                }
                if(((block < slot[back_to_front]) || (block == slot[front_to_back])) && (test_empty(19,slot))){
                        while((block < slot[back_to_front]) || (block == slot[back_to_front]) &&(block!= NULL)){
                            block = switch_blocks(block,back_to_front-1,slot);
                            back_to_front--;
                            switch_counter ++;
                            if((block == slot[back_to_front]) && (switch_counter != block_counter + 1)){
                                for(int i = back_to_front; i< sizeof(slot); i++){
                                   block = switch_blocks(block,i,slot);
                                   if(test_empty(back_to_front-1,slot)){
                                       break;
                                   }
                                }
                            }
                        }
                }
            }
        }
        block_counter = 0;
        print_slots(slot);
        counter++;
        switch_counter = 0;
    } while (counter < 19);
    for(int i = 0; i < sizeof(slot); i++){
        cout << slot[i];
    }
}

This seemed to work until I introduced redundant characters so I have sense scrapped this approach for something much simpler.

int main() {
    char block = get_block();
    char temp_block = ' ';
    char array = *slot;
    for (int i = 0; i < 20; i++) {
        slot[i] = NULL;
    }
    int counter = 0;

    //Put the first block from the chute INTO SLOT 10(hardware restriction)
    put_block(block, 10, slot);
    do {
        block = get_block();
        if (block > slot[10]) {
            for(int i = 9; i < 19; i ++){
                if((block > slot[i]) &&!(test_empty(i,slot)) || i == 19){
                    shift_all_left(slot);
                    put_block(block,i,slot);
                    break;
                }
            }
        }
        if (block <= slot[10]) {
            for(int i = 10; i > 0; i --){
                if((block <= slot[i]) || i == 0){
                    shift_all_right(slot,20, block);
                    put_block(block,i,slot);
                    break;
                }
            }
        }
        counter++;
        print_slots(slot);
    }while(counter < 19);
    return 0;
}

the shift right function that I have come up with doesn't actually shift the first character of my array and frankly it doesn't work at all.

this is what I have so far.

void shift_all_right(char array[],int n, char block){
    int i = 10;
    while(i < n - 1){
        block = switch_blocks(block,array[i],array);
        i++;
    }
    return;
}

I still have all of the functions from the first attempt, and I have a shift_all_left function as well that was another idea that I tried that doesn't work as intended. In fact when I combine the two it does some weird stuff in my program.

void shift_all_left(char array[]){
    for(int i = 10; i > 0; i--){
        switch_blocks(array[i],array[i-1],array);

    }

}

I'm sorry again if this is something that I should be able to figure out but I feel like I'm bashing my head against a brick wall. I really want to be able to solve this without help, but I simply can not.

N is meant to be the size of the array. My idea was that I would always start at slot 10 and sort from there, but the more I think about this it seems like I should start at the position that the block was <= in the array.

void shift_right(int position, int size, char array[], char block){
   while(position < size - 1){
block = switch_block(block,array[position+1],array);}

am I on the right track?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM