The output should be "The distance is 0" (The expected).
==================== YOUR OUTPUT =====================
0001: Enter~the~x~coordinate~for~point~1:~0
0002: Enter~the~y~coordinate~for~point~1:~0
0003: Enter~the~x~coordinate~for~point~2:~0
0004: Enter~the~y~coordinate~for~point~2:~0
0005: The~distance~is~-nan
=================== MISMATCH FOUND ON LINE 0005: ===================
ACTUAL : The~distance~is~-nan
EXPECTED: The~distance~is~0
======================================================
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
float squareRoot(float s) {
float xn;
xn = s / 2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/xn))/2.0;
counter = counter + 1;
}
return xn;
}
int square(int what){
int answer = what * what;
return answer;
}
float distance (int x1, int y1, int x2, int y2) {
int diffx, diffy, sqx, sqy, total;
diffx = x2-x1;
diffy = y2 - y1;
sqx = square(diffx);
sqy = square(diffy);
total = sqx + sqy;
float dist = squareRoot(total);
return dist;
}
int main() {
int x1, y1, x2, y2;
cout<<"Enter the x coordinate for point 1: ";
cin>>x1;
cout<<x1 <<endl;
cout<<"Enter the y coordinate for point 1: ";
cin>>y1;
cout<<y1 <<endl;
cout<<"Enter the x coordinate for point 2: ";
cin>>x2;
cout<<x2 <<endl;
cout<<"Enter the y coordinate for point 2: ";
cin>>y2;
cout<<y2 <<endl;
float dist = distance(x1, y1, x2, y2);
cout<<"The distance is ";
cout<<dist <<endl;
return 0;
}
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
float squareRoot(float s) {
float xn;
xn = s / 2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/xn))/2.0;
counter = counter + 1;
}
return xn;
}
int square(int what){
int answer = what * what;
return answer;
}
float distance (int x1, int y1, int x2, int y2) {
int diffx, diffy, sqx, sqy, total;
diffx = x2-x1;
diffy = y2-y1;
sqx = square(diffx);
sqy = square(diffy);
total = sqx + sqy;
if (total==0) return 0; //USE THIS CONDITION TO DEAL WITH
TOTAL==0
float dist=squareRoot(total);
return dist;
}
int main() {
int x1, y1, x2, y2;
cout<<"Enter the x coordinate for point 1: ";
cin>>x1;
cout<<x1 <<endl;
cout<<"Enter the y coordinate for point 1: ";
cin>>y1;
cout<<y1 <<endl;
cout<<"Enter the x coordinate for point 2: ";
cin>>x2;
cout<<x2 <<endl;
cout<<"Enter the y coordinate for point 2: ";
cin>>y2;
cout<<y2 <<endl;
float dist = distance(x1, y1, x2, y2);
cout<<"The distance is ";
cout<<dist <<endl;
return 0;
}
The output should be "The distance is 0" (The expected). ==================== YOUR OUTPUT =====================
C++ code for CIS054 Create a program that uses a function to determine the length of a line by inputting the X,Y coordinates of the line endpoints. Show the result with a precision of four decimal places. The function prototype is to be specified as follows: double LengthOfLine (double X1, double Y1, double X2, double Y2); // returns length of line by using this code: #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(int argc, char* argv[])...
C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...
Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...
The following is solved using C++ in Vocareum: In this problem, you will be prompting a student for grades and credits for courses taken. From that, you will calculate a GPA. This site: http://www.back2college.com/gpa.htm shows you how to calculate a GPA. Keep prompting the student if they want to add more courses. IMPORTANT NOTES! The course name is prompted for, but nothing is done with it. We are just using the grades A, B, C, D, and F so you...
Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...
This question is about calculating and printing payslips. User inputs his name, number of worked hours and hourly rate. below is the source file for my program...It's supposed calculate salaries. Given that a work week has 40 hours and over time is 1.5xnormalRate for each our of overtime My output is not working. what is wrong with this code? // Calculate and print payslips #include <iostream> #include <iomanip> using namespace std; const float workingHours = 40.0; void getData( string &employeeP,...
Code is in C++: Im wondering how i can make the program continue to ask the user to enter Y/N with the if statment. Is it possible with an If statment? #include <iostream> using namespace std; int main() { float usDollars,cYuan; float *Dollars; char choice; Dollars = &usDollars; while(usDollars >= 0){ cout <<"Enter the amount in U.S Dollars: "; cin >> usDollars; cout << usDollars<< " U.S Dollar in Chinese Yuan is :"<<*Dollars*7.09<<endl; if (choice == 'y' || choice ==...