jahir anderson
jahir anderson

Reputation: 17

Bubble sort is not sorting or outputting array values properly

I have a program that accepts a minimum of three scores, and a maximum of thirty scores and stores them in an array. I am trying to use a bubble sort in a function that sorts the values inputted by the user in ascending order. The sort is not outputting the values in ascending order, which is something i've been trying to fix for the past hour. The code portion i'm trying to fix is located in "sortAscending."

#include <iostream>
#include <iomanip>

using namespace std;

void programInfo();
void inputList(double arr[], int &count);
void printList(double arr[], int n, int &count);
void sortAscending(double arr[], int n, int &count);

int main()
{
    double endProgram = 0;
    const int size = 30;
    double arr[size];
    int count = 0;
    int n = 0;
    while(endProgram != -1 || endProgram != -1.0)
    {
        programInfo();
        inputList(arr, count);
        printList(arr, n, count);
        sortAscending(arr, n, count);
        cout << "\n";
        cout << "Run program again? Enter -1 or -1.0 to end program."<< endl;
        cin >> endProgram;
            if (endProgram == -1 || endProgram == -1.0)
            {
                cout << "Thank you for using my program." << endl;
            }
    }
}

void programInfo()
{
     cout << "Statistical Calculator." << endl;
     cout << "Please follow instructions carefully." << endl;
     cout << "Enter one value at a time up to 30 values." << endl;
     cout << "You must enter valid data or program will not work." << endl;
     cout << "Enter -1 to signal end of data (-1 or -1.0)" << endl;
}

void inputList(double arr[], int &count)
{
    count = 0;
    char answer;
    cout <<"Input at least three values minimum, thirty values maximum." << endl;
    while (count < 30)
    {
        cout <<"Please enter a value." << endl;
        cin >> arr[count];
        count++;
        if (count == 3)
        {
            cout << "You have entered the minimum amount of values necessary." << endl; 
            cout << "Do you want to stop inputting values? (y/n)" << endl;
            cin >> answer;
            if (answer == 'y')
            {
                break;
            }
        }
    }
}

void printList(double arr[], int n, int &count)
{

    cout <<"Here is the list of values entered:" << endl;
    for (n = 0; n < count; n++)
        {
            cout << setw(8) << arr[n];
        }
}

void sortAscending(double arr[], int n, int &count)
{
    double temp;
    for (n = 0; n < count; n++)
    {
        for (int i = 0; i < count - 1; i++)
        {
            if(arr[i] < arr[n])
               {
                   temp = arr[n];
                   arr[n] = arr[i];
                   arr[i] = temp;
               }
            cout << arr[n] << endl;
        }
    }
}

Upvotes: 0

Views: 171

Answers (2)

dserrano3
dserrano3

Reputation: 56

Hi your problem is that in bubble sort you should not compare n with i, that is insertion sort, you have to compare i with i+1, thats why the condition in the loop is i < count-1.

Also as sugested you have to print the array outside the double loop, with another loop.

Hope that helps.

for (int n = 0; n < count; n++)
{
  for (int i = 0; i < count - 1; i++)
  {
    if(arr[i] < arr[i+1])
    {
      temp = arr[i];
      arr[i] = arr[i+1];
      arr[i+1] = temp;
    }

  }
}
for(int i = 0; i < count; i ++){
  cout<<arr[i]<<" ";
}

Upvotes: 1

pinkboi
pinkboi

Reputation: 259

It looks like you're outputting the values while you're in the middle of sorting instead of after. I would remove the cout from there, and iterate through the array afterwards to print after they're sorted.

Upvotes: 0

Related Questions