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