o yes, you are correct! i have the locations returning to main fine, the only problem im having now is getting the comparisons to return correctly via parameters.
prototypes
Code:
int sortarray (int *dyn_ary, int ele, int *comparisons, int *swaps);
int binarysearch(int *dyn_ary, int data_item, int ele, int *bcomparisons);
int lsearch(int *ary_nbr, int data_item, int ele, int *lcomparisons);
main
Code:
#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
#include "type-proto.h"
#define bool int
#define true 1
#define false 0
#define in_filename "Prog2Data.txt"
#define out_filename "Report.txt"
#define maxsize 100
int main(int argc,char *argv[])
{
/* declare array */
int ary_nbr[maxsize];
/* declare dynamic array */
int *dyn_ary;
/* number to search for */
int data_item = 0;
/* comps & swaps for bubble sort */
int *comparisons = 0;
int *swaps = 0;
/* comps & location for binary search */
int *bcomparisons = 0;
int b_location = 0;
/* comps & location for linear search */
int *lcomparisons = 0;
int l_location = 0;
/* I/O pointers */
FILE *InFile, *OutFile;
/* error checking */
bool file_error_flag;
/* Open the file to read */
file_error_flag = false;
InFile = fopen(in_filename, "r");
if (InFile == NULL)
{
printf("Error opening \"%s\"\n\n", in_filename);
file_error_flag = true;
}
/* Creates and Opens the Write File */
OutFile = fopen(out_filename, "w");
if (OutFile == NULL)
{
printf("Error opening \"%s\"\n\n", out_filename);
file_error_flag = true;
}
if (file_error_flag)
{
printf("\nProgram terminated due to FILE error!\n\n");
/* Identify problem file and close files that are open */
if (InFile == NULL)
printf("Error with %s\n",in_filename);
else
fclose(InFile);
if (OutFile == NULL)
printf("Error with %s\n",out_filename);
else
fclose(OutFile);
}
else /* Files open successfully */
{
/* elements catches # of actual data loaded */
int elements = 0;
/* runs & catches return from load function */
elements = load(InFile, ary_nbr, maxsize);
printf("\n\n\n\tArray loaded with %d integers!\n\n", elements);
dyn_ary = copyarray (ary_nbr, elements);
/* prints actual data loaded in array pre-sort */
printtab (OutFile, dyn_ary, elements);
printf("\n\n\tArray copied with %d integers!\n\n", elements);
/* bubble sorts the array */
sortarray (dyn_ary, elements, &comparisons, &swaps);
printf("\tBubble sort Performance Data:\n");
printf("\t%i comps\n", comparisons );
printf("\t%i swaps\n\n", swaps );
/* prints actual sorted data */
printtab (OutFile, dyn_ary, elements);
printf("\nenter the data to search for:");
scanf("%d", &data_item);
b_location = binarysearch(dyn_ary, data_item, elements, &bcomparisons);
printf("\n\nthis is the bsearch location in main: %d\n", b_location);
l_location = lsearch(ary_nbr, data_item, elements, &lcomparisons);
printf("\n\nthis is the lsearch location in main: %d\n", l_location);
/* close files */
fclose(InFile);
fclose(OutFile);
}
/* else file_error_flag */
return file_error_flag;
}
and the function bodies
Code:
int sortarray (int *dyn_ary, int ele, int *comparisons, int *swaps)
{
int j;
int i;
int temp;
/* perform bubble sort */
for( i=0; i<ele; i++)
{
for(j=0; j<ele-1; j++)
{
*comparisons ++;
if( dyn_ary[j] > dyn_ary[j+1])
{
*swaps++;
temp = dyn_ary[j+1];
dyn_ary[j+1] = dyn_ary[j];
dyn_ary[j] = temp;
}
}
}
// to test values
printf("this is comparisons: %d", comparisons);
printf("this is swaps: %i", swaps);
}
// Recursive binary search
int binarysearch(int *dyn_ary, int data_item, int ele, int *bcomparisons)
{
// point to beginning and end of the array
int low, high, mid;
low = 0, high = ele - 1;
while(low <= high)
{
mid = (low + high)/2;
printf("\n mid points to index %i", mid);
// is the data_item in lower or upper half?
if (data_item < dyn_ary[mid])
{
high = mid -1;
*bcomparisons++;
}
else if(data_item > dyn_ary[mid])
{
low = mid +1;
*bcomparisons++;
}
else
{
printf("\nbinary search for %i found at location [%d] with %d comps!\n", data_item, mid, *bcomparisons);
return mid;
}
}
return -1;
getchar();
getchar();
free(dyn_ary);
}
int lsearch(int *ary_nbr, int data_item, int ele, int *lcomparisons)
{
int i = 0;
for (i=0; i<ele;i++)
{
if(ary_nbr[i]==data_item)
{
*lcomparisons++;
printf("\nlinear search for %i found at location [%d] with %d comps!\n", data_item, i, *lcomparisons);
return i;
}
}
printf("\nlinear search for %i found at location [%d] with %d comps!\n", data_item, i, *lcomparisons);
return -1;
}
i had the swaps working for the sort array, but i had the values as global, and i cant do that, they need to be local and sent via parameters. i just cant seem to get these comparisons to work correctly!!!