Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)

 riz09 7May2009 20:03

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)

--------------
Raza Elahi

Solution for Problem 1: (recursive)

Code: c++

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

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++.

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

Thanx alot.. but could u please explain it in detail.. i'll be very thankful to u

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 !

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

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

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