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

average time on a sequential search

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:



Expand|Select|Wrap|Line Numbers
  1. sum = 0;
  2.      for (i=1; i<=10; i++)
  3.     {
  4.  
  5.          x = rand() % LIMIT + 1;
  6.          printf ("A number between 1 and 30000 : %d\n", x);
  7.  
  8.          start = clock();
  9.  
  10.          sum2 = 0;
  11.          done = 0;
  12.          j=1;
  13.          do
  14.          {
  15.  
  16.              if (a[j]==x)
  17.              {
  18.                   done = 1;
  19.              }
  20.              j++;
  21.              sum2 = sum2 + 1;
  22.          }
  23.          while ((j<=n) && (done == 0));
  24.  
  25.          average= sum2 / n;
  26.          printf("he average of comparison between the contents of the table for the %d search is: %.2f\n", i, average);
  27.  
  28.          end = clock();
  29.          elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
  30.          sum = sum + elapsed;
  31. avetime= sum / 10;
  32.     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?
Oct 18 '08 #1
5 3143
Ganon11
3,652 Expert 2GB
What types are sum2 and m? If they are both ints, you probably won't get accurate results.
Oct 18 '08 #2
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?
Oct 18 '08 #3
Ganon11
3,652 Expert 2GB
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.
Oct 18 '08 #4
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!!
Oct 19 '08 #5
JosAH
11,448 Expert 8TB
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 ;-)
Oct 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
176
by: basecamp | last post by:
just checking the average age of programmers using this group -- thanks
2
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...
2
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. ...
6
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...
2
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...
31
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...
8
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?...
2
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
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...

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.