Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Floating Point Error: Stack Fault revisited...

Posted on 1998-03-14
13
Medium Priority
?
235 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

604 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