473,406 Members | 2,273 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,406 software developers and data experts.

Why this program fails for larger input value ?

#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
Jun 27 '08 #1
2 1346
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: <mailto:Th*************@gmail.com>

Jun 27 '08 #2
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.

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Just starting out | last post by:
I am very new to C code and I'm having a lot of trouble with a homework assignment. This program is supposed to take the amount of Euros that the user enters and convert it to US dollars. It...
5
by: jyck91 | last post by:
please~help me to write a program to convert the baseN(2-9) to base10 eg. 101(2)----->5(10) 1211(3)------->X(10) ...... P.S. i am really misunderstand the C programming, plz write the simple...
7
by: bazymew | last post by:
I'm new to C++(I've only started learning about a month ago), so please don't call me stupid for asking a dumb question. You see I was experimenting with Exception operators, trying to make a...
3
by: haelly | last post by:
Write a program that prompts the user to enter three different integer values.If the values are not different, the program prints a message"equal values" and terminates(hint: use the return...
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
9
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.