sqrt function doesn't evaluate correct value [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm newbie in C++ and I'm trying to evaluate a square root. I've written the following:
#include <math.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
double a;
a=sqrt(2.0);
printf("Square root from 2 is %d\n",a);
return 0;
}
But output is Square root from 2 is 1719614413. I really don't understood this. Please explain me.

You are using C (which mostly compiles as C++ as well), not C++ and you made a mistake doing so. You can either learn C or use C++, where it's harder to make those mistakes:
#include <cmath>
#include <iostream>
int main()
{
double a = std::sqrt(2.0);
std::cout << "Square root from 2 is " << a << std::endl;
return 0;
}
Additional explanation:
printf("Square root from 2 is %d\n",a);
This prints a string and expects an integer (%d) to be passed. You passed a double, wich comes out as garbage. You can use (%lf) for doubles.

If you still want to stick to our example, please consider using %f rather than %d:

Related

Why am i getting wrong ans(rather 2 different answers) when comparing 2 strings [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When normally comparing the string this way:
#include <iostream>
using namespace std;
int main()
{
string a = "yb";
string b = "ya";
cout<<(a>b);
return 0;
}
result comes out 1. Which is right.
But when performing the same operation in this way:
#include <iostream>
using namespace std;
int main()
{
cout<<("yb">"ya");
return 0;
}
result is coming out 0.
How is this possible?
"yb" and "ya" have type char const[3]; they are arrays located somewhere in the memory containing the chars in the string literal and the terminating 0 char.
When doing ("yb">"ya") those objects decay to char const* and you're comparing pointers. Where the data is stored is compiler implementation defined and you cannot rely on the result.
To compare std::strings, you'd need to write
std::cout<<(std::string("yb")>std::string("ya"));
instead.

Why does the Sleep() function behave like this? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm playing around with the Sleep() function in C++ right now, and I am not understanding why this code operates the way it does.
I made a program to find the difference between two times, but it is not working as expected.
#include <iostream>
#include <chrono>
#include <Windows.h>
using namespace std;
int main() {
int timeA = (int)chrono::system_clock::now;
cout << timeA << "\n";
for (int i = 0; i < 5; i++) Sleep(1000);
int timeB = (int)chrono::system_clock::now;
cout << timeB << "\n";
int timeDifference = timeB - timeA;
cout << timeDifference;
cin.get();
return 0;
}
It seems as if the program is setting the variables at the same time, and then sleeping. Is this the case? If so, help me to understand why, please.
now is a function, not an attribute or variable. You failed to call it, and are casting the function pointer itself to int, which will always produce the same value for a given run (on a typical 64 bit system, the low 32 bits of the address where now is located).
Change both lines to use chrono::system_clock::now(), not chrono::system_clock::now.
Note that this is one of the reasons to avoid C-style casts, as well as a reason to compile with warnings turned up; it protects you from casting to wildly incorrect end results, without at least some sort of alert.

c++ help me to understand why there is no output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
this program is simple :
1)take an input string .
2) convert it to long .
3) print convert result.
expected an output,but nothing found.
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string ch;
scanf("%s",ch);
long l=stol(ch);
printf("%l",l);
return 0;
}
Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.
#include <iostream>
#include <string>
int main()
{
std::string input;
std::cin >> input; // take an input string
long lval = stol(str); // convert to long
std::cout << lval << '\n' // print the result
}
Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn C++.

printf working fine when kept outside of for loop but causes some error while running when kept in the for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.

Returning boolean value does not print anything and doesn't show any compiler errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is a function to check a number for a power of two. Although the compiler does not print anything, it's also not showing any errors. Please let me know if the logic is correct or not.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
bool isPowerofTwo(long long n)
{
// Your code here
for (int i = 1; i <=n; i<<1)
{
if(i==n){
return true;
}
}
return false;
}
int main()
{
cout << isPowerofTwo(2);
return 0;
}
The expression i<<1 in the third statement (the "iteration expression") of your for loop doesn't actually do anything (that is, it doesn't modify the value of i). You need, instead, i<<=1 (or, in 'long form', i = i << 1).
Also, please read: Why should I not #include <bits/stdc++.h>?. In your code, the only standard header you need is #include <iostream>. Another good post to read is: Why is "using namespace std;" considered bad practice?.