The Output comes correct in some cases and wrong in some..

Here's the program. It was supposed to make the user enter the contents of an integer array. Display the contents of the array. Then sort the contents and again display them. Then prompt the user to search for an element from the sorted array and then display its respective position if found in the array or prompt the user " *element* not found in the array." if the element the user wants to search for is not present in the integer 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
#include<iostream.h>
#include<conio.h>
void enterarr(int [], int);
void displayarr(int [], int);
void ssortarr(int [], int);
void bsearcharr(int [], int);
void main()
{
 clrscr();
 int A[100], n;
 do
 { 
   cout<<"Enter no of elements of the array <=100: ";
   cin>>n;
 }while(n>100);
 cout<<endl<<endl;
 enterarr(A,n);
 displayarr(A,n);
 ssortarr(A,n);
 bsearcharr(A,n);
 getch();
}
void enterarr(int B[], int n)
{
  cout<<"Enter array elements:"<<endl<<endl;
  for(int i=0; i<n; i++)
	  cin>>B[i];
  cout<<endl<<endl;
}
void displayarr(int B[], int n)
{
	cout<<"The array you entered is:"<<endl<<endl;
    for(int i=0; i<n; i++)
	  cout<<B[i]<<" ";
	cout<<endl<<endl;
}

void ssortarr(int B[], int n)
{

  for(int i=0; i<(n-1); i++)
  {int small=i;
  for(int j=i+1; j<n; j++)
	  if(B[j]<B[small])
		  small=j;
   if(small!=i)
   {
	  int temp=B[i];
	  B[i]=B[small];
	  B[small]=temp;
   }
  }
cout<<"The sorted array is: "<<endl<<endl;
for(i=0; i<n; i++)
 cout<<B[i]<<" ";
cout<<endl<<endl;
}


void bsearcharr(int B[], int n)
{
  int mid, lbound=0, ubound=n-1, found=0, data;
  cout<<endl;
  cout<<"Enter element to be searched: ";
  cin>>data;
  cout<<endl<<endl;
  while((lbound<=ubound) && !(found))
  {
	  mid=(lbound+ubound)/2;
	  if(data>B[mid])
		  lbound=mid+1;
	  else if(data<B[mid])
		  ubound=mid-1;
	  else;
		  found++;
  }
  if(found)
	  cout<<data<<" found at position "<<mid+1<<".";
  else if(!found)
	  cout<<data<<" not found in the array. ";
}

Here is the various lists of outputs:
(a)

Enter no of elements of the array <=100: 4


Enter array elements:

1
6
2
4


The array you entered is:

1 6 2 4

The sorted array is:

1 2 4 6


Enter element to be searched: 5


5 found at position 2.


But this is wrong since it should display: "5 not found in the array."

(b)
Enter no of elements of the array <=100: 2


Enter array elements:

2
2


The array you entered is:

2 2

The sorted array is:

2 2


Enter element to be searched: 4


4 found at position 1.


But this is wrong since it should display: "4 not found in the array."

(c)
Enter no of elements of the array <=100: 5


Enter array elements:

52
73
12
24
44


The array you entered is:

52 73 12 24 44

The sorted array is:

12 24 44 52 73


Enter element to be searched: 52


52 found at position 3.


But this is wrong since it should display: "52 found at position 4."

But also it displays some correct outputs like:

(d)
Enter no of elements of the array <=100: 5


Enter array elements:

1
2
3
4
5


The array you entered is:

1 2 3 4 5

The sorted array is:

1 2 3 4 5


Enter element to be searched: 3


3 found at position 3.


What is the error in the code as it is giving wrong outputs (a),(b), (c)? Reply asap..urgent! :)
Last edited on
line 74: else;
I will post your while loop with changed formatting here to show what actually gets done:
1
2
3
4
5
6
7
8
9
10
while((lbound<=ubound) && !(found))  {
    mid=(lbound+ubound)/2;
    if(data>B[mid])
        lbound=mid+1;
    else if(data<B[mid])
        ubound=mid-1;
    else
        ; //Empty operator
    found++;
}
So found will be incremented in any case
Last edited on
Such minute mistakes! :/ Thanks! :D
Note: c) is correct 52 is in position 3.... remember the first element in an array is at array[0]
Topic archived. No new replies allowed.