Recursive call to main() in C

rijinpk1

Aspiring Novelist
Write a c pgm which rewrites "The universe is never ending" using recursion so that it terminate after 17 calls. Your pgm should consist of a single main() function that calls itself recursively.
This question seems to be simple but asked for 15 marks. So I need answers from u guys.
Help me. Thanks in advance.
 

sakumar79

Technomancer
Re: help in a C program

Why dont you try to answer it yourself and see if you get errors. If you do, try to solve them yourself and only if you cannot solve a problem, post your code and note where you have a problem...

Please dont use the forum to solve your assignments for you.

Arun
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Re: help in a C program

Yes it's simple. I'd say try it yourself and post your attempt. No one would write your program for your assignment.

But if you do mistakes, everyone will help you.
 
OP
rijinpk1

rijinpk1

Aspiring Novelist
Re: help in a C program

This question is asked in my theory exam for the subject computer programming in engineering... Not in pratical xam.
What I actully written in the xam is given below.
Code:
#include<stdio.h>
int main()
{
 static int i=0;
 while(i<17)
 {
    printf("THE  UNIVERSE IS NEVER ENDING");
 i**;
 main();
 }
}
read * as plus symbol. I dont know why it is not printed here.
is this the right answer? If yes then does this question deserves 15 marks?
 

RBX

In the zone
Re: help in a C program

You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Re: help in a C program

Code:
#include<stdio.h>
int main()
{
    static int i = 0;
    rec_func();
    return 0;
}

void rec_func()
{
    if(i>=17)
        return;
    else
    {
        print("THE UNIVERSE IS NEVER ENDING");
        i++;
        rec_func();
    }
}

Should be something like this.
 

vickybat

I am the night...I am...
Re: help in a C program

Its offtopic but here's the java version:

Code:
public class Universe {
public static void univ(int i){
	
	if ( i <= 16 ){
	System.out.println("The universe is never ending");
	
	univ(i+1);
	
	}
}
	
	public static void main(String [] args ){
		
		univ(0);
	}
}
 
OP
rijinpk1

rijinpk1

Aspiring Novelist
Re: help in a C program

You need to declare and define a counter before entering recursion, you can implement termination condition within method's body.

I could not understand you. Can you explain in detail? What is a counter?

Code:
#include<stdio.h>
int main()
{
    static int i = 0;
    rec_func();
    return 0;
}

void rec_func()
{
    if(i>=17)
        return;
    else
    {
        print("THE UNIVERSE IS NEVER ENDING");
        i  ;
        rec_func();
    }
}

Should be something like this.

Is my answer wrong?
Is there any mistakes?
In the question it is clear that the main() function should call it recursively. So yours is wrong?
Pls clarify.
I wrote only this question. Did not consider the "OR" question. Will my 15 marks be waived off?
 

Hrishi

******************
Re: help in a C program

I could not understand you. Can you explain in detail? What is a counter?

Counter is actually any integer variable (e.g. int ctr=0;) which is used in a loop to find out how many times the loop has actually revolved around.

for e.g.
int ctr=0;
int x=0;
for(x=0;x<5;x++)
{
ctr++;
}

the output will be
ctr=5 since the loop has revolved for 5 times.

I hope you got it.
 

pulkitpopli2004

Cyborg Agent
Re: help in a C program

#include<stdio.h>
void main()
{
static int i=0;
while(i<17)
{
printf("THE UNIVERSE IS NEVER ENDING");
i**;
main();
}
}
read * as plus symbol. I dont know why it is not printed here.

yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command..
You should have written that. :twisted::razz:
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Re: help in a C program

I could not understand you. Can you explain in detail? What is a counter?



Is my answer wrong?
Is there any mistakes?
In the question it is clear that the main() function should call it recursively. So yours is wrong?
Pls clarify.
I wrote only this question. Did not consider the "OR" question. Will my 15 marks be waived off?

I'm not too good in C :p

I'd like to wait for some other member to verify that.

But I don't know whether main() can call itself.
 

Vyom

The Power of x480
Staff member
Admin
Yes, main function can call itself. Afterall main is also a function. And just like other function main also can accept parameters and return values.
Try attempting the conversion of that recur function to main. It should be simple.

Posted from a mobile device.
 
OP
rijinpk1

rijinpk1

Aspiring Novelist
Re: help in a C program

yes this is completely correct.. A small thing you can get or teacher can deduct mark for is "\n" in printf command..
You should have written that. :twisted::razz:

I actually put "\n" in my answer in the xam. But forgot to put here;).
But my question is does this question deserve 15 marks? I absolutely dont think so...

I'm not too good in C :p

I'd like to wait for some other member to verify that.

But I don't know whether main() can call itself.

yes.. main() can call itself recursively if needed.
 

sarthak

In the zone
Re: help in a C program

But my question is does this question deserve 15 marks? I absolutely dont think so...

You can expand letters or articles or reports in english if you feel that they are small, but you can't do the same with a computer program. There is nothing like minimum or maximum word limit here.
The 15 marks are for the logic, or the teachers want to make passing easy.
 

RBX

In the zone
Re: help in a C program

Sorry, didn't read the question carefully. Since it requires recursion of main(), you need a static counter declared obviously inside the main()'s body as you have done in post #4, it just doesn't need a while loop, but a selection (if..else, switch-case) which terminates the loop when counter reaches 17.

You shouldn't be seeing this
Code:
#include<stdio.h>
int main(void) {
	static int i=0; //This is counter
	if(i==16) return 0; //Termination Condition
	printf("The World is Never Ending\n");
	i++; //Counting : )
	main();
}
 

Hrishi

******************
Re: help in a C program

Recrusion of main() in C++ is not legal.The compiler will produce error.

HOwever , in C you are allowed to do it but its little complicated in barely used.
 
OP
rijinpk1

rijinpk1

Aspiring Novelist
Re: help in a C program

Recrusion of main() in C++ is not legal.The compiler will produce error.

HOwever , in C you are allowed to do it but its little complicated in barely used.

I didnt know that. I will check it for sure. Need to clarify that asap. Anyway thanks for commenting.
 

RBX

In the zone
Re: help in a C program

The 15 marks are for the logic, or the teachers want to make passing easy.

Never happens with me. I write just what's asked in the question, but unless you explain history of every term encountered in the question, you've got very slim chances of scoring :(
 
Top Bottom