201k views
3 votes
Given a vector of students, the function FinalAvg is supposed to return the average final exam score across all the students in the vector; if the vector is empty, the function is supposed to return 0.0. Write a test case using the Catch framework to test this function. You can assume that the Student class exists as presented the first day of class, any other inputs and object construction should be part of your test case.

User Justengel
by
5.6k points

1 Answer

2 votes

Answer:

The C++ code is given below with appropriate comments

Step-by-step explanation:

#define CATCH_CONFIG_MAIN //This tells Catch to provide a main()

#include"catch.hpp" //Header for Catch Framework

double FinalAvg( vector <int> students) // Function to calculate average.

{

double sum =0;

int i;

for( i=0;i<v.size();i++) sum+= v[i];

return v.empty() ? 0.0 : (double(sum)/ v.size());

}

vector<int> func(int argvq[ ]) // This function is made because we can not provide vector in command line argument that's why we used array which is then conerted into vector.

{

vector<int> v;

int j;

int l= sizeof(argvq)/sizeof(argvq[0]);

for(j=0;j<l;j++)

v.push_back(argq[j]);

return v;

}

TEST_CASE(" Final Average marks are computed","[func]") // TEST_ CASE macro to take one or more arguments

{

REQUIRE( func(9,5,-7) == 7); // We write our individual test assertions using the REQUIRE macro.

REQUIRE(func(2,4,6,-2) ==4);

}

Explanation Using different IDE:

#include<bits/stdc++.h>

using namespace std;

double FinalAvg(vector<int> students)

{

double sum=0;

for(int i=0;i<v.size();i++) sum+= v[i];

return v.empty() ? 0.0 : (double(sum)/v.size());

}

int main()

{

vector<int> v;

int i;

while(cin>>i&&i>0) v.push_back(i);

cout<<Final Avg(v);

return 0;

}

User Steffen Binas
by
5.1k points