Solved

Sort Routine Fails to Sort Final Sequence in Array

Posted on 2011-03-15
4
335 Views
Last Modified: 2012-05-11
So far, so good... HOWEVER, I want this code to print the array in descending order, based on the second column... it does that, but leaves the last (highest) pair of numbers at the bottom of the list!  What can be done to fix this?
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>

using namespace std;
 
const int FINISH = 2;
const int START = 5;
 
void getData(int arr[][FINISH]);
void displayData(int arr[][FINISH]);
bool insertionSort(const vector<int>& inner1, const vector<int>& inner2); 

int i = 0;
int j = 0;
 
int main()
{
      int timeArray[START][FINISH];

      getData(timeArray);
      displayData(timeArray);

        vector<vector<int> > vect;

        for (int i = 0; i < START - 1; ++i) 
		{
                vector<int> inner(timeArray[i], timeArray[i] + 2);
                vect.push_back(inner);
        }
        
        sort(vect.begin(), vect.end(), insertionSort);

        for (int i = 0; i < START - 1; ++i) 
		{
                timeArray[i][0] = vect[i][0];
                timeArray[i][1] = vect[i][1];
        }

	  displayData(timeArray);
 
      cout << endl << endl << endl;

      system("PAUSE");
      return 0;
}

// used to prompt user to fill array with values
void getData(int arr[][FINISH])
{
      cout << "Enter start/finish times." << endl
        << "One at a time when prompted: " << endl;

         cout << endl;

   for(int i = 0; i <= START - 1; i++)
   {
	   cout << "Enter start time #" << i + 1 << ": ";
      cin >> arr[i][j];

	  for(int j = 0; j <= 0; j++)
		{
			cout << "Enter finish time #" << i + 1 << ": ";
		  cin >> arr[i][j + 1];
		}

      cout << endl;
   }
}// end getData()
 
// used to display array contents
void displayData(int arr[][FINISH])
{
	cout << endl << endl;
      for (int i = 0; i < START; i++)
      {
            for (int j = 0; j < FINISH; j++)
                  cout << arr[i][j] << " ";
            cout << endl;
      }
}// end displayData()
 
bool insertionSort(const vector<int>& inner1, const vector<int>& inner2) 
{
        return inner1[1] >= inner2[1];
}

Open in new window

I'm SO close...
0
Comment
Question by:Member_2_4213139
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35141007
I notice that some of your loops go from 0 to (START - 1), and others go from 0 to (START - 2).
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 35141035
I'm not too sure how you have this working, but shouldn't
for (int i = 0; i < START - 1; ++i)
be this?
for (int i = 0; i < START; ++i)

You are not copying the last item which is why it's still at the end.
You should notice it shows up twice.
0
 

Author Closing Comment

by:Member_2_4213139
ID: 35141204
I didn't notice ANY "START - 2" ... but changing the START -1 to START did it!  THANK YOU!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35141230
>> I didn't notice ANY "START - 2"

If you have a loop condition that says :

        i < START - 1

it means it'll go up to (START - 2) and no further.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now