Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
December 2nd, 2010, 12:32 PM
 DLH112
Contributing User

Join Date: Feb 2010
Posts: 45
Time spent in forums: 13 h 13 m 7 sec
Reputation Power: 4
Infinite loop floating point exception

If I compile this with warnings turned up then it says the two for loops are statements with no effect. The goal of this is to keep calculating larger and larger prime numbers in a slow method until i press Ctrl+C, at which point it prints a report saying the largest prime found and how many numbers were checked.
This is C on Ubuntu using gcc.

Code:
```#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
int i=2;
int max=0;
int main(int ac, char *av[])
{
void f(int);
int j,k, prime;
j = k = 2;
prime = 1;

signal(SIGINT, f);
while(1)
{
j= 0;
k=0;
prime = 1;
if(i%2 == 0)
{
for(j;j<= i/2;j++)
{
if(i%j == 0)
prime = 0;
}
}
if(i%2 != 0)
{
for(k;k<= ((i/2)+1);k++)
{
if(i%k == 0)
prime = 0;
}
}
if(prime == 1)
max = i;

i++;
}
}

void f(int signum)
{   printf("numbers checked: %d\n maximum prime found: %d\n", i, max);
exit(1);

}```

Without warnings turned up theres a floating point exception at runtime. Did I not use a slow enough method? Why do the for loops supposedly have no effect?

#2
December 2nd, 2010, 12:55 PM
 RAJ_55555
Hats off to Mr. Joseph donahue

Join Date: Aug 2009
Posts: 752
Time spent in forums: 1 Week 6 Days 16 h 26 m 42 sec
Reputation Power: 1106
I didn't compile but I don't think that's what the compiler said. Only the first part of your for loop syntax has no effect.
Code:
`for(j;j<= i/2;j++)`

is the same as
Code:
`for(;j<= i/2;j++)`

The same goes for the next loop.
----------------EDIT-----------------
BTW its safer to reinstall the signal handler at the beginning of the handling function.
Code:
```void f(int signum)
{
signal(SIGINT, f);
printf("numbers checked: %d\n maximum prime found: %d\n", i, max);
exit(1);
}```

and also check signal's return for SIG_ERR for error.

Last edited by RAJ_55555 : December 2nd, 2010 at 01:05 PM.

#3
December 2nd, 2010, 01:25 PM
 DLH112
Contributing User

Join Date: Feb 2010
Posts: 45
Time spent in forums: 13 h 13 m 7 sec
Reputation Power: 4
using Werror Wall and pedantic now yields no errors. so thanks for that :]. But I still get the floating point exception at runtime. Maybe i can use usleep to slow down the process? I can't think of a way to not use ints for the whole thing. Usleep is really the only thing i can think of, unless there's a slower method to finding prime numbers.
The description of what to do doesn't seem to suggest that I should have to slow it down manually though.
"Interruptions are not always destructive. (Office work analogy)... For example, write a program that finds prime numbers using a low method. The program should keep track of the largest prime number it has found so far. Add to this program a handler function for SIGINTthat prints out a brief report showing how many numbers it has checked and the largest prime it has found."

In any case the code is now this
Code:
```#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
int i=2;
int max=0;
int main(int ac, char *av[])
{
void f(int);
int j,k, prime;
j = k = 2;
prime = 1;

if(signal(SIGINT, f) == SIG_ERR)
exit(1);

while(1)
{
prime = 1;
if(i%2 == 0)
{
for(j=0;j<= i/2;j++)
{
if(i%j == 0)
prime = 0;
}
}
if(i%2 != 0)
{
for(k=0;k<= ((i/2)+1);k++)
{
if(i%k == 0)
prime = 0;
}
}
if(prime == 1)
max = i;

i++;
}
}

void f(int signum)
{
signal(SIGINT, f);
printf("numbers checked: %d\n maximum prime found: %d\n", i, max);
exit(1);
}```

#4
December 2nd, 2010, 01:54 PM
 RAJ_55555
Hats off to Mr. Joseph donahue

Join Date: Aug 2009
Posts: 752
Time spent in forums: 1 Week 6 Days 16 h 26 m 42 sec
Reputation Power: 1106
the error is divide by zero in these lines
Code:
```if(i%j == 0)
//and
if(i%k==0)```

since j and k are set to zero in the for loop. hope you can sort it out now.

Last edited by RAJ_55555 : December 2nd, 2010 at 01:58 PM.

#5
December 2nd, 2010, 02:17 PM
 DLH112
Contributing User

Join Date: Feb 2010
Posts: 45
Time spent in forums: 13 h 13 m 7 sec
Reputation Power: 4
>_< I avoided division by zero somewhere else, I don't know why that got by me. It runs now :], but my logic is still slightly off(always says the max prime is zero). I'll take a detailed look through it in a while, I should be done with this now :].

another quick question though. how can I use scanf to read 1 character. getchar() is a problem because it reads from buffered input, which causes a glitch.

#6
December 2nd, 2010, 02:44 PM
 RAJ_55555
Hats off to Mr. Joseph donahue

Join Date: Aug 2009
Posts: 752
Time spent in forums: 1 Week 6 Days 16 h 26 m 42 sec
Reputation Power: 1106
Code:
`scanf("%c",&character);`

But I don't think this will solve whatever glitch you are experiencing by using getchar. What sort of problems are you having anyway?

#7
December 2nd, 2010, 03:14 PM
 DLH112
Contributing User

Join Date: Feb 2010
Posts: 45
Time spent in forums: 13 h 13 m 7 sec
Reputation Power: 4
When I press Ctrl+C it's supposed to stop the loop(which is printing hello once per second) and ask "ok to quit(y/n)". which works every other time.
the first time it stops the loop and asks, and i say n
the second time i press ctrl+c it asks, but the loop doesn't stop
third time is like the first
fourth is like the second
... etc

#8
December 2nd, 2010, 03:17 PM
 DLH112
Contributing User

Join Date: Feb 2010
Posts: 45
Time spent in forums: 13 h 13 m 7 sec
Reputation Power: 4
ok so, I was told that was the reason, and using scanf would probably fix it. but I still get the same problem.
Code:
```#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>

int main(int ac, char *av[])
{
void f(int);
int i;

signal(SIGINT, f);
for(i=0; i<8; i++){
printf("hello\n");
sleep(1);
}
}

void f(int signum)
{
char a = 'a';
printf("Interrupted!");
printf("OK to quit?(y/n)");
scanf("%c", &a);
if(a == 'y' || a == 'Y')
exit(1);
else
if(a != 'y' || a != 'Y')
{
a = 'a';
return;
}
return;

}```

This behavior makes little sense to me. The only thing I can think of is that theres some variable i don't know about/can't identify that is being set back and forth between 2 values.

#9
December 2nd, 2010, 04:48 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,968
Time spent in forums: 1 Month 4 Days 3 h 59 m 12 sec
Reputation Power: 1801
Quote:
 Originally Posted by DLH112 the first time it stops the loop and asks, and i say n the second time i press ctrl+c it asks, but the loop doesn't stop third time is like the first fourth is like the second... etc

Think about it, you are not entering 'n' you are entering 'n' + <newline>, the first scanf( "%c", &a ) gets 'n', the next time it is called, it gets the <newline> already in the input buffer. Console input is normally line-buffered, so you need to write the code to get the first character of an entire line of any length.

Code:
```a = getchar() ;
while( a != '\n' && getchar() != '\n' ){ /* do nothing*/}
```

#10
December 2nd, 2010, 08:42 PM
 RAJ_55555
Hats off to Mr. Joseph donahue

Join Date: Aug 2009
Posts: 752
Time spent in forums: 1 Week 6 Days 16 h 26 m 42 sec
Reputation Power: 1106
Although Clifford's solution is much more elegant, you may also try:
Code:
`scanf(" %c",&character);//notice the blank space before %c which deals with the extraneous '\n'`

Add the necessary error checking though, otherwise it's an accident waiting to happen.

#11
December 3rd, 2010, 04:37 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,968
Time spent in forums: 1 Month 4 Days 3 h 59 m 12 sec
Reputation Power: 1801
Quote:
 Originally Posted by RAJ_55555 Code: `scanf(" %c",&character);//notice the blank space before %c which deals with the extraneous '\n'`
Until the user enters more than just n<newline> that is. It is easy to miskey the enter key and end up with n#<newline> (on a UK keyboard, where # is adjacent to <enter> - YMMV). Or a user may just idly enter junk out of boredom or deliberate intent to break the program. Dealing with 'normal' input is not enough.

#12
December 3rd, 2010, 09:18 AM
 RAJ_55555
Hats off to Mr. Joseph donahue

Join Date: Aug 2009
Posts: 752
Time spent in forums: 1 Week 6 Days 16 h 26 m 42 sec
Reputation Power: 1106
Quote:
 Originally Posted by clifford Until the user enters more than just n that is. It is easy to miskey the enter key and end up with n# (on a UK keyboard, where # is adjacent to - YMMV). Or a user may just idly enter junk out of boredom or deliberate intent to break the program. Dealing with 'normal' input is not enough.
I didn't know this, could you please explain a little more? what is n#<newline>?

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Infinite loop floating point exception

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |