Please Help me..

Discussion in 'C' started by riz09, May 7, 2009.

  1. riz09

    riz09 New Member

    Joined:
    May 7, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone.. raza here..
    i m a new user of Go4Expert...
    well im a studentof b.cs.. i have some problems in C language..
    pls help me out... i'll be very thankful to you all..:happy:

    here are the problems..

    problem no.1:

    what is the code of a possible C function of following prototype to calculate the factorial, of a given integer n.
    int Factorial (int n)

    prob no 2:

    What are all possible outputs of the following code fragment?
    void f(int a, int b)
    {
    printf("%d %d\n", a, b);

    }
    void main(void)
    {
    int i = 5;
    f(++i , ++i );
    }


    problem no 3:

    Using following C structure

    struct Student{
    char[20] Name;
    char[20] FName;
    int Class;
    }

    What is a C function which will open file in Write Mode and append this structure in the end of the file (It should return 0 on success and -1 on error).
    int WriteStudentRec(Student * StdRec, char* filename)

    i will wait for ur answers.. thanks in advance..
    --------------
    Raza Elahi
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Solution for Problem 1: (recursive)

    Code:
    int Factorial (int n)
    {
    if (n==1)
          return 1;
    else
          return n*Factorial(n-1);
    }
    
     
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Solution for Problem 2 :

    Output is always 7 6.

    PS : You have used void main(...), which will give an error on compilation with latest ANSI C++; but might be OK with Turbo C++.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Solution for Problem 3:

    Code:
    int WriteStruct(Student A)
    {      ofstream FileOut("xyz.txt",ios::binary);
            if (ios::fail) return -1;
            fin.write((char *)(&A), sizeof(A));
            return 0;
    }
    
     
  5. riz09

    riz09 New Member

    Joined:
    May 7, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Thanx SaswatPadhi..
    Thanx alot.. but could u please explain it in detail.. i'll be very thankful to u
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    No problem, your questions are fairly simple.

    Explanations:

    Problem 1 :
    The factorial of n i.e. n! = n * (n-1) * (n-2) * . . . * 2 * 1
    = n * (n-1)!
    So, we get a recursive factorial relation.

    Problem 2 :
    The arguments are evaluated right to left :
    Code:
    f(++i , ++i );
    So, first ++i (2nd arg) increases i to 6 and passes it as b.
    The second ++i (1st arg) increases i to 7 and passes it as a.

    Problem 3 :
    Simple, just create a file output stream, check if the file creation failed, and finally, write the structure !
     
  7. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    I just noticed a small typo in the solution to Problem 3. In line 4, I wrote fin.write(...), it should be FileOut.write(...).

    And before return 0, I forgot to place FileOut.close().

    So, the code becomes :
    Code:
    int WriteStruct(Student A)
    {      ofstream FileOut("xyz.txt",ios::binary);
            if (ios::fail) return -1;
            FileOut.write((char *)(&A), sizeof(A));
            FileOut.close();
            return 0;
    }
     
    shabbir likes this.
  8. riz09

    riz09 New Member

    Joined:
    May 7, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    thanks.. but i m using it in Turbo C.. nd in my 3rd problem its not working...?:worried:
     
  9. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Turbo C :surprised: not C++ ? OK.
    This should work then :

    Code:
    int WriteStruct(Student A)
    {      FILE * FileOut;
            fopen(FileOut,"w");
            if (FileOut == NULL) return -1;
            fwrite(A, sizeof(A), 1, FileOut); 
            fclose(FileOut);
            return 0;
    }
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > Solution for Problem 2 :
    > Output is always 7 6.

    Wrong. The output is UNDEFINED. Use of side effects twice in the same expression does not have a guaranteed result and is compiler dependent. For example in Visual Studio 2008, the output is 7 7.

    I like the way you nitpick about the void main speck having missed this plank.
     
  11. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Yeah, that's right ! :eek: Thanx xpi0t0s.
    I had tested only with TC++ and MinGW.

    But, as the author is using TC, he will get the result as 7 6.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice