#include <stdio.h>
float function (float x, float y);
float function2 (float x, float z);
float function3 (float y, float z);
float main()
{
float x;
float y;
float z;
{
printf("Please insert length of adjacent side");
scanf("%f", &x);
printf("Please insert length of opposite side");
scanf("%f", &y);
printf("Please insert length of the hypotenuse");
scanf("%f", &z);
}
{
if (z = 0){
printf("The length of the hypotenuse is %f", function (x, y));}
else if (y = 0){
printf("The length of the opposite side is %f", function2(x, z));}
else if (x=0){
printf("The length of the adjacent side is %f", function3(y, z));}
}
}
float function(float x, float y) {
return(sqrt(((x*x)+(y*y))));
}
float function2(float x, float z) {
return(sqrt(((z*z)-(x*x))));
}
float function3(float y, float z){
return(sqrt(((z*z)-(y*y))));
}
This is the code that I have to figure out the missing side of a right triangle. The input for the side that you do not know is 0. When I run the program it asks me for all the sides but then it does not go on and give me the answer...Could anyone please explain this?
Thanks
= is an assignment operator. Replace z = 0 and any others like it with z == 0
if (z == 0){ // = changed to ==
printf("The length of the hypotenuse is %f", function (x, y));}
else if (y == 0){ // = changed to ==
printf("The length of the opposite side is %f", function2(x, z));}
else if (x == 0){ // = changed to ==
printf("The length of the adjacent side is %f", function3(y, z));}
C Operators Reference Sheet
Related
Im writing a simple program to show the distance/time between two converging trains. I wanted to test the program and return output value through the function float converge, and then apply that to the main function, but it seems that the converge function does not work.
#include <stdio.h>
float converge(int x, int y, int z) {
return x / (y + z);
}
int main(void) {
int dist, speed1, speed2;
printf("Enter distance in miles");
scanf("%d\n", &dist);
printf("Enter train 1 speed and train 2 speed in mph");
scanf("%d%d\n", &speed1, &speed2);
converge(dist, speed1, speed2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
float converge (float x, float y, float z)
{
int time=x/(y+z);
return time;
}
int main ()
{
float dist, speed1, speed2;
printf("Enter distance in miles:\t");
scanf("%f", &dist);
printf("Enter speed of first train in mph:\t");
scanf("%f", &speed1);
printf("Enter speed of second train in mph:\t");
scanf("%f", &speed2);
printf("Time between this two trains is %f",converge(dist, speed1, speed2));
}
There are multiple problems in your code:
converge performs integer arithmetic and converts the result to float only for the return value. If you want to compute a fractional number, you should change it to: double converge(int x, int y, int z) { return (double)x / ((double)y + z); } or better use double for the input values and the argument types:
double converge(double x, double y, double z) { return x / (y + z); }
There are trailing newlines in the scanf() conversion formats: this will cause scanf() to consume any trailing white space typed after the numbers, including any number of newlines typed at the prompts. You will not get the second prompt as long as you enter empty lines. Remove these \n from the format strings.
The result of the computation is not printed.
Here is a modified version:
#include <stdio.h>
double converge(double x, double y, double z) {
return x / (y + z);
}
int main(void) {
double dist = 0, speed1 = 0, speed2 = 0;
printf("Enter distance in miles: ");
scanf("%lf", &dist);
printf("Enter train 1 speed and train 2 speeds in mph: ");
scanf("%lf%lf", &speed1, &speed2);
if (speed1 + speed2 <= 0)
printf("No collision\n");
else
printf("Time until collision: %f seconds\", 3600 * converge(dist, speed1, speed2));
return 0;
}
Why is this code not producing an output?
It produces no output for the expected output from the result of converge() because there is no statement in the provided code, which could cause this output.
You need for example one printf() statement after the call to converge() in order to print the result of converge():
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
float converge_result;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
converge_result = converge(dist, speed1, speed2);
printf("The time until the two trains encounter each other is:\n %f",converge_result);
return 0;
}
or alternatively:
#include <stdio.h>
float converge (int x, int y, int z)
{
return x/(y+z);
}
int main (void)
{
int dist, speed1, speed2;
printf("Enter the distance between the two trains in miles:\n");
scanf("%d", &dist);
printf("\n");
printf("Enter the speed of train 1 and the speed of train 2 in mph:\n");
scanf("%d %d", &speed1,&speed2);
printf("\n");
printf("The time until the two trains encounter each other is: \n%f ",
converge(dist,speed1,speed2);
return 0;
}
By the way, the calculation of the distance in time seems incorrect or at least incomplete.
I am new to the C programming language. I am attempting to run the code below an N amount of times (based on the user input of "Enter amount of iterations"). I am trying to do this using a for loop (also tried with a while loop) but have been unsuccessful.
Whenever I run the code below, my terminal continuously repeats "Enter two float numbers:". I have to close the terminal and reopen it to try again. Does the issue have to do with my for loop? I am interpreting my for loop as: "a=0; if a > 0; increment a". Is there a way I can set a limit for "if a > 0" or should I be using a while loop? If the user enters "3" for amount of iterations, I am expecting the program to ask "Enter two float numbers" 3 times (with the answer).
float sum (float m, float n){
return m+n;}
int main() {
float x, y;
int a;
printf("Enter amount of iterations: ");
scanf("%d", &a);
for (int i; i < 0; i++) {
printf("Enter two float numbers: ");
scanf("%f %f", &x, &y);
float su = sum(x,y);
printf("%f and %f = ", x, y);
printf("%f\n", su);}
return 0;}
CORRECT ANSWER Formatted for readability:
float sum(float m, float n)
{
return m + n;
}
int main()
{
float x, y;
int a;
printf("Enter amount of iterations: ");
scanf("%d", &a);
for (int i = 0; i < a; i++)
{
printf("Enter two float numbers: ");
scanf("%f %f", &x, &y);
float su = sum(x, y);
printf("%f and %f = ", x, y);
printf("%f\n", su);
}
return 0;
}
This should behave more like you would like it to:
#include <stdio.h>
static float sum(float m, float n)
{
return m + n;
}
int main(void)
{
float x, y;
int a;
printf("Enter amount of iterations: ");
if (scanf("%d", &a) != 1)
{
fprintf(stderr, "Invalid input for iterations\n");
return 1;
}
for (int i = 0; i < a; i++)
{
printf("Enter two float numbers: ");
if (scanf("%f %f", &x, &y) != 2)
{
fprintf(stderr, "Failed to read to floating point numbers\n");
return 1;
}
float su = sum(x, y);
printf("%f and %f = ", x, y);
printf("%f\n", su);
}
return 0;
}
Note that it checks that the input operations are successful, and reports errors on standard error (stderr). The code uses a standard C for loop to count from 0 up to a limit — this is idiomatic C. You should get used to using it.
As I noted in a comment, the a in the for loop is different from and unrelated to the a declared earlier in your code and set by the input operation. The a in the for loop is not initialized; you can't tell how many times the loop will be executed. A good compiler should warn you about redefining or shadowing a.
for (i = 0; i < a; i++); - answer provided by J.S!
Making a program that should calculate a polynomials y values derivative and integral... Right now having problems formatting the for loop so it cycles through all the x values I want it to (as determined by user).
Pretty sure the math func isnt fully correct right now too but I can go at them later once i get this loop working haha
heres my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x=0;
double fx=0, fD=0, A=0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
for(int i=xi; i<xf; i++) {
math(a, b, c, x, xi, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2*a) + b; //finding derivative values
*A = ((a/3)*pow(x,3) + (a/2)*pow(x,2) + c*x) - ((a/3)*pow(xi,3) + (a/2)*pow(xi,2) + c*xi); //finding integral values
return;
}
Heres a screenshot of the output, as you can see the table is printing only till 1 instead of 5 like wanted (indicated by inputs). I need to change whats in the for loop to get it to output right but Im not sure what to do
I saw some problem with your code:
1> Your derivation function is not correct. should be 2*a*x + b
2> I change the for loop to while loop with step is xc inputed from stdin
Here is my solution with your newest answer:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);
int main()
{
double a, b, c;
double xi, xf, xc, x = 0;
double fx = 0, fD = 0, A = 0;
printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
explain();
printf("\n\nEnter a value for a: ");
scanf("%lg", &a);
printf("Enter a value for b: ");
scanf("%lg", &b);
printf("Enter a value for c: ");
scanf("%lg", &c);
printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);
printf("\n\nEnter your initial x-value: ");
scanf("%lg", &xi);
printf("Enter your final x-value: ");
scanf("%lg", &xf);
printf("Enter what you would like to increment by: ");
scanf("%lg", &xc);
printf("| x | f(x) | f'(x) | A |\n"); //printing table
printf("----------------------------------------\n");
x = xi;
double nextX;
while (x <= xf) {
nextX = x + xc;
math(a, b, c, x, nextX, &fx, &fD, &A);
printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
x = x + xc;
}
return 0;
}
void explain() {
printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
*fx = (a*(x*x)) + (b*x) + c; //finding y values
*fD = (2 * a * x) + b; //finding derivative values
*A = ((a / 3)*pow(x, 3) + (a / 2)*pow(x, 2) + c * x) - ((a / 3)*pow(xi, 3) + (a / 2)*pow(xi, 2) + c * xi); //finding integral values
return;
}
While analyzing can't seem to avoid the value stored to 'delta' not being read... what part of my loop isn't working and why?
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float delta;
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
float x, x1, x2;
delta=((b*b)-(4*a*c));
if("delta==0")
{
x=((-b)/(2*a));
printf("x=%f \n", x);
}
else if("delta>0")
{
x1=((-b+sqrt(delta))/(2*a));
x2=((-b-sqrt(delta))/(2*a));
printf("x1=%f i x2=%f \n", x1, x2);
}
else printf("Nie ma w zbiorze liczb rzeczywistych rozwiazania tego rownania.\n");
return 0;
}
1st thing, if("delta==0") inside "" whatever is present that will be treated as a address so if(address) means true, remove the double quotation.
if(delta==0)
// some code
else if (delta > 0)
// some code
2nd thing, you are comparing(==) a float & an integer, so be aware of behaviour of comparing different operands.
I have an assignment to compute the roots of quadratic equations in C, should be pretty simple and I know what I need to do with the program but I am having a problem nonetheless.
It works fine when the roots are imaginary and when the term inside the square root is zero.
But when I enter coefficients a, b and c which would give real roots it gives me the wrong answer, and I cant figure out whats wrong. (I am testing it with a=2, b = -5 and c=1)
This is my code, it compiles and runs, but gives the wrong answer.
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, D, x, x1, x2, y, xi;
printf("Please enter a:\n");
scanf("%f", &a);
printf("Please enter b:\n");
scanf("%f",&b);
printf("Please enter c:\n");
scanf("%f", &c);
printf("The numbers you entered are: a = %f, b = %f, c = %f\n", a, b, c);
D = b*b-4.0*a*c;
printf("D = %f\n", D);
if(D > 0){
x1 = (-b + sqrt(D))/2*a;
x2 = ((-b) - sqrt(D))/2*a;
printf("The two real roots are x1=%fl and x2 = %fl\n", x1, x2);
}
if(D == 0){
x = (-b)/(2*a);
printf("There are two identical roots to this equation, the value of which is: %fl\n", x);
}
if (D<0){
y = sqrt(fabs(D))/(2*a);
xi = (-b)/(2*a);
printf("This equation has imaginary roots which are %fl +/- %fli, where i is the square root of -1.\n", xi, y);
}
return 0;
}
You don't calculate the result correctly:
x = y / 2*a
is actually parsed as
x = (y / 2) * a
so you have to put parentheses around 2*a.
you want this:
x = y / (2 * a)