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 4 years ago.
Improve this question
I'm not a novice, but I may be making a novice mistake here. I'm writing code and I have declared a constant at the top of my program. Whenever I try to use that constant in one of my defined functions, I get an error.
#include <iostream>;
#include <fstream>;
#include <string>;
#include <cmath>;
#define PI 3.14159265358979323846;
#define RADI 300.0;
void CreatePieChart(unsigned char pixels[][WID][DEP], const int dims[3],
double percentages[7], double radius)
{
double radians, distance, deg;
for (int i = 0; i < HITE; i++) {
for (int j = 0; j < WID; j++) {
radians = get_theta(j, i, center);
distance = get_distance_from_center(j, i, center, radians);
deg = quadrant_converter(j, i, center, radians);
if ( RADI < distance ) {
pixels[i][j][0] = 0;
}
}
}
}
When I try to access RADI I get an error.
Syntax Error: ')'
syntax error: missing ';' before '{'
'<' result of expression not used
language feature 'init-statements in if/switch' requires compiler flag '/std:c++17'
All on the same line
Please help.
A #define literally replaces the thing on the left, with the thing on the right.
So when you write
#define RADI 300.0;
if ( RADI < distance )
that is the same as writing
if ( 300.0; < distance )
which has an extra ; in the middle of it. Delete the ;.
I needed to delete the semi-colons after the headers and define declarations
Related
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
i'm having some issues with this bit of code. basically "k" doesn't increment more than 1. I've already tried to declare it outside the loop but doesn't fix it. basically what the code does is generating a grid of crystals.
this is a uni assessment and at the moment I'am a newbie with console functions, especially managing the cursor. as you can see, at every iteration i add +2 to pos.x. it seems to work, but when it starts again, pos.x returns to the start value and instead is pos.y to increment(?).
void gridGeneration(Crystal simbols[][Columns])
{
COORD pos = {10, 55};
for (int i = 0; i < Rows; i++)
for (int k = 0; k < Columns; k++)
{
WriteCrystalAt(simbols[i][k].crystal, pos.X, pos.Y, simbols[i][k].color= rand() % light_yellow + light_blue);
pos.X += 2;
if (k = 1)
{
pos.X = 10;
pos.Y += 2;
}
}
}
It would increment further than 1, but you keep setting it to 1 again:
if (k = 1)
You should use == for comparisons.
Your compiler should have issued a warning about this. If it did not, review your warning settings. If it did, stop ignoring warnings.
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 7 years ago.
Improve this question
How can I integrate an equation including bessel functions numerically from "0" to "infinity" in Fortran or/and C?
I did in matlab, but it's not true for larger inputs and after a specific values , the bessel functions give completely wrong results(there is a restriction in Matlab)
There's a large number of analytic results for various integrals of the Bessel functions (see DLMF, Sect. 10.22), including definite integrals over precisely this range. You'd be much better off, and almost certainly faster and more accurate, trying hard to recast your expression into something that's integrable and using an exact result.
Last time I had to do with such things, it was state of the art to do simple integration of the intervals defined by the zero crossings. That is in most cases relatively stable and if the integrand is approaching zero reasonable fast easy to do.
As a starting point for playing around I´ve included a bit of code. Of course you need to work on the convergence detection and error checking. This is no production code but I thought maybe it provides a starting point for you. Its using gsl.
On my iMac this code takes about 2 µs per iteration. It will not become faster by including a hardcoded table for the intervals.
I hope this is of some use for you.
#include <iostream>
#include <vector>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_sf.h>
double f (double x, void * params) {
double y = 1.0 / (1.0 + x) * gsl_sf_bessel_J0 (x);
return y;
}
int main(int argc, const char * argv[]) {
double sum = 0;
double delta = 0.00001;
int max_steps = 1000;
gsl_integration_workspace * w = gsl_integration_workspace_alloc (max_steps);
gsl_function F;
F.function = &f;
F.params = 0;
double result, error;
double a,b;
for(int n=0; n < max_steps; n++)
{
if(n==0)
{
a = 0.0;
b = gsl_sf_bessel_zero_J0(1);
}
else
{
a = n;
b = gsl_sf_bessel_zero_J0(n+1);
}
gsl_integration_qag (&F, // function
besselj0_intervals[n], // from
besselj0_intervals[n+1], // to
0, // eps absolute
1e-4,// eps relative
max_steps,
GSL_INTEG_GAUSS15,
w,
&result,
&error);
sum += result;
std::cout << n << " " << result << " " << sum << "\n";
if(abs(result) < delta)
break;
}
return 0;
}
You can pretty much google and find lots of Bessel functions implemented in C already.
http://www.atnf.csiro.au/computing/software/gipsy/sub/bessel.c
http://jean-pierre.moreau.pagesperso-orange.fr/c_bessel.html
https://msdn.microsoft.com/en-us/library/h7zkk1bz.aspx
In the end, these use the built in types and will be limited to the ranges they can represent (just as MATLAB is). At best, expect 15 digits of precision using double precision floating point representation. So, for large numbers, they will appear to be rounded. eg. 1237846464123450000000000.00000
And, of course, others on Stack Overflow have looked into it.
C++ Bessel function for complex numbers
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I have a grid with tetragons and i want to save all the vertices in an array. I wrote ths code:
int counter=0;
int i = 0;
for(i=0; i<=600; i+=40){
verticePosition[counter] = i;
verticePosition[counter+1] = i;
verticePosition[counter+2] = i+40;
verticePosition[counter+3] = i;
verticePosition[counter+4] = i;
verticePosition[counter+5] = i+40;
verticePosition[counter+6] = i+40;
verticePosition[counter+7] = i+40;
counter += 8;
}
I want to save four-four vertices in the table and then i call a function to fill every tetragon with a different color but im getting an error in this for loop:
prog.c:13:1: error: expected identifier or ‘(’ before ‘for’
for(xpos=0; xpox<=600; xpos+=40){
^
and also another error:
prog.c:13:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<=’ token
for(xpos=0; xpox<=600; xpos+=40){
^
I cant find what is wrong with my loop.
The variable xpos is used but not declared, you must declare and initialize it:
for (int xpos = 0; xpos <= 600; xpos += 40) {
Or declare it before the loop:
int xpos;
for (xpos = 0; xpos <= 600; xpos += 40) {
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.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Here comes my code. Firstly I have defined the three class
class matrix {protected:
double *mdata;
int rows,columns;
class polyg{protected: matrix x,y,z,centre;
class triangle: public polyg{protected:public:
triangle(matrix x1,matrix y1,matrix z1){x=x1;y=y1;z=z1;
centre=(x+y+z).change_scale(1/3);
}
and I have defined a overloading function for matrix multiplication such as
matrix operator*(matrix &m) const{
if(columns!=m.getrows()) {
cout<<"invalid size!!"<<endl; exit(1);
}
matrix temp(rows,m.getcols());
for(int x=0; x<rows; x++) {
for(int y=0; y<m.getcols(); y++) {
double value = 0;
for(int z=0; z<columns; z++) {
value = value+(mdata[index(x+1,z+1)]*m.mdata[index(z+1,y+1)]);
}
temp.mdata[index(x+1,y+1)] = value;
}
}
return temp;}
index function is defined as
int index(int m, int n) const // Return position in array of element (m,n){
if(m>0 && m<=rows && n>0 && n<=columns) return (n-1)+(m-1)*columns;
else {cout<<"Error: out of range"<<endl; exit(1);}
}
and I defined member function, which overrides the pure virtual function in polyg class, in triangle class such as
void rotate(double angle){
double pi=4*atan(1.0);
double angle_rad=pi*angle/180;
matrix m_rot(2,2);
m_rot(1,1)=cos(angle_rad);
m_rot(1,2)=sin(angle_rad);
m_rot(2,2)=cos(angle_rad);
m_rot(2,1)=-sin(angle_rad);//matrix of rotation of angle inserted
x=m_rot*x;
y=m_rot*y;
z=m_rot*z;//rotating the triangle
}
problem occurs from
x=m_rot*x;
y=m_rot*y;
z=m_rot*z;
this part. Heap corruption detected error occurs from this part. if I remove this part, code runs totally well without any problem.
Also, if I define in main
int main(){matrix a,b,c; c=a*b;
it works totally well also.
however if I use the function I made in triangle class,for example,
triangle tri(a,b,c); tri.rotate(30);
problem occurs
no errors occur before the debugging
but after I compile,
Heap Corruption detected error occurs
could someone explain what is the problem? and how to fix it?
This code looks suspect to me:
for(int x=0; x<rows; x++) {
for(int y=0; y<m.getcols(); y++) {
double value = 0;
for(int z=0; z<columns; z++) {
value = value+(mdata[index(x+1,z+1)]*m.mdata[index(z+1,y+1)]);
}
temp.mdata[index(x+1,y+1)] = value;
With z you are iterating over column indexes, but you pass that value as the first argument to the index function (source now no longer present in your post) which appears to assume that the first argument is always a row index.