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

  • Thread starter Thread starter Lykin
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
The discussion revolves around a C++ programming problem that involves calculating the square root of a number and comparing the differences between successive inputs. The user is struggling with while loops and syntax errors in their initial code attempt. Key issues identified include using the comparison operator '==' instead of the assignment operator '=', an incorrect while loop syntax, and the presence of extra braces that disrupt the code structure. A suggested solution is provided, demonstrating a straightforward implementation that prompts the user for two numbers, calculates their square roots, and checks if the absolute difference exceeds 50. If it does, the program terminates with a return value of 1; otherwise, it returns 0. The user acknowledges the corrections and expresses a desire to understand while loops better, indicating plans to experiment with the provided code upon returning to their home computer. The conversation highlights common pitfalls in C++ programming for beginners and the importance of syntax accuracy.
Lykin
Messages
4
Reaction score
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
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:
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.
 
You also appear to have "n== 1" where you surely intended "n= 1".
 
DrKn - why you didn't intend code? Did you have too much experience with fortran 60?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
5
Views
3K
Replies
30
Views
6K
Replies
14
Views
34K
Replies
7
Views
35K
Replies
15
Views
4K
Back
Top