Solved

Floating Point Error: Stack Fault revisited...

Posted on 1998-03-14
13
232 Views
Last Modified: 2010-04-10
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
 
0
Comment
Question by:matlurz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1183451
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183452
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
 
LVL 22

Expert Comment

by:nietod
ID: 1183453
Another problem you sort foor loop goes to j<=MAX should be just j<MAX.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1183454
I'm halucinating.  Ignore that last comment.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183455
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
 

Author Comment

by:matlurz
ID: 1183456
Yes, I've tested that portion of the code & it works just fine!
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183457
what 's your problem then?
0
 

Author Comment

by:matlurz
ID: 1183458
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
 
LVL 84

Expert Comment

by:ozo
ID: 1183459
if (subset[i].ID > subset[j].ID)

the way you constructed subset, I don't think this will ever be true.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183460
Yes, matlurz .  It would be great if you can email you code to
q2data@yahoo.com
0
 

Expert Comment

by:jann
ID: 1183461
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
 
LVL 3

Accepted Solution

by:
q2guo earned 300 total points
ID: 1183462
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
 

Author Comment

by:matlurz
ID: 1183463
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

687 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