T-Bird
T-Bird

Reputation: 187

How to remove an element from an array and then shift other elements over?

My problem is with the last function, print_valid_data. I'm supposed to delete the second minimum value from array A[]. After the value is deleted the elements in the array need to be shifted over, with the last open spot in the array to be empty. What I did in my function was skip over it, and print out what was left. How would I got about doing it by deletion and then shifting?

Side note: The Var.h is a file only with the data "const int NUM = 10" could be subject to change, it's for the index of the array. The "data.txt" file has some integers (the program accounts for >10 numbers or <10 numbers) that are read in with the Load_file_to_array function.

#include <iostream>
#include "Var.h"
#include <fstream>
#include <string>

using namespace std;

void Load_File_To_Array(int A[], string filename, int size);
int Get_2nd_Smallest(int A[], int size);
void Print_valid_data(int A[], int SecondMin, int size);

int main()
{
    int A[NUM];
    int size = NUM;
    int SecondMin = 0;
    string filename = "Data.txt";

    Load_File_To_Array(A, filename, size);
    Get_2nd_Smallest(A, size);
    Print_valid_data(A, SecondMin, size);

    cout << endl;

    system("Pause");
    return 0;
}

void Load_File_To_Array(int A[], string filename, int size)
{
    ifstream infile;
    infile.open(filename);

    cout << "The array is: ";

    for (int i = 0; i < NUM; i++)
    {
        if (infile.eof())
        break;

        infile >> A[i]; 
        cout << A[i] << ", ";
    }

    return;
}

int Get_2nd_Smallest(int A[], int size)
{
    int Min = A[0];
    int SecondMin = 0;

    for (int i = 0; i < NUM; i++)
    {
        if (Min > A[i])
            Min = A[i];
    }

    //Exchange-sort to sort the array in descending order
    int j, k;
    int counter;
    for (j = 0; j < (size - 1); j++)
    {
        for (k = (j + 1); k < size; k++)
        {
            if (A[j] < A[k])
            {
                counter = A[j];
                A[j] = A[k];
                A[k] = counter;
            }
        }
    }

    for (int n = (NUM - 1); n > -1; n--)
    {
        SecondMin = A[n];
        if (SecondMin >= 0)
        {
            SecondMin = A[n - 1];
            break;
        }
        else;
    }

    cout << endl << "The minimum number is: " << Min << endl;
    cout << "The second smallest integer is: " << SecondMin << endl;

    return SecondMin;
}

void Print_valid_data(int A[], int SecondMin, int size)
{
    cout << "The new array is: ";

    for (int i = 0; i < size; i++)
    {
        if (A[i] != SecondMin)
        {
            cout << A[i] << ", ";
        }
    }

    return;
}

Upvotes: 0

Views: 703

Answers (1)

Jarod42
Jarod42

Reputation: 217085

Following may help, the removal is done by copying value over the previous one:

const int size = 6;
int v[] = {5, 6, 8, 2, 3, 1};

std::partial_sort(std::begin(v), std::begin(v) + 2, std::end(v));

std::cout << "The minimum number is: " << v[0] << std::endl;
std::cout << "The second smallest integer is: " << v[1] << std::endl;

std::copy(std::begin(v) + 2, std::end(v), std::begin(v) + 1);
std::cout << "remaining values: ";
for (int i = 0; i != size - 1; ++i) {
    std::cout << v[i] << ", ";
}

Live example

Upvotes: 1

Related Questions