Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Add on another non-member function with the prototype

0 votes
asked Sep 15, 2021 by Lavenesh Devendran (120 points)
This a program i have done  but not sure how to add the last part. part need to be add is as below decsription

Achievement find_highest(…)
 Parameter: an array of objects
The function will determine which object element hold the highest score and returns
the object element back to function caller.
Update main() to include the additional codes:
…….
…….
 cout<<"----------------------------------------"<<endl;
 cout<<" Best result "<<endl;
 cout<<"----------------------------------------"<<endl;
Achievement high = find_highest(A);
high.display_scale();
 return 0;
} //end of main function

#include<iostream>
#include<iomanip>

using namespace std;

class Achievement{
    private:
      string name;
      int score;
    public:
    void set_data(string n,int s){
        name = n;
        score= s;
    }
    int getScore(){
        return score;
    }
    void display_scale(){
        cout<<"==========================="<<endl;
        cout<<"\tname"<<endl;
        cout<<"The Class Passing Grade Achievement:"<<score<<"%"<<endl;
        if(score>=85 && score<=100)
           cout<<"* * * * *"<<endl;
        else if(score>=70 && score<85)
           cout<<"* * * *"<<endl;
           
         else if(score>=60 && score<70)
        cout<<"* * *"<<endl;
        
         else if(score>=50 && score<60)
        cout<<"* *"<<endl;
        
         else
        cout<<"Poor Achievement"<<endl;
    }
};
void summary(Achievement a[]){
  int d=0, m=0, p=0, f=0;
    for(int i=0;i<4;i++){
        int score = a[i].getScore();
        if(score>=85)
        d++;
        else if(score>=70)
        m++;
        else if(score>=50)
        p++;
        else
        f++;
    }
      cout<<"-----------------------------------"<<endl;
      cout<<"         SUMMARY                   "<<endl;
      cout<<"Distinction : "<<d<<"              "<<endl;
      cout<<"Merit : "<<m<<"                    "<<endl;
      cout<<"Pass :   "<<p<<"                    "<<endl;
      cout<<"Fail :   "<<f<<"                    "<<endl;
        
}
int main(){
 Achievement a[4];
 string classname;
  int score;

cout<<"==========================================="<<endl;
cout<<"          Enter Class Achievement          "<<endl;
cout<<"==========================================="<<endl;
for(int i=0;i<4;i++){
    cout<<"Enter the class name :";
    getline(cin,classname);
    cout<<"Enter the class achievement (% ) :";
    cin>>score;
    a[i].set_data(classname,score);
    }
    cout<<"THE SUMMARY OF UPSR TRIAL EXAM RESULT"<<endl;
    cout<<"====================================="<<endl;
    for(int i=0;i<4;i++){
    a[i].display_scale();
    
        
    }
    summary(a);

}

Output of that should be displayed at the end part after including my codes:

--------------------------------------------------------------

Best result

 ========================================  

The Class Passing Grade Achievement: 93%

* * * * *

1 Answer

0 votes
answered Sep 20, 2021 by Peter Minarik (86,040 points)

You need to add a new function to you Achievement class called find_highest().

Achievement find_highest(…)

Parameter: an array of objects

Hm... This is wrong. You cannot use an array as a parameter as you do not know the length of the said array.

You're using C++, not just C, so why not take advantage of the language elements and utilize the std::vector, instead of dealing with primitive arrays? The vector knows the number of elements in it (unlike the array), it's easy to add new elements to it. So it saves you from lot of troubles.

So the correct thing to do here would be either using two arguments for the new function: array and length; or just use the C++ way and use an std::vector.

Also, I wouldn't return an Achievement (copy of the original elements) but I'd return a (const) reference of the original elements

const Achievement & find_highest(const Achievement * array, size_t length)
{
    // Implement your find logic that finds the index in array that's the highest score
    return array[i];
}

And this is the alternative (which I would chose instead of the above)

const Achievement & find_highest(const std::vector<Achievement> & achievements)
{
    // Implement your find logic that finds the index in array that's the highest score
    return array[i];
}

Good luck and post your code if you get stuck.

Also, there's so much else that could be polished in your code. I'm happy to help with it if you're interested.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...