Max of 4 numbers using another function (max of 2) - c

This is about how I can use a function inside another function. I'm trying to get the max of 4 numbers using another function that determines the max of 2. The errors I get are
expected expression before int
and
to few arguments to function max2
I tried to search what they mean however I didn't really understand much... thank you for any help
int max2(int a, int b) {
if(a > b) {
printf("%d is the max\n", a);
}
else {
printf("%d is the max\n", b);
}
}
int max4(int a, int b, int c, int d) {
if(a > b)
{
if(a > c)
{
max2(int a, int d);
}
else
{
max2(int c, int d);
}
}
else
{
if(b > c)
{
max2(int b, int d);
}
else
{
max2(int c, int d);
}
}
}
int main() {
max4(666,853,987,42);
}

You declare functions returning int, but those functions return nothing. Probably you'd want something like this:
#include <stdio.h>
int max2 (int a, int b) { return a > b ? a : b; }
int max4 (int a, int b, int c, int d) { return max2(max2(a, b), max2(c, d)); }
int main (void) {
printf("%d is the max\n", max4(666,853,987,42));
}

Welcome to programming!
You should take a closer look here to see why you are incorrectly calling (important keyword) your function. Additionally you should look into the return keyword. (A quick look at this answer or the more in depth Microsoft article should help)
Also, as mentioned in the comments, your solution is a bit weird. Try finding the max of 20 numbers, by using for or while loops, as well as an array.
Happy learning!

Related

how to fix "Function definition is not allowed here" in Xcode

This is my lecture example. And, I follow it.
How to fix the error?
#include <stdio.h>
#define SPACE ' '
int main(){
void branching_if_judgement(int a, int b)
{
if (a > b)
{
printf("a(%d) is larger than b(%d)\n", a, b);
}else
{
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
}
A function definition/declaration is not allowed in another function.
main is a function and branching_if_judgement is another function.
correct (read compile-able) code:
#include <stdio.h>
#define SPACE ' '
static void branching_if_judgement(const int a, const int b){
if (a > b)
{
printf("a(%d) is larger than b(%d)\n", a, b);
} else
{
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
int main(){
branching_if_judgement(2, 3);
}

Which approach is better - More conditions or More Variables?

I had to write one function to get the largest among four numbers. I find two ways to do that -
#include <stdio.h>
int main()
{
int a, b, c, d;
int max_of_four(int a, int b, int c, int d);
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
int max_of_four(int a,int b,int c,int d)
{
int result,result2,result3;
if(a>b)
result=a;
else
result=b;
if(result>c)
result2=result;
else
result2=c;
if(result2>d)
result3=result2;
else
result3=d;
return result3;
}
Or I can write the function like this also -
int max_of_four(int a,int b,int c,int d)
{
int greatest_int;
if (a>b && a>c && a>d)
greatest_int=a;
else if (b>c && b>d)
greatest_int=b;
else if (c>d)
greatest_int=c;
else
greatest_int=d;
return greatest_int;
}
May I know which would be the better as in the first function I am using more variables and in the next one I am using more conditions. I tried running both & they are taking same amount of time so I couldn't differentiate between the two. As I am just starting with programming with C it would be good to know this as I go forward. Thank You.
Or you can write this:
int max_of_four(int a,int b,int c,int d)
{
int greatest_int = a;
if (b > greatest_int) {
greatest_int = b;
}
if (c > greatest_int) {
greatest_int = c;
}
if (d > greatest_int) {
greatest_int = d;
}
return greatest_int;
}
Or something like this...
int max_of_four(int a,int b,int c,int d)
{
int greatest_int = a;
int *iter = (int[]){b, c, d}, *end = iter + 3;
for (; iter < end; iter ++) {
if (*iter > greatest_int) {
greatest_int = *iter;
}
}
}
Which approach is better - More conditions or More Variables?
First you need to define what you mean by better
Is it better performance?
Is it less memory usage?
Is it better maintenance?
Guessing about performance by looking at the C code is something that you shouldn't do - especially when being new to C. The compiler makes all kinds of optimizations on your C code, so there is (nearly) no way to predict performance. The only solution is to profile.
The same apply to memory usage - even though you define some variables, the compiler is likely to optimize them away. You'll have to inspect the generated assembler code to get an answer.
Regarding maintenance - in nearly all cases this is where you should focus. Make sure that your code is easy to understand (and their by to maintain). Performance issues come second.
Let's look at this code:
int max_of_four(int a,int b,int c,int d)
{
int result,result2,result3;
if(a>b)
result=a;
else
result=b;
if(result>c)
result2=result;
else
result2=c;
if(result2>d)
result3=result2;
else
result3=d;
return result3;
}
Here you say that you worry about the number of variables...
Well let's rewrite the code - let's pretend I'm a compiler.
The first thing I notice is that once result is initialized, the variable a isn't used anymore. So why introduce a new variable result when I have a available already. So instead of result I simply use a and rewrite to:
int max_of_four(int a,int b,int c,int d)
{
int result2,result3;
if(a>b)
a=a;
else
a=b;
if(a>c)
result2=a;
else
result2=c;
if(result2>d)
result3=result2;
else
result3=d;
return result3;
}
Now the first if is rather strange, so I rewrite to:
int max_of_four(int a,int b,int c,int d)
{
int result2,result3;
if(b >= a) a=b;
if(a>c)
result2=a;
else
result2=c;
if(result2>d)
result3=result2;
else
result3=d;
return result3;
}
Once again I notice that once result2 is initialized, the variable a isn't used anymore. So I can repeat the pattern from above and get rid of result2 by replacing it by a. After that I can repeat the same pattern to get rid of result3 and my code looks:
int max_of_four(int a,int b,int c,int d)
{
if(b >= a) a = b;
if(c >= a) a = c;
if(d >= a) a = d;
return a;
}
Still worried about the number of variables?
Since the compiler can see when the various variables are still in use (or no longer in use), the compiler may optimize your original code just like above by reusing "dead" variables.
But... the compiler will probably do something even more optimal. What? I don't know before I take a look at the generated assembly code.
So the conclusion is - don't look at the C code when finding the better way.
static int max(int a, int b)
{
return (a > b) ? a : b;
}
int f(int a, int b, int c, int d)
{
return max(max(max(a, b), c), d);
}

How to get a recursive algorithm (C) to print steps?

Trying to work out how to get this code to print out its iterations as it runs for form checking. New to C and just trying to get my head around it while I work on things I already know (should know anyway) how they work. Recursive Euclidean Algorithm here.
int gcd(int a, int b)
{
if (b == 0) return a;
else return gcd(b , a%b);
}
try the following
int gcd(int a, int b)
{
if (b == 0)
{
printf("%d\n",a);
return a;
}
else
{
printf("%d\n",b);
return gcd(b , a%b);
}
}

C language overload

#include <stdio.h>
int Add(int a, int b);
int Add(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add(1,2));
printf("3+4+5=%d\n",Add(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add(int a, int b)
{
return a+b;
}
int Add(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
I wrote with C language and using Xcode. While studying "overload", Xcode keeps show error message that overload cannot be worked. Instead it shows "Conflicting types for 'Add'" message.
With Xcode, would overload cannot be worked?
In Simple words, C doesn't allow function overloading! So, you can't write multiple functions with the same name!
Try to give different function name and try-
#include <stdio.h>
int Add2int(int a, int b);
int Add3int(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add2int(1,2));
printf("3+4+5=%d\n",Add3int(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add2int(int a, int b)
{
return a+b;
}
int Add3int(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
As mentioned C doesn't support function overloading (like in C++). Neverthless C99 introduced function-like macros with empty arguments, however commas must be preserved with exact number. Here is an example:
#include <stdio.h>
#define Add(a,b,c) (a+0)+(b+0)+(c+0)
int main(void)
{
int a = 1, b = 2, c = 3;
double x = 1.5, y = 2.25, z = 3.15;
printf("%d\n", Add(a, b, c)); /* 6 */
printf("%d\n", Add(a, b, )); /* 3 */
printf("%d\n", Add(, , c)); /* 3 */
printf("%g\n", Add(, y, z)); /* 5.4 */
printf("%g\n", Add(x, , )); /* 1.5 */
return 0;
}
Note that due to due usual arithmetic conversions for arguments with floating-point type 0 would be properly promoted to such type.
This may be only possible if you are using C++.
But in C you can't think of function overloading.
So try to change the function name and then it will work.
Actually C language does not allow function or method overloading
To do method overloading you have to go to C++ or Java

stuck in GCD of 2 numbers in C using recursion

I am getting segmentation fault with the following program. I am trying to find out the GCD using recursive function. The code is:
#include<stdio.h>
int gcd(int a, int b)
{
int temp, g, c;
if(a>b)
{
c=a;
a=b;
b=c;
}
//printf("The values of a and b are: %d %d",a,b);
temp = a % b;
if(temp != 0)
{
g = gcd(b, temp);
return(g);
}
if(temp == 0)
g= b;
return g;
}
int main()
{
int a,b;
printf("Enter two numbers: \n");
scanf("%d %d", &a, &b);
printf("The GCD of two numbers you entered are: %d\n", gcd(a,b));
}
The problem that I found out is in swapping variables. If I am removing it then the code is working fine. Can anybody tell me where I am going wrong? I am trying to implement it using Euclidean algorithm. So no other method can be implemented.
if(a>b)
{
c=a;
a=b;
b=c;
}
temp = a % b;
The problem is here. First, you're making sure that b > a. Now, if b is greater than a, it's not difficult to prove that a % b == a. For instance, 2 % 5 = 2.
Just replace the last line with
temp = b % a;
or, even better, reverse the condition for swapping :
if(a < b)
Instead of swapping the variables at the GCD function,Find the max number and send it as a and min number as b in the main function itself.
int GCD(int a, int b)
{
if (b == 0)
return a;
else
return GCD(b, a % b);
}
The condition should be
if (a < b)
Because doing
// Pre-condition: a >= b
temp = a % b;
// Post-condition: temp < b or temp == 0.
then the call gcd(b, temp)
has: temp < b, b <= min(original a, original b)
And so gcd terminates (variant a + b getting smaller on every call).
i think there is no need for the if (a > b) test, just remove it. If a >= b, the code works just as you want; if not, the first recursive call just do the swap.
You really don't need to check for each step which is greater (a or b).
Use following function to calculate gcd of two numbers.
int gcd(int a ,int b){
if(a % b == 0)
return b;
return gcd(b,a%b);
}
That's it.
Why to complicate things that much? Use this:
int findgcd(int x,int y){
while(x!=y){
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}

Resources