Segment violation (`core 'generated) error in c program - c

I'm currently working in a c program that calculates the pi value using Leibniz summation, but I'm getting the error "segment violation (`core 'generated)" in the console once the program is executed. Also I've checked similar questions but I'm still unable to fit that info into my problem. This is the code I have:
#include <stdio.h>
double calPi(int x);
double calPi(int x)
{
double sum;
int i;
sum=0.0;
for (i=0; i<=x; i++)
{
if ((i=2)||((i%2)==0))
{
sum=sum+(1/(2*i+1));
}
else sum=sum-(1/(2*i+1));
}
return 4*sum;
}
int main(void)
{
int x;
double PI;
printf("Enter the number of terms you want to calculate");
scanf("%i",x);
PI=calPi(x);
printf("Pi value is: %f", PI);
system("pause");
return 0;
}
Thanks in advance for any hint, help or correction.

This is your problem:
scanf("%i", x);
Should be
scanf("%i", &x);
You need to take the address of x, otherwise just using x , will be treated as a pointer pointing to who knows what. Make sure you compile with warnings on, as your compiler should warn you about stuff like that. My compiler complains with
"format string '%i' requires an argument of type 'int *', but variadic argument 1 has type 'int'"

Related

I have trouble with the following code with different variable types and pointers for finding Bessel functions

This assignment is one of my labs and it wants us to find the value of the 1st order Bessel function according to the input-ed user value. So long, my mindset is: first I will ask the user for input, then I use the number's rounding, the number above and the number below. After that, I perform the interpolation according to the numbers, find the error, then present everything on screen. (We must use pointers and arrays else we will not get any marks. That makes sense, how are we gonna list out every case for every rounded number from 0 to 10......)
The problem is, I seem to not get the pointers right, and the program always say that we have long int and *double mixed, even though I explicitly told the program that this is a double.
Code:
#include <stdio.h>
#include <math.h>
#include <Stdlib.h>
int y;
int readdata(double rho[y], double Jrho[y])
{
double a, b, c, x;
FILE* fp;
fp= fopen("Bessel1.dat", "r");
for (y=0; y<=10; y++)
{
fscanf(fp, "%lf %lf", &rho[y], &Jrho[y]);
}
}
double Lagrange2nd(int y, double rho[y], double Jrho[y], double x)
{
double a, b, c;
double output;
a=(x-rho[(y+1)])*((x-rho[y])*Jrho[((y-1))]+(rho[((y-1))]-
x)*Jrho[y])/(rho[((y-1))]-rho[y]);
b=(((rho[(y-1)]-x)*((x-rho[(y+1)])*Jrho[y]+(rho[y]-
x)*Jrho[(y+1)]))/(rho[y]-rho[(y+1)]));
output = c = (a+b)/(rho[(y-1)]-rho[(y+1)]);
return output;
}
double errx(int y, double rho[y], double Jrho[y], double x)
{
double a, b, c;
double output;
output= (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c-
(Jrho[y]*(x-rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;
return output;
}
int main()
{
double x, Jrho[y], rho[y];
printf("x=");
scanf("%lf", &x);
y=round(x);
Lagrange2nd(&rho[y], &x, &Jrho[y], y);
errx(&Jrho[y], &x, &rho[y], y);
printf("%d, %d", Lagrange2nd, errx);
return(0);
}
I know this is messy, but I did not know any programming until 2 months ago, yet we now have to do this......gee, I am not very good at this, am I?
What compiler do you use ? with gcc you will get MANY warnings. In particular, you have few functions that declare to return void, but return an int.
Looks like you compiler does treat 'void' as 'int' - which will cause lot of precision issue. If you have turned of warning - consider re-enabling them, or use better compiler :-)
void errx(double* rho[y], double* Jrho[y], double* x)
{
...
double output;
output= (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c-(Jrho[y]*(x-
rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;
return output;
}
Style note: consider combing declaration and assignment in one statement. It will reduce many possible errors, and will get you better grade!
double output = (abs(c-(Jrho[(y-1)]*(x-rho[y])+Jrho[y]*(rho[(y-1)]-x)))+abs(c-(Jrho[y]*(x-rho[(y+1)])+Jrho[(y+1)]*(rho[y]-x))))/2;

Compiler shows "error lvalue required as left operand of assignment". What does it mean and how to fix it?

I'm new to programming. So, details are appreciated.
#include<stdio.h>
#include<math.h>
int main()
{
float x, f(x);
printf("Enter x=");
scanf("%f", &x);
f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x); /*in this line compiler shows error*/
printf("f(x)= %f", f(x));
return 0;
}
Excuse me for assuming that you are a C beginner looking for the way to write a function in C. There are many C tutorials out there, I recommend finding one for further learning.
Here is an example of using an actual C function for what you are doing.
#include<stdio.h>
#include<math.h>
/* first define the function */
float f(float x) /* function head */
{ /* start of function body */
return 3*pow(x, 5) - 5*sqrt(x)-6*sin(x);
} /* end of function body and definition */
int main(void)
{
float x; /* definition of function has already happened, so no f(x) here */
printf("Enter x=");
scanf("%f", &x);
printf("f(x)= %f", f(x) /* call the function */);
/* Note that some coding styles do not like calling a function
from within a more complex statement. Using a variable to
take the result of the function is preferred.
I chose this way to stay more similar to your own code.
*/
return 0;
}
The following could work.
#include <stdio.h>
#include <math.h>
int main()
{
float x, f;
printf("Enter x=");
scanf("%f", &x);
f = 3 * pow(x, 5) - 5 * sqrt(x) - 6 * sin(x);
printf("f(x)= %f", f);
return 0;
}
In your code, f(x) is a valid identifier, but not a variable. It's a very poorly written (and now invalid, as per the latest C standard) function prototype. You cannot assign to it, it's not a modifiable lvalue.
That is why in case of
f(x)= 3*pow(x, 5)- 5*sqrt(x)-6*sin(x);
compiler screams.
On why your code did not throw an error for the invalid format, it's the legacy support in compiler. In your case
float x, f(x);
is treated the same as
float x, float f ( int x ) ; //missing data type is treated as 'int',
// DON'T rely on this "feature"

redefinition of ‘main’

As I am new to programming, I was trying to write a simple code using functions which will give me the addition of three numbers. Here's the code!
/* Your includes go here */
#include <stdio.h>
int addThreeNumbers(int a, int b, int c)
{
int d;
d = a + b + c;
return(d);
/* Complete this function only
DO NOT write main function.
*/
}
int main()
{
int x, y, z, sum;
printf("Enter the three numbers: ");
scanf(" %d %d %d", &x, &y, &z);
sum = addThreeNumbers(x, y, z);
printf("The sum is %d", sum);
return 0;
}
And the error was as follows:
solution.c:30:5: error: redefinition of ‘main’
solution.c:15:9: note: previous definition of ‘main’ was here
You have another main function in the code somewhere. Post the complete code and I will take a closer look. But that is the only way you can receive this error
In modern C, empty argument parentheses mean that the type and number of arguments is unknown.
Although this segment runs fine with most compilers, yours might be picky. Try declaring main with zero arguments explicitly, like this:
int main(void) {
//code
}
Pretty sure this is one of the online coding sites' question. They put in the main function themselves by appending it to the code, you don't have to explicitly write it. Delete the main function you have written and check if that works out.

Please help me to understand these error

#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!

Using pow() in C

I have this problem with this homework I'm supposed to do.
[ It says Create a program that's able to calculate and show the sum of S]
Like S=1+1/4+1/8+1/16 ... till 1/ [2 pow n]
So I worked on it and came up with this code
#include <stdio.h>
void main()
{
int n,i;
float p,s;
printf("Enter the maximum power n :");
scanf("%d",&n);
s=0;
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",&s);
}
But when I execute it is always like S=0.
What am I doing wrong?
You are printing an address ('&s) with%f` variable. Using a wrong specifier invokes undefined behavior. You may get anything.
Also, No need of variable s. Remove the line
s+=p;
It should be like:
#include <stdio.h>
int main(void)
{
int n,i;
float p;
printf("Enter the maximum power n :");
scanf("%d",&n);
p=0;
for (i=0;i<n;i++)
{
p+=1/pow(2, i);
printf("p = %f\n",p);
}
printf("The sum of this equation is :%f",p);
}
You need to include <math.h> to get the proper prototype of pow().
You might need to link to the math library too gcc main.c -Wall -lm
#include <math.h>
....
for (i=0;i<n;i++)
{
p=1/pow(2, i);
s+=p;
printf("s = %f\n",s);
}
printf("The sum of this equation is :%f",s);
Your program has multiple problems. Enabling compiler warnings should tell you about some of them.
You should include the C header which contains the declaration of the pow function.
You add each addend twice.
In your second printf statement, you pass a float. But the %f format specifier expects a double argument. In your third printf statement, you pass a pointer to a float.
Another cosmetic problem is that your main function should return an int.
just a guess, may you replace the line
p+=1/pow(2, i);
with
p+=1.0f/(float)pow(2, i);
and
printf("The sum of this equation is :%f",&s);
with
printf("The sum of this equation is :%f",s);
Typo may be.. but you will have say %f and s (not &s)
printf("The sum of this equation is :%f",s);
On side note:
Once you include <math.h> you will get compiler warning for using correct pow(..) prototype. Below code would be relevant.
p+=1.0f/(float)pow(2.0f, i);

Resources