Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
#include <stdio.h>
double minimum(double x, double y, double z) {
double temp = 0;
//Logic here
}
int main(void) {
double x, y, z, minVal;
printf("Please enter three numeric values: ");
scanf("%lf%lf%lf", &x, &y, &z);
minVal = minimum(x, y, z);
printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);
return 0;
}
Logic of the code should go within comment in first function. Function should then result in minVal and printed too console
#include <stdio.h>
#include <math.h>
double minimum(double x, double y, double z)
{
double temp = 0;
if (isnan(x) || isnan (y) || isnan(z))
return NAN;
temp = (x < y) ? x : y;
return (temp < z)? temp : z;
}
int main(void) {
double x, y, z, minVal;
printf("Please enter three numeric values: ");
scanf("%lf%lf%lf", &x, &y, &z);
minVal = minimum(x, y, z);
printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);
return 0;
}
method for double:
int main(void)
{
double a, b, c, temp, min;
printf ("Enter three nos. separated by spaces: ");
scanf ("%lf%lf%lf", &a, &b, &c);
temp = (a < b) ? a : b;
min = (c < temp) ? c : temp;
printf ("The Minimum of the three is: %lf", min);
/* indicate success */
return 0;
}
method for int:
int main(void)
{
int a, b, c, temp, min;
printf ("Enter three nos. separated by spaces: ");
scanf ("%d%d%d", &a, &b, &c);
temp = (a < b) ? a : b;
min = (c < temp) ? c : temp;
printf ("The Minimum of the three is: %d", min);
/* indicate success */
return 0;
}
First of all you should have return type double for the minValue function
Then your logic should be
Consider 3 numbers a, b, c and another double temp
Then...
If (a < b)
Temp = a
Else
Temp = b
If(temp < c)
Return temp
Else
Return c
Related
I just implemented my swap function but it does not print anything. Do you know why does the line printf does not execute?
#include <stdio.h>
int swap(int x, int y) {
scanf("%d", &x);
printf("%d, x is",x);
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
int main() {
swap(6,5);
}
You should not take user input inside the swap function. Its purpose should be to swap two integers only. You can move the scanf statements to main function.
#include <stdio.h>
int swap(int x, int y){
int temp = x;
x = y;
y = temp;
printf("After Swapping in swap function: x = %d, y = %d", x, y);
return 0;
}
int main(void){
int x, y;
scanf("%d", &x);
printf("%d, x is", x);
scanf("%d", &y);
printf("%d, y is", y);
swap(x, y);
printf("After Swapping in main function: x = %d, y = %d", x, y);
}
But the above code has a major issue. Though the swap function prints the integers passed as they are swapped but the fact is x and y in the main remains unaffected.
In this case to make it work, using pointers would be helpful
void swap(int *ptrx, int *ptry){
int temp = *ptrx;
*ptrx = *ptry;
*ptry = temp;
}
In the main function call the swap as swap(&x, &y);
Use this code for swapping.
#include <stdio.h>
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("After Swapping: x = %d, y = %d", x, y);
}
int main()
{
swap(6,5);
return 0;
}
And I don't understand why you need to scan x & y
In this code I thought I would get the result of calculation x / y and x - y but the program shows 0 for i and j. What is wrong?
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It is undefined behavior, when you call the calculate() function within the same printf and i as well as j are calculated within that printf (same function). By the way, it is not a good idea to use global variables ( i, j ) ... For test purpose only, you could calculate() before the next printf of i and j.
You can test that behavior with:
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f", calculate(a, b));
printf(" %f %f\n", i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It might be related to the parsing, reference, and execution order of the arguments in printf function. The printf function uses the arguments right-to-left direction. You can easily check the order through below codes.
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
//printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
printf("\nThe results are: %f %f %f\n", i, j, calculate(a, b));
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
when I am giving input as -1 1 0 my output should be 1 but it is giving 0
why?
it is all about adding three numbers
int calsum (int x,int y,int z)
{
return ;
}
Your function is declared and defined to return an int, but your return statement is expressionless. It's a language constraint violation.
The behavior of your program is undefined. Funny results is a possible outcome in this case.
Update your calsum function as below. You are assigning d=1 in else part but not returning it.
int calsum (int x,int y,int z){
int d;
d=x+y+z;
if(d>2)
return d;
else
return 1;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to write a C Function that takes three integers as arguments and returns the value of the largest one.
int largest(int x,int y,int z)
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);
if(y>=x && y>=z)
printf("Largest number = %d", y);
if(z>=x && z>=y)
printf("Largest number = %d", z);
}
I have tried this codes but they don't work i need help please I am also a beginner at this
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
This is a pretty short way of doing what you want:
int largest(int a, int b, int c)
{
a = (a > b) ? a : b;
a = (a > c) ? a : c;
return a;
}
To return a value you must use a return statement in function.
Let us inspect what you've done,
#include<stdio.h>
int largest(int x,int y,int z)/* missed ';' */
/* missed 'void main()' */
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3); /* missed format specifier for other two values */
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);/* written print statement instead of return statement */
if(y>=x && y>=z)
printf("Largest number = %d", y);/* written print statement instead of return statement */
if(z>=x && z>=y)
printf("Largest number = %d", z);/* written print statement instead of return statement */
}
After modification the code should be like this,
#include<stdio.h>
int largest(int x,int y,int z);
int main()
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
return x;
if(y>=x && y>=z)
return y;
if(z>=x && z>=y)
return z;
}
While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}