Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have created a C++ which compiles successfully, but when I run it I get c=0.
Can anybody explain why?
int main()
{
double U0, U, C, A, B, D;
U = 0.2;
A = U/U0;
B = 1+1/(16*pow(A, 2));
D = pow(B, 2)-(1/4)*A;
for (U0=0.2; U0<=1; U0=U0+0.2)
{
if (U <= (4*U0))
{
C= (1/2)*(B+sqrt(D));
cout <<" | U0 | "<< U0 <<" | U | "<< U <<" | C^2 | "<< C << endl;
U = U + 0.2;
}
}
return 0;
}
Because of these kind of statements:
C= (1/2)*(B+sqrt(D));
C++ interprets 1/2 as an integer operation (not a floating one), hence 1/2 = 0 (for integers)
This is an error everybody has done once in his life!
After you will always write something like 1/2. with the dot to force a division using the double type.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
cin >> a >> b >> n;
int ans=0;
c=max(a,b);
d=min(a,b);
while(n>c)
if(d+c>n) {
ans++;
break;
}
cout << ans;
}
why if I insert 1,2,2 as input the result will be 0 instead of one
If you had a debugger that you could step through the code with, the mistake would have been easy to find.
When you get to the while loop, a = 1, b = 2, n = 2, c = 2, d = 1 and ans = 0.
Since the condition n > c is false (because !(2 > 2)) the body does not get executed and you get what you started with.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I recently started learning how to program and one of the tasks I was assigned was to write an algorithm in c++ that would ask for three numbers, then pick out the smallest one. A segment of the code I wrote is as follows.
if (a <= b) { a = d; }
else { b = d; }
This is supposed to pick out the smallest of the first two numbers and then "link it" to d, but after a while of getting the wrong results I realized that regardless of the values of a and b it gives d a value of 0.
Turns out this only works if I write the same thing with the characters flipped, with the input variables coming last:
if (a <= b) { d = a; }
else { d = b; }
Why is that? Is there something about the syntax of c++ that I should know?
Seems like a strangely specific rule to have...
EDIT: I accidentally wrote {a = d} instead of {d = a} when writing this post, it should as I intended now.
Here's the rest of the code (it works, but only after flipping the characters as I said earlier)
#include <iostream>
using namespace std;
int main() {
int a,b,c,d,min;
cin >> a >> b >> c;
if (a<=b) {d = a;}
else {d = b;}
if (c<=d) {min = c;}
else {min = d;}
cout << min << endl;
}
In the first code fragment, you are not assigning to d at all. Assignment (=) operator assigns the rvalue (right hand side) to the lvalue (left hand side). This is very standard syntax.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm really fresh to C++, and I faced difficulties with the next question :
Write a program that asks the user to enter a number : n
THEN calculate f(n).
Notice that :
f(0)=3 and f(n+1) = 3/4 * f(n) + 4 ?
For example :
f(1)= 6.25
f(2)= 8.69
f(3)= 10.52
f(4)= 11.89
f(5)= 12.92
====================================================
So, how can I solve this?
thanks for all..
I try this code depending on Mr. paxdiablo answer :
#include<iostream>
using namespace std;
int main()
{
double x=0.0;
cout<<" enter an integer N:";
cin>> x;
double f1(double x)
{
if x==0.0
return 3;
return 3 / 4 * f1 (x-1) + 4;
}
return 0;
}
But the program never runs!
================================================
The Correct Solution is :
#include<iostream>
#include<iomanip> //To enable "setprecision" tool
using namespace std;
double f(int x){
if (x==0)
{return 3;}
return (3.0/4.0) * f(x-1) + 4.0; //we add zeros to get "double" results
}
int main()
{
int n=0;
cout<<" Please, enter an integer :";
cin>> n;
cout<<fixed<<setprecision(2)<<f(n); //"setprecision" used to get only two digits after the point
return 0;
}
BIG thanks to everyone gave me a hand and special thank to Mr. paxdiablo.
This sounds like a job for ... Recursion Man!
Simply define a recursive function that returns 3 when n is zero or calls itself with a reduced problem.
Pseudo-code would be something like
def f(x):
if x == 0:
return 3
return 3 / 4 * f(n-1) + 4
You can use std::cin >> dv to get a value from the user and that function (recoded in C++) to do the grunt work. Then std::cout << result to output the result. Just remember to use doubles rather than ints, including constants like 3.0.
You have to define the recursive function f, then read the input from the user, then apply the function to the input you've read (probably as an integer?), and print the output.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can we calculate the value/degree of power exponent of a certain number?
I mean if it's like a^n = b, then how can we calculate n?
For example assume that a = 2 and b = 8, then how can we calculate that n = 3? Is there any special function?
Use std::log. Example from the reference page:
#include <cmath>
#include <iostream>
int main()
{
double base = 2.0;
double arg = 8.0;
double result = std::log(arg) / std::log(base);
std::cout << result << '\n'; // prints 3
}
More to learn at wikipedia.
What you are looking for is the logarithm of b to the base a (at least we call it that in german).
C++ example:
#include <cmath> /* log */
int main ()
{
int a = 2;
int b = 8;
float n = log(b) / log(a); // 3
}
Well, you can use the same logarithmic functions here too. Include cmath.
In-code
.
.
cout << log(8) / log(3) << endl;
.
.
Output
.
.
.
...3...
.
.
.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
How can I get non-trival solution of M*x=0 using Eigen linear algebra library.
I tried this but solution is all zeros.
Matrix<float,2,3> m ;
Matrix<float,2,1> y ;
m << 2 , 3 ,5 , -4 , 2, 3;
y(0,0) = 0;
y(1,0) = 0;
cout << "Here is matrix m:" << endl << m << endl;
cout << "Here is matrix y:" << endl << y << endl;
cout<<"solution: \n"<<m.fullPivLu().solve(y);
You are trying to solve M * x = 0, so it makes sense to return x=0. If you want to avoid this trivial solution, then you have to add additional constraints. For instance you might say that you want to minimize |M * x|^2 subject to |x|=1 in which case you end up with an eigenvalue problem (through Lagrange multipliers). Your solution is the eigenvector corresponding to the minimal eigenvalue. Using Eigen:
Matrix3f A = m.adjoint() * m;
Vector3f x = SelfAdjointEigenSolver<Matrix3f>(A).eigenvectors().col(0);
Here I get:
x = 0.032739 0.851202 -0.523816
and m * x is in the order of 1e-16.
You want to find a nonzero element in the kernel of your matrix (the kernel is the set of x such that Mx = 0), look in Eigen for a decompositon offering a kernel() method, for instance fullPivLu does:
http://eigen.tuxfamily.org/dox/classEigen_1_1FullPivLU.html#a6e8f1d2fcbd86d3dc5a8a013b6e7200a