-1

I'm beginning to learn about C++. I've been putting it off for a long time and I decided to start learning it.

Currently, I'm having trouble finding the issue with my program. My program is supposed to take integers from the input, insert it into an array, and then sort it. Everything is working correctly-- even the sorting... most of the time...

Sometimes the sorting works as intended. Sometimes it spits the numbers out in random orders. Sometimes it output really weird negative and positive integers that are very close to the upper and minimum bounds that integers can go. After hours of trying to figure out something about this, I just can't figure it out. I've been thinking that it has something with the pointers for the array? But I'm unsure because I barely know how pointers work.

I've tried setting the array to hard-coded values and sorting with different algorithms, none of which helped whatsoever.

#include <iostream>

/*Sorting program*/

using namespace std;

int* sortArray(int* array, int size) {  

    for (int i = 0; i < size; i++) {
        int lowest;

        for (int k = i; k < size; k++) {
            if (array[k] < array[i] && array[k] < array[lowest]) {
                cout << array[k] << " is less than " << array[i] << endl;

                lowest = k;
            }
        }
        
        int temp = array[lowest];
        array[lowest] = array[i]; 
        array[i] = temp;
    }
 
    return array;
}


int main() {
    int low, high, target, size;

    cout << "Enter size of array : ";
    cin >> size;

    int *array = new int[size];

    for (int i = 0; i < size; i++) {
        cout << "Enter array[" << i << "] : " << endl;

        int entry;
        cin >> entry;

        array[i] = entry;
    }

    /*     */

    array = sortArray(array, size);

    for (int i = 0; i < size; i++) {
        cout << "array[" << i << "] = " << array[i] << endl;
    }


    return 1;
}

Output

>>OutputFile.exe
Enter size of array : 4
Enter array[0] :
3
Enter array[1] :
4
Enter array[2] :
7
Enter array[3] :
4
array[0] = 2059292487
array[1] = 3
array[2] = 4
array[3] = 7

Output (program ran again, nothing changed)

>>OutputFile.exe
Enter size of array : 8
Enter array[0] :
3
Enter array[1] :
4
Enter array[2] :
8
Enter array[3] :
1
Enter array[4] :
88
Enter array[5] :
4
Enter array[6] :
5
Enter array[7] :
6
array[0] = 1
array[1] = 3
array[2] = 4
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 8
array[7] = 88
Aidan
  • 1
  • 1
  • 1
    You can use `lowest` without it being initialized. If it's not initialized then it will have an *indeterminate* value. And using indeterminate values in any way leads to *undefined behavior*. – Some programmer dude Oct 10 '22 at 04:14
  • 1
    Your learning material is probably out of date. You should not be using C-style arrays or `new` in C++. Prefer `std::vector` instead. You should also enable [compiler warnings](https://stackoverflow.com/questions/57842756) (though the particular error you have is not always detected this way) and use [sanitizers](https://godbolt.org/z/11PEsnh6j) routinely. – n. m. could be an AI Oct 10 '22 at 04:54
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Oct 10 '22 at 06:57

2 Answers2

0

In your sorting algorithm,

if (array[k] < array[i] && array[k] < array[lowest]) {
     cout << array[k] << " is less than " << array[i] << "\n";
     lowest = k;
            }

Changes to lowest index to k every time that k < i. This means that even if the array has smaller values in k, the last value in the list that happened to be smaller than i will be swapped with i. This is why your list is being answered back strangely.

To fix it, you need to have the smallest value of the list (or part being checked) change each iteration of the loop.

int *sortArray(int *array, int size) {  
    int lowest_index; //initialized variable(s)

    //bubble-sort algorithm from front to back, smallest to largest
    for (int i = 0; i < size-1; i++) {     //go through entire array (except the first since it is being compared as the "smallest" for the moment)
        int lowest_index = i; //initialize as something within the [part of the] array [being analyzed], they may all be equal.
        for (int j = i+1; j < size; j++) { //go through each remaining element of the array
        
            
            if (array[j] < array[lowest_index]) { //comparison to smallest found element
                cout << array[j] << " is less than " << array[i] << "\n";
                lowest_index /*for this loop*/ = j;
            }
            
        }
            //swap front item (i) with smallest found in previous loop
            int temp = array[lowest_index];
            array[lowest_index] = array[i]; 
            array[i] = temp;
    }
 
    return array;
}

So, the good news is that you're learning the language itself just fine; keep at it!

-1

I assume as selection sort algorithm.


/*Sorting program*/

using namespace std;

int *sortArray(int *array, int size) {

  for (int i = 0; i < size; i++) {
    int lowest = i;

    for (int k = i; k < size; k++) {
      if (array[k] < array[i] && array[k] < array[lowest]) {
        cout << array[k] << " is less than " << array[i] << endl;

        lowest = k;
      }
    }

    int temp = array[lowest];
    array[lowest] = array[i];
    array[i] = temp;
  }

  return array;
}

int main() {
  int low, high, target, size;

  cout << "Enter size of array : ";
  cin >> size;

  int *array = new int[size];

  for (int i = 0; i < size; i++) {
    cout << "Enter array[" << i << "] : " << endl;

    int entry;
    cin >> entry;

    array[i] = entry;
  }

  /*     */

  array = sortArray(array, size);

  for (int i = 0; i < size; i++) {
    cout << "array[" << i << "] = " << array[i] << endl;
  }

  return 1;
}

int lowest = i;

Basavaraj
  • 34
  • 1
  • 4