floating point invalid operation

hello

i wrote the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

void Swap(double &a, double &b)
{
 double z;
 z=a;
 a=b;
 b=z;
} 


void FindMO(double B[][150], double AM[])  
{
 double temp[150];
 double sum;
 for(int i=0; i<150; i++)  
 {
  sum=0;
  for(int j=0; j<6; j++)
  {
   sum+=B[j][i];
  }
   temp[i]=sum/6.;
 } 
  for (int i=0; i<150; i++)  
   for(int j=i+1; j<150; j++)  
    if(temp[j]>temp[i])    // FAULT
    {
     Swap(temp[j],temp[i]);
     Swap(AM[j],AM[i]);
    }  

   cout.width(5);
   cout<<"AEM"<<cout.width(5)<<"MO"<<endl;
   for(int i=0; i<150; i++) 
   {
    cout.width(5);
    cout<<AM[i];
    cout.width(5);
    cout<<temp[i]<<endl;
   }

The AM array has a code that represents each student
and Temp array has the average value of 6 lessons for each student

Swap function swaps 2 double variables
FindMO function finds the average value of each collumn of array B and uses a temporary array (temp) to save the average values of each collumn. Then it sorts in decreasing order the temp and AM arrays and prints them.

When i try to run the code it comes up with the following message: fault floating point invalid operation. and it prompts me to line 26.

What am I doing wrong?
Last edited on
I believe it is because your argument arrays are not entirely filled with proper floating point values.

Trying to compare, for example, 1.7 to NAN (Not A Number) will produce a fault. Make sure you completely initialize the arrays you pass as argument to FindMO() to a valid number, like 0.0 or -1.0 or etc.

If that isn't the problem, you'll have to post a little more code.

Hope this helps.
That must be it. The program is supposed to read the values from a .txt file which has no more than 150 (lines). but the sum of the values is unknown.
I wrote to a text just 6 lines to test my program but I save values to an array of 150 positions. the program puts "garbage" in the arrays after the 6th entry to the array. I'll check it and come back and post.
cheers
Last edited on
thats it. thanks again
:-)
Topic archived. No new replies allowed.