R
Rahul
Hello Friends ~
We have the assignment to write efficient function to decide if an integer
is a square. An integer n is a square if n = m * m where m is an other
integer. Then write a test program to show whether the first ten integers
are squares.
I believe my code is correct and caches all results in an Array for
efficiency (only look up is needed). But it generates a Segmentation Fault
everytime I run it.
Can anyone see what the problem is?
~~ Thanks
#include"stdio.h"
#include"limits.h"
#include"stdbool.h"
static bool squares[INT_MAX]; // automatically initialized to 0, no
memset needed
setupsquares ()
{
int i;
for(i = 0; i < INT_MAX; squares[i * i++] = true);
}
bool
issquare (int i)
{
return squares;
}
printsquare (int i)
{
printf ("%d\t%s\n", i, issquare (i) ? "true" : "false");
}
main ()
{
int i;
setupsquares ();
for(i = 0; i <= 10; printsquare (i++));
}
We have the assignment to write efficient function to decide if an integer
is a square. An integer n is a square if n = m * m where m is an other
integer. Then write a test program to show whether the first ten integers
are squares.
I believe my code is correct and caches all results in an Array for
efficiency (only look up is needed). But it generates a Segmentation Fault
everytime I run it.
Can anyone see what the problem is?
~~ Thanks
#include"stdio.h"
#include"limits.h"
#include"stdbool.h"
static bool squares[INT_MAX]; // automatically initialized to 0, no
memset needed
setupsquares ()
{
int i;
for(i = 0; i < INT_MAX; squares[i * i++] = true);
}
bool
issquare (int i)
{
return squares;
}
printsquare (int i)
{
printf ("%d\t%s\n", i, issquare (i) ? "true" : "false");
}
main ()
{
int i;
setupsquares ();
for(i = 0; i <= 10; printsquare (i++));
}