0

Hi Guys,

this is searching coding.
I needs to ;-
- read data from file - DONE
- bubble sort - DONE
- to find the data (searching) - undone.

can anyone help me to find the error in my coding..
coz when i run my program, my algorithm void MultiSearc(int, int); not able to read my input to get the location of my data....

thanks guy..

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#define MaxSize 50
#define MaxFound 30



class dSearch
{
private:
int MyArray[MaxSize];

public:

   int way,element,MySize;
   int index;
   dSearch();
   void ReadFile(int []);
   void Bubles(int []);
   void MultiSearc(int, int);
   void DisplayData(int []);
};

dSearch::dSearch()
{
	element = 0;
      index = -1;
}


void dSearch:: ReadFile(int MyArray[])
{
int x=0;
int y=50;
  ifstream inputFile;
  inputFile.open("d:\\dataM.txt");
  if (!inputFile)
  {
		cout << "While opening a file an error is encountered" << endl;
      return;
  }
  while(x<y && inputFile>>MyArray[x])
  x++;

 cout<<"\nData Read From File:\n ";
  for( int i = 0; i < MaxSize; i++ )
  cout<<setw(3)<<MyArray[i]<<" ";
  inputFile.close();
 }

void dSearch:: Bubles(int MyArray[])
{
   int way,x;
   int  temp;
   for(way=1;way<MaxSize;way++){
   	for(x=0;x<MaxSize - way;x++){
      	if(MyArray[x]> MyArray[x+1]){
            temp = MyArray[x];
            MyArray[x] = MyArray[x+1];
            MyArray[x+1] = temp;
         }
      }
   }
}

//read input from user than find the location of data.. - error
void dSearch:: MultiSearc(int MyFindItem, int MySize)
{
int center, left, right;
int indexes[MaxFound] = {{0}};
char status[8] = "Failed";
*indexes = -1;
left = 0;
right= MySize - 1;

while (( left <= right) && (strcmp (status, "Failed") ==0))
{
	center = ( left + right )/2;
   if ( MyArray == MyFindItem)
   {
   *indexes = center;
	if (MyArray == MyFindItem)
	{
	*indexes = left;
	strcpy ( status, "Success....!");
	}
 	else if (MyArray  > MyFindItem)
 	right = center -1;
 	else
 	left = center +1;
 }
}
}

void dSearch:: DisplayData(int MyArray[])
{
	int i;
   cout <<"\n";
   for(i=0; i<= MaxSize;i++)
   cout<<setw(3)<<MyArray[i]<<" ";
   cout<<"\n\n";
}
                      
int main()
{
   int FileSize, FindData;
   int MyArray[MaxSize];

   dSearch S;

   S.ReadFile(MyArray);
   cout<<"\nSorted number ";
   S.Bubles(MyArray);
   S.DisplayData(MyArray);
   cout<<"\nEnter number to be found: ";
   cin>>FindData;
   S.MultiSearc(FindData,FileSize);
   getch();
   return 0;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

On line 13, 16, 21 (of your function 'MultiSearc', code posted below) you're trying to access 'MyArray', but you aren't giving any subscript with it ...
You should add a counter to your loop and access the data in 'MyArray' using MyArray[counter] ...

I reposted your code:

void dSearch::MultiSearc(int MyFindItem, int MySize)
{
    int center, left, right;
    int indexes[MaxFound] = {{0}};
    char status[8] = "Failed";
    *indexes = -1;
    left = 0;
    right= MySize - 1;

    while (( left <= right) && (strcmp (status, "Failed") ==0))
    {
        center = ( left + right )/2;
        if ( MyArray == MyFindItem)
        {
			*indexes = center;
			if (MyArray == MyFindItem)
			{
				*indexes = left;
				strcpy ( status, "Success....!");
			}
			else if (MyArray  > MyFindItem)
				right = center -1;
			else
				left = center +1;
        }
    }
}

Hope this helps !

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.