ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to write a program to solve the quadratic exuation and find the vertex. I managed to get it to compile in both the GCC and the G++ (It's written in C), but I can't get it to actually run. Right after entering the first number, it gets a segmentation fault.
Konsole:
Code:
[neil@localhost ~]$ gcc '/home/neil/Desktop/calc.c' -o '/home/neil/Desktop/calc.exe' -lm
[neil@localhost ~]$ '/home/neil/Desktop/calc.exe'
Welcome to my quadratic program,
the Quradratic Calculator;
The format is =_x^2 + _x + _
=2
Segmentation fault
Source:
Code:
#include <stdio.h>
#include <math.h>
float quad1(int x1, int x2, int x3);
float quad2(int x1, int x2, int x3);
float med(float y1, float y2);
float val(float z1, float z2, float z3, float mid);
int main()
{
int ax, bx, cx;
float y1, y2, mid, mival;
printf("Welcome to my quadratic program,\n the Quradratic Calculator;\nThe format is =_x^2 + _x + _\n=");
scanf("%d", ax);
printf("x^2 +");
scanf("%d", bx);
printf("x + ");
scanf("%d", cx);
y1 = quad1(ax, bx, cx);
y2 = quad1(ax, bx, cx);
mid = med(y1, y2);
mival = val(ax, bx, cx, mid);
printf("The 0s are (%f, 0) and (%f, 0)\nThe vertex is (%f, %f).\n", y1, y2, mid, mival);
return 0;
}
float quad1(int x1, int x2, int x3)
{
float y;
y = (-x2 + sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
return y;
}
float quad2(int x1, int x2, int x3)
{
float y;
y = (-x2 - sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
return y;
}
float med(float y1, float y2)
{
float median;
median = (y1+ y2)/2;
return median;
}
float val(float z1, float z2, float z3, float mid)
{
float mima;
mima = (z1 * pow(mid, 2)) + (z2 * mid) + z3;
return mima;
}
Segmentation faults have caused delays with my programming before. What do they mean? How can I fix this one?
A segmentation fault means "Invalid memory reference". Your program is given a certain range of memory to play with, and if you attempt to access memory outside of that range the kernel will stop it before it does damage.
To fix it, you should find your programming error =)
EDIT: Looking at the code, I found the problem within 5 seconds. scanf() needs the address of where to store the data. You should be using like &ax instead of ax.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.