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

1. ### riz09New Member

Joined:
May 7, 2009
Messages:
7
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)

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

2. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
Solution for Problem 1: (recursive)

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

3. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
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~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
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. ### riz09New Member

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

6. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
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~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
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. ### riz09New Member

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

9. ### SaswatPadhi~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA
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. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
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~ Б0ЯИ Τ0 С0δЭ ~

Joined:
May 5, 2009
Messages:
1,343
55
Trophy Points:
0
Occupation:
STUDENT !
Location:
Orissa, INDIA