Binary Search

Question: Given an array (ArrayInts) compute the sum of all the even integers, the odd integers, and search the array for the target value of the sum of all odd integers using Binary Search. If the target value was found within the array, display target value found, otherwise display target not found

I keep getting errors on the Binary Search part, but I do not understand why


#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int ArrayInts[16] = {54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60};
int i = 0, even = 0, odd = 0;
int BubbleSort(int array[]);

// sort the array in order to perform a binary search
BubbleSort(ArrayInts,16);

// Sum evens and odd
for (i=0; i<15; i++)
{
// even portion
if (ArrayInts[i]%2 == 0)
even += ArrayInts[i];
// odd portions
else
odd += ArrayInts[i];
}

printf("Even Sum: %d\n",even);
printf("Odd Sum: %d\n",odd);
if (BinarySearch(ArrayInts,even,16) == -1)
printf("Target was not found\n");
else
printf("Even target is at index: %d\n",BinarySearch(ArrayInts,odd,16));

if (BinarySearch(ArrayInts,odd,16) == -1)
printf("Target was not found\n");
else
printf("Odd target is at index: %d\n",BinarySearch(ArrayInts,odd,16));

return 0;
}

// sorting algorithm
int BubbleSort(int array[], int len)
{
int i = 0, j = 0, swp = 0;

for (i=0; i<len; i++)
{
for (j=0; j<len-1; j++)
{
if (array[j] > array[j+1])
{
swp = array[j];
array[j] = array[j+1];
array[j+1] = swp;
}
}

}
}

// BinarySearch algorithm
int BinarySearch(int array[], int target, int hi)
{
int low = 0;
int high = hi;
int mid = 0;
int i = 0;

while (low <= high)
{
// calculate new mid
mid = low + ((high-low)/2);

// mid becomes the high
if (array[mid] > target)
high = mid - 1;

// mid becomes the low
else if (array[mid] < target)
low = mid + 1;

// we found the target
else
return mid;
}

// target not found
return -1;
}



Hi,
Did the code compile?
Can you post full complier error logs here please?
11 24 C:\Users\ [Error] too many arguments to function 'int BubbleSort(int*)'
8 5 C:\Users\ [Note] declared here
26 35 C:\Users\ [Error] 'BinarySearch' was not declared in this scope
31 34 C:\Users\ [Error] 'BinarySearch' was not declared in this scope
You declare your function like this
int BubbleSort(int array[]);
but call it like this
BubbleSort(ArrayInts,16);

You need to change your declaration to this.
int BubbleSort(int array[], int numElems);

It's also better to put your declaration in front of main.
Ok so I fixed a lot of the code and can actually compile it now, however, I'm not getting the correct answers. I don't believe the Array I want to be read is being read. When I compile, I get that the Array is {0}, the sum of even numbers is 678, the odds, 549, and Target not found for the Binary Search. Here's my code right now:

//This program will
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


double ArrayInts[]={54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60};
int Bin_Search(double ArrayInts[],int low, int high, double target);

int main(void)
{
int i,n=16,sum_even,sum_odd, index;
double ArrayInts[16]={54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60}, target;

//Function call to add even and odd numbers from array
for(i=0;i<16;i++)
{
if(i%2==0)
sum_even+=ArrayInts[i];

else
sum_odd+=ArrayInts[i];
}

printf("The array is {%d}\n",ArrayInts[i]);
printf("The sum of all even numbers in the array is: %d\n",sum_even);
printf("The sum of all odd numbers in the array is: %d\n",sum_odd);

//Function call to search for target value
index = Bin_Search(ArrayInts,0,15,target);

if(index != -1)
printf("Target found at index %d\n", index);
else
printf("Target was not found\n");

system("pause");
return (0);
}

int Bin_Search(double ArrayInts[],int low, int high, double target)
{
int mid;

if (low>high)
return -1;

mid=(low+high)/2;
if(ArrayInts[mid]==target)
return(mid);

else if (ArrayInts[mid]<target)
return Bin_Search(ArrayInts, mid+1, high, target);
else
return Bin_Search(ArrayInts, low, mid-1, target);
}
1
2
3
4
5
6
7
8
for (i = 0; i < 16; i++)
  {
    if (i % 2 == 0) // ??????????
      sum_even += ArrayInts[i];

    else
      sum_odd += ArrayInts[i];
  }


Why do you check the loop var ?

You better check the number in the array.
Also you need to initialize your variables in main.
The check is there to check for even numbers I believe. I don't quite understand your suggestion of checking the number in the array. Do you mean the numbers that are part of the array? The size? Also, I'm not entirely sure on how to initialize my variables in the main. Sorry if these questions seem dumb, I'm very new at this. Thanks
Also, I'm not entirely sure on how to initialize my variables in the main.
int i, n = 16, sum_even = 0, sum_odd = 0, index = 0;

A concrete example:
1
2
3
4
5
6
7
8
  for (i = 0; i < 16; i++)
  {
    if (i % 2 == 0)
      sum_even += ArrayInts[i];

    else
      sum_odd += ArrayInts[i];
  }


If i is 1 means i is not even so sum_odd += ArrayInts[i]; will be executed. ArrayInts[1] is 152. Adding 152 to sum_odd is wrong.



I see. So what I'm doing with that statement is summing the even POSITIONS of the array and not the even numbers within it. What I should have is

1
2
3
4
5
6
7
for(i=0;i<16;i++)
{
 if(ArrayInt[i]%2==0
    sum_even+=ArrayInts[i];

else
    sum_odd += ArrayInts[i];


However, when I try this I get an error:
30 19 C:\Users\4.cpp [Error] invalid operands of types 'double' and 'int' to binary 'operator%'
You missed the closing bracket after if(ArrayInt[i]%2==0
However, when I try this I get an error:
30 19 C:\Users\4.cpp [Error] invalid operands of types 'double' and 'int' to binary 'operator%'
 
double ArrayInts[]={54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60};

line 3: ArraryInt is of type double. You can't take a modulo of a double.
Why is ArrayInt of type double? Or why is it called ArrayInt if you intended it to be doubles?
Line 3 is also missing a )
Last edited on
Topic archived. No new replies allowed.