0
#include <iostream>
#include <cmath>
#include <limits.h>
#include <stdlib.h>

using namespace std ;

long long NumberHouses = 0 ;
long long ClosestHouse[2] = {0,0} ;
long long TravelTimes = 0 ;

void GetHouses(long long NumH, long long House_X[], long long House_Y[])
{ 
     for (long long a = 0 ; a < NumH ; a++)
     {
         cin >> House_X[a] ;
         cin >> House_Y[a] ;
     }
}

void SortArray(long long NumH, long long SortedArray[], long long HouseArray[])
{
     long long InsertPoint = 0 ; 

     for (int a = 0 ; a < NumH ; a++)
     {
         SortedArray[a] = LONG_MAX ;
     }

     for (int a = 0 ; a < NumH ; a++)
     {
         for (int b = 0 ; b < NumH ; b++)
         {
             if (HouseArray[a] < SortedArray[b]) 
             {
                 InsertPoint = b ; 

                 for (int c = NumH ; c > b ; c--)
                 {
                     SortedArray[c] = SortedArray[c-1] ;
                 }

                 SortedArray[InsertPoint] = HouseArray[a] ;
                 b = NumH ;
             }
         } 
     }
}                 

void FindMedians(long long NumH, int NumMeds, long long SortedArray[], long long MediansArray[])
{
     int MedianNum = 0 ;

     long long *Sorted_X = new long long [NumH] ;
     long long *Sorted_Y = new long long [NumH] ;

     MedianNum = NumH / 2 ;

     if (NumMeds == 1)
     {
         MediansArray[0] = SortedArray[MedianNum] ; 
     }
     else
     {
         MediansArray[0] = SortedArray[MedianNum] ;
         MediansArray[1] = SortedArray[MedianNum + 1] ;
     }
}        

void FindMidHouse(long long NumH, int NumMeds, long long Cords_X[], long long Cords_Y[], long long Meds_X[], long long Meds_Y[]) 
{
     long long *CloseHouses = new long long [NumH] ;

     long long NumPoints = 0 ;
     long long LowestMoves = LONG_MAX ;

     for (int a = 0 ; a < NumH ; a++)
     {
         if (abs(Cords_X[a] - Meds_X[0]) > abs(Cords_X[a] - Cords_Y[0]))
         {
             if (abs(Cords_X[a] - Meds_X[0] <= LowestMoves))
             {
                 LowestMoves = abs(Cords_X[a] - Meds_X[0]) ;
                 CloseHouses[a] = a ;
                 NumPoints++ ;
             }
         }
         else
         {
             if (abs(Cords_Y[a] - Meds_Y[0] <= LowestMoves))
             {
                 LowestMoves = abs(Cords_Y[a] - Meds_Y[0]) ;
                 CloseHouses[a] = a ;
                 NumPoints++ ;
             }
         }
     }

     long long *CloseHousesSums = new long long [NumH] ;

     for (int a = 0 ; a < NumPoints ; a++)
     {
         for (int b = 0 ; b < NumH ; b++)
         {
             if ((abs(Cords_X[CloseHouses[a]]) - Cords_X[b]) > (abs(Cords_Y[CloseHouses[a]]) - Cords_Y[b]))
             {
                 CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_X[CloseHouses[a]] - Cords_X[b]) ;
             }
             else
             {
                 CloseHousesSums[a] = CloseHousesSums[a] + abs(Cords_Y[CloseHouses[a]] - Cords_Y[b]) ;
             }
         }
     }

     for (int a = 0 ; a < (NumPoints - 1) ; a++)
     {
         if (CloseHousesSums[a] < CloseHousesSums[a+1])
         {
             ClosestHouse[0] = Cords_X[CloseHouses[a]] ;
             ClosestHouse[1] = Cords_Y[CloseHouses[a]] ;
         }
     }
}

void GetClosestHouse(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[]) 
{   
     int MedianAmt = 0 ; 

     long long *Sorted_X = new long long [NumH] ;
     long long *Sorted_Y = new long long [NumH] ;

    /* long long *Sorted_1 = new long long ;//[NumH] ;
     long long *Sorted_a = new long long ;//[NumH] ;
     long long *Sorted_b = new long long ;//[NumH] ;
     long long *Sorted_c = new long long ;//[NumH] ;
     long long *Sorted_d = new long long ;//[NumH] ;
     long long *Sorted_e = new long long ;//[NumH] ;
     long long *Sorted_f = new long long ;//[NumH] ;*/


     SortArray(NumberHouses, Sorted_X, House_X) ;
     SortArray(NumberHouses, Sorted_Y, House_Y) ;

     for (int a = 0 ; a < NumH ; a++)
     {
         cout << Sorted_X[a] << "," << Sorted_Y[a] << endl ;
     }

     if (NumH % 2 == 0)
     {
         MedianAmt = 1 ;
     }
     else
     {
         MedianAmt = 2 ;
     }
     cout << MedianAmt << endl ;
     cout << "sun" ;
     long long *Medians_X = new long long [MedianAmt] ;
     long long *Medians_Y = new long long [MedianAmt] ;
     cout << "shine" << endl ;
     FindMedians(NumberHouses, MedianAmt, Sorted_X, Medians_X) ;
     FindMedians(NumberHouses, MedianAmt, Sorted_Y, Medians_Y) ;

     FindMidHouse(NumberHouses, MedianAmt, House_X, House_Y, Medians_X, Medians_Y) ;

}   

void GetHouseDistances(long long NumH, long long House_X[], long long House_Y[], long long ClosestHouse[], long long& TravelTimes)
{ 
      long long Difference_X = 0 ;
      long long Difference_Y = 0 ; 

      for (int a = 0 ; a < NumH ; a++)
      {
          Difference_X = abs(ClosestHouse[0] - House_X[a]) ;     
          Difference_Y = abs(ClosestHouse[1] - House_Y[a]) ;

          //cout << Difference_X << " - Difference_X" << endl ;
          //cout << Difference_Y << " - Difference_Y" << endl ;

          if (Difference_X > Difference_Y)
          {
              TravelTimes = TravelTimes + Difference_X ;
          }
          else
          {
              TravelTimes = TravelTimes + Difference_Y ;
          }   
      }
      //cout << TravelTimes << endl ;
}                                 

int main()
{
     cin >> NumberHouses ;

     long long *House_X = new long long [NumberHouses] ; // x coordinate of houses
     long long *House_Y = new long long [NumberHouses] ; // y coordinate of houses

     GetHouses(NumberHouses, House_X, House_Y) ;
     GetClosestHouse(NumberHouses, House_X, House_Y, ClosestHouse) ;   
     GetHouseDistances(NumberHouses, House_X, House_Y, ClosestHouse, TravelTimes) ;

     cout << TravelTimes << endl ;
}

Now - you'll notice that I have a huge commented out section of code of other arrays being created similarly - my code works when I have those uncommented - but when I comment them it crashes after outputting 'sun' but before 'shine'.

I have tried using other arguments for the length of memory to create - eg MedianAmt, and this includes other variables and actual numbers. I have also tried using a long long variable as an argument as well.

I am not sure why it doesn't work - and if you would like for me to provide any more information about my code or situation I will do my best to answer them - thanks for the help.

edit: I run the executable and it gets to 'sun' and then it freezes - it can take a second or two before it goes unresponsive and closes. NuMH can be up to long long max amount - 64 bits I don't know where I freed any memory - I am a relatively novice programmer - and no error message was given. I was thinking it was a heap corruption problem as well - but why would adding in all of the extra long long arrays above that I have commented out cause it to not crash?

I can post the rest of my source code - but it's 200 lines long and a probably a tad messy - would this be beneficial?

edit 2: As for heap corruption I have my IDE and program saved on my flashdrive - but have tried to run it on two different computers - so I think it's safe to assume that the heap isn't corrupted. Maybe an invalid pointer to the memory is the problem, can the IDE affect where my pointer would go to choose the memory?

edit 3: I run windows 7 on both machines that I have tried running.

edit 4: Updated to have my full source code..

10
  • 2
    What happens when it "crashes"? Commented Mar 6, 2012 at 21:08
  • 1
    Where do you free that memory ? And why don't you just use vectors ? Commented Mar 6, 2012 at 21:10
  • 1
    What is SortArray and where is NumberHouses defined? Commented Mar 6, 2012 at 21:16
  • 1
    Incidentally, on an unrelated matter, if you include the standard header stdint.h (pubs.opengroup.org/onlinepubs/007904975/basedefs/stdint.h.html) you get types named things like int64_t, which saves having to use the messy and non-standard long long. Commented Mar 6, 2012 at 21:34
  • 1
    Heap allocation keeps some private info alongside allocated blocks. My guess is: this info gets corrupted by SortArray function which writes outside allocated arrays. The crash in new operator is just a result of earlier undetected error. Commented Mar 6, 2012 at 21:36

1 Answer 1

1

I think I've found at least one error in SortArray:

for (int c = NumH ; c > b ; c--)
{
  SortedArray[c] = SortedArray[c-1] ;
}

Assuming SortedArray is of size NumH, you're writing beyond the array boundary at the very first iteration. I haven't dived into your algorithm too much, but you might have meant int c = NumH - 1.

Sorry, can't promise it's the only one.

Sign up to request clarification or add additional context in comments.

Comments

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.