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

Why does it run so long?

Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

Thanks for any help.

Yours sincerely,

Michael

P.S.:
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between
0 and 1;
if(check(x)==0) goto mark;
for(j=1; j<=100; j++) A[i][j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x[i]<0)||(x[i]>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array[i]*x[i];
if(sum != 0) return 0;
return 1;
}



Nov 15 '05 #1
3 1511
Michael wrote:
Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

Thanks for any help.

Yours sincerely,

Michael

P.S.:
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between
0 and 1;
if(check(x)==0) goto mark;
you have a goto here that depends on the output of a random number
generator.
If you can work out an upper bound on the number of times this loops
you're smarter than me (not hard).

What is it supposed to do? Why do you use a goto?

for(j=1; j<=100; j++) A[i][j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x[i]<0)||(x[i]>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array[i]*x[i];
if(sum != 0) return 0;
return 1;
}

--
Nick Keighley

"Anyone attempting to generate random numbers by deterministic
means is, of course, living in a state of sin."
-- John Von Neumann

Nov 15 '05 #2
Michael wrote:
Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?


It's hard to tell for sure, since you haven't provided
code that will compile: you've omitted the definitions of
`C', or `N', and of `array', and given no clue to their
values. (For example, if the Random() function behaves as
you say and if C has a negative value, it's very easy to
see why the program will run for a long time ...)

Even so, an incomplete diagnosis is possible. One of
your problems is that you haven't read and understood
Questions 14.4 and 14.5 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3
"Michael" <yl*********@126.com> wrote in message
news:df***********@mail.cn99.com...
Hi.

Here is my codes, which run too long. It takes about 8 minutes to complete
the "for" loop. Why does it run so long? What's wrong?

[snip]
for(i=1; i<=30; i++)
{
mark:
for(j=1; j<=100; j++) x[j]=Random();//Random() will generate a value between 0 and 1;
if(check(x)==0) goto mark;
for(j=1; j<=100; j++) A[i][j]=x[j];
}

where check() is£º
static int check(double x[])
{
for(i = 1; i <= 100; i++)
if((x[i]<0)||(x[i]>C) return 0;
double sum = 0;
for(i = 1; i <= N; i++)
sum += array[i]*x[i];
if(sum != 0) return 0;
Try this line instead, and see whether or not it dramatically decreases run
time:
if(sum>SUM_EPS) return 0;
where SUM_EPS is #defined as something like 0.0001 (or some other, suitable
small, epsilon).
return 1;
}


The problem may be that for many interations "sum" comes out to something
like 0.0000438764, which for your purposes *equals* 0. However, since
that's not actually equal to 0 (to your processor, anyway), your "check"
function returns 0 and the program loops again.

In fact, as far as I can tell, unless "array[]" is full of 0s your code is
dependent upon floating-point underflow to ever complete. IMHO depending on
underflow for program completion is usually not a Good Thing.

-Charles
Nov 15 '05 #4

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

Similar topics

8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
40
by: Matt | last post by:
Please skip to the last paragraph if you are in a hurry. Some of the integer variables in my application will need to hold values bigger than 2^32-1. Others won't need to be that big. Time...
3
by: Jérôme de Lagausie | last post by:
Hello, In one hand, I've got a set of more than 130 different structures, mostly rather simple (a set of simple data members such as int, long, char , float ...) sample : #ifndef _H2_H_...
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
0
by: vinodhnair | last post by:
I am trying to link my C++ application (Solaris) to 32 bit libraries of ORACLE 10G. But the application gives the following error in oratypes.h: "rdbms/public/oratypes.h:152: warning: ISO C++...
0
by: remya1000 | last post by:
i'm using VB.Net 2005 application program. i'm trying to convert VB6 code to VB.Net 2005. QSockB is DLL file. this is the code i used for VB6. This is code i'm using to create socket, when...
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
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
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.