Thread: [beginner problem] program stuck running forever, what did I do wrong?

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    [beginner problem] program stuck running forever, what did I do wrong?

    Hello, I'm a beginner in C++ (started 1 week ago), but I have some basic knowledge in algorithms and data structures and java, so I understand the basics of OO programming.

    I am building a simple program to sort a vector of values, that lets the user choose which sorting algorithm to use and keeps track of running time for each sorting method.

    So far I did the bubble sort and selection sort algorithms. The program itself gives no warnings nor errors, but it gets stuck once the vector of values has been formed and it needs to be sorted. In fact, I believe that the call to the bubble sort / selection sort function never materializes.

    It is likely a rookie mistake, anyone can help?

    Here is the code:

    main.cpp

    Code:
    #include <iostream>
    #include <string>
    #include <locale>
    #include <vector>
    #include "bubblesort.h"
    #include "selectionsort.h"
    
    
    using namespace std;
    
    
    struct sorting_system {
        int ID;
        string name;
    };
    
    
    vector<int> populateArray();
    
    
    int main (int argc, char** argv)
    {
    
    
        setlocale(LC_ALL, "");
    
    
        time_t starting_time = 0, ending_time = 0;
    
    
        std::cout << "Scegli dalla legenda il tipo di ordinamento che vuoi effettuare: \n";
        std::cout << "1. Bubble Sort" << endl;
        std::cout << "2. Selection Sort" << endl;
        std::cout << "3. Insertion Sort" << endl;
        std::cout << "4. Merge Sort" << endl;
        std::cout << "5. Quick Sort" << endl;
        std::cout << "0. Termina l'applicazione" << endl;
    
    
        sorting_system sort;
    
    
        std::cin >> sort.ID;
    
    
        do{
            switch(sort.ID)
            {
            case 1: {
                sort.name = "Bubble Sort";
    
    
                vector<int> array_to_be_sorted;
    
    
                array_to_be_sorted = populateArray();
                starting_time = time(0);
    
    
                bubblesort bs;
    
    
                cout << "Attendere. Ordinamento dell'array in corso... \n";
                bs.BubbleSort(array_to_be_sorted);
                ending_time = time(0);
                }
            case 2: {
                sort.name = "Selection Sort";
                vector<int> array_to_be_sorted;
    
    
                array_to_be_sorted = populateArray();
                starting_time = time(0);
    
    
                selectionsort ss;
    
    
                cout << "Attendere. Ordinamento dell'array in corso... \n";
                ss.SelectionSort(array_to_be_sorted);
                ending_time = time(0);
                }
            /* (... other algorithms (case 3, 4, 5) ...) */
            case 0: {
                cout << "Terminazione dell'applicazione." << endl;
                break;
                }
            default:
                cout << endl << "Error in menu input. Valid menu options are 0 to 5." << endl;
                break;
            }
    
    
            "Tempo trascorso per il metodo ", sort.name, " : ", ending_time - starting_time,".\n";
    
    
        } while (sort.ID != 0);
    
    
        return 0;
    }
    
    
    vector<int> populateArray()
    {
        int size;
        cout <<  endl << "Enter the size of the list with integers:" << endl ;
        cin >> size;
        
        vector<int> numbers_array(size);
        int random_number;
    
    
        for (int i = 0; i < size; i++) 
        {
            random_number = rand()%size + 1;
            numbers_array[i] = random_number;
        }
    
    
        return numbers_array;    
    
    
    }
    bubblesort.h

    Code:
    #pragma once
    #include <vector>
    
    using std::vector;
    
    class bubblesort
    {
    public:
        bubblesort(void);
        ~bubblesort(void);
        bool BubbleSort (std::vector<int> array_to_be_sorted);
    };
    bubblesort.cpp

    Code:
    #include "bubblesort.h"
    #include <iostream>
    #include <vector>
    
    
    using std::vector;
    
    
    bubblesort::bubblesort(void)
    {
    }
    
    bubblesort::~bubblesort(void)
    {
    }
    
    bool bubblesort::BubbleSort(vector<int> array_to_be_sorted)
    {
        try {
            int temp = 0;
    
            for (unsigned int i = 0; i < array_to_be_sorted.size(); i++)
            {
                    if (array_to_be_sorted[i] > array_to_be_sorted[i + 1])
                    {
                        temp = array_to_be_sorted[i + 1];
                        array_to_be_sorted[i + 1] = array_to_be_sorted[i];
                        array_to_be_sorted[i] = temp;
                    }
            }
    
            std::cout << "La sequenza ordinata  la seguente: ", array_to_be_sorted, ".\n";
            return true;
        } catch(...) {
            std::cout << "Si  verificato un errore. Terminazione del programma di ordinamento.\n";
            return false;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,679
    > if (array_to_be_sorted[i] > array_to_be_sorted[i + 1])
    Well it seems like this would be an out of bound access once you hit the end of the vector.

    Also, pass the vector as a reference rather than by value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    Quote Originally Posted by Salem View Post
    > if (array_to_be_sorted[i] > array_to_be_sorted[i + 1])
    Well it seems like this would be an out of bound access once you hit the end of the vector.

    Also, pass the vector as a reference rather than by value.
    This program has got quite a few problems, actually, I forgot to add a "break" in all switch cases, I wrote incorrectly the bubblesort (this has no excuses, really) and even a few cout statements... it's actually a mess.

    Thank you for your suggestion, the program was stuck probably because of what you said, I passed the vector as a value rather than reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with running simpel beginner aritmetic program
    By danielj1988 in forum C Programming
    Replies: 2
    Last Post: 04-18-2016, 08:10 PM
  2. Replies: 3
    Last Post: 11-13-2012, 12:17 AM
  3. Replies: 3
    Last Post: 09-06-2009, 01:48 PM
  4. Replies: 7
    Last Post: 03-09-2006, 12:06 PM
  5. Please help! Beginner stuck on table and array problem!
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-10-2005, 11:42 AM

Tags for this Thread