0

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;
        }
    }
}
2
  • 1
    Help me out: when you used the debugger, which lines are problematic? Commented Nov 12, 2014 at 0:27
  • nothing seemed to go wrong in the debugger. this seems like a logical error rather than a syntax/run time error. Commented Nov 12, 2014 at 0:32

2 Answers 2

1

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]<<" ";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

i've tried moving the cout statement around, but that doesn't seem to work either.
I'm not good at sorts but something looks off about your bubble sort. mathbits.com/MathBits/CompSci/Arrays/Bubble.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.