Solved

Floating Point Error: Stack Fault revisited...

Posted on 1998-03-14
13
226 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
  • 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
 
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
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!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 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

18 Experts available now in Live!

Get 1:1 Help Now