I have to change the values of two variables with each other using the following function:
I made this code
#include <stdio.h>
#include <stdlib.h>
void change_double(double *d1, double *d2);
int main()
{
double *a, *b;
printf("Write two variables of the type double.\n");
scanf("%d", &a);
scanf("%d", &b);
printf("Normal variables: %d %d\n", a, b);
change_double(&a, &b);
printf("Changed variables: %d %d\n", a, b);
return 0;
}
void change_double(double *d1, double *d2)
{
double aux;
aux=*d1;
*d1=*d2;
*d2=aux;
}
When I run this programm, I generally always get this result(example):
Normal variables: 321 123
Changed Variables: 82 321
Number 82 appears for some reason I don't know.
Thank you
Changed Code
#include <stdio.h>
#include <stdlib.h>
void swap_double(double *d1, double *d2);
int main()
{
double a, b;
printf("Write two variables of the type double.\n");
scanf("%lf", &a);
scanf("%lf", &b);
printf("Normal variables: %lf %lf\n", a, b);
swap_double(&a, &b);
printf("Changed variables: %lf %lf\n", a, b);
return 0;
}
void swap_double(double *d1, double *d2)
{
double aux;
aux=*d1;
*d1=*d2;
*d2=aux;
}
Related
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
// scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(2, 7));
Please focus on this line why it gives address + 0 = 9//
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
// produces outPut : 199164000 + 0 = 9
You commented the scanf function and also didn't given values for the a and b. So it printed garbage values. You also need to pass a and b into the add(a,b) function.
#include<stdio.h>
int add(int, int); // function prototype
int main()
{
int a, b;
// printf("Enter 2 integer numbers\n");
scanf("%d%d", &a, &b);
//function call add(a, b);
printf(" %d + %d = %d \n", a, b, add(a, b));
return 0;
}
//function definition
int add(int x, int y)
{
return x+y;
}
It is not printing any address. It is printing garbage values. You have not given any values to the variable a and b. So it will print garbage values. Why you commented scanf statement. Just stop commenting it and it will work.
I wrote a simple C program with a function that asks for two floating point numbers from the user passed by reference and the two numbers are printed to the screen using printf in main. My compiler is warning me about return at the end of the function "Expression result unused" and recommending to change it to this instead return (void)(*a),*b; What is the correct way to return this function?
#include <stdio.h>
float getFloats(float *a, float *b);
int main()
{
float num1,num2;
getFloats(&num1,&num2);
printf("%.2f, %.2f\n", num1, num2);
return 0;
}
float getFloats(float *a, float *b)
{
puts("Enter numbers:");
scanf("%f", a);
scanf("%f", b);
return *a,*b;
}
changed it to void and it works. Thank you
#include <stdio.h>
void getFloats(float *a, float *b);
int main()
{
float num1,num2;
getFloats(&num1,&num2);
printf("%.2f and %.2f\n", num1, num2);
return 0;
}
void getFloats(float *a, float *b)
{
puts("Enter numbers:");
scanf("%f", a);
scanf("%f", b);
}
In C you can't return more than one value. If you want to return more you need to wrap them into the struct.
struct TwoFloats
{
float a,b;
};
struct TwoFloats getTwoFloats(void)
{
struct TwoFloats tf;
if(scanf("%f %f", &tf.a, &tf.b) != 2)
{
/* error handling */
}
return tf;
}
I'm writing a quadratic equation root solver for class. I either get %f expects argument type double but arguments 2 and 3 have type float on lines 45 and 51, or I get -nan and-inf as an answer when I get it to compile. I can't figure out any other way to get it to work. I cannot use doubles on this. Only floats and ints with the sub programs.
#include <stdio.h>
#include <math.h>
void solve_linear(int, int);
void solve_quad(int, int, int);
void solve_real(int,int, int);
void solve_complex(int, int, int);
int main (int argc, char *argv[]){
int a, b, c;
if (argc==4) {
sscanf(argv[1], "%d", &a);
sscanf(argv[2], "%d", &b);
sscanf(argv[3], "%d", &c);
if (a=0){
if (b=0){
printf("Error. A and B cannot both be 0\n");
}
else solve_linear(b, c);
}
else solve_quad(a,b,c);
}
else printf("Error. Must enter 3 numbers on command line.\n");
}
void solve_linear(b, c){
float root;
root=(float)-c/b;
printf("%f\n", root);
}
void solve_quad(a, b, c){
if(b*b-4*a*c<0){
solve_complex(a,b,c);
}
else{
solve_real(a, b, c);
}
}
void solve_real(a, b, c){
float x1, x2;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("%f, %f\n", &x1, &x2);
}
void solve_complex(a, b, c){
float x_real, x_img;
x_real=-b/(2.0*a);
x_img=(sqrt(abs(b*b-4*a*c)))/(2*a);
printf("%f + %fi\n", &x_real, &x_img);
}
As soon as you call printf, all float arguments are automatically converted to double. You cannot prevent this.
Enable your compiler's warnings, they will tell you that printf expects direct values instead of pointers, so the code should be printf("%f +%f = %f\n", a, b, a + b);.
This is the difference: scanf needs pointers (since it writes to the variables), pintf only needs the values themselves (so no need for pointers).
I am new to both this site and C programming and I am attempting to make a quadratic formula but I cannot get the roots to work. I believe I am not calling a function or perhaps there is something else wrong. Any help would be appreciated, thank you!
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
return 0;
}
float root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f,x1 , x2");
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f,x");
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2f,x3i and");
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2f,x4i");
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
You never call the function root() so it will never be executed. Put the function call somewhere before return 0; of main():
root(a, b, c);
Also, fix the printf() calls as mentioned above.
I'm not sure if this will fix everything but in your code you have your print statements formatted incorrectly. For example, this line:
printf("%.2f and %.2f,x1 , x2");
Should be printf("%.2f and %.2f", x1, x2);
The variables whose values you are trying to use should be outside of the quotation marks. Hopefully this helps.
You have several errors.
You never call root().
root is declared to return float, but it doesn't return anything, it just prints the roots. It should be declared void.
You have mistakes in many of your printf() calls. When you're displaying the equation, you have |n instead of \n, and %+f instead of +%f. When you're displaying the roots, you have the variables that you want to print inside the format string, instead of as separate arguments to printf.
Here's the corrected code.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
void root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
root(a, b, c);
return 0;
}
void root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f" ,x1 , x2);
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f",x);
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2fi and ", x3);
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2fi", x4);
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
DEMO
The function with the pass by value works like it ought to, I believe, as it prints out the value 0 whenever I enter two numbers. However the adderRef (pass by reference function) doesn't work. All it prints out is "the pass by reference of c is" and that's it. There is no value or anything. I just wanted to inquire whether there was something wrong with my syntax or something....
Okay guys sorry about the question being vague and for my errors. It's my first time asking on stackoverflow and I should have been more mindful. I'm aware of my error and why I made it. I got muddled in class when my teacher was altering the code a bit and I copied it down incorrectly/ Thanks everyone for your help. Yes i was indeed quite dumb .
#include <stdio.h>
#include <stdlib.h>
void adderval(int a, int b);
void adderRef(int *, int *);
int main()
{
int a, b, c = 0;
printf("enter two numbers.\n");
scanf("%d %d", &a, &b);
adderval(a, b);
printf("the pass by value of c is %d \n", c);
adderRef(&a, &b);
printf("the pass by reference of c is \n", c );
return 0;
}
void adderRef(int *a, int *b )
{
int c;
c = *a + *b;
}
void adderval(int a, int b )
{
int c;
c = a + b;
}
corrected as below.
#include <stdio.h>
#include <stdlib.h>
void adderval(int a,int b,int c);
void adderRef(int a,int b,int *c );
int main()
{
int a,b,c=0;
printf("enter two numbers.\n");
scanf("%d %d",&a,&b);
adderval(a,b,c);
printf("the pass by value of c is %d \n",c);
adderRef(a,b,&c);
printf("the pass by reference of c is %d \n",c );
return 0;
}
void adderRef (int a, int b, int *c )
{
*c = a + b;
}
void adderval (int a , int b,int c )
{
c=a+b;
}