Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

This is a sorting program that outputs in ascending order.

This is a sorting program that outputs in ascending order. It works perfectly except the last input is outputted as 0, please help .

#include <iostream>

#include <fstream>

using namespace std;

void Swapper(double numbers[],int index)

{

  int swapHolder;

  if (numbers[index] > numbers[index+1])

  {

    swapHolder= numbers[index+1];                //how to swap the two numbers

    numbers[index+1]=numbers[index];

    numbers[index]=swapHolder;

  }

}

int main()

{

  ifstream infile;

  ofstream outfile;

  infile.open("input.txt");                      //general input file setup

  outfile.open("output.txt");

  int thenum=100;

  double numbers[thenum];

  int count;

  int numofnum;

  int swapHolder= -1;

  count = 0;

  while (!infile.eof()){

    infile >> numbers[count];                    //getting data from file stored into the array

    count ++;

  }

  numofnum= count;

  int ending= numofnum;

  int length= numofnum;

  for (int counter = length -1; counter > 0; counter--)         //repeating loop so that it does not stop after one swoop but goes back and run the swoop verification again

  {

    for ( int index = 0; index < ending -1; index ++)           //repeating loop until the entered array number reaches the final swap

    {

      Swapper(numbers,index);

    }

    ending--;

                              //as the highest number is now at the end there is no need to compare until the last number anymore, just go one less

  }

  for (int index=0; index < numofnum; index ++)              //displaying output of each stored array place

  {

    cout<< numbers[index] <<endl;

  }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question