Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Please Help me.. (http://www.go4expert.com/forums/please-help-me-t17396/)

riz09 7May2009 20:03

Please Help me..
 
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

SaswatPadhi 7May2009 21:17

Re: Please Help me..
 
Solution for Problem 1: (recursive)

Code: c++

int Factorial (int n)
{
if (n==1)
      return 1;
else
      return n*Factorial(n-1);
}


SaswatPadhi 7May2009 21:18

Re: Please Help me..
 
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++.

SaswatPadhi 7May2009 21:30

Re: Please Help me..
 
Solution for Problem 3:

Code: c++

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


riz09 8May2009 20:14

Re: Please Help me..
 
Thanx SaswatPadhi..
Thanx alot.. but could u please explain it in detail.. i'll be very thankful to u

SaswatPadhi 8May2009 20:25

Re: Please Help me..
 
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: c++

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 !

SaswatPadhi 8May2009 20:28

Re: Please Help me..
 
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: c++

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;
}


riz09 8May2009 21:47

Re: Please Help me..
 
thanks.. but i m using it in Turbo C.. nd in my 3rd problem its not working...?:worried:

SaswatPadhi 8May2009 21:55

Re: Please Help me..
 
Turbo C :surprised: not C++ ? OK.
This should work then :

Code: c

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


xpi0t0s 9May2009 03:34

Re: Please Help me..
 
> 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.


All times are GMT +5.5. The time now is 09:56.