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
Solution for Problem 1: (recursive) Code: 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: int WriteStruct(Student A) { ofstream FileOut("xyz.txt",ios::binary); if (ios::fail) return -1; fin.write((char *)(&A), sizeof(A)); return 0; }
Thanx SaswatPadhi.. 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: 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: 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; }
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; }
> 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.
Yeah, that's right ! 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.