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);
}
}
Related
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!
I would like to return the minimum of three given numbers like this.
I don't know why it doesn't return anything
#include <stdio.h>
int minimum3(int un, int deux , int trois)
{
int minimum;
if (un<deux && un <trois)
minimum= un;
else if (deux<trois && deux<un)
minimum= deux;
else if (trois<deux && trois<un )
minimum= trois;
return minimum;
}
int main(void) {
minimum3(4,88,8999);
return 0;
}
As others mentioned in the comments, you ignore the returned value, a quick fix for that would be :
int main(void) {
int min = minimum3(4,88,8999);
printf("min: %d\n",min);
return 0;
}
Despite that, your algorithm isn't that effective, as Jonathan mentioned, if 2 of the numbers you process are equal, there is no way to calculate the minimum. The better would,imo, would be to have another function that calculates the minimum of 2 numbers and then use that to compare to the third number. Much cleaner this way.
#include <stdio.h>
int min2(int a,int b)
{
return ((a <= b) ? a : b);
}
int min3(int a,int b,int c)
{
int mintmp = 0;
mintmp = min2(a,b);
return ((mintmp <= min2(mintmp,c)) ? mintmp : min2(mintmp,c));
}
int main(void)
{
printf("%d\n",min2(5,10));
printf("%d\n",min3(5,-1,1));
return 0;
}
You can of course replace the conditional expressions with simpler if else .
/**
* #input A : Integer
*
* #Output Integer
*/
int solve(int A){
int ans,i;
ans=0;
i=1;
while(i<=A){
ans=((ans+def(i))%1000000007);
i++;
}
return (ans);
}
int def(int m){
int ans,k;
ans=1;
k=1;
while(k<=m){
if(gcd(k,m)==1){
ans=ans*k;
}
k++;
}
return (ans%m);
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
main function is calling solve function, everything is fine. I just need to reduce the time complexity. For the input A: 114233 The expected output is 902650983, but my program is giving time limit exceeded. Please help. How can I reduce the time complexity in this problem?
Edit:
Actually main function is passing a value A to solve function then question has asked to implement these following functions:
Def P(m):
ans=1
k=1
while k<=m:
if GCD(k,m)==1:
ans*=k
k+=1
return (ans % m)
def F(A):
ans = 0
i=1
while i<=A:
ans+=P(i)
i+=1
return ans % 1000000007
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);
}
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;
}