473,383 Members | 1,818 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,383 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 1509
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.