exchange the value of two variables - visual-c++

Write appropriate C++ code to exchange the value of two variables (for example first Variable and second Variable)
while
a.Using one additional variable
b. Without using any additional variable

The a.answer:
int a = 1, b = 5, c;
c = a;
a = b;
b = c;
std::cout << a << b;
system("pause");
The b.answer:
int a = 1, b = 5;
a = a + b;
b = a - b;
a = a - b;
std::cout << a << b;
system("pause");

You can write:
int a = 1, b = 5;
std::swap(a, b);

Related

How to use & (AND) operator in a signal in Circom

I'm trying to use & operator on a signal and get another signal in Circom circuit compiler language like so:
pragma circom 2.0.0;
template MAIN() {
signal input a;
signal output x;
signal v;
v <== 168;
x <== v & 31;
}
component main = MAIN();
I'm getting this error:
error[T3001]: Non quadratic constraints are not allowed!
┌─ "/Users/ilia/compiling/main-circom/main.circom":146:5
│
146 │ x <== v & 31; // 0b00011111
│ ^^^^^^^^^^^^ found here
│
= call trace:
->MAIN
How can I generate a constraint for x signal so that it is quadratic?
I did it with Num2Bits:
// the code bellow is a quadratic equivalent of:
// x <== v & 31; // 0b00011111
component num2Bits = Num2Bits(8);
num2Bits.in <== v;
signal vbits[8];
for(k = 0; k < 8; k++) {
vbits[k] <== num2Bits.out[k];
}
var lc1=0;
var e2 = 1;
for (var i = 0; i<5; i++) {
lc1 += vbits[i] * e2;
e2 = e2 + e2;
}
lc1 ==> x;

use of the comma-operator in a tested expression causes the left argument to be ignored when it has no side-effects

I got that warning when I defined 2 conditions for a for loop in C++. Here is the code
int a;
cin >> a;
int n;
cin >> n;
int b = pow(10, a - 1), c = pow(10, a - 1) * 1.2;
for (int i = b; i < c, i <= n; i++)
//do sth
Does anybody know what is this warning about?
Expression with comma operator returns value of the second part ( i <= n in your case ). Value of the first part is ignored.
Read here about comma operator:
https://en.wikipedia.org/wiki/Comma_operator
Use i < c && i <= n if you want both conditions to be true (or other operators depending on your task).

Need advice on my quadratic formula functions

Okay The program compiles, but i am getting wrong output. Any help would be awesome. i do understand there are many other examples on this topic and i have looked at them all. None seemed to have help me clearly understand.
#include <iostream>
#include <cmath>
using namespace std;
double quadplus(float a, float b, float c);
double quadminus(float a, float b, float c);
int main()
{
double a, b, c;
double discriminant;
cout << "Please Enter variables for a, b, and c." << endl << endl;
cout << "Enter number for variable a." << endl << endl;
cin >> a;
cout << "Enter number for variable b." << endl << endl;
cin >> b;
cout << "Enter number for variable c." << endl << endl;
cin >> c;
discriminant = (b * b) - (4 * a * c);
if(discriminant == 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant > 0)
cout << quadplus(a,b,c) << endl;
else if(discriminant < 0)
cout << quadminus(a,b,c) << quadplus(a,b,c) << endl;
return 0;
}
double quadplus(float a, float b, float c)
{
return ((-1 * b) + (sqrt(( b * b) - (4 * a * c))) / (2 * a));
}
double quadminus(float a, float b, float c)
{
return ((-1 * b) - (sqrt(( b * b) - (4 * a * c))) / ( 2 * a));
}
cout << quadplus << endl;
should probably be
cout << quadplus(a, b, c) << endl;
Also, if the discriminant is negative, your solutions will be
complex numbers of the form x + yi and your code doesn't properly account
for that. If discriminant != 0, you should probably print both roots.

verilog: how do I add parameters

I want to have parameterized module. It has the following definition:
module example (...);
parameter A = 2;
parameter B = 2;
parameter C = A + B;
endmodule
However, when I print out the parameter values, I get A = 2, B = 2, and C = 1... Any ideas why?
Found a solution - use localparam.
module example (...);
parameter A = 2;
parameter B = 2;
localparam C = A + B;
endmodule

How to use calcCovarMatrix on IplImage?

There's a single-channel grayscale IplImage whose covariance matrix is to be calculated. There does happen to be a similar question on SO but no one has answered it and the code is significantly different.
Here's the code that's throwing an "unhandled exception":
int calcCovar( IplImage *src, float* dst, int w )
{
// Input matrix size
int rows = w;
int cols = w;
int i,j;
CvScalar se;
float *a;
a = (float*)malloc( w * w * sizeof(float) );
long int k=0;
//image pixels into 1D array
for(i = 0; i < w; ++i)
{
for(j = 0; j < w; ++j)
{
se = cvGet2D(src, i, j);
a[k++] = (float)se.val[0];
}
}
CvMat input = cvMat(w,w,CV_32FC1, a); //Is this the right way to format input pixels??
// Covariance matrix is N x N,
// where N is input matrix column size
const int n = w;
// Output variables passed by reference
CvMat* output = cvCreateMat(n, n, CV_32FC1);
CvMat* meanvec = cvCreateMat(1, rows, CV_32FC1);
// Calculate covariance matrix - error is here!!
cvCalcCovarMatrix((const void **) &input, rows, output, meanvec, CV_COVAR_NORMAL);
k = 0;
//Show result
cout << "Covariance matrix:" << endl;
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
cout << "(" << i << "," << j << "): ";
printf ("%f ", cvGetReal2D(output,i,j) / (rows - 1));
dst[k++] = cvGetReal2D(output,i,j) / (rows - 1);
//cout << "\t";
}
cout << endl;
}
return(0);
}
read in the manual about this function. if you store your values in the single matrix 'input', the second function parameter must not be 'rows'. The second parameter is used to say how many matrices you passes in the first parameter. You have passed just one matrix, 'input'. Segfault is no surprise.

Resources