Binary Search & Mean

need help with finding why the sorted numbers in the array arent passing to the fuction to calculate the mean of the data set. And why does it say item found when i enter a number that isint stored in the array?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include <stdio.h>
#include<conio.h>
#include <iostream>
#include <vector>

using namespace std;
int main()
{
    int bsearch(int AR[], int N, int VAL);
    int array[50], n, c, d, found, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);



	cout<<"\nEnter the number you want to search ";
	cin>>c;

	found=(n,c,d);

	if(found==1)
		cout<<"\nItem found";
	else
		cout<<"\nItem not found";

	getch();
	return 0;
}

int bsearch(int n[], int c, int d)
{
	int Mid,Lbound=0,Ubound=c-1;

	while(Lbound<=Ubound)
	{
		Mid=(Lbound+Ubound)/2;
		if(d>n[Mid])
			Lbound=Mid+1;
		else if(d<n[Mid])
			Ubound=Mid-1;
		else
			return 0;
	}

double GetMean(int n [], int d);
{
  double dSum = n [0];
    for (int i = 1; i < d; ++i) {
        dSum += n [i];
    }
    return dSum/d;

 cout << "Mean = " << GetMean(n,d) << endl;
}


  return 0;
}


Last edited on
Where are you even calling the bsearch() function?

You also need to check the position of your opening and closing braces, and function prototypes, line 10 and line 70 should really be placed after your includes and before main().

Topic archived. No new replies allowed.