Counting number of searches
Posted
by shinjuo
on Stack Overflow
See other posts from Stack Overflow
or by shinjuo
Published on 2010-05-04T19:27:09Z
Indexed on
2010/05/04
19:38 UTC
Read the original article
Hit count: 203
I am trying to figure out how to get the total number of tests each search makes in this algorithm. I am not sure how I can pass that information back from this algorithm though. I need to count how many times while runs and then pass that number back into an array to be added together and determine the average number of test.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "percentage.h"
#include "sequentialSearch.h"
#define searchAmount 100
int main(int argc, char *argv[])
{
int numbers[100];
int searches[searchAmount];
int i;
int where;
int searchSuccess;
int searchUnsuccess;
int percent;
srand(time(NULL));
for (i = 0; i < 100; i++){
numbers[i] = rand() % 200;
}
for (i = 0; i < searchAmount; i++){
searches[i] = rand() % 200;
}
searchUnsuccess = 0;
searchSuccess = 0;
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where)){
searchSuccess++;
}else{
searchUnsuccess++;
}
}
percent = percentRate(searchSuccess, searchAmount);
printf("Total number of searches: %d\n", searchAmount);
printf("Total successful searches: %d\n", searchSuccess);
printf("Success Rate: %d%%\n", percent);
system("PAUSE");
return 0;
}
sequentialSearch.h
bool seqSearch (int list[], int last, int target, int* locn){
int looker;
looker = 0;
while(looker < last && target != list[looker]){
looker++;
}
*locn = looker;
return(target == list[looker]);
}
© Stack Overflow or respective owner