Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7626125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:00+00:00 2026-05-31T05:17:00+00:00

#include <iostream> #include <cmath> #include <limits.h> #include <stdlib.h> using namespace std ; long long

  • 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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:17:01+00:00Added an answer on May 31, 2026 at 5:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <cmath> using namespace std; void input(int(&x)[10]); int copy(int (&x)[10], int(&y)[10]); void
#include <iostream> #include <cmath> #include <string> using namespace std; string STRING; bool isEqual(double a,
//Testing numbers for primality #include <iostream> #include <cmath> using namespace std; int main() {
#include <iostream> #include <cmath> using namespace std; float f (int a, int b) {
The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace
#include <iostream> using namespace std; // This first class contains a vector and a
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.