Using c++
You may #include only:
<iostream>
<cmath>
“functions.h”
Write the function void readForceValuesFromStdIn(double*
leftTeam, double* rightTeam, unsigned const int noParticipants)
that will read the list of forces exerted by each wizard
alternating by the team into the correct array of doubles (see
example in problem description)
Define the function bool validForces(const double* forces, unsigned const int noParticipants) using the provided code. This function will be used to ensure that the forces exerted by each team’s wizard are non-negative
The first is an array of forces (as a pointer to the first element)
The second parameter is the number of participants on each team (i.e., the size of each team’s force array)
The function will return true if all values are non-negative, false otherwise.
You will need to call this function for each
team
bool validForces(const double *forces, unsigned const int noParticipants) { for (int i = 0; i < noParticipants; ++i) { if (forces[i] < 0) { return false; } } return true; } |
Write a function double calculateForce(const double* leftTeam, const double* rightTeam, unsigned const int noParticipants) that returns the net force on the wall (magnitude and direction)
The first and second parameters are the arrays of forces (as pointers to the respective first elements)
The third parameter is the number of participants on each team (i.e., the size of each array)
The function will return the net force on the wall
(magnitude and direction). Assume that the net force will be
negative if the right team wins; positive
otherwise
Write the function void printWinnerToStdOut(const char* leftTeamName, const char* rightTeamName, const double netForce) that prints winner to standard output.
The first parameter is the name of the team standing on the left side of the wall passed in as a C-style string
The second parameter is the name of the team standing on the right side of the wall, which is also passed as a C-style string
In the function body,
Given sufficient negative net force, the function will print to standard output the name of the right team [space] “wins!\n”
Given sufficient positive net force, the function will print to standard output the name of the left team [space] “wins!\n”
Given an insufficient net force, the function will print “Tie.\n” to standard output.
This function will not return
anything
In your program’s main function, you should
Read in
the number of participants on each team (assume max is 200)
the name of the team on the left-hand side of the wall (assume that it is a single word without spaces and its max length is 255)
the name of the team on the right-hand side of the wall (assume that it is a single word without spaces and its max length is 255)
Using the function readForceValuesFromStdIn, read a list of alternating double values (left-hand side team, right-hand side team) for the exact magnitude of the force applied by each wizard on each team
Validate the forces read using your validForces() function, if
a single team has an invalid force, print the name of the team [space] “has an invalid force!” to standard output and exit from the program with return 1
if both teams have an invalid force, print “both teams have an invalid force!” to standard output and exit the program with return 1
Calculate the net force using your calculateForce() function
Examples
The force values (in kiloNewtons) are given to you as an array of non-negative real numbers. Print which team will win the competition.
Input
2
Gryffindor
Slytherin
4.56 3.21 15.57 5.32
Expected Output
Gryffindor wins!
Input
2
Gryffindor
Hufflepuff
1.51 7.01 3.27 5.32
Expected Output
Hufflepuff wins!
Input
2
Gryffindor
Slytherin
1.0 2.0 3.0 4.32
Expected Output
Tie.
How we'll approach the solution :
We divide our functionalities into 3 files.
1. We create a header file "functions.h" which will create the declarations of all the functions we're going to define and use.
2.We create "functions.cpp" to define the functions declared in header file.
3. Main cpp file is where we use all the functions we've created.
Header File Code (functions.h) :
functions.cpp Code :
Main function code :
Hope this helps.
Get Answers For Free
Most questions answered within 1 hours.