' variable' was not declared in this scope [closed] - c++

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'm new to Arduino coding and I'm not sure what issue I'm having with displaying the variable as serialPrint. Here is part of my code:
void loop()
{
int force1raw = analogRead(FSR_PIN1);
int force2raw = analogRead(FSR_PIN2);
int force3raw = analogRead(FSR_PIN3);
float force1 = force1raw;
float force2 = force2raw;
float force3 = force3raw;
for (int i = 0; i < 1000; i++)
{
// Use ADC reading to calculate voltage:
float f1v = force1 * VCC / 1023.0;
// Use voltage and static resistor value to
// calculate FSR resistance:
float f1r = R_DIV * (VCC / f1v - 1.0);
}
Serial.println("Resistance 1: " + String(f1r) + " ohms");
// Estimate force based on slopes in figure 3 of
// FSR datasheet:
float force;
float f1g = 1.0 / f1r; // Calculate conductance
// Break parabolic curve down into two linear slopes:
if (f1g <= 600)
force1f = (f1g - 0.00075) / 0.00000032639;
else
force1f = f1g / 0.000000642857;
Serial.println("Force 1: " + String(force2f) + " g");
Serial.println();
delay(100);

Well,
Serial.println("Resistance 1: " + String(f1r) + " ohms");
it uses the variable f1r whose scope just ended.
In C++, when a variable is defined inside a scope (between some { and }), it does not exist outside of it.

Related

Erosion in image processing [closed]

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
I writed this code below, and I got bad results, can anyone help me, and tell me what is wrong ? I writed this in Qt. . If Anyone can help me it would be great.
my Matrix is just some random integral 0 or 1
while( x < obraz.width())
{
while( y < obraz.height())
{
piksel2 = obraz.pixel(x, y);
kolor2 = QColor::fromRgb(piksel2);
minR = kolor2.red();
minG = kolor2.green();
minB = kolor2.blue();
for(i = 0; i < w; i++)
{
for(j = 0; j < h; j++)
{
if (matrix[i][j] == 1 && x - o + i >= 0 && y - u + j >= 0 && x - o + i < obraz.width() && y - u + j < obraz.height())
{
piksel = obraz.pixel(x - o + i, y - u + j);
kolor = QColor::fromRgb(piksel);
if (kolor.blue() < minB)
{
minB = kolor.blue();
}
if(kolor.green() < minG)
{
minG = kolor.green();
}
if(kolor.red() < minR)
{
minR = kolor.red();
}
}
}
}
obraz.setPixel(x, y, qRgb(minR, minG, minB));
y++;
}
y=1;
x++;
}
Input file:
Output file:
The main problem with the code is that it writes the result for each pixel into the input image. This result will be used when computing the min value for the next pixel. Thus, the dark patch at the top-left of the image gets propagated across the whole image.
It is important for this type of algorithm to write into a separate output buffer, leaving the input unchanged until the whole image has been processed.
Do note also that the erosion is well defined for gray-value images, but not for color images. You seem to want to apply marginal ordering, which is equivalent to computing the erosion for each channel independently. Be advised that this method will introduce new colors to the image. There are better approaches, but they all have some sort of downside. I wrote a small overview about this some years ago on my blog.

Unit Testing non returning functions in Qt [closed]

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 3 years ago.
Improve this question
you might remember me or running a kind of a 'Lightroom' panel, using C++ and Qt for GUI.
Today I was reading about implementing a unit testing for my main classes, but my question is, how can I test a function that does not return anything?
for example, I got that function:
void ImgProcessing::processMaster(cv::Mat& img, cv::Mat& tmp, int brightness, int red, int green, int blue, double contrast){
for(int i = 0; i < img.rows; i++)
for(int j = 0; j < img.cols; j++)
for(int k = 0; k < 3; k++){
if(k == 0) //_R
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + red )*(259 * (contrast + 255) / (255 * (259 - contrast))));
if(k == 1) //_G
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + green )*(259 * (contrast + 255) / (255 * (259 - contrast))));
if(k == 2) //_B
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + blue )*(259 * (contrast + 255) / (255 * (259 - contrast))));
}
this function just take the obj 'mat img', and modify the 'mat tmp' obj.
than I update the UI for display the modified image, by using another dedicated function in my gui class.
Has someone already encounter something like that?
It does not make a difference if it returns a value the regular way or via an output parameter. The procedure is the same anyway. Run the function and check that the output parameter has the expected value.
This is C code, but it does not make a difference for understanding the concept. Consider these functions:
int addOne1(int x) { return x+1; }
void addOne2(int x, int* ret) { *ret = x+1; }
These can now be tested in this way:
const int x = 3;
int ret1, ret2;
ret1 = addOne1(x);
addOne2(x, &ret2);
assert(ret1 == 4);
assert(ret2 == 4);
If the output parameter also is an input parameter, then you of course need to make sure that you know the initial value.
void inc(int *x) { (*x)++; }
int x=3;
inc(&x);
assert(x == 4);
Technically, modifying a parameter IS considered a side effect. But as long as you are careful it's not a big issue. The difference compared to using a member variable is huge. And if you start modifying globals you will soon make it REALLY hard to test the code.

Encoding numbers in BCD (Casio serial interface) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am attempting to create a device that talks to a Casio fx-9750 calculator through its serial port with an Arduino. I have figured out how to receive values and decode the BCD, but I'm stuck on how to create the required values from a float (to transmit back).
The calculator sends a data packet, which has an exponent value, several data values, and a byte that contains information about negativity, imaginary parts, etc. Each data value is worth one hundredth of the previous one, so the first is the amount of 10s, the next the amount of 0.1s, the next the amount of 0.001s, etc. This continues on until the 0.0000000000001s, though this is out of the range of what I'll really need, so that level of accuracy is not really important to me. The output of my receiving program looks like this:
Exponent: 1
10s: 1
0.1s: 23
0.001s: 40
This represents 12.34.
The general equation I worked out was: (let a=10s, b=0.1s, e=exponent etc)
((a*10)+(b*0.1)+(c*0.001))*10^(E-1)
If the exponent were to change to two:
Exponent: 2
10s: 1
0.1s: 23
0.001s: 40
This would represent 123.4
This method of dropping by hundredths each time is presumably used because they can store two digits in each byte with BCD, so it is most efficient to let each row have two digits as each row is stored as one byte.
I have come up with an equation that can calculate the exponent by counting the amount of digits before the decimal point and subtracting two, however this seems messy as it involves strings. I think a purely mathematical solution would be more elegant, if it is possible.
What is the fastest and simplest way to go from a normal number (e.g. 123.4) into this arrangement?
A solution in Arduino language would be greatly appreciated, but any insight whatsoever into the mathematical process needed would be equally valued.
Edit regarding floats:
I should clarify - I will be dealing with floats in other parts of my program and would like my inputted values to be compatible with numbers of any size (within reason, as stated before). I have no problem with multiplying them to be ints or casting them as other datatypes.
Hah, that was fun!
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <float.h>
struct num_s {
// exponent
int e;
// v[0] is *10
// v[1] is *0.01
// v[2] is *0.0001
// and so on...
// to increase precision increase array count
int v[6];
};
#define NUM_VALSCNT (sizeof(((struct num_s*)0)->v)/sizeof(((struct num_s*)0)->v[0]))
// creates num_s object from a double
struct num_s num_create(double v) {
struct num_s t;
// find exponent so that v <= 10
t.e = 0;
while (fabs(v) >= 10.0) {
++t.e;
v /= 10.0;
}
// for each output number get the integral part of number
// then multiply the rest by 100 and continue
for (size_t i = 0; i < sizeof(t.v) / sizeof(t.v[0]); ++i) {
const double tmp = fmod(v, 1.0);
t.v[i] = v - tmp;
v = tmp * 100;
}
return t;
}
// converts back from num object to double
double num_get(struct num_s t) {
double denom = 10;
double ret = 0;
for (size_t i = 0; i < sizeof(t.v) / sizeof(t.v[0]); ++i) {
ret += denom * t.v[i];
denom /= 100;
}
return ret * pow(10, t.e - 1);
}
void num_println(struct num_s t) {
printf("%f =", num_get(t));
for (size_t i = 0; i < sizeof(t.v) / sizeof(t.v[0]); ++i) {
printf(" %d", t.v[i]);
}
printf(" %d\n", t.e);
}
// returns the precision of numbers
// the smallest number we can represent in num object
double num_precision(void) {
return pow(0.1, (NUM_VALSCNT - 1) * 2) * 10;
}
int num_unittests(void) {
const double tests[][3] = {
{ 123.49, 123.5, 123.51, }
};
for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
const double tmp = num_get(num_create(tests[i][1]));
if (!(tests[i][0] <= tmp && tmp <= tests[i][2])) {
return i + 1;
}
}
return 0;
}
int main() {
num_println(num_create(12.3456789));
num_println(num_create(123.5));
num_println(num_create(12.35));
printf("%d\n", num_unittests());
return 0;
}

I was trying to write a Greedy Algorithm in C [closed]

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 8 years ago.
Improve this question
Does this make any sense?
I got stuck in here with 4 errors and it is because I didn't declared the ints q,d,n,p. But if I do so it'll keep sending me more errors.
There might be something about having mixed ints and floats.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
{
printf("O hai! ");
}
float valueTotal, quarter, valueQuarter, dime, valueDime,nickel, valueNickel, penny, valuePenny;
do
{
printf("How much change is owed?\n");
valueTotal = GetFloat();
}
while (valueTotal <= 0);
for (float quarter = 0; valueTotal >= 0.25; quarter--)
{
valueQuarter = valueTotal - ( q * 0.25);
}
for (float dime = 0; valueQuarter >= 0.10; dime--)
{
valueDime = valueQuarter - ( d * 0.10);
}
for (float nickel = 0; valueDime >= 0.05; nickel--)
{
valueNickel = valueDime - ( n * 0.05);
}
for (float penny = 0; valueNickel >= 0.01; penny--)
{
valuePenny = valueNickel - ( p * 0.01);
}
printf("q+d+n+p\n");
}
I didn't declared the ints q,d,n,p.
This is exactly your problem - at least one of them, anyways. If these variables are undeclared, how in the world is the program/code supposed to evaluate something like q * 0.25 ? If I said "Hey man, what is x times 0.25?" You'd have absolutely no idea, or tell me that the answer depends on x. The same goes with this code.
You said:
But if I do so it'll keep sending me more errors.
I'm assuming you also need to initialize them (or, in layman's terms, set them equal to something ie. q = 0)
Also, none of your loop conditions are actually changing.... meaning they're infinitely looping. Make sure that the code inside your loop is actually helping you reach the goal of satisfying the loop condition; for example:
for (float quarter = 0; valueTotal >= 0.25; quarter--)
{
valueQuarter = valueTotal - ( q * 0.25);
}
valueTotal is ALWAYS going to be greater than 0.25 (if it is less than 0.25 to begin with) since you are never changing it at all.

Generating random reals uniformly using Boost [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 years ago.
Improve this question
I am trying to generate some uniform real numbers for a Monte Carlo integration but the routine I build was returning some really strange values. Upon closer inspection I notices that Boost was returning some crazy looking random numbers e.g.:
temp = -0.185276
temp = -0.864523
temp = -0.0942081
temp = -0.164991
temp = -0.873013
temp = -0.0311322
temp = -0.0866241
temp = -0.778966
temp = -0.367641
temp = -0.691833
temp = 5.66499e-310
temp = 9.42007e-311
temp = 6.29821e-310
temp = 5.80603e-310
temp = 8.82973e-311
temp = 6.73679e-310
temp = 6.35094e-310
temp = 1.53691e-310
temp = 4.39696e-310
temp = 2.14277e-310
Whilst these numbers are technically still reals generated between the bounds -1 and 1 I would prefer it if they weren't quite so small!
My implementation of the call to boost is in a function which is called multiple times (for different bounding values) as follows:
// Define Boost typedefs
typedef boost::mt19937 Engine;
typedef boost::uniform_real<double> Distribution;
typedef boost::variate_generator <Engine, Distribution> Generator;
int main (void) {
...
Integral = MCRecursion(...);
...
return 0;
}
double MCRecursion (int Count, double Lower, double Upper, double (*Integrand)(double)) {
// Define Boost objects
Engine Eng;
Distribution Dist (Lower, Upper);
Generator RandomGen (Eng, Dist);
Eng.seed(time(0));
// Variables for Monte Carlo sample sums
double Sum = 0.0;
double temp;
for (int i = 0; i < Count; i++) {
temp = RandomGen();
std::cout << " temp = " << temp << std::endl;
Sum += Integrand(temp);
}
return (Upper - Lower) * Sum / Count;
}
I assume the problem is something with my implementation but I can't find any errors. Any and all help appreciated!
Cheers,
Jack
EDIT
Code for calling MCRecursion:
The Code I am writting runs a Monte Carlo on the entire domain I am interested in [Lower, Upper] and then looks again at the left half of the whole domain and the right half of the domain.
e.g. if we were integrating f(x) between -a and a I calculate the full integral using:
double FullDomain = MCRecursion (1e5, LowerBound, UpperBound, f);
double Centre = (Upper + Lower) / 2.0;
double LeftHalf = MCRecursion (1e5, LowerBound, Centre, f);
double RightHalf = MCRecursion (1e5, Centre, UpperBound, f);
and I then look at the uncertainty by calculating:
double difference = fabs(FullDomain - LeftHalf - Righthalf);
to see if more samples is 'worth it' in some sense
Jack
Based on the pastebin the questioner posted in the comments:
This is not a problem with the random library but rather a simple programming error. Compiling the code throws the warning:
../src/Test.cpp: In function ‘double AdaptiveMCRecursion(double, double, double, double, int, double, double (*)(double))’:
../src/Test.cpp:100:72: warning: ‘Right’ is used uninitialized in this function [-Wuninitialized]
double Right = MCSample (Count, Central, Right, Integrand);
So all the behaviour from that line on is basically undefined. Especially it results in calling the function MCSample with an undetermined Upper parameter. So your result is not unexpected. You are actually lucky the program runs at all.