I have a problem, and I don't know how to deal with it. I made a simple calculator with few functions. Every option is working fine, but I have problem when it comes to quadratic equation. When I put any numbers the response that I get is "The roots of the equations are : x=1.79599e-307. I don't know to deal with it - can you help me ? I will post parts of my code that includes the problem. Thanks You for any feedback and help!
main:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "kalkulator.cpp"
...
using namespace std;
case 10:
cout<<"\nQuadratic Equation \n";
cout<<"\nSelect number \n";
cin>>a;
cout<<"\nSelect number \n";
cin>>b;
cout<<"\nSelect bumber \n";
cin>>c;
int r;
r=equation(a, b, c, x1, x2);
if (r==0) cout << "\n No roots\n" << endl;
else if (r==1) cout << "\n 1 root x = " << x1 << endl;
else if (r==2) cout << "\n 2 roots x1 = " << x1 << " and x2 = " << x2 << endl;
break;
kalkulator.cpp
#include "kalkulator.h"
using namespace std;
...
double equation(double a, double b, double c, double x1, double
x2)
{
double delta=b*b-4*a*c;
if (delta<0.0)
{
return 0;}
if (delta==0.0)
{
x1=-b/(2*a);
return 1;}
else
{
delta=sqrt(delta);
x1==(-b-delta)/(2*a);
x2==(-b+delta)/(2*a);
return 2;}}
kalkulator.h
#ifndef kalkulator_H
#define kalkulator_H
class kalkulator
{
private:
double a;
double b;
double c;
double x1;
double x2;
public:
double equation(double, double, double, double, double);
};
#endif
If you want to set x1 and x2 through the supplied parameters, you need to pass them by reference:
double equation(double, double, double, double&, double&);
and
double equation(double a, double b, double c, double & x1, double & x2)
Also, in the last lines of your code, "x1==" and "x2==" should be "x1=" and "x2=".
Related
Assume that the given input are 5,+,5 in C.
ex)
5
+
5
And I would like to get the answer 10.
I thought code like the below.
#include
using namespace std;
int main(){
char a,b,symbol;
cin >> a >> symbol >> b;
printf("%d", (a-'0') symbol (b-'0'));
return 0;
}
Expected value is 10. However, I got an error in symbol
How would I get the solution?
There are multiple things that I noticed:
First, you are printing integer, but a and b are both chars.
Knowing that there are only a few symbols that you can use, maybe a switch case would be more appropriate?
int main(){
int a, b;
char symbol;
switch(symbol){
case('-'): print("%d", a - b);
case('+'): print("%d", a + b);
case('*'): print("%d", a * b);
case('/'): print("%d", a / b);
case('%'): print("%d", a % b);
default : print("Not a known symbol");
}
Second: Why are you substracting a from the character 0?
I'm writing a Lex code for scanning Verilog code, and I define a pattern that I want to match. But when I run the code, I find that the pattern I defined won't match. Where did I make a mistake?
I run the code on a Linux workstation. The flex version is 2.5.4.
%option c++
%option noyywrap
%{
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<string> input_vec; /*To remember which signal is input*/
int input_num = 0; /*To count the # of input signals*/
%}
%x INPUT
var_string [a-z]+
%%
input {
cout << "BEGIN INPUT" << endl;
BEGIN INPUT;
}
[ \n\t\r\f]+
.
<INPUT>{var_string} {
cout << "IN INPUT MATCHING var_string:" << yytext << endl;
input_num++;
input_vec.push_back(yytext);
}
<INPUT>; {BEGIN 0;}
<INPUT>,
<INPUT>[ \n\t\r\f]+
%%
int main(int argc, char* argv[])
{
ifstream input_file("test.v");
FlexLexer* lexer;
lexer = new yyFlexLexer(&input_file, &cout);
while(lexer->yylex()!=0);
cout << "Input Number: " << input_num << endl;
return 0;
}
Here is the Verilog code I want to scan.
module test(a, b, c, sum, carry);
input
a,
b,
c;
output
sum,
carry;
wire
d;
assign
d = (~b & a) | (b & ~a),
sum = (d & c) | (d & ~c),
carry= (a & b) | (b & c) | (a & c);
endmodule
I except that when the Lex code scan a, b, and c in input section, the output will be:
BEGIN INPUT
IN INPUT MATCHING var_string:a
IN INPUT MATCHING var_string:b
IN INPUT MATCHING var_string:c
Input Number: 3
However, the real output is:
BEGIN INPUT
Input Number: 0
I want to write the following matlab code in Eigen (where K is pxp and W is pxb):
H = (K*W)>0;
However the only thing that I came up so far is:
H = ((K*W.array() > 0).select(1,0));
This code doesn't work as explained here, but replacing 0 with VectorXd::Constant(p,0) (as suggested in the link question) generates a runtime error:
Eigen::internal::variable_if_dynamic<T, Value>::variable_if_dynamic(T) [with T = long int; int Value = 1]: Assertion `v == T(Value)' failed.
How can I solve this?
You don't need .select(). You just need to cast an array of bool to an array of H's component type.
H = ((K * W).array() > 0.0).cast<double>();
Your original attempt failed because the size of your constant 1/0 array is not match with the size of H. Using VectorXd::Constant is not a good choice when H is MatrixXd. You also have a problem with parentheses. I think you want * rather than .* in matlab notation.
#include <iostream>
#include <Eigen/Eigen>
using namespace Eigen;
int main() {
const int p = 5;
const int b = 10;
MatrixXd H(p, b), K(p, p), W(p, b);
K.setRandom();
W.setRandom();
H = ((K * W).array() > 0.0).cast<double>();
std::cout << H << std::endl << std::endl;
H = ((K * W).array() > 0).select(MatrixXd::Constant(p, b, 1),
MatrixXd::Constant(p, b, 0));
std::cout << H << std::endl;
return 0;
}
When calling a template member function in a template, you need to use the template keyword.
#include <iostream>
#include <Eigen/Eigen>
using namespace Eigen;
template<typename Mat, typename Vec>
void createHashTable(const Mat &K, Eigen::MatrixXi &H, Mat &W, int b) {
Mat CK = K;
H = ((CK * W).array() > 0.0).template cast<int>();
}
int main() {
const int p = 5;
const int b = 10;
Eigen::MatrixXi H(p, b);
Eigen::MatrixXf W(p, b), K(p, p);
K.setRandom();
W.setRandom();
createHashTable<Eigen::MatrixXf, Eigen::VectorXf>(K, H, W, b);
std::cout << H << std::endl;
return 0;
}
See this for some explanation.
Issue casting C++ Eigen::Matrix types via templates
For example we have expression using rdivide in Matlab:
B = bsxfun(#rdivide, A, A(4,:));
How can we write equavalent expression for opencv?
Opencv has divide function, but seems it can't be used for matrix with different dimensions:
Mat t1= Mat::ones(2,3,CV_64FC1);
Mat t2= Mat::ones(1,3,CV_64FC1);
Mat dst;
divide(t1,t2,dst);
this don't work, so we need to replicate one row to matrix to match dimensions of t1 or use divide with 1 row in cycle.
My solution for opencv(A modified inplace):
for(int i=0;i<A.rows;++i)
{
divide(A.row(i),A.row(3),A.row(i));
}
Is there any simpler way?
You can use the repeat function of OpenCV to replicate a matrix.
The equivalent OpenCV code for the above mentioned MATLAB command is following:
cv::Mat B = A/cv::repeat(A.row(3),4,1);
In addition to #sgarizvi solution, you may find this wrapper to Matlab rdivide helpful:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat rdivide(const Mat& A, const Mat& B)
{
int nx = A.cols / B.cols;
int ny = A.rows / B.rows;
return A / repeat(B, ny, nx);
}
Mat rdivide(const Mat& A, double d)
{
return A / d;
}
int main()
{
Mat1f A = (Mat1f(3, 5) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
Mat B = rdivide(A, A.row(2)); // Divide by matrix, works also for cols: e.g. A.col(2)
Mat C = rdivide(A, 2); // Divide by scalar
cout << "A: " << endl << A << endl << endl;
cout << "B: " << endl << B << endl << endl;
cout << "C: " << endl << C << endl << endl;
return 0;
}
I want to convert integers to double in the following piece of code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a , b;
double c;
cout<<"Enter two integers: ";
cin>>a>>b;
try
{
if (b == 0)
throw 0;
}
catch (int a)
{
cout<<"You made a division by zero?"<<endl<<a;
}
c = static_cast<double>(a/b);
cout<<"The division is: "<<fixed<<setprecision(2)<<c;
}
How to change the code to output the double value?
As mentioned you should cast not a result of division but variables itself, to get double value. All three options works.
int a, b;
double c;
std::cout << "Enter two integers: ";
std::cin >> a >> b;
try
{
if (b == 0)
throw 0;
}
catch (int a)
{
std::cout << "You made a division by zero?" << std::endl << a;
}
c = static_cast<double>(a) / b;
c = a / static_cast<double>(b);
c = static_cast<double>(a) / static_cast<double>(b);
std::cout << "The division is: " << std::fixed << std::setprecision(2) << c;
std::cin >> a >> b;
You are casting the result of the division, you should cast the operands instead.
Don't use an exception to catch a condition that you already catch using a simple if. Just use the if, and use an else to skip the division if the second operand is zero.
int a , b;
double c;
cout<<"Enter two integers: ";
cin>>a>>b;
if (b == 0) {
cout<<"You made a division by zero?"<<endl;
} else {
c = static_cast<double>(a) / static_cast<double>(b);
cout<<"The division is: "<<fixed<<setprecision(2)<<c;
}