So I have this problem pretty much right, except that white space, highlighted i
ID: 3858822 • Letter: S
Question
So I have this problem pretty much right, except that white space, highlighted in yellow. I don't know what I added to get that, but I need to get rid of that to get the answer right. I have posted my code below. Only get rid of rthe white space. DO NOT change anything else for this answer is correct besides the whitespase issue.
Thanks!
***************
#include <iostream>
#include <string>
using namespace std;
//Function that calculates square root of a function
float squareRoot(float num)
{
float result;
int i;
//Return 0 if num is 0
if (num == 0)
return 0;
//Calculating half the number
result = num/2.0;
//Iterate and accumulate the result for calculating square root of a function
for(i=1; i<=10; i++)
result = (result + (num/result))/2.0;
//Returning result
return result;
}
//Function that calculates square of a function
float square(float num)
{
//Returning square value
return num * num;
}
//Main function
int main()
{
float sideA, sideB, sideC;
string tryAgain = "yes";
//Loop till user wants to stop
while(tryAgain == "yes")
{
//Reading first side
cout << " Enter side A: ";
cin >> sideA;
cout << sideA;
//Reading second side
cout << " Enter side B: ";
cin >> sideB;
cout << sideB;
//Calculating third side
sideC = squareRoot( square(sideA) + square(sideB) );
//Printing result
cout << " Side C is " << sideC;
cin.ignore();
//Reading user choice
cout << " Continue? ";
cin>>tryAgain;
cout<<tryAgain;
cout << " ";
}
return 0;
}
Explanation / Answer
You added space after
program:
#include <iostream>
#include <string>
using namespace std;
//Function that calculates square root of a function
float squareRoot(float num)
{
float result;
int i;
//Return 0 if num is 0
if (num == 0)
return 0;
//Calculating half the number
result = num/2.0;
//Iterate and accumulate the result for calculating square root of a function
for(i=1; i<=10; i++)
result = (result + (num/result))/2.0;
//Returning result
return result;
}
//Function that calculates square of a function
float square(float num)
{
//Returning square value
return num * num;
}
//Main function
int main()
{
float sideA, sideB, sideC;
string tryAgain = "yes";
//Loop till user wants to stop
while(tryAgain == "yes")
{
//Reading first side
cout << "Enter side A: "; //Space removed
cin >> sideA;
cout << sideA;
//Reading second side
cout << " Enter side B: "; //Space removed
cin >> sideB;
cout << sideB;
//Calculating third side
sideC = squareRoot( square(sideA) + square(sideB) );
//Printing result
cout << " Side C is " << sideC; //Space removed
cin.ignore();
//Reading user choice
cout << " Continue? "; //Space removed
cin>>tryAgain;
cout<<tryAgain;
cout << " ";
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-.2$ main
Enter side A: 3
3
Enter side B: 4 4
Side C is 5
Continue? yes yes
Enter side A: 33
33
Enter side B: 44 44
Side C is 55
Continue? no no
sh-4.2$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.