I got: "expected ';', ',' or ')' before '&' token "in C [closed] - c

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 1 year ago.
Improve this question
Why does &x1 cannot be accpeted
int calculte (float a, float b, float c, float &x1, float &x2)
I'm trying to solve quadratic equation, here's my full program
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float &x1, float &x2){
float delta = b*b -4*a*c;
if (delta <0) {
x1 =x2 = 0.0;
return 0;
} if (delta == 0) {
x1 = x2 = -b/(2*a);
return 1;
} if (delta >0) {
delta = sqrt(delta);
x1 = (-b-delta)/(2*a);
x2 = (-b - delta)/(2*a);
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,x1,x2);
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}
I am new to programming, so hope you guys can explain me more details.

You cannot use references in C.
Instead of that, you should make the arguments to pointers by changing & to *.
Also the function body and calling have to be changed according to that.
#include<stdio.h>
#include<math.h>
int calculte (float a, float b, float c, float *x1, float *x2){ /* change the arguments to pointers */
float delta = b*b -4*a*c;
if (delta <0) {
*x1 =*x2 = 0.0; /* dereference the pointers */
return 0;
} if (delta == 0) {
*x1 = *x2 = -b/(2*a); /* dereference the pointers */
return 1;
} if (delta >0) {
delta = sqrt(delta);
*x1 = (-b-delta)/(2*a); /* dereference the pointer */
*x2 = (-b - delta)/(2*a); /* dereference the pointer */
return 2;
}
}
int main () {
float a, b, c, x1, x2;
do {
scanf ("%f %f %f", &a, &b, &c);
}
while (!a);
int ans = calculte (a,b,c,&x1,&x2); /* add & to get the pointers */
if (ans==0) {
printf ("NO");
}
if (ans==1){
printf ("%.2f",x1);
}
if (ans==2) {
printf ("%.2f %.2f", x1, x2);
}
}

Related

How to pass different amount of arguments in a function in c

I want to pass different amount of arguments by satisfying condition to the function. But the problem is the arguments have to be the same amount of parameter of a function.In every condtion I have different variables value which I want to pass as arguments. How can avoid this and successfully execute my code.
#include<stdio.h>
float file_w(float root1,float root2, float real,float imag)
{
FILE *fileWrite;
fileWrite= fopen("fileNames.txt", "a+");
if(root2==NULL && real==NULL && imag==NULL){
fprintf(fileWrite,"%.2f",root1);
fclose(fileWrite);
}
}
float file_r()
{
system("cls");
printf("\n\n\n\t\t\t\tCalculation History\n\n\t\t\t\t");
FILE *file;
char c;
file=fopen("fileName.txt","r");
if(file==NULL)
{
printf("file not found");
}
else
{
while(!feof(file))
{
c=fgetc(file);
printf("%c",c);
}
fclose(file);
printf("\n");
system("Pause");
main();
}
}
int main(){
double a, b, c, discriminant, root1, root2, realPart, imagPart;
int opt;
printf("Enter coefficients a, b and c: \n");
scanf("%lf", &a);
scanf("%lf",&b);
scanf("%lf",&c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("\n\t\t\t\troot1 = %.2lf and root2 = %.2lf\n\n\t\t\t\t", root1, root2);
file_w(root1,root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("\n\t\t\t\troot1 = root2 = %.2lf\n\n\t\t\t\t", root1);
file_w(root1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("\n\t\t\t\troot1 = %.2lf + %.2lfi and root2 = %.2f - %.2fi\n\n\t\t\t\t", realPart, imagPart, realPart, imagPart);
file_w(realPart,imagPart);
}
return 0;
}
Variable Length Argument in C
Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.
Sum of given numbers.
Minimum of given numbers.
and many more.
Variable number of arguments are represented by three dotes (…)
For example:
#include <stdio.h>
#include <stdarg.h>
double average(int num,...) {
va_list valist;
double sum = 0.0;
int i;
/* initialize valist for num number of arguments */
va_start(valist, num);
/* access all the arguments assigned to valist */
for (i = 0; i < num; i++) {
sum += va_arg(valist, int);
}
/* clean memory reserved for valist */
va_end(valist);
return sum/num;
}
int main() {
printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}

Wrong Answer while solving differential equation in C [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 3 years ago.
Improve this question
I am new to C programming and am writing a program to solve simple differential equations which gives output as the value of x. But I'm not getting the correct result.
I am getting the correct value of the equation, but the value of the differential equation is wrong. The code compiles without any warnings or errors.
#include <stdio.h>
#include <conio.h>
#include <math.h>
float poly(float a[], int, float);
float deriv(float a[], int, float);
int main()
{
float x, a[10], y1, dy1;
int deg, i;
printf("Enter the degree of polynomial equation: ");
scanf("%d", &deg);
printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", &x);
for(i=0;i<=deg;i++)
{
printf("Enter the coefficient of x to the power %d: ",i);
scanf("%f",&a[i]);
}
y1 = poly(a, deg, x);
dy1 = deriv(a, deg, x);
printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1);
printf("\nThe value of the derivative of the polynomial equation at x = %.2f is: %.2f",x,dy1);
return 0;
}
/* function for finding the value of polynomial at some value of x */
float poly(float a[], int deg, float x)
{
float p;
int i;
p = a[deg];
for(i=deg;i>=1;i--)
{
p = (a[i-1] + x*p);
}
return p;
}
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-1)*a[deg-1]*ps;
pd = pd + d[i];
}
return pd;
}
You are making a simple logical error. In the function float deriv(float a[], int deg, float x) It should be d[i] = (deg-i)*a[deg-i]*ps;. So your function would look something like this
/* function for finding the derivative at some value of x */
float deriv(float a[], int deg, float x)
{
float d[10], pd = 0, ps;
int i;
for(i=0;i<=deg;i++)
{
ps = pow(x, deg-(i+1));
d[i] = (deg-i)*a[deg-i]*ps;
pd = pd + d[i];
}
return pd;
}
Good luck for the future.

How do I call a function and use its result?

I have to use prototype poly float to compute f(x)=5x^2+12.55x+0.75. I have error every time I run this code because poly is not used. Any help will be good and any tips for prototypes too.
#include<stdio.h>
float poly(float x)
{
return 1;
}
int main()
{
float b, c, a;
printf("Podaj x=");
a = scanf("%f", &b);
c = 5 * b * b + 12.55 * b + 0.75;
if(a<1)
{
printf("Incorrect input");
return 1;
}else
{
printf("Wynik: %.2f", c);
return 0;
}
}
Change poly to:
float poly(float x)
{
return 5*x*x + 12.55*x + .75;
}
In main, you can use the function:
print("poly(%g) = %g.\n", b, poly(b));

Run Time Check Failure #3 - T

Error in my code
Run Time Check Failure #3 - T
I tried many times to fix it,
but I failed.
I added pointer to x, y,
but "Run Time Check Failure #3 - T" — same error.
Can you help me to fix this error?
#include<stdio.h>
#include<math.h>
typedef struct {
double x, y;
}location;
double dist(location a,location b)
{
return sqrt(pow(b.x - a.x, 2.0) + pow(b.y -a.y, 2.0));
}
void func(location l, location e)
{
double z;
location a = l;
location b = e;
printf("enter two dots:");
scanf("%lf %lf", a.x, a.y);
printf("enter two dots:");
scanf("%1",a, b);
printf("%.2lf", z);
}
void main()
{
location l;
location e;
func(l, e);
}
The problems in the code were these:
1) scanf variable args must be passed as pointers. See scanf changes below.
2) initialise your variables in struct - thats the Run Time Check Failure #3 warning. see location initialisation below.
I also simplified a little. Hope that helps.
#include<stdio.h>
#include<math.h>
typedef struct {
double x, y;
}location;
double dist(location a, location b)
{
return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
void main()
{
location start = { 0 };
location end = { 0 };
printf("Enter start x, y co-ordinates: ");
scanf("%lf %lf", &start.x, &start.y);
printf("Enter end x, y co-ordinates: ");
scanf("%lf %lf", &end.x, &end.y);
printf("The distance between start and end: %lf\n", dist(start, end));
}

C programming good practice or not ?

I have written a small piece of code to calculate quadratic equations, but if the discriminant is negative, i wanted it to write that there are no real numerical values for this quadratic equation.
To make this happen, I had to call a function with a fourth parameter of 0, which i think , i have no idea why, would be a bad programming practice ? Is it the case or am i just being too picky of my code ? Thank you. (The reason I'm asking this is because i dont want to pick up some bad habits early on in my programming 'career').
Here's the code.
#include <stdio.h>
#include <math.h>
#include <string.h>
double quadratic_equation(double a, double b, double c, double d);
int main(void)
{
char command[20];
int i;
printf("Enter your command: ");
fgets(command, 20, stdin);
for (i = 0; i < 20; i++) {
if (command[i] == '\n') {
command[i] = '\0';
break;
}
}
if (strcmp(command, "quadratic equation") == 0) {
double a, b, c, x;
printf("Enter A: ");
scanf("%lf", &a);
printf("Enter B: ");
scanf("%lf", &b);
printf("Enter C: ");
scanf("%lf", &c);
x = quadratic_equation(a, b, c, 0); // THIS PIECE HERE MIGHT BE BAD PRACITCE ?
if (x == 0) {
printf("There are no real numerical values to this quadratic equation.");
}
else {
printf("------------\n");
printf("x1 = %.2f\n", quadratic_equation(a, b, c, 1));
printf("x2 = %.2f", quadratic_equation(a, b, c, -1));
}
}
return 0;
}
double quadratic_equation(double a, double b, double c, double d) {
double discriminant, x, insideroot;
insideroot = ((b*b) - (4*a*c));
if (insideroot < 0) {
return 0;
}
discriminant = sqrt(insideroot);
x = (-b + (d * discriminant)) / (2 * a);
return x;
}
Thank you very much for your help :d !
This certainly is bad practice. Since the roots of a formula a, b, and c an be any double you do need some sort of passing.
I would suggest a parameter that is a pointer to an int. If the pointer is NULL it is ignored, otherwise it will be set to 1 or 0 depending whether a real root exists:
double quadratic_equation(double a, double b, double c, int *root_exists) {
double discriminant;
discriminant = ((b*b) - (4*a*c));
if (discriminant < 0) {
if (root_exists != NULL) *root_exists = 0;
return 0.0;
}
x = (-b + sqrt(discriminant)) / (2 * a);
if (root_exists != NULL) *root_exists = 1;
return x;
}
A more rigorous approach is this:
typedef struct {
int num_roots;
double roots[2];
} quadratic_roots_t;
quadratic_roots_t quadratic_equation(double a, double b, double c) {
quadratic_roots_t roots;
double d;
d = b*b - 4*a*c;
if (d < 0.0) {
roots.num_roots = 0;
} else if (d == 0.0) {
roots.num_roots = 1;
roots.roots[0] = -b / (2 * a);
} else {
roots.num_roots = 2;
roots.roots[0] = (-b - sqrt(d)) / (2 * a);
roots.roots[1] = (-b + sqrt(d)) / (2 * a);
}
return roots;
}
I'd say it's not great to do. What you could do is something like this:
int quadratic_equation(double a, double b, double c, double *root_a, double *root_b) {
double discriminant = ((b*b) - (4*a*c));
if (discriminant < 0) {
return -1;
}
if (root_a != NULL) {
*root_a = (-b + sqrt(discriminant)) / (2 * a);
}
if (root_b != NULL) {
*root_b = (-b - sqrt(discriminant)) / (2 * a);
}
return 0;
}
Then you could call that like so:
double root_a;
double root_b;
int ok = quadratic_equation(a, b, c, &root_a, &root_b);
if (ok < 0) {
// It wasn't OK. Print out an error.
} else {
// It was OK. Print out the results.
}
Note that you should also check other error cases in the function and return -1 for them as well. E.g. a being zero.
Consider using the return value to indicate whether everything worked, and passing an array to the function to receive the return values:
enum QE_Status { QE_OK = 0, QE_NON_QUADRATIC, QE_COMPLEX_ROOTS, QE_NULL_POINTER };
enum QE_Status quadratic_equation(double a, double b, double c, double *r)
{
double discriminant;
if (r == 0)
return QE_NULL_POINTER;
if (a == 0.0)
return QE_NON_QUADRATIC;
discriminant = (b * b) - (4 * a * c);
if (discriminant < 0)
return QE_COMPLEX_ROOTS;
discriminant = sqrt(discriminant);
r[0] = (-b + discriminant) / (2 * a);
r[1] = (-b - discriminant) / (2 * a);
return QE_OK;
}
You can extend the system to handle numerical instability (because b*b is almost equal to 4*a*c, or because a is very small, etc).
The calling code can then be:
double a, b, c, x[2];
if (quadratic_equation(a, b, c, x))
...oops, something went wrong...
Or:
switch (quadratic_equation(a, b, c, x))
{
case QE_OK:
...print or use results in x...
break;
case QE_NON_QUADRATIC:
case QE_COMPLEX_ROOTS:
...print appropriate error message about user's data...
break;
case QE_NULL_POINTER:
...Oops - programming error...
break;
}
I would certainly call it bad practice because the code is very unclear.
Firstly, you are calling the function three times, when once should be enough.
I'd consider returning/filling a list in your quadratic_equation() function instead of returning the roots one by one.
This would also allow you to determine if there are real roots -- if there aren't, just return an empty list.
As a whole, this would be much more elegant than your current solution, and it would eliminate the need for checking whether there are any solutions beforehand.

Resources