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

Hello so we have a program with an array of sructs. struct Candidate { string fi

ID: 3881610 • Letter: H

Question

Hello so we have a program with an array of sructs.

struct Candidate {

string first;

string last;

int votes;

double pScore;

};

void roundScore(Candidate &cand) – updates single element, passed by reference. Function is rounding the pScore (example: 74.29% is rounded to 74%, 74.64% is rounded to 75%). //For three records with the highest pScore, use the roundScore function to round the pScore

we are supposed to implement this function void roundScore (i aready have void input and output functions), how would I execute this function passing an element by reference ?

Explanation / Answer

#include<iostream>
#include<math.h>
using namespace std;
struct Candidate {
string first;
string last;
int votes;
double pScore;
};

void roundScore(struct Candidate &cand) //roundup method which rounds the score
{
  
   double n = cand.pScore;//taking pScore
   double t=n-floor(n);//removing floor so, that decimal places are maining
   if(t>=0.5)
   {
       t=ceil(n);//rounding up
          
   }
   else
   {
       t=floor(n);//rounding down

   }
   cand.pScore = t;//setting rounded value
  
}

int main()
{
//testing
struct Candidate p,q;
p.pScore =74.29;
q.pScore = 74.69;
roundScore(p);
roundScore(q);

cout<<p.pScore<<endl;
cout<<q.pScore<<endl;
  
   return 0;
}

output:

74
75


Process exited normally.
Press any key to continue . . .

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