Solve C++ Program Issue: Compute Square Root & Compare Difference >50

In summary, the conversation is about a person trying to teach themselves C++ and coming across an old C++ problem that they are struggling with. The problem involves constructing a program that computes the square root of a number and compares the difference between two inputs. The goal is to terminate the program if the difference is greater than 50. The conversation includes a snippet of code that is giving the person an error, and an expert offers a solution and explains how to use while loops. The conversation ends with the person thanking the expert for their help.
  • #1
Lykin
4
0
I'm attempting to teach myself C++, and I came across this old C++ problem in my closet. It's a ripped page and I'm not sure what book it's from so it has no solution. It's easy, but I seem to have issues with while loops. If someone would explain the logic or show me an example as to how they would do it that would be much appreciated.

The problem:
-Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

I have something like this:

Code:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>

double positivePrint (double num, double square);

int main ()
{
    double num, square;
    
    positivePrint(double num, double square);
}

double positivePrint (double num, double square)
{
       num==1;
       while(num>0, num++);
       {
                    square=sqrt(num);
                    cout << square << endl;
       }
}
       }
}

But that gives me an "expected primary expression before" error and I'm not really sure where to proceed from here anyhow. Any help is appreciated and thanks in advance.
 
Technology news on Phys.org
  • #2
you have an extra

Code:
 }
}
at the end. == is a comparison, not an assignment. having a ; after the num++) ends the while loop there. you arent returning a value from positivePrint or main. Also look up passing by reference , cause i think your code is intending to do that.

for Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

One implementation is:

Code:
#include <iostream>
#include <math.h>

int main(void)
{
double num1=0;
double num2=0;
std::cout << "Enter num1: ";
std::cin >> num1;
std::cout << "Enter num2: ";
std::cin >> num2;
double sqrtnum1=sqrt(num1);
double sqrtnum2=sqrt(num2);
std::cout << "The difference of sqrt of both nums is " << fabs(sqrtnum1-sqrtnum2);
if (fabs(sqrtnum1-sqrtnum2) >50)
{
return 1;
}
return 0;
}

or something like that. Terminates with 1 if the difference is greater than 50, terminates with 0 if it isnt.
 
Last edited:
  • #3
Thanks for the quick response.

I didn't notice the extra braces and using == instead of = was just dumb, but that fixed my syntax error. I read through your code and understand what you're doing - I think I was trying to do to much by trying to call another function in main. However, I still don't really understand how to implement and use while loops.

Not on my home PC now, but I plan on trying a variation of your code when I get home. Once again I thank you for your quick help.
 
  • #4
You also appear to have "n== 1" where you surely intended "n= 1".
 
  • #5
DrKn - why you didn't intend code? Did you have too much experience with fortran 60?
 

Related to Solve C++ Program Issue: Compute Square Root & Compare Difference >50

1. What is the purpose of computing the square root in a C++ program?

The purpose of computing the square root in a C++ program is to find the value that, when multiplied by itself, gives the original number. This is often useful in mathematical calculations and can also be used to solve certain problems in programming.

2. How is the square root calculated in C++?

In C++, the square root can be calculated using the sqrt() function from the cmath library. This function takes the number whose square root needs to be calculated as an argument and returns the square root as a double value.

3. What is the significance of comparing the difference between the square root and a given number?

Comparing the difference between the square root and a given number allows us to determine how close the square root is to the original number. This can be useful in checking the accuracy of the square root calculation or in solving certain problems that involve finding an approximate value.

4. Why is the difference between the square root and a given number compared to 50?

The number 50 is commonly used as a benchmark for comparison because it is a relatively large value that can easily show the difference between two numbers. If the difference between the square root and a given number is greater than 50, it indicates that the square root is significantly different from the original number.

5. How can I use the result of the square root calculation and comparison in my C++ program?

The result of the square root calculation and comparison can be used in various ways in a C++ program. For example, it can be used to validate user input, to improve the accuracy of a mathematical calculation, or to solve a specific problem that requires the use of square roots and comparisons.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
980
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
14
Views
31K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
7
Views
34K
Back
Top