• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

sortowanie, tablice, c++

VMware Cloud PRO - przenieś swoją infrastrukturę IT do chmury
+1 głos
324 wizyt
pytanie zadane 5 maja 2018 w C i C++ przez Hikori Nowicjusz (180 p.)

Witam,
Czy ktoś z Was mógłby sprawdzić czy dobrze wykonałem 3 zadania, które znajdują się pdfie?
Wszystko polecenia wykonałem (tak, wszystko ma się odbywać w jednym programie), jednak nie jestem do końca pewien czy dobrze zrozumiałem treść poleceń.
Chodzi mi tylko o potwierdzenie poprawności napisanego programu bądź wskazanie nad czym powinienem jeszcze popracować.
Z góry dzięki za jakąkolwiek pomoc :)

#include <iostream>
#include <algorithm>


struct Element {
    int key;
};

void copyElementsDescending (int *array1, int *array2) {  
    int j = 2;
    for(int i = 0; i < 3; i++) {
        array2[j] = array1[i];
        j--;
    }
}

void sortByOrder(Element *list_array, int size) {  //Task 2
    int ccounter = 0; // comprasion counter
    int ecounter = 0; // exchange counter
    for(int i = 0; i < size - 1; i++) {
        int    min = i;
        for(int j = i; j < size; j++) {
            if(list_array[j].key < list_array[min].key) {
                   min = j;
                ccounter++;
            }
        }
        if(min != i) {
            std::swap(list_array[min],list_array[i]);
               ecounter++;
        }
        ccounter++;
        for(int k = 0; k < size; k++) {

            std::cout << list_array[k].key << " ";

        }
        std::cout << "\n";
    }
    std::cout << "Comprasion counter: " << ccounter;
    std::cout << "\nExchange counter: " << ecounter << "\n";

}

void sortByInsertion(Element *list_array, int size) { //Task 3
     int counter = 0;
     for(int i = 1; i < size; i++) {
        int j = i;
        while(j > 0 && list_array[j].key < list_array[j-1].key) {
            std::swap(list_array[j], list_array[j - 1]);
            j--;
            counter++;
        }
        for(int k = 0; k < size; k++) {

            std::cout << list_array[k].key << " ";

        }
        std::cout << "\n";
     }
     std::cout << "Exchange counter: " << counter << "\n";
}

void printArray(int *array, int size) {
    for(int i = 0; i < size; i++) {
        std::cout << array[i] << " ";
    }
}

void printStructArray(Element *list_array, int size) {
    for(int i = 0; i < size; i++) {

        std::cout << list_array[i].key << " ";

    }
}


int main() {

    int A[3] = { 3, 2, 1 };
    int B[3];

    std::cout << "\nHello!\nThis is array A which elements have been copied to array B in descending order:\n";
    std::cout << "Array A : ";
    printArray(A,3);
    copyElementsDescending(A,B);
    std::cout << "\n";
    std::cout << "Array B : ";
      printArray(B,3);
    std::cout << "\n";


    int length = 8;

    Element list_array2[length] = { {44}, {55}, {12}, {42}, {94}, {18}, {6}, {67} };
    Element list_array3[length] = { {44}, {55}, {12}, {42}, {94}, {18}, {6}, {67} };

    std::cout << "\nThis is task 2. Sorting by selection.";
    std::cout << "\nThis is array of struct elements before sorting : \n";
    printStructArray(list_array2,length);
    std::cout << "\n";
    sortByOrder(list_array2,length);
    std::cout << "This is an array of struct elements after sorting : \n";
    printStructArray(list_array2,length);
    std::cout << "\n";

    std::cout << "\nThis is task 3. Sorting by insertion.";
    std::cout << "\nThis is array of struct elements before sorting : \n";
    printStructArray(list_array3,length);
    std::cout << "\n";
    sortByInsertion(list_array3,length);
    std::cout << "This is an array of struct elements after sorting : \n";
    printStructArray(list_array3,length);
    std::cout << "\n\n\n\n";

    int length2 = 8;

    Element list_array4[length2] = { {94}, {67}, {55}, {44}, {42}, {18}, {12}, {6} };
    Element list_array5[length2] = { {94}, {67}, {55}, {44}, {42}, {18}, {12}, {6} };

    std::cout << "\nThis is task 2. Sorting by selection.";
    std::cout << "\nThis is array of struct elements before sorting : \n";
    printStructArray(list_array4,length2);
    std::cout << "\n";
    sortByOrder(list_array4,length);
    std::cout << "This is an array of struct elements after sorting : \n";
    printStructArray(list_array4,length2);
    std::cout << "\n";

    std::cout << "\nThis is task 3. Sorting by insertion.";
    std::cout << "\nThis is array of struct elements before sorting : \n";
    printStructArray(list_array5,length2);
    std::cout << "\n";
    sortByInsertion(list_array5,length2);
    std::cout << "This is an array of struct elements after sorting : \n";
    printStructArray(list_array5,length2);


    return 0;

}

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
1 odpowiedź 1,737 wizyt
0 głosów
1 odpowiedź 157 wizyt
0 głosów
2 odpowiedzi 269 wizyt
pytanie zadane 24 stycznia 2018 w C i C++ przez mn130496 Gaduła (3,530 p.)

93,440 zapytań

142,431 odpowiedzi

322,679 komentarzy

62,802 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

...