Floating Point Error: Stack Fault revisited...

For some reason my question was yanked and so I did not receive some of your answers.  To reiterate....I have a struct of balloons having an ID(int), day(int) and one float(distance).
...I need to

1.Read values corresponding to these into an array of balloons.  This I have!
2.Sort the array ascendingly and print it...I think that I have this part correct also...
3.Find the maximum distance travelled by a balloon for each day and print the ID, day, and distance for each of five days.
3.Find the average distance travelled by the balloons released on the ist and fifth days combined and print the average.

It sounds simple enough, but for some reason I have just had way too many problems.  So here is the code....assuming the struct is declared etc...

int main()
{
      balloon records[MAX];
      balloon subset[MAX];

      int day = 1, len = 0;

      get_data(records);
      sort_data(records);
      print_sorted(records);

      for(day = 1; day = 5; day++)
      {
      make_subset_array(subset, records, day, len);
      find_max_dist(subset, len);
      }

      clean_array(subset, len);
      find_avg_dist(records);

      return 0;
}

void get_data(balloon records[])
{
int count;

fstream infile;
infile.open("input.dat", ios::in);
  if (infile.fail())
  {
    cout << "I/O Error: Unable to open the file for
    input."   << endl;
  }
  else
  {
    for (count = 0; count < 15; count++)
  {
  infile >> records[count].ID >> records[count].day >>      records[count].distance;
  }
  infile.close();
  }
}


void sort_data(balloon records[])
{
balloon temp;

  for(int i = 1; i < MAX; ++i)
  {
    for (int j = 0; j <= i; ++j)
    if (records[i].ID < records[j].ID)
    {
     temp.ID  = records[i].ID;
     temp.day =      records[i].day;
     temp.distance  = records[i].distance;

     records[j].ID  = records[i].ID;
     records[j].day = records[i].day;
     records[j].distance  = records[i].distance;

     records[i].ID = temp.ID;
     records[i].day = temp.day;
     records[i].distance  = temp.distance;
     }
  }
}


void print_sorted(balloon records[])
{
int count = 0;

for (count = 0; count < 15; count++)
cout << records[count].ID << " " <<  records[count].day << " " << records[count].distance << endl;
}



void  make_subset_array(balloon subset[], balloon records  [], int day, int &len)
{
  int i = 0, j = 0, k =0;

  len = 0;

   for (i = 0; i < 15; i++)
   {
     if(records[j].day == day)
     {
      subset[k].ID = records[j].day;
      subset[k].day = records[j].day;
        subset[k].distance = records[j].distance;

      ++k;
      len = k;
   }
   ++j;
  }

}

void find_max_dist(balloon subset[], int &len)
{
 balloon temp;

 for(int i = 1; i < len; ++i)
 {
   for (int j = 0; j <= i; ++j)
   if (subset[i].ID < subset[j].ID)
   {
    temp.ID = subset[i].ID;
    temp.day = subset[i].day;
    temp.distance = subset[i].distance;

    subset[j].ID = subset[i].ID;
    subset[j].day = subset[i].day;
    subset[j].distance = subset[i].distance;

    subset[i].ID = temp.ID;
    subset[i].day = temp.day;
    subset[i].distance = temp.distance;
   }
 }
}

void find_avg_dist(balloon subset[])
{
  float avg = 0, sum = 0;

  int i = 0, counter = 0;

  for(i = 0; i < 15; i++)
  if((subset[i].day == 1) || (subset[i].day == 5))
  {
    ++counter;
    sum += subset[i].distance;
  }
  avg = sum/counter;
cout << "The average distance travelled on day 1 and day 5 is " << avg << endl;
}

void clean_array(balloon subset[], int len)
{
  int i = 0;

  for(i = 0; i < len; i++)
  {
    subset[i].ID = 0;
    subset[i].day = 0;
    subset[i].distance = 0;
  }
}

What am I doing wrong??? Please help and my apologies to those of you whose anwers I was unable to get to before...m
 
matlurzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
My compiler warns:
"matlurz.c", line 23: warning(3271): possible use of "=" where "==" was intended
     for(day = 1; day = 5; day++)
                  ^

But there seems to be another problem in sort_data
  temp.ID  = records[i].ID;
...
  records[j].ID  = records[i].ID;
...
  records[i].ID = temp.ID;  
just replaces everything with records[i].ID, which may not be what you want.

BTW, this bug would have been easier to spot if you had just said
  temp = records[i];
  records[j] = records[i];
  records[i] = temp;
instead of cluttering the routine with individual element assignments.
0
nietodCommented:
Although this probably isn't the problem, it is a potatential for a problem.  If you don't have any information for day 1 or day 5 you will get a divide by zero exception and the program will crash.  The program should check that it isn't about to divide by zero and, if so, it should skip the calculation.
0
nietodCommented:
Another problem you sort foor loop goes to j<=MAX should be just j<MAX.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

nietodCommented:
I'm halucinating.  Ignore that last comment.
0
q2guoCommented:
Can you make sure that you are reading in the correct data
for the file?  

Here is the code again
----------------------
int main()
{
    balloon records[MAX];
    balloon subset[MAX];

    int day = 1, len = 0;

    get_data(records);
    sort_data(records);
    print_sorted(records);

    for(day = 1; day = 5; day++)
    {
          make_subset_array(subset, records, day, len);
          find_max_dist(subset, len);
    }

    clean_array(subset, len);
    find_avg_dist(records);

    return 0;
}


void get_data(balloon records[])
{
    int count;

    fstream infile;
    infile.open("input.dat", ios::in);
    if (infile.fail())
    {
        cout << "I/O Error: Unable to open the file for input."   << endl;
    }
    else
    {
        for (count = 0; count < 15; count++)
        {
           infile >> records[count].ID >> records[count].day >> records[count].distance;
        }
        infile.close();
    }
}


void sort_data(balloon records[])
{
    balloon temp;

    for(int i = MAX; i > 0; i--)
    {
        for (int j = 0; j <= i; ++j)  {
          if (records[j+1].ID < records[j].ID)
          {
            temp.ID  = records[j].ID;
            temp.day = records[j].day;
            temp.distance  = records[j].distance;

            records[j].ID  = records[j+1].ID;
            records[j].day = records[j+1].day;
            records[j].distance  = records[j+1].distance;
 
            records[j+1].ID = temp.ID;
            records[j+1].day = temp.day;
            records[j+1].distance  = temp.distance;
          }
        }
    }
}


void print_sorted(balloon records[])
{
    int count = 0;

    for (count = 0; count < 15; count++)
       cout << records[count].ID << " " <<  records[count].day << " " << records[count].distance << endl;
}



void  make_subset_array(balloon subset[], balloon records  [], int day, int &len)
{
     int i = 0, j = 0, k =0;

     len = 0;

     for (i = 0; i < 15; i++)
     {
         if(records[j].day == day)
         {
            subset[k].ID = records[j].day;
            subset[k].day = records[j].day;
            subset[k].distance = records[j].distance;

            ++k;
            len = k;
         }
         ++j;
      }

}

void find_max_dist(balloon subset[], int &len)
{
    balloon temp;

    for(int i = 1; i < len; ++i)
    {
       for (int j = 0; j <= i; ++j)
       if (subset[i].ID < subset[j].ID)
       {
           temp.ID = subset[i].ID;
           temp.day = subset[i].day;
           temp.distance = subset[i].distance;

           subset[j].ID = subset[i].ID;
           subset[j].day = subset[i].day;
           subset[j].distance = subset[i].distance;

           subset[i].ID = temp.ID;
           subset[i].day = temp.day;
           subset[i].distance = temp.distance;
       }
     }
}


void find_avg_dist(balloon subset[])
{
    float avg = 0, sum = 0;

    int i = 0, counter = 0;

    for(i = 0; i < 15; i++)  {
        if((subset[i].day == 1) || (subset[i].day == 5))
        {
           ++counter;
           sum += subset[i].distance;
        }
        avg = sum/counter;
        cout << "The average distance travelled on day 1 and day 5 is " << avg << endl;
    }
}


void clean_array(balloon subset[], int len)
{
      int i = 0;

      for(i = 0; i < len; i++)
      {
        subset[i].ID = 0;
        subset[i].day = 0;
        subset[i].distance = 0;
      }
}
0
matlurzAuthor Commented:
Yes, I've tested that portion of the code & it works just fine!
0
q2guoCommented:
what 's your problem then?
0
matlurzAuthor Commented:
Well, at this point the only issue that I have not resolved is in the find_max distance function. There are five passes through a loop in main, {First a smaller is created according to the day, and then the find_max_dist function is called).  For some reason??? It does not work on the 3rd and fifth passes? Here is an updated version of the code...

int main()
{
      balloon records[MAX];
      balloon subset[MAX];

      int day = 1, len = 0;

      proceed();

      get_data(records);
   sort_data(records);
      print_sorted(records);

      for(day = 1; day <= 5; day++)
      {
            make_subset_array(subset, records, day, len);
            find_max_dist(subset, day, len);
      }

      //clean_array(subset, len);
      find_avg_dist(records);

      return 0;
}


void get_data(balloon records[])
      {
            int count;

            fstream infile;

            infile.open("input.dat", ios::in);

            if (infile.fail())
            {
                  cout << "I/O Error: Unable to open the file for input." << endl;
            }
            else
            {
                  for (count = 0; count < 15; count++)
                  {
                        infile >> records[count].ID >> records[count].day >> records[count].distance;
                  }
                  infile.close();
            }
      }


      void sort_data(balloon records[])
      {
            balloon temp;

            for(int i = 1; i < 15; ++i)
            {
                  for (int j = 0; j <= i; ++j)
                        if (records[i].ID < records[j].ID)
                        {
                              temp.ID                               =            records[j].ID;
                              temp.day                              =            records[j].day;
                              temp.distance              =            records[j].distance;

                              records[j].ID                  =     records[i].ID;
                              records[j].day             =            records[i].day;
                              records[j].distance  =            records[i].distance;

                              records[i].ID                  =            temp.ID;
                              records[i].day                  =            temp.day;
                              records[i].distance  =            temp.distance;
                        }
            }

      }


      void print_sorted(balloon records[])
      {
            int count = 0;

            for (count = 0; count < 15; count++)
                  cout << records[count].ID << " " <<  records[count].day << " " << records[count].distance << endl;
      }



      void  make_subset_array(balloon subset[], balloon records[], int day, int &len)
      {
            int i = 0, j = 0, k =0;

            len = 0;

            for (i = 0; i < 15; i++)
            {
                  if(records[j].day == day)
                  {
                        subset[k].ID                  =            records[j].day;
                        subset[k].day                  =            records[j].day;
                        subset[k].distance   =            records[j].distance;

                        ++k;
                        len = k;
                  }
                  ++j;
            }
      }

   //      Possible error in this looping

      void find_max_dist(balloon subset[], int day, const int len)
      {
            balloon temp;

            for(int i = 1; i < len; ++i)
            {
                  for (int j = 0; j <= i; ++j)
                        if (subset[i].ID > subset[j].ID)
                        {
                              temp.ID                               =            subset[j].ID;
                              temp.day                              =            subset[j].day;
                              temp.distance              =            subset[j].distance;

                              subset[j].ID                  =     subset[i].ID;
                              subset[j].day                   =            subset[i].day;
                              subset[j].distance        =            subset[i].distance;

                              subset[i].ID                  =            temp.ID;
                              subset[i].day                  =            temp.day;
                              subset[i].distance        =            temp.distance;
                        }
            }
            cout << "The maximum distance travelled on day " << day << "was " << subset[len - 1].distance << endl;
      }

      void find_avg_dist(balloon subset[])
      {
            float avg = 0, sum = 0;

            int i = 0, counter = 0;

            for(i = 0; i < 15; i++)
                  if((subset[i].day == 1) || (subset[i].day == 5))
                  {
                        ++counter;
                        sum += subset[i].distance;
                  }
      if(counter != 0)
      {
                  avg = sum/counter;
                  cout << "The average distance travelled on day 1 and day 5 is " << avg << endl;
      }
      else
            cout << "Error:No Data" << endl;
      }

      void clean_array(balloon subset[], int len)
      {
            int i = 0;

            for(i = 0; i < len; i++)
            {
                  subset[i].ID                  =            0;
                  subset[i].day                  =            0;
                  subset[i].distance   =            0;
            }
      }
...or if anyonw would rather I will email the code!
0
ozoCommented:
if (subset[i].ID > subset[j].ID)

the way you constructed subset, I don't think this will ever be true.
0
q2guoCommented:
Yes, matlurz .  It would be great if you can email you code to
q2data@yahoo.com
0
jannCommented:
When you implement make_subset_array, why you change ID to day, e.g. subset[k].ID = records[j].day?  Then, when you implement "find_max_dist", why you want to compare subset[i].ID with subset[j].ID instead of directly comparing subset[i].distance with subset[j].distance?
0
q2guoCommented:
Hi matlurz, I fixed up the make_subset_array function and
the find_max_dist function.  They are shown below
------------------------------------------------------------

void  make_subset_array(balloon subset[], balloon records[], int day, int &len)
{
       int i = 0, j = 0, k =0;

       len = 0;

       for (i = 0; i < 15; i++)
       {
          if(records[i].day == day)
          {
             subset[k].ID = records[i].ID;
             subset[k].day = records[i].day;
             subset[k].distance   = records[i].distance;

             ++k;
             ++len;
           }  
       }
}


void find_max_dist(balloon subset[], int day, const int len)
{
       balloon temp;
       temp.distance = 0;

       for(int i = 0; i < len; ++i)
       {
           if (temp.distance < subset[i].distance)
           {
               temp.ID = subset[i].ID;
               temp.day = subset[i].day;
               temp.distance   = subset[i].distance;
            }
     }
     cout << "The maximum distance travelled on day " << day << "was " << temp.distance
       << endl;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matlurzAuthor Commented:
Thanks a lot...the code works as needed, I just need to go back over it & see what I was doing wrong so that I don't continue to make the same logic errors!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.