开发者

Counting number of searches

开发者 https://www.devze.com 2022-12-28 22:50 出处:网络
I updated my main and sequetialSearch and now it crashes when it runs. It compiles okay, but then crashes.

I updated my main and sequetialSearch and now it crashes when it runs. It compiles okay, but then crashes.

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 testAmounts[searchAmount];
  int i;
  int where;
  int searchSuccess;
  int searchUnsuccess;
  int percent;
  int looker;
  int sum;
  int average;

  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;
  sum = 0;

  for(i = 0; i < searchAmount; i++){
        if(seqSearch(numbers, 100, searches[i], &a开发者_如何学编程mp;where, &looker)){
              searchSuccess++;
              testAmounts[i] = looker;

        }else{
              searchUnsuccess++;
              testAmounts[i] = looker;
        }
  }
  for(i = 0; i < searchAmount; i++){
        sum = sum + testAmounts[i];
  }

  average = sum / searchAmount;

  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);
  printf("Total number of tests ran: %d\n", average);
  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]);
}


Pass looker in by reference, so that you can access its value from the caller.

int looker;

...

for(i = 0; i < searchAmount; i++){
      if(seqSearch(numbers, 100, searches[i], &where, &looker)){
            searches[i] += looker;
            searchSuccess++;       
      }else{
            searchUnsuccess++;
      }
}


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]);
}

By the way, you may wish to reconsider defining functions in your header file; this could cause problems with duplicate symbol when linking if you have more than one c file including this file.


Why not just pass looker in as an int*, use it essentially as you have been, look at the value after seqSearch(...) returns, and add it to a running total back in main()?


One problem is that the increment of looker in seqSearch is incrementing the pointer rather than the value. It should probably be:

(*looker)++;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号