problems in c programming

What is the basic principle behind recursion, other than function calling itself again & again and in which real time scenario we use it? can anyone explain me?

Replies

  • Morningdot Hablu
    Morningdot Hablu
    hello anand,
    recursion is the process of calling function again and again.
    During the recursion many push and pop operation is done in our registers memory.That's why recursion has very poor performance.
    Our computer takes more memory to perform any recursive operation.
    If you want to perform a speedy operation you must have to go with looping.
    .
    It's easy to go with recursion while solving a problem so that's why we use to perform recursive approach.Nothing more!!
  • anandkumarjha
    anandkumarjha
    thank you mohit for clearing my doubt
  • Whats In Name
    Whats In Name
    What I know is-
    Our computer takes less memory while performing recursive operation as we reduce the space complexity of the program?
    Please explain and correct me.
  • Morningdot Hablu
    Morningdot Hablu
    Whats In Name
    What I know is-
    Our computer takes less memory while performing recursive operation as we reduce the space complexity of the program?
    Please explain and correct me.
    I hope you read about complexity theory which deals with the runtime of a program.You must ask that if looping is faster then recursion then why we use recursion in finding the complexity of a program.
    Well Recursion may be faster where the alternative is to explicitly manage a stack, like in the sorting or binary tree algorithms.
    But in Java, C,C++ and Python it seems expansive to use recursion as compared to iteration(Looping) (in general) because it requires the allocation of a new stack frame.
    So that's why i put this answer above.
    .
    Let me know if any more problem is there.
  • vijaymp
    vijaymp
    Then can u tell me the real time scenarios where we can find the usage of these recursive functions?
  • anandkumarjha
    anandkumarjha
    main()
    {
    char *p1=“name”;
    char *p2;
    p2=(char*)malloc(20);
    memset (p2, 0, 20);
    while(*p2++ = *p1++);
    printf(“%sn”,p2);

    }
    what will be the output of this program?
  • Whats In Name
    Whats In Name
    mohit007kumar00
    I hope you read about complexity theory which deals with the runtime of a program.You must ask that if looping is faster then recursion then why we use recursion in finding the complexity of a program.
    Well Recursion may be faster where the alternative is to explicitly manage a stack, like in the sorting or binary tree algorithms.
    But in Java, C,C++ and Python it seems expansive to use recursion as compared to iteration(Looping) (in general) because it requires the allocation of a new stack frame.
    So that's why i put this answer above.
    .
    Let me know if any more problem is there.
    Thank You,It cleared my doubt.
  • anandkumarjha
    anandkumarjha
    Why do we use pointer instead of variable?
  • Whats In Name
    Whats In Name
    anandkumarjha
    Why do we use pointer instead of variable?
    We use pointers to directly access the variable's address,the concept is more clear when we talk about function callings,when we use the call by reference method,we change the original value(the value at the address) and there we use the pointers and when we talk about call by value method,we first make the copy of the original value and do changes to the copy and hence no need of using the pointer.
  • anandkumarjha
    anandkumarjha
    can anyone tell me the difference between (*p)++ and *(p++)?
  • Whats In Name
    Whats In Name
    anandkumarjha
    can anyone tell me the difference between (*p)++ and *(p++)?
    Taking p to be declared as pointer:
    ++(*p)
    Value at p will be incremented.

    *(++p)
    First the p will be incremented(p is a address) so it is the value at the incremented address.

    Eg:

    In memory at address 1000 the value is 20 and at address 1001 the value is 34.

    So,
    if p=1000

    ++(*p) means 21
    and *(++p) means 34
    -----------------------------------
    Please correct me if I am wrong.
  • anandkumarjha
    anandkumarjha
    thank you dear for helping me with the pointer
  • Whats In Name
    Whats In Name
    Whats In Name
    Taking p to be declared as pointer:
    -(*p)++
    Value at p will be incremented.

    -*(p++)
    First the p will be incremented(p is a address) so it is the value at the incremented address.

    Eg:

    In memory at address 1000 the value is 20 and at address 1001 the value is 34.

    So,
    if p=1000

    (*p)++ means 21
    and *(p++) means 34
    -----------------------------------
    Please correct me if I am wrong.
    My logic was for the preincrement operator,not for the post increment operator because the answers will be changed in that case.So I will edit it.
  • anandkumarjha
    anandkumarjha
    I could not get you whats in name....how the answers may differ ?????please elaborate it
  • Whats In Name
    Whats In Name
    anandkumarjha
    I could not get you whats in name....how the answers may differ ?????please elaborate it
    When we preincrement something ,then first the increment takes place.
    But When we post increment something then increment takes place but is seen in the next time it is used.
    -----
    Eg:

    i=1
    when we will print ++i,2 will be printed.
    ---
    i=1
    when we will print i++,1 will be printed and when we print i again next then 2 will be printed(incremented value).
  • BCA_GIRL
    BCA_GIRL
    Hi Anand!
    As whats in name has explained above, i'm adding to this. If we access value of any variable using pointers then we can directly perform any modifications in value of that variable. And if we perform any operation on a variable then at first compiler goes to the address of that variable, then fatches its value and performs the operation. But in case of pointers, the compiler directly fatches the value from its address and performs the operation. Pointers provides the facility to us to modify the value of a variable without performing any operation on that variable. Pointers also speed up compiling as well as execution of the program.
  • anandkumarjha
    anandkumarjha
    why do we use fflush() in the c programming language?
  • Whats In Name
    Whats In Name
    anandkumarjha
    why do we use fflush() in the c programming language?
    As per what I know fflush() is used to clear the buffer,buffer is used for temporary storage.

    Eg:

    When we use fflush() before a character input,it means we are clearing the buffer to store character.
    When we press the enter key after the previous statement in the output window,the buffer inputs the enter because enter is also a character,so to enter the real character we need to clear the buffer.
  • anandkumarjha
    anandkumarjha
    hello everyone
    can anyone explain me the difference between the arrays of pointers and pointer to an array?
  • paresh006
    paresh006
    in case of pointer the memory allocation will be once and a single memory is allocated but in case of variable memory allocation is not fixed and according to the user input the memory is allocated...
  • anandkumarjha
    anandkumarjha
    hello everyone
    while going through one article on c i found that static variables can't be declared in the header file.but again it said that it can be declared but it would cause each source file which include header files to have its own private copy of variables.what it exactly want to convey i din't get ...can anyone explain it to me?
  • anandkumarjha
    anandkumarjha
    the return value which printf() function returns
    Have we ever thought that which return type(value) does actually printf() function returns?its interesting to know that printf () function returns an integer type value.let me explane it with an example.
    suppose we have one program as follows:-
    main()
    {
    int a=10;
    printf("\n %d %d %d",a,a,a);
    }
    then this will print the value of a three times with 2 spaces like this
    10 10 10.Here we find that the printf() function actually returns the number of characters(three characters for the value of a and two values for spaces).
  • xxxabhash007
    xxxabhash007
    Please give the output of the above program
  • anandkumarjha
    anandkumarjha
    the output of the above program is 10 10 10. still if any doubt bothers you then feel free to ask me
  • anandkumarjha
    anandkumarjha
    So far as i know the difference between static variable and a global variable in c is that static variable has its scope inside the function in which it has been declared and it will be in the memory forever and whenever that function is again called it will have the same value as before but in the case of global variable the variable has it's scope throughout the program...if anyone knows other differences please share it to me
  • anandkumarjha
    anandkumarjha
    We know that there are two methods to make a variable constant
    1>By using const keyword and 2nd using #define preprocessor but now the question arises that which of the two method is suitable?The answer is by using the keyword const as it can represent that the variable can be of any data type.Using this keyword also has the advantage that the scope of the variable can be easily determined.....
  • anandkumarjha
    anandkumarjha
    An array is a lvalue or rvalue?
  • Special
    Special
    can we write a c program without using the main function????
  • xxxabhash007
    xxxabhash007
    Ya...I have this program for you:

    #include
    #define decode(s,t,u,m,p,e,d) m##s##u##t
    #define begin decode(a,n,i,m,a,t,e)
    int begin()
    {
    printf(” hello “);
    }
  • Special
    Special
    @abhash
    explain me the above program?
  • xxxabhash007
    xxxabhash007
    It has hidden main function.
  • xxxabhash007
    xxxabhash007
    The ‘##‘ operator is called the token pasting or token merging operator.
  • Special
    Special
    xxxabhash007
    It has hidden main function.
    where is it hiden please do explain..
  • anandkumarjha
    anandkumarjha
    Basically c program without main() function is not logical but anyway we can write the program without using main() function by using the preprocessor #define.here is the program
    #include
    #define begin main
    void begin()
    {
    printf("we can write the program without main function");
    }
    here we see that this is the program without main() function but internally #define preprocessor converts begin into main and then execution of program takes place.
    if you have further query feel free to ask me
  • Special
    Special
    anandkumarjha
    #include
    #define begin main
    but you have used the keyword main in the header section...
  • xxxabhash007
    xxxabhash007
    @Special:
    In my program int begin() is replaced by "int main()" by the preprocessor, used above in the program, before the program is passed to the compiler.
  • Special
    Special
    xxxabhash007
    @Special:
    In my program int begin() is replaced by "int main()" by the preprocessor, used above in the program, before the program is passed to the compiler.
    so int begin acts as the main function according to you???

    and thanks! for the reply
  • xxxabhash007
    xxxabhash007
    Do you want any explaination for how the conversion took place? Or you are ok.
  • anandkumarjha
    anandkumarjha
    Actually #define is preprocessor also and we can use main over there and as #define defines the value as constant so here in the program begin is replaced by main internally which means it has all the properties of main now and it will function as similar to the main() function
  • Special
    Special
    @ abhash

    ya why not.
  • Special
    Special
    anandkumarjha
    Actually #define is preprocessor also and we can use main over there and as #define defines the value as constant so here in the program begin is replaced by main internally which means it has all the properties of main now and it will function as similar to the main() function
    but as per the rules we should not use keywords??
    correct me if i am wrong?
  • anandkumarjha
    anandkumarjha
    Special
    but as per the rules we should not use keywords??
    correct me if i am wrong?
    actually main is not the keyword rather it's a function which iniciate the programming and we can absolutely use any function in the preprocessor directives.
    does it answer your query?
  • Special
    Special
    @anand

    ok!! i will check it out the other functions and use them as preprocessors and let you know,
    thanks for the reply
  • xxxabhash007
    xxxabhash007
    I told you that ## operator is token merging operator, this means we can merge two or more characters with it.
    Look at the 2nd line of program, the ## operator merges m,s,u & t into msut. The logic is that when we pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).

    Now look at the third line of the program, here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,'a’,'i’ & ‘n’. So the third line "int begin" is replaced by "int main".
  • Special
    Special
    @abhash
    am not very clear about token concepts if you can explain me in simple words...
  • xxxabhash007
    xxxabhash007
    Do you know what does token mean?
  • vijaymp
    vijaymp
    Special
    can we write a c program without using the main function????
    Yes we can write a c program without using main function. You can give another name to main function also. But while compiling the program, you should change the entry point (give the name of function what you have used ),so that you can compile and run the program successfully.
  • anandkumarjha
    anandkumarjha
    main()
    {
    char *p = “ayqm”;
    printf(“%c”,++*(p++));
    }
    can anyone please suggest me with the output of this code?
  • anandkumarjha
    anandkumarjha
    xxxabhash007
    Do you know what does token mean?
    token are the basic entity in c or c++ language where you find all the informations about variables,keywords,constants e.t.c. tokens are the building blocks for the programming language
  • anandkumarjha
    anandkumarjha
    void main()
    {
    char c;
    while(c=getchar()!='\n')
    printf("%d",c);
    }
    what will be the output of the program ?explain
  • vijaymp
    vijaymp
    anandkumarjha
    main()
    {
    char *p = “ayqm”;
    printf(“%c”,++*(p++));
    }
    can anyone please suggest me with the output of this code?
    The output will be b. Correct me if I am wrong.
  • sumant_414
    sumant_414
    anandkumarjha
    hello everyone
    can anyone explain me the difference between the arrays of pointers and pointer to an array?
    hello friend ,
    array of pointers means - its an array which contains different pointers.
    pointer to an array - it is basically a pointer which is pointing towards a particular array.
  • anandkumarjha
    anandkumarjha
    sumant_414
    hello friend ,
    array of pointers means - its an array which contains different pointers.
    pointer to an array - it is basically a pointer which is pointing towards a particular array.
    thanks brother for your valuable reply but can you please explain it with an example to grasp it in the nice way???
  • Ankita Katdare
    Ankita Katdare
    Array of Pointers

    For e.g. if we have array of 10 int pointers i.e. int *a[10]
    then each element that which is stored in array are pointed by pointers.
    Thus we will have ten pointers.

    See this program:
    int main ()
    {
    int data[5];
    int *array[5];


    for (int i = 0; i <5;i++)
    {
    data = i;
    }

    //Assigning address of elements of array data to array of pointers.

    for (int i = 0; i <5;i++)
    {
    array = &data;
    }

    for (int i = 0; i <5;i++) //Accessing Array value using index
    {
    printf ("\n%d",data);
    }

    for (int i = 0; i <5;i++) //Access Array value using array of pointers
    {
    printf ("\n%d",*array );
    }
    return 0;
    }


    Pointer to an Array


    For e.g. int(*a)[10]
    Here all the elements that is all the ten elements are pointed by a single pointer.

    See this Program:
    void main( )
    {
    char name[ ] = "engineer" ;
    char *ptr ;

    ptr = name ;

    while ( *ptr != `\0' )
    {
    printf ( "%c", *ptr ) ;
    ptr++ ;
    }
    }
  • haru31773
    haru31773
    is it possible to use do while w/ if else program??
  • haru31773
    haru31773
    main()
    {
    int num;
    float y, m, v, p, m1, c, E, E1, p1, c1, m2, J, r, p2, T, r1, F;
    clrscr();
    printf("1. Relativistics Momentum\n2. Rest Mass Energy\n3. Total Energy(Relativistic)\n4. Angular Momentum\n5. Torque\nChoose a Problem: ");
    scanf("%d",&num);
    if(num<=1)
    {
    printf("\nFormula: p=ymv");
    printf("\n\nEnter Value For: y= ");
    scanf("%f",&y);
    printf("\n\t\t m= ");
    scanf("%f",&m);
    printf("\n\t\t v= ");
    scanf("%f",&v);
    p=y*m*v;
    printf("\n\t Answer: p= %5.2f",p);
    }

    else
    if(num<=2)
    {
    printf("\nFormula: E=mcý");
    printf("\n\nEnter Value For: m= ");
    scanf("%f",&m1);
    printf("\n\t\t c= ");
    scanf("%f",&c);
    E=(m1*c*c);
    printf("\n\t Answer: E= %5.2f",E);
    }

    else
    if(num<=3)
    {
    printf("\nFormula: E=û(pýcý+mýc");
    printf("\n\nEnter Value For: p= ");
    scanf("%d",&p1);
    printf("\n\t\t c= ");
    scanf("%d",&c1);
    printf("\n\t\t m= ");
    scanf("%d",&m2);
    }

    else
    if(num<=4)
    {
    printf("\n\nFormula: J=r*p");
    printf("\n\nEnter Value For: r= ");
    scanf("%f",&r);
    printf("\n\t\t p= ");
    scanf("%f",&p2);
    J=r*p2;
    printf("\n\t Answer: J= %5.2f",J);
    }

    else
    if(num<=5)
    {
    printf("\n\nFormula: T=r*F");
    printf("\n\nEnter Value for: r= ");
    scanf("%f",&r1);
    printf("\n\t\t F= ");
    scanf("%f",&F);
    T=r1*F;
    printf("\n\t Answer: T= %5.2f",T);
    }

    else
    printf("Error, please run again!");
    getch();
    }
    look at this, i want the program to return to the menu after selecting and finishing the chosen formula,

    how will i do it??
    
  • Whats In Name
    Whats In Name
    haru31773
    is it possible to use do while w/ if else program??
    Yes.It is possible.
  • haru31773
    haru31773
    can i do it my program above??
  • Whats In Name
    Whats In Name
    Dont put (num<= ) but use (num== ) operator.
    Take a char variable,eg. ch

    char ch;
    do{
    printf("1. Relativistics Momentum\n2. Rest Mass Energy\n3. Total Energy(Relativistic)\n4. Angular Momentum\n5. Torque\nChoose a Problem: ");
    ---
    ---
    else
    printf("Error, please run again!");
    printf("Do you want to continue");
    fflush(stdin);
    scanf("o/od",&ch);
    }

    while(ch=='y'||ch=='Y');
    }
  • Whats In Name
    Whats In Name
    Instead of if else,you can also use switch and case.

    switch(num)
    {

    case 1: --
    break;
    case 2:--
    --
    --
    --
    }
  • haru31773
    haru31773
    o thx, but it wont run the fflush(stdin) .. ?
  • d_vipul
    d_vipul
    suggest me some books on C programming.......
  • Whats In Name
    Whats In Name
    Let us C-Y.K
    Test your C skills-Y.K
  • mywbut
    mywbut
    No body is clearing the real life example of Recursion ???????
  • Whats In Name
    Whats In Name
    Real Life Example of recursion:

    Searching a word in dictionary.
    First we search for the first alphabet,then from the searched out list,we search for the second alphabet and recursively keep on doing it until we find the word.

    Source:Internet

You are reading an archived discussion.

Related Posts

Both HP and DELL are eager to buy the Fremont (California) based company that operates in the data-storage domain for $24/share. The deal is currently at $1.6 billion. "There's a...
hi guys. dis is yuvan. n am doing my b.tech final year. so plz suggest me some live projects in mechanical field.
Why does my computer desktop screen takes a lot of time (like 10 minutes) to fully open up(start-up or can say Load) when I switch on the computer?
actually am making project on the same topic.... using cylinders .... i want to mke a bit of inovation in it ...plzz help me...
A very important note before starting. In Japanese, the letter "de" is pronounced as "the" in "there" AND "su" is pronounced as "ss" and So read "Desu" as "Thess" Haajimemaashite...