Hi, this program is fine but Binary search is not working. Can u plz try the \"k
ID: 3630728 • Letter: H
Question
Hi, this program is fine but Binary search is not working. Can u plz try the "key" at different positions to see if your solution is consistent, cuz sometimes result comes out by a fluke! AND plz dont overcomplicate the solution as im not an expert.!!! THANKS A LOT.
ENTER THE ROLL NOS IN SORTED(ASCENDING) ORDER INTO THE ARRAY.
ALSO LINEAR SEARCH DOES NOT WORK, PLZ FIND FAULT IN MY METHOD, DONT INTRODUCE NEW ONE.
#include<iostream>
#include<string>
using namespace std;
class reg
{
private:
char name[50];
float cgpa;
int roll;
public:
void setname()
{
cout<<"Enter name ";
cin>>name;
}
int rollret(){return roll;}
void setcgpa()
{
cout<<"Enter cgpa ";
cin>>cgpa;
}
void setrollno ()
{
cout<<"Enter roll number ";
cin>>roll;
}
void find(char nam[])
{if(strcmp(name,nam)==1)
cout<<"Search succesful ";
}
};
void main()
{
reg student[5];
char names[50];
for(int u=0;u<5;u++)
{
student[u].setname();
student[u].setcgpa();
student[u].setrollno();
}
cout<<"Enter name to search, the LINEAR SEARCH will be implemented ";
cin>>names;
cout<<"Enter name to search, the LINEAR SEARCH will be implemented ";
cin>>names;
for (int y=0;y<5;y++)
{student[y].find(names);}
/////////////roll no's
cout<<" Now to use BINARY SEARCH to search for a roll number ";
cout<<" Enter roll no to search ";
int element;
cin>>element;
int lb=0,ub=4,mid;
for(;lb<ub;)
{
mid=(lb+ub)/2;
if(student[mid].rollret()==element)
{
cout<<" SEARCH SUCCESSFUL ";
cout<<"Element found at "<<mid+1;
break;
}
else if(element>student[mid].rollret()){
lb=mid+1;
}
else if(element<student[mid].rollret()){
ub=mid-1;
}
}
cout<<"If the prompt SEARCH SUCCESFUL was not displayed this means such roll no. does not exist ";
}
Explanation / Answer
please rate - thanks
#include<iostream>
#include<string>
using namespace std;
class reg
{
private:
char name[50];
float cgpa;
int roll;
public:
void setname()
{
cout<<"Enter name ";
cin>>name;
}
int rollret(){return roll;}
void setcgpa()
{
cout<<"Enter cgpa ";
cin>>cgpa;
}
void setrollno ()
{
cout<<"Enter roll number ";
cin>>roll;
}
void find(char nam[])
{if(strcmp(name,nam)==0)
cout<<"Search succesful ";
}
};
int main()
{
reg student[5];
char names[50];
for(int u=0;u<5;u++)
{
student[u].setname();
student[u].setcgpa();
student[u].setrollno();
}
cout<<"Enter name to search, the LINEAR SEARCH will be implemented ";
cin>>names;
cout<<"Enter name to search, the LINEAR SEARCH will be implemented ";
cin>>names;
for (int y=0;y<5;y++)
{student[y].find(names);}
/////////////roll no's
cout<<" Now to use BINARY SEARCH to search for a roll number ";
cout<<" Enter roll no to search ";
int element;
cin>>element;
int lb=0,ub=4,mid;
for(;lb<=ub;)
{
mid=(lb+ub)/2;
if(student[mid].rollret()==element)
{
cout<<" SEARCH SUCCESSFUL ";
cout<<"Element found at "<<mid+1;
break;
}
else if(element>student[mid].rollret()){
lb=mid+1;
}
else if(element<student[mid].rollret()){
ub=mid-1;
}
}
cout<<"If the prompt SEARCH SUCCESFUL was not displayed this means such roll no. does not exist ";
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.