Sortowanie tablic nie zmienia kolejności

0

witam mam pewien problem z posortowanie tablicy mianowicie (sortowanie jest dopiero na początku) wpisując

1
2
A 2 1
B 1 1

powinienem dostać

B 1 1
A 2 1

ale mam

A 2 1
B 1 1

problem jest w funkcji exchangeValue ale nie wiem jaki?

#include <iostream>
#include <string>
#include <cstdio>

struct points {
    char * letter = new char;
    int *A = new int;
    int *B = new int;

    ~points() {
        delete letter;
        delete A;
        delete B;
    }
};

void comparison (points tabPoints[] , int size , points tabSecondary[]);
void exchangeValue (points tabMin[] , points tabPoints[] , int i , int j );

int main()
{
    int step = 0;
    int howManyPoints = 0;
    int index = 0;
    std::cin >> step >> howManyPoints;
    int sizeTab = howManyPoints;

    points * tabPoints      =   new points [howManyPoints];
    points * tabSecondary   =   new points ;
    points * tabSort        =   new points [howManyPoints];

    while (step != 0) {
        while (howManyPoints != 0) {
            std::cin >> *tabPoints[index].letter >> *tabPoints[index].A >> *tabPoints[index].B;
            ++index;
            --howManyPoints;
        }

        --step;
    }
    comparison (tabPoints, sizeTab, tabSecondary);

    delete [] tabPoints;
    return 0;
}

void comparison (points tabPoints[] , int size , points tabSecondary[]) {
    for (int i = 0 ; i < size ; i++) {
        points tabMin;
        tabMin.letter  = tabPoints[i].letter;
        tabMin.A       = tabPoints[i].A;
        tabMin.B       = tabPoints[i].B;
        for (int j = i+1; j < size ; j++) {
            if (*tabMin.A > *tabPoints[j].A) {
                exchangeValue( &tabMin , tabPoints , i , j );
            }
        }
    }
}

void exchangeValue (points tabMin[] , points tabPoints[] , int i , int j ) {
    points tabTamp;

    tabTamp.letter  = tabMin->letter;
    tabTamp.A       = tabMin->A     ;
    tabTamp.B       = tabMin->B     ;

    tabMin->letter  = tabPoints[j].letter;
    tabMin->A       = tabPoints[j].A     ;
    tabMin->B       = tabPoints[j].B     ;

    tabPoints[i].letter = tabTamp.letter;
    tabPoints[i].A      = tabTamp.A     ;
    tabPoints[i].B      = tabTamp.B     ;

    for (int k = 0 ; k < 2 ; ++k) {
            std::cout << *tabPoints[k].letter << " " << *tabPoints[k].A << " " << *tabPoints[k].B << std::endl;
    }
}
3

Gdybyś nie próbował na siłę wskaźników tutaj wsadzić, tylko miał normalną strukturę

struct Point
{
    char letter;
    int A;
    int B;
};

oraz std::vector zamiast dynamicznych tablic to całe sortowanie by się sprowadziło do

std::sort(myPoints.begin(), myPoints.end(),
           [](const Point& left, const Point& right) { return left.A < right.A; });

Twój kod jest mocno niepoprawny, więcej tu: https://4programmers.net/Forum/1339809

1 użytkowników online, w tym zalogowanych: 0, gości: 1