How can I get my program to print true or false like my function calls for? After entering the three sides nothing happens. Thanks.
#include <cs50.h>
#include <stdio.h>
//declare functions
bool valid_triangle(float x, float y, float z);
int main(void)
{
float x = get_float("Side 1 length: ");
float y = get_float("Side 2 length: ");
float z = get_float("side 3 length: ");
bool valid_triangle(float x, float y, float z);
}
bool valid_triangle(float x, float y, float z)
{
if (x + y > z && y + z > x && x + z > y)
{
return true;
}
else
{
return false;
}
}
You have never called your function. The syntax that appears in your main function to call the function actually is the declaration syntax. A function call statement should not include type information. You should call your function as follows and then decide to print true or false based on your function:
int main(void)
{
float x = get_float("Side 1 length: ");
float y = get_float("Side 2 length: ");
float z = get_float("side 3 length: ");
if(valid_triangle(x, y, z))
printf("True");
else
printf("False");
}
Your validation function can be written more tersely as follows:
bool valid_triangle(float x, float y, float z)
{
return (x + y > z && y + z > x && x + z > y);
}
This is because the validation expression evaluates to a boolean expression already. So you don't need the if-else.
You never call the function. You also have no code to print "true" or "false". You probably want something like:
if (valid_triangle (x, y, z))
puts("true\n");
else
puts("false\n");
int main(void)
{
float x = get_float("Side 1 length: ");
float y = get_float("Side 2 length: ");
float z = get_float("side 3 length: ");
valid_triangle(x, y, z);
}
printf(valid_triangle(x, y,z)?"true":"false");
Related
I have in my code the specificacions of the bool to return true or false but I don't figure how to printf depending if it is true or false.
I have some float prompted by the user and the bool to determine if it is true or false.
How can I use these both datas to printf depending?
bool valid_triangle(float x, float y, float z);
int main(void)
{
float x;
float y;
float z;
do
{
x = get_float("Enter a length: ");
}
while(x <= 0);
do
{
y = get_float("Enter other length: ");
}
while(y <= 0);
do
{
z = get_float("The last one: ");
}
while(z <= 0);
}
if(valid_triangle = 1)
{
printf("It's a triangle");
}
bool valid_triangle(float x, float y, float z)
{
if(x <= 0 || y <= 0 || z <= 0)
{
return false;
}
if((x + y <= z) || (x + z <= y) || (y + z <= x))
{
return false;
}
return true;
}
I tried an if conditional with the bool but doesn`t compile.
This:
if(valid_triangle = 1)
tries to re-assign the valid_triangle function to the integer 1. That does not compile; the names of functions behave like constants and cannot be assigned to.
You need something like:
if (valid_triangle(x, y, z))
{
printf("yay, got a valid triangle with sides %f, %f and %f!\n", x, y, z);
}
The above calls the valid_triangle() function, passing it the three values you collected from the user. If the return vale is non-zero (which true is), the if will execute its body, otherwise it will be skipped.
Note that I (strongly) advise against explicit comparisons against booleans, since the comparison operator itself simply results in a boolean value which makes it feel painfully recursive to me. I also don't think it's any clearer.
Your valid_triangle function expects 3 arguments x, y, z. You have to pass the values you take from user to the function.
if(valid_triangle(x, y, z) == true)
{
printf("It's a triangle");
} else {
printf("It's not a triangle");
}
As other questions have shown ways to correct the errors in your code, I'll try to answer your primary question:
printf("%s a triangle\n",
valid_triangle(x, y, z)
? "It's"
: "It is not");
can be a compact and efficient way to print your boolean result in a user
friendly way. Also
puts(valid_triangle(x, y, z)
? "It's a triangle"
: "It is not a triangle");
is even more compact, and more efficient, as it avoids the parameter substitution that requires printf(3) to substitute the proper string
above.
I have problems for my function, since after the first expression, the values inserted to my parameters (float x, float y) suddenly becomes 0.
I called this function with main (), the output for c = x/y works as expected. But for d = x - (y * c), it gives me an output of 0, I check where the problem is and it appears to be because of x and y since they both have 0 values for some reason
I have not finished my function for greatest common divisor, since I'm still stuck at this problem
'''
int gcd (float x, float y)
{
int c = x / y;
printf ("This's your quotient: %d\n", c);
int d = x - ( y * c ); // d = 0 since y, x = 0
printf ("This's your remainder: %d\n", d);
printf ("c %d\n",c); // works normally
printf ("y %f\n",y); // y = 0
printf ("x: %f\n",x); // x = 0
}
Here is example code that fixes the issues I noticed and changing the signature to separate calculations from output:
#include <stdio.h>
void quotient_reminder(unsigned x, unsigned y, unsigned *q, unsigned *r) {
if(y == 0) return;
*q = x / y;
*r = x - (y * *q); // d = 0 since y, x = 0
}
int main() {
unsigned q, r;
quotient_reminder(25.0, 7.1, &q, &r);
printf ("This's your quotient: %u\n", q);
printf ("This's your remainder: %u\n", r);
}
and it will return:
This's your quotient: 3
This's your remainder: 4
I have a problem with this code as I can't seem to find the problem? Here is the problem that I tried to solve: - Declare and write a function called valid_triangle that takes three real numbers representing the lengths of the three sides of a triangle as its arguments, and outputs either true or false, depending on whether those three lengths are capable of making a triangle.
Following rules about triangles:
A triangle may only have sides with a positive length.
The sum of the lengths of any two sides of the triangle must be greater than the length of the third side.
//includes
#include <stdio.h>
#include <cs50.h>
bool valid_triangle(float x, float y, float z);
int main (void)
{
float x = get_float("x:");
float y = get_float("y:");
float z = get_float("z:");
bool w = valid_triangle(x, y, z);
}
bool valid_triangle(float x, float y, float z)
{
// only positive sides
if (x <= 0 || y <= 0 || z <= 0)
{
return false;
printf("false\n");
}
// sum of the lengths of any two sides of the triangle must be greater than the length of the third side.
else if (x + y < z || x + z < y || y + z < x)
{
return false;
printf("false\n");
}
else
{
return true;
printf("True\n");
}
return 0;
}
For starters this if statement
else if (x + y < z || x + z < y || y + z < x)
is not correct, It should look like
else if (x + y <= z || x + z <= y || y + z <= x)
And statements after return statements like in this code snippet
return false;
printf("false\n");
do not have an effect. It seems you mean
printf("false\n");
return false;
Though the calls of printf should not be inside the function.
You could place only one call of printf in main like
bool w = valid_triangle(x, y, z);
printf( "%s\n", w == false ? "False" : "True" );
Also the last return statement within the function
return 0;
is redundant.
Here is a demonstrative program.
#include <stdio.h>
#include <stdbool.h>
bool valid_triangle(float x, float y, float z)
{
// only positive sides
if (x <= 0 || y <= 0 || z <= 0)
{
return false;
}
// sum of the lengths of any two sides of the triangle must be greater
// than the length of the third side.
else if ( x + y <= z || x + z <= y || y + z <= x)
{
return false;
}
else
{
return true;
}
}
int main(void)
{
float a = 4.0f, b = 5.0f, c = 6.0f;
printf( "%.1f, %.1f, %.1f are sides of a triangle - %s\n", a, b, c,
valid_triangle( a, b, c ) ? "true" : "false" );
return 0;
}
The program output is
4.0, 5.0, 6.0 are sides of a triangle - true
// This code should work as a solution for the valid_triangle program. Hope it helps
#include <cs50.h>
#include <stdio.h>
// first declare the function with the semi-colon at the end
bool valid_triangle(float x, float y, float z);
int main(void)
{
// Ask users for inputs
float a = get_float("Give me the first integer to form a triangle: ");
float b = get_float("Give me the second integer to form a triangle: ");
float c = get_float("Give me the third integer to form a triangle: ");
// function call
bool result=valid_triangle(a, b, c);
//set condition for your boolean variable 'result'
if (result == true)
{
printf("True \n");
}
else
{
printf("False \n");
}
}
//function definition, without any semi-colon at the end
bool valid_triangle(float x, float y, float z)
{
//checking if any of the sides is negative or equal to zero
if (x<=0 || y<=0 || z<=0)
{
return false;
}
// sum of the lengths of any two sides of the triangle must be greater than the length
//of the third side.
else if ((x+y<=z) || (x+z<=y) || (z+y<=x))
{
return false;
}
return true;
}
I am now learning C programming language and try to understand the functions.
The code below does not give error in Visual Studio and returns the first argument sent to that function.
I think it should give an error but it does not.
How does the given code work and how to return the first argument which is 3 in the code?
#include <stdio.h>
#include <stdlib.h>
int hesapla(int x, int y);
void f();
int main()
{
int a, b, sonuyc;
int s;
printf("\n");
printf("sonuc = %d", hesapla(3, 10));
system("pause");
return 0;
}
int hesapla( int x, int y) {
if (x > y )
return y;
if (x == y)
return y + 1;
}
Thank you..
Your function hesalpha does not return anything when x < y , therefore, it exhibits undefined behaviour .
Either you can handle this case with an if else or just return a value as default when both the previous conditions are false .
Toy need to ensure that all pathes explicitly return a value, otherwise you'll get whatever happens to be left in whatever register is used to return values from a function which is non-deterministic.
int hesapla( int x, int y)
{
if (x > y )
return y;
if (x == y)
return y + 1;
return x ;
}
Though for your own sanity when your code becomes more complex, and to avoid the issue altogether I suggest:
int hesapla( int x, int y)
{
int ret = x ;
if (x > y )
{
ret = y;
}
else if(x == y)
{
ret = y + 1;
}
return ret ;
}
For simple functions such as this, it is possible to return the result in a single expression thus:
int hesapla( int x, int y)
{
return (x > y) ? y : (x == y) ? y + 1 : x ;
}
Succinct perhaps, but I am not sure I'd recommend it.
Neither if-statement is true, so hesapla () will return 0, the default return value for int functions.
It's unclear exactly what you're trying to do, but maybe you're after this?
int
hesapla (int x, int y)
{
if (x == y) {
return y + 1;
}
if (x > y) {
return y;
}
return x;
}
Though, this this better...
int
hesapla (int x, int y)
{
if (x == y) {
return x + 1;
}
return min (x, y);
}
I have the following code in C to make some arithmatic calculation
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x,y;
float z;
printf("Enter x y z \n");
scanf("%f %f %f ", &x, &y , &z);
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
printf("\n z = %f", z);
return 0;
}
when i build the program , i get the following error message in the following code line
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
called object is not a function or dunction pointer
That's a typo, you're missing an operator:
z = ((4.2 * (x + y))) / (z - (0.25*z)) / (y + z) / ((x + y) * (x + y));
^
whatever the operator is
C has no support for mathematics-implicit multiplication operator (more or less as you would write in an equation in school). E.g.
// y = 2x
int y;
y = 2 * x;