Hello guys,i'm new to c programming and programming in general.
I have a question about recursive functions .
My questions are(so far)(with reference to tutorialspoint.com about:recursion)
1.Why this isn't an infinte loop?
2.What is the original value of i when the programme starts?
3.Is there any problem if i write the main() function 1st and not the factorial() function?
This is the code tutorialspoint.com provides:
Code:
include<stdio.h>
int factorial(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
int main()
{
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
Output:Factorial of 15 is 2004310016
Also i would like some help on my programme about recursive functions:
This is the excercise:
Write a RECURSIVE functions that checks if in an array that consists of ten [10] there is on quartet of integer numbers A,B,C,D so that A+D=B+C
For example, the array 5 10 20 21 31 4 3 9 contains the integers 10 20 21 31 that meet the requirements
Your recursive function must print a message to point whether the array input contains a suitable quartet of numbers, the pointer of the 1st number of the quartet, and the 4 numbers.Your recursive function must return the pointer of the 1st number of the quartet.In case of failure, a proper message must be printed and your function must return -1;
YOU ARE NOT ALLOWED TO USE AND LOOPS OR GLOBAL/STATIC VARIABLES.
______
This is my code so far(neither built or debugged as i know i have many mistakes that i can't spot )
Code:
#include <stdio.h>#include <stdlib.h>
int quadruple(int i)
{
int j=0,a[10];
int *ptr;
if (j>7)
{
return -1;
printf("There are no numbers so that A+D=B+C is true");
}
else if ((a[j+1]+a[j+4])=(a[j+2]+a[j+3]))
{
ptr=&a[j+1];
printf("There is a suitable quartet of numbers");
printf("The numbers are:%d %d %d %d", a[j+1],a[j+2],a[j+3],a[j+4])
printf("The position of the 1st digit is :%d", ptr);
return ptr;
j++;
}
return quadruple(i, a[10]);
}
int main ()
{
int i=10,a[10];
quadruple(i, a[10]);
return 0;
}
Pretty confused here guys.
Sorry for any grammar,vocab.,syntax mistakes.English is not my native lang.