We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Floating Point Error: Stack Fault revisited...

on
Medium Priority
274 Views
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

Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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.

Commented:
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.

Commented:
Another problem you sort foor loop goes to j<=MAX should be just j<MAX.

Commented:
I'm halucinating.  Ignore that last comment.

Commented:
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;
}
}

Commented:
Yes, I've tested that portion of the code & it works just fine!

Commented:

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!
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
if (subset[i].ID > subset[j].ID)

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

Commented:
Yes, matlurz .  It would be great if you can email you code to
q2data@yahoo.com

Commented:
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?
Commented:
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;
}

Not the solution you were looking for? Getting a personalized solution is easy.

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!
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile