Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi basically i have to enter the roll no. , cgpa , and name of five students and

ID: 3630314 • Letter: H

Question

Hi basically i have to enter the roll no. , cgpa , and name of five students and form an array of objects. Then i have to use linear search to search for a name(entered by user later) from the array of objects and binary search to search for a roll no. (entered entered by user)also from the array.

Im 90% there, there are no SYNTAX errors, but obviously some SEMANTIC ones.
PS= I HAVE JUST BEGUN OOP SO PLZ GO EASY ON THE SOLUTION!! THANKS



#include<iostream.h>
#include<string.h>

class reg

{
private:

char name[20];

float cgpa;

int roll;

public:

void setname()
{

cout<<"Enter name ";
cin>>name;


}

void setcgpa()

{


cout<<"Enter cgpa ";
cin>>cgpa;


}


void setrollno ()

{

cout<<"Enter roll number ";
cin>>roll;

}


int rollret()

{return roll;}

char* findname()
{return name;}


};



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 ";

cin>>names;

for (int y=0;y<5;y++)

{if (strcmp(student[y].findname(),names)==1)

{cout<<"Name found at index "<<y+1;}


}

//Enter roll no's in sorted order

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 ";
break;
}

else
if(student[mid].rollret()<element)
ub=mid-1;

else
if(student[mid].rollret()>element)
lb=mid+1;







}

cout<<"If the prompt SEARCH SUCCESFUL was not displayed this means such roll no. does not exist ";

}

Explanation / Answer

You had most of it, the logic in your binary search was backwards for the mid.

#include<iostream>
#include<string>
using namespace std;

class reg
{
    private:
        char name[20];
        float cgpa;
        int roll;

    public:

    void setname()
    {
        cout<<"Enter name ";
        cin>>name;
    }

    void setcgpa()
    {
        cout<<"Enter cgpa ";
        cin>>cgpa;
    }

    void setrollno ()
    {
        cout<<"Enter roll number ";
        cin>>roll;
    }

    int rollret(){return roll;}
    char* findname(){return name;}

};


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 ";
    cin>>names;

    for (int y=0;y<5;y++)
    {
        if (strcmp(student[y].findname(),names)==0)
        {
            cout<<"Name found at index "<<y+1;
        }
    }

    //Enter roll no's in sorted order
    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 ";
            return 0;
        }
        else if(student[mid].rollret()<element){
            lb=mid;
        }
        else if(student[mid].rollret()>element){
            ub=mid;
        }
    }
    cout<<"If the prompt SEARCH SUCCESFUL was not displayed this means such roll no. does not exist ";
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote