Question

Complete this program, by adding a function called mag( ) to calculate the magnitude of a...

Complete this program, by adding a function called mag( ) to calculate the magnitude of a vector. You'll need to add a prototype before main( ), and the implementation (header & body) of the function after main( );

reminder: mag = sqrt (x 2 + y 2)

Calling it with something like:

            vec_length = mag(x, y);

Name your program functions1.cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the running code of your problem with output snippent, please go through the comments for better understanding

#include <iostream>
#include<math.h>
using namespace std;
double mag(int, int); // Prototype declaration
int main() {
   int x, y;
   cout<<"Enter the value of X: "; // input x
   cin>>x;
   cout<<"Enter the value of Y: "; //input y
   cin>>y;
   double vec_length = mag(x, y); // function call to mag
   cout<<vec_length; //print vec_length
   return 0;
}
double mag(int a, int b){ //Function definition of mag
   double magnitude;
   int z= (a*a)+(b*b);
   magnitude= sqrt(z); // calculate magnitude
   return magnitude; //return value
  
}

Output:

EAPinnacle Personal\Study Placement Coding\mag vector.exe nter the value of R: 8 Enter the value of Y 6 Process exited after

Add a comment
Know the answer?
Add Answer to:
Complete this program, by adding a function called mag( ) to calculate the magnitude of a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because ...

    This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because we do some thing like this on exams. All you will need to submit is your member function code BUT your code should compile and run if I were to add it to ArrayBag.h. That means you should put your code in ArrayBag.h and make sure it compiles before submitting. You should test with a simple main. DO NOT submit main or your ArrayBag.h file just submit...

  • Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...

    Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h). The RemoveRandom member function should remove a random entry from the list (so use your srand and rand functions). Do not forget to TEST... that means you will need to add the prototype and test with a simple main. All you need to submit though is the member function implementation Add an overloaded constructor to the ArrayBag class (so add it to 2. ArrayBag.h)....

  • We want to write a function called sortTwo that takes two integer parameters, and after the...

    We want to write a function called sortTwo that takes two integer parameters, and after the function is called the first parameter is the smaller of the two values, and the second parameter is the larger of the two values. In other words, this is a miniature sorting function. For this problem you must supply the missing code for PARTs 1, 2, and 3 in the program below. // PART 1: function prototype int main ( ) { int x,y;...

  • I need the real code for this program: (convert pseudocode to C++) ——————————————————— Start Program Type...

    I need the real code for this program: (convert pseudocode to C++) ——————————————————— Start Program Type Vector is Structure Defined x is double variable y is double variable End Vector Type Function Prototype Get Choice(parameters: int &amp;) Function Prototype vAddition (parameters: vector v1, vector v2, vector &amp;v3) Function Prototype vSubstract(parameters: vector v1, vector v2, vector &amp;v3) Function Prototype sMult(parameters: int k, vector v, vector &amp;v3) Function Prototype sProduct (parameters: vector v1, vector v2) Function Prototype getMag (parameter: vector v) Function...

  • 2. Write a program with three functions that will be called from the main function The functions will calculate the...

    2. Write a program with three functions that will be called from the main function The functions will calculate the volume, area and perimeter of a cuboid. You should able to pass three parameters such as length, width and height, and a function will return area, another will return volume and final one will return perimeter. Define all your variables in floating point numbers 3. Modify the problem#2 to calculate the volume, area and perimeter in a sing le function...

  • On Dev C++ Using the header file provided below "myClock.h", implement a C++ program that present...

    On Dev C++ Using the header file provided below "myClock.h", implement a C++ program that present the user with the following menu: "Press 1 to read the time." "Press 2 to enter a new time." "Press 3 to exit." -------------------------------------------------------------- Submit both file, myClock.h and your implementation .cpp myClock.h class myClock { public: myClock(); ~myClock(){}; void setTime(int, int, int); // Add the getter function prototype here . private: int hour; int minute; int second; };

  • The class declaration (interface) and the function definitions (implementation) must be in separate files - the...

    The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private. Write a class called BankAccount that has: a string data member called customerName, a string data member called customerID, and a double data member called customerBalance a constructor that takes two strings and a double (name, ID, balance) and uses them...

  • Create a class called planet with private members name, percent02, temp. Create a constructor prototype and...

    Create a class called planet with private members name, percent02, temp. Create a constructor prototype and implementation. In the main, create 2 objects of class planet, one with arguments and the other without. Create a function to change name, percent02 and temp after the object has been created and a function that displays the current values of the members. Implement the operations specified above in the main of your program. This is all in C++, please help.

  • Write a program called problem1.cpp that produces a table showing sample point co-ordinates on a parabola....

    Write a program called problem1.cpp that produces a table showing sample point co-ordinates on a parabola. The program should contain a function that takes in a given value of x and produces the corresponding value of y according to the equation y=x^2+x+1. The main program should then range the x values from -10 to 10, printing all the x and y co-ordinates of the corresponding points on the screen.(15 points)

  • 1. Which of the following appear in a function header (the first line of a function...

    1. Which of the following appear in a function header (the first line of a function definition/implementation) a. The function name, return data type, argument data types, and argument names. b. The function name, return data type, and argument names 2. When a function is finished executing, it ALWAYS branches back to main, regardless of where it was called from in the program. (True or False) c. The function name, return data type, and argument data types

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT