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 8 years ago.
Improve this question
I am having difficulty getting the real output from a C function. For example:
int max3(int a, int b, int c){
if ((a>b)&&(a>c))
return a;
if ((b>c)&&(b>a))
return b;
return c;
}
Can you give me an idea of how to specify the real output (e.g. tools, algorithms, etc.)?
In this above example, the real output is 6 (in case (a,b,c) = (1,2,6)).
Thank in advance very much.
I'd have probably written it, as it is simple, using the ternary operator:
int max3(int a, int b, int c){
if (a>b)
return (a>c)?a:c;
else
return (b>c)?b:c;
}
Related
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 3 months ago.
Improve this question
Which could be a good method to compute the distance between a cluster and a point to decide if the point can be assigned to the cluster?
I read about Mahalanobis distance that I have implemented in this way:
float mahalanobis(float *cluster, float *point, const float *variance, int d) {
float distance=0;
for (int i = 1; i < d; ++i) {
distance+=pow((point[i] - cluster[i])/variance[i-1], 2);
}
return sqrt(distance);
}
but it seems that the result isn't good. Maybe I'm wrong about the implementation
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
so I've written some codes to add two numbers in C, but I can't figure out how to add three instead without spilling the results into another variable or clobbering the caller's version of the variables?
int add(a,b,c)
int a, b, c;
{ int tempr;
for(;b--;++a); // danger
/*tempr = c+a;*/
tempr = a+c;
return (tempr);
}
Here's how, if you must use a function for some reason.
int add(int a,int b,int c) {
return a+b+c;
}
When you define a function in C, you have to define the type of the parameters as well.
Please note the following:
It is an error to define a variable in a function that has the same name as a function parameter
Although not an error, there's no need for parentheses when calling return (tempr);.
return tempr; is perfectly fine
if you use a for loop, ending it with ; immediately after the for statement will result in the following statement not being a part of the loop. This might not be what you had in mind.
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
Hi can anyone help me to correct this code, the result should be
/c=4.000000/
/d=4.0000 /
I know by putting the logic in single printf() i will get my result but i am not understanding that how to use two printf() and the varibles will be given by the second printf().
Here is my code:-
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
putchar(10);
return 0;
}
If I change my code to this, I will get the result,
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
/*
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
*/
printf("/c=%12f/\n/d=%-12.4f/",c,d);
putchar(10);
return 0;
}
but I want to use two printf() statements.
Thanks in advance.
You can not do this:
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
because you are lying to both printfs, in the first one you don't use the specifiers and in the second one you use specifiers that are not expected.
You can do this:
printf("/c=%12f/\nd=%"
"-12.4f/",c,d);
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 5 years ago.
Improve this question
I came across this obfuscated code recently:
#include <stdio.h>
#define _____(i,s,o,g,r,a,m)(i##r##s##o)
#define _ _____(m,i,n,u,a,l,s)
int _()
{
printf("Hello World!");
return 0;
}
What is happening here? How is it that _() is main()?
Edit:
I was looking for the 'technical term' that has been used here.
_ is replaced by ____(m,i,n,u,a,l,s)
____(m,i,n,u,a,l,s) which is filtered through the macro ____(i,s,o,g,r,a,m)(i##r##s##o)
i##r##s##o pastes the arguments i, r, s, o together to form text. i = m, r = a, s = i, o = n, thus you get main
This technique is called 'token pasting'. It is not something you'll use everyday, but there are times where it can be very useful. See GCC's documentation on token pasting.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;