Question

Using c++ You may #include only: <iostream> <cmath> “functions.h” Write the function void readForceValuesFromStdIn(double* leftTeam, double*...

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

    • Output the winner using your printWinnerToStdOut() 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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
Your assignment is to write a c++ function that can calculate values for any 5 th...
Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5 Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x). The second parameter should be an array with 6 elements that contain the coefficients a0 to a5. The output of your function should be the...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
**C code only In this part, you will write a simple user-defined function that prints a...
**C code only In this part, you will write a simple user-defined function that prints a message. This function does not require any parameters or return value. The purpose is simply to get you started writing functions.Open repl project Lab: User-Defined Functions 1. Write a program that calls a function. This function should do the following: 1.Ask the user their name 2. Print a "hello" message that includes the user's name Example output: Please enter your name: ​Mat Hello, Mat!...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT