2

I've got some working sorting going on with numbers in ascending order, monetary values ranging from 500 to 25000. However, I need addresses assigned to each of these values, but these addresses (Ex: 151 Acorn) Do not follow an alphabetical or numerical pattern such as highest to lowest.

Is there any algorithm for sorting that could assist me here? My current code will be posted below, but my goal is to have the set arrays have the addresses and values matching, starting from [0] - [6].

    #include<iostream>
#include<string>
#include <iomanip>
#include <cstdlib>


using namespace std;

int totalsList(int value[7], string address[7]);

void lookUp(int value[], string address[]);
void display(int value[], string address[]);
void showHighest(int value[], string address[]);
void sortArray(int[], int);
void sortStrings(string[], string);
void showArray(const int[], int);

int main() {
    //Definitions
    int size = 0;
    int value[7] = { 500, 1000, 1500, 6000, 15000, 20000, 25000 };
    string address[7]{ "151 Acorn", "120 Xenia", "161 Acorn", "161 Acorn", "200 Main", "200 Acorn", "500 Arcade" };

    // MAIN MENU
    int choice;
    cout << "County Auditor's Tax Look Up Program:\n";
    cout << "1.) Display The Data.\n";
    cout << "2.) Look up Taxes.\n";
    cout << "3.) Sort taxes in ascending order.\n";
    cout << "4.) Show Property with largest tax due.\n";
    cout << "5.) Exit Program.\n";
    cin >> choice;

    while (choice <= 5) {

        if (choice == 1) {
            display(value,address);
        }

        if (choice == 2) {

            lookUp(value,address);

        }

        if (choice == 3) {

            sortArray(value, 7);
            sortString(address, 7);
            cout << "Sorted Values: \n";
            showArray(value, 7);


            system("pause");



}


        if (choice == 4) {
            showHighest(value, address);
        }
    }

    //exit
    if (choice == 5) {
        return 0;
    }
    system("pause");



}


void lookUp(int value[], string address[]) {
    string add;
    cout << "Enter Address: ";


    getline(cin, add);
    getline(cin, add);
    // Validate String entry
    if (add != address[0] || add != address[1] || add != address[2] || add != address[3] || add != address[4]
        || add != address[5] || add != address[6] || add != address[7]) {
        cout << "This address is not found, please re-enter\n";

    }

    // Look up function shows cost and address according to arrays
    int valCor = 0;
    if (add == address[0]) {
        valCor = 500;
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[1]) {
        valCor = 1000;
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[2]) {
        valCor = value[2];
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[3]) {
        valCor = value[3];
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[4]) {
        valCor = value[4];
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[5]) {
        valCor = value[5];
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }
    if (add == address[6]) {
        valCor = value[6];
        cout << "The address: " << add << " has tax due of: " << "$" << valCor << endl;
    }

    system("pause>nul");

}

void display(int value[], string address[]) {
    // Display all arrays in table format
    cout << "All Addresses and Owed Taxes, correspondingly: \n";
    cout << "----------------------------------------------\n";

    cout << address[0] << " Tax: $" << value[0] << endl;
    cout << address[2] << " Tax: $" << value[0] << endl;
    cout << address[3] << " Tax: $" << value[0] << endl;
    cout << address[4] << " Tax: $" << value[0] << endl;
    cout << address[5] << " Tax: $" << value[0] << endl;
    cout << address[6] << " Tax: $" << value[0] << endl;
    system("pause");

}

void showHighest(int value[], string address[]){

    cout << "Highest Value: \n";
    cout << "The Highest Property tax is on the location " << address[6] << "with a tax of: $" << value[6];


}

void sortArray(int array[], int size)
{
    bool swap;
    int temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void sortString(string array[], int size) 
{
    bool swap;
    int temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void showArray(const int array[],int size)
{
    for (int count = 0; count < size; count++)
        cout << "$" << array[count] << "\n";
    cout << endl;
}
Ace
  • 31
  • 4
  • Instead of parallel arrays, think about a single array of `struct`. You can use `std::sort` to sort it. – Cheers and hth. - Alf Sep 14 '15 at 00:52
  • I'd suggest that you create a struct of two members: an `int` for your monetary value, and a `string` for the address. Have a single array of structs instead of two separate arrays. Then when you sort the array on value, the addresses will come along. – Logicrat Sep 14 '15 at 00:53
  • Could you give me a small example or link? Sounds useful, I've just never seen this method. – Ace Sep 14 '15 at 00:54

1 Answers1

1

I would use std::sort. You will need your own data structure to package the int and the std::string together.

Side note: Try to avoid using namespace std;. It is bad practice

Community
  • 1
  • 1
fuzzything44
  • 701
  • 4
  • 15
  • Every tool has its place. No tool is inherently bad practice. `using namespace std;` is not a good idea in the global namespace in a header file (because it will tend to add work), but it is a good idea in a short little explorative program or ad-hoc tool (because it there saves work), and some other cases are not clear cut. In short, the absolutism of "is bad practice" is just bullshit. You might as well say that pointers are bad pracrtice. They can mess up things. C++ programming needs intelligence. One can't do it well by applying mechanical absolute rules. – Cheers and hth. - Alf Sep 14 '15 at 01:14
  • I'm doing a lot of research on the sort and struct method but cannot find a very specific idea to this. Is there any way you may be able to provide a basic one? – Ace Sep 14 '15 at 01:43