tags:

views:

40

answers:

3

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], &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]);
}
+2  A: 

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.

WhirlWind
okay, but my main problem is how do I put that into an array with the way I have main written?
shinjuo
ohhh I see. Thanks
shinjuo
Sorry, updated my answer for you.
WhirlWind
I updated my code and now it crashes. Did I do it the way you were talking about?
shinjuo
I fixed a couple typos... but you should really try to understand what you're doing -- I offer unwarranted guidance only. ;)
WhirlWind
+1  A: 

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()?

John at CashCommons
+1  A: 

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

(*looker)++;
Mark Wilkins