简体   繁体   中英

Binary Search sorting and indexing in C++

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 the sum of all odd integers using Binary Search. If the target value was found within the array, display array index at which target value was found, otherwise display target not found.

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 {6487456}, the sum of even numbers is 678, the odds, 549, and Target not found for the Binary Search. Here's my code right now:

#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,j,n=16,sum_even,sum_odd, index,len;
double ArrayInts[16]={54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60}, target;

//Implemeting Bubble sort
for(i=0;i<n-1;i++)
    for(j=0;j<n-1;j++)
    {
        if (ArrayInts[j]>ArrayInts[j+1])
        {
            len=ArrayInts[j];
            ArrayInts[j]=ArrayInts[j+1];
            ArrayInts[j+1]=len;
        }
    }

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

sum_even is not initialized.

sum_odd is not initialized.

target is not initialized.

None of these variables are initialized. As such, this results in undefined behavior.

Given that this is allegedly c++ , use std::sort to sort your array, instead of coding your own sort. If this is really C , use qsort(3) instead.

With range/V3 , it would be:

std::vector<int> v = {54,152,99,-8,3,19,-11,260,27,-4,10,12,15,58,480,60, 480-163};
auto is_even = [](int i) {return i % 2 == 0;};
auto is_odd = [](int i) {return i % 2 == 1;};

const auto even_sum = ranges::v3::accumulate(v | ranges::v3::view::filter(is_even), 0);
const auto odd_sum = ranges::v3::accumulate(v | ranges::v3::view::filter(is_odd), 0);
ranges::v3::sort(v);
const auto target = 27;
auto found = ranges::v3::binary_search(v, target);

std::cout << "The sum of all even numbers in the array is:" << even_sum << std::endl;
std::cout << "The sum of all odd numbers in the array is:" << odd_sum << std::endl;
std::cout << target << (found ? " Found" : " Not found") << std::endl;

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM