Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why this program fails for larger input value ?

Reply
Thread Tools

Why this program fails for larger input value ?

 
 
pereges
Guest
Posts: n/a
 
      04-29-2008
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
unsigned long int numpoints;
printf("Enter num of points\n");
scanf("%lu", &numpoints);
unsigned long int numpointsx = sqrt((double)numpoints);
unsigned long int numpointsy = sqrt((double)numpoints);
numpoints = numpointsx * numpointsy;
unsigned long int i = 0;
typedef struct point
{
float x, y;
} point;
point *pointinarray;

pointinarray = calloc(sizeof(point), numpoints);
float xlength, ylength;
xlength = ylength = 20;
float xmin, ymin;
xmin = ymin = -10;
float xmax, ymax;
xmax = ymax = 10;
float xsize = xlength /( numpointsx-1);
float ysize = ylength / (numpointsy-1);

printf("%u\n", numpoints);
unsigned long int xi, yi;

/* I'm confident the error is here */
for ( yi = 0; yi <= numpointsy; ++yi)
{
for (xi = 0; xi <= numpointsx; ++xi)
{
pointinarray[i].x = xmin + xi * xsize;
pointinarray[i].y = ymin + yi * ysize;
++i;
}
}


printf("i: %lu xmax: %f ymax: %f\n",i, pointinarray[i-1].x,
pointinarray[i-1].y);
return 0;

}

The program is working for anything upto 60000 or so but fails for
anything greater eg. 100000
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      04-29-2008
pereges wrote:
[...]
> typedef struct point
> {
> float x, y;
> } point;
> point *pointinarray;
>
> pointinarray = calloc(sizeof(point), numpoints);

[...]
> The program is working for anything upto 60000 or so but fails for
> anything greater eg. 100000


"Fails", how?

Pure speculation: the calloc(), for which you do not check for
failure, is failing on large values of "numpoints".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      04-29-2008
pereges wrote:

> #include <stdio.h>
> #include <math.h>
> #include <stdlib.h>
>
> int main(void)
> {
> unsigned long int numpoints;
> printf("Enter num of points\n");
> scanf("%lu", &numpoints);
> unsigned long int numpointsx = sqrt((double)numpoints);
> unsigned long int numpointsy = sqrt((double)numpoints);
> numpoints = numpointsx * numpointsy;
> unsigned long int i = 0;
> typedef struct point
> {
> float x, y;
> } point;
> point *pointinarray;
>
> pointinarray = calloc(sizeof(point), numpoints);
> float xlength, ylength;
> xlength = ylength = 20;
> float xmin, ymin;
> xmin = ymin = -10;
> float xmax, ymax;
> xmax = ymax = 10;
> float xsize = xlength /( numpointsx-1);
> float ysize = ylength / (numpointsy-1);
>
> printf("%u\n", numpoints);


You want the %lu conversion specifier here.

> unsigned long int xi, yi;
>
> /* I'm confident the error is here */
> for ( yi = 0; yi <= numpointsy; ++yi)
> {
> for (xi = 0; xi <= numpointsx; ++xi)
> {
> pointinarray[i].x = xmin + xi * xsize;
> pointinarray[i].y = ymin + yi * ysize;
> ++i;
> }
> }
>
>
> printf("i: %lu xmax: %f ymax: %f\n",i, pointinarray[i-1].x,
> pointinarray[i-1].y);
> return 0;
>
> }
>
> The program is working for anything upto 60000 or so but fails for
> anything greater eg. 100000


Why don't you put in a series of printf statements to print out the
values of your variables at selected points in the program. Then you
might be able to narrow down the point of failure. Alternatively you
could step through the program in a debugger.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why this program fails for larger input value ? pereges C Programming 1 04-29-2008 07:38 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Pass input Array value to function to calc different input value Susan Cranford Javascript 2 07-05-2005 02:53 AM
Question: Can I make textarea larger (larger text)? Peter Williams HTML 1 06-03-2005 08:17 AM
20" LCD: setting 4 g Larger text in all Programs? and larger icons lbbss Computer Support 6 04-14-2005 05:46 PM



Advertisments