So, I'm about to do a sequential search on a table (n contents) of random numbers.
I have to print - the average between the number of comparisons and the contents of the table (n)
- and the average time
for each sequential search.
I wrote: - sum = 0;
-
for (i=1; i<=10; i++)
-
{
-
-
x = rand() % LIMIT + 1;
-
printf ("A number between 1 and 30000 : %d\n", x);
-
-
start = clock();
-
-
sum2 = 0;
-
done = 0;
-
j=1;
-
do
-
{
-
-
if (a[j]==x)
-
{
-
done = 1;
-
}
-
j++;
-
sum2 = sum2 + 1;
-
}
-
while ((j<=n) && (done == 0));
-
-
average= sum2 / n;
-
printf("he average of comparison between the contents of the table for the %d search is: %.2f\n", i, average);
-
-
end = clock();
-
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
-
sum = sum + elapsed;
-
avetime= sum / 10;
-
printf("the average time for each sequential search %.2f\n", avetime);
BUT I get 1 for every average and zero for the average time. Why?
5 3143
What types are sum2 and m? If they are both ints, you probably won't get accurate results.
What types are sum2 and m? If they are both ints, you probably won't get accurate results.
I have
int i, j, x, sum2;
int n, done;
float average;
clock_t start, end;
double elapsed, sum, avetime;
should i change it? into what?
Well, an int divided by an int is an int...no matter what. So if you have 24/51, it evaluates to 0. If you have 50/51, it evaluates to 0. If you have 199/100, it evaluates to 1. I have a feeling this is what is happening.
Try changing n or sum2 (or both) to a double. A double divided by a double (or an int divided by a double, or a double divided by an int) is a double.
Well, an int divided by an int is an int...no matter what. So if you have 24/51, it evaluates to 0. If you have 50/51, it evaluates to 0. If you have 199/100, it evaluates to 1. I have a feeling this is what is happening.
Try changing n or sum2 (or both) to a double. A double divided by a double (or an int divided by a double, or a double divided by an int) is a double.
thanks. i think it worked out!!
If you have correctly implemtented your little simulation you should've found out
that in order to find an item in a random sequence of n items you'd have to search
n/2 items on average.
Corollary: a rotating hard disk needs half a revolution on average to position the
correct wanted sector under the read/write head of the disk; therefore I propose
a revolutionary speedup: mount those read/write heads 180 degrees opposite
from where they are now to establish a zero rotation seach time. qed.
kind regards,
Jos (<--- genius ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: GregM |
last post by:
Hi,
I'm hoping that someone can point me in the right direction with this.
What I would like to do is calculate the average time it takes to load
a page. I've been searching the net and reading...
|
by: basecamp |
last post by:
just checking the average age of programmers using this group
--
thanks
|
by: alex |
last post by:
Hi,
I've got a problem with a script that needs about 1 minute to run as the
max_execution_time is set to 30 seconds only. I don't have the rights to
modify the max_execution_time parameter on...
|
by: Bruce W.1 |
last post by:
ASP.NET processes one request at a time, sequentially. If one process
blocks or sleeps then it brings down the entire application for all
users.
This fact is hidden under the rug by Microsoft. ...
|
by: J |
last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller
program.
Heres's what is supposed to happen:
Roll 2 6-sided dies. Add rolls together put total in
rolls(d6total).
Display...
|
by: Mike |
last post by:
I'm writing an application for Windows XP Embedded. This application
requires that the user be able to change the time zone from within the
application. I'm trying to do this using...
|
by: lovecreatesbeauty |
last post by:
Any comments are welcome for following the two sequential search C
functions of both integer and string versions. Though they are simple,
please do not laugh at it :) Could you give your great...
|
by: Allan Ebdrup |
last post by:
What would be the fastest way to search 18,000 strings of an average size of
10Kb, I can have all the strings in memory, should I simply do a instr on
all of the strings? Or is there a faster way?...
|
by: Shum |
last post by:
Hi ... I'm given the task of keeping a running average of number of
patients over 20 days, and plotting it in a graph.. What is basically
running average? is it simple average or what.. ? and how...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
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...
| |