473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Float Point Exception trouble

3
For Some reason when I compile the screen says "Floating point exception". Here is my code was wondering if anyone could lend me a hand.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char file[100];
  6. int total = 0;
  7.  
  8. struct student
  9. {
  10.     int SID;
  11.     int quiz[5];
  12. } students[40], highscore, lowscore, avg;
  13.  
  14. void Textfile()
  15. {
  16.     FILE* numbers;
  17.     int counter = 0;
  18.     numbers = fopen(file, "r");
  19.  
  20.     if (numbers != NULL)
  21.  
  22.     {
  23.         while (!feof(numbers))
  24.         {
  25.             fscanf (numbers, "%d %d %d %d %d %d", &students[counter].SID, &students[counter].quiz[0], &students[counter].quiz[1], &students[counter]. quiz[2], &students[counter]. quiz[3], &students[counter].quiz[4]);
  26.             counter++;
  27.         }
  28.  
  29.     total = counter - 1;
  30.     fclose(numbers);
  31.     }
  32.  
  33.     else
  34.     {
  35.         printf("The could not be opened");
  36.     }
  37. }    
  38.  
  39. int dataHighScore (int quiznum)
  40. {
  41.     int highscore = -1;
  42.     int x = 0;
  43.  
  44.     for (x=0; x<total; x++)
  45.     {
  46.         if (x == 0)
  47.         {
  48.             highscore = students[x].quiz[quiznum];
  49.         }
  50.  
  51.         else
  52.  
  53.         {
  54.                 if(highscore < students[x].quiz[quiznum])
  55.                 highscore = students[x].quiz[quiznum];
  56.             }    
  57.         }
  58.  
  59.         return highscore;
  60.     }
  61.  
  62.     int dataLowScore (int quiznum)
  63.     {
  64.         int lowscore = -1;
  65.         int x = 0;
  66.  
  67.         for(x=0;x < total;x++)
  68.         {
  69.             if(x==0)
  70.             {
  71.                 lowscore = students[x].quiz[quiznum];
  72.             }
  73.  
  74.             else
  75.             {
  76.                 if(lowscore > students[x].quiz[quiznum])
  77.                 lowscore = students[x].quiz[quiznum];
  78.             }
  79.         }
  80.  
  81.         return lowscore;
  82.     }
  83.  
  84.     int dataAvg (int quiznum)
  85.     {
  86.         int avg = 0;
  87.         int x = 0;
  88.         for (x=0;x < total; x++)
  89.         {
  90.             avg = avg + students[x].quiz[quiznum];
  91.         }
  92.  
  93.         avg = avg/total;
  94.         return avg;
  95.     }
  96.  
  97.     void print()
  98.     {
  99.         int x = 0;
  100.         printf("....................................Student Quiz Grades...................................\n");
  101.         printf("___________________________________________________________________________________________\n");
  102.         printf("Student                Quiz1            Quiz2            Quiz3            Quiz4            Quiz5\n");
  103.  
  104.         for(x=0; x<total;x++)
  105.         {
  106.             printf("%4d                %3d                %3d                %3d                %3d                %3d\n", students[x].SID, students[x].quiz[0], students[x].quiz[1], students[x].quiz[2], students[x].quiz[3], students[x].quiz[4]);
  107.         }
  108.  
  109.         printf("High Score            %3d                %3d                %3d                %3d                %3d\n", highscore.quiz[0], highscore.quiz[1], highscore.quiz[2], highscore.quiz[3], highscore.quiz[4]);
  110.         printf("Low Score            %3d                %3d                %3d                %3d                %3d\n", lowscore.quiz[0], lowscore.quiz[1], lowscore.quiz[2], lowscore.quiz[3], lowscore.quiz[4]);
  111.         printf("Avg Score            %3d                %3d                %3d                %3d                %3d\n", avg.quiz[0], avg.quiz[1], avg.quiz[2], avg.quiz[3], avg.quiz[4]);
  112.         printf("____________________________________________________________________________________________\n");
  113.  
  114.     }
  115.  
  116.     int main()
  117.     {
  118.         int x = 0;
  119.         highscore.SID = 9999;
  120.         lowscore.SID = 9998;
  121.         avg.SID = 9997;
  122.  
  123.         printf("Please enter the text file:\n");
  124.         scanf("%s", file); 
  125.  
  126.         Textfile();
  127.         for(x=0;x < 5; x++)
  128.         {
  129.             highscore.quiz[x] = dataHighScore(x);
  130.             lowscore.quiz[x] = dataLowScore(x);
  131.             avg.quiz[x] = dataAvg(x);
  132.         }
  133.         print();
  134.         return 0;
  135.     }
  136.  
May 10 '10 #1

✓ answered by Banfa

Interestingly you appear to be doing no floating point arithmetic so when you get this sort of thing you need to check for maths errors.

Typical errors are an accidental divide by 0 or calling a maths function with out of range values (trying to take the logarithm of a negative number for instance).

Your code has a divide so verify that you are not dividing by 0.


On a style point having global variables is very bad style and hinders maintenance and bug fixing. Both of your 2 global variables, file and total could easily be made local to main. Pass the variables to the functions that need them and have TextFile return a value for total. However I think this is just bad style rather than the cause of your problem.

1 2128
Banfa
9,065 Expert Mod 8TB
Interestingly you appear to be doing no floating point arithmetic so when you get this sort of thing you need to check for maths errors.

Typical errors are an accidental divide by 0 or calling a maths function with out of range values (trying to take the logarithm of a negative number for instance).

Your code has a divide so verify that you are not dividing by 0.


On a style point having global variables is very bad style and hinders maintenance and bug fixing. Both of your 2 global variables, file and total could easily be made local to main. Pass the variables to the functions that need them and have TextFile return a value for total. However I think this is just bad style rather than the cause of your problem.
May 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andrew Straw | last post by:
I'm running into trouble when calling a 3rd party library (the Intel IPP library to do some fast math on a P4 processor). This is on debian linux 2.6.7 using python 2.3.4. I've been using Pyrex...
1
by: riz | last post by:
We get the following error message. "a floating point exception occured in the user process. current transaction is cancelled". this message comes when trying to excute a stored procedure. This...
3
by: Marc Schellens | last post by:
I would liketo handle a floating point exception in my program. As I learned I can do a longjump from the signal handler, but: 1. Will local objects be cleaned up properly? 2. Lets say I...
2
by: chris.higginson | last post by:
Hi, I'm running SQL Server Version 8.00.194 on Windows 2000. I am am running this query: select TOP 2000 TheoVolImplied from OptionTrades where ReutersSymbol = 'IBM.N' and TheoVolImplied...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
2
by: Steven Jones | last post by:
Im just learning C an do not understand why the following code fails. The while loop apperas to work fine until it bombs out with a floating point exception. Whats going on? Thanks /* *...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
1
by: maidonical | last post by:
I am trying to run "select * from <table> where <columnname> = 0.0" in query analyzer <columnname> is a float I get the following error: Server: Msg 3628, Level 16, State 1, Line 1 A floating...
2
by: Martin Manns | last post by:
Hi, I am experiencing some trouble with gmpy v1.01. Multiplying an mpq with inf results in a floating point exception that exits python. Has this already been fixed in newer gmpy versions? ~...
6
by: Massa Batheli | last post by:
sql server 2000 sp4 build 2184 runs stats with maintenance plan sp_msforeachtable stored procedure updating full stats for each table and the results are identical A floating point...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.