All C/C++ Qurries here

Discussion in 'Programming' started by Maverick340, Nov 15, 2005.

Tags:
Not open for further replies.
1. Maverick340New Member

Joined:
Mar 19, 2004
Messages:
635
0
Trophy Points:
0
I ve seen lot of assorted C/C++ querries floating around in the forums and sum be myself too.
Thus i have Decided to start a common thred for all C/C++ related querries..
Hopefully it bocomes a sticky :roll:

2. OP

Maverick340New Member

Joined:
Mar 19, 2004
Messages:
635
0
Trophy Points:
0
First one by me!
How do you write a code for the sum of the following:
x + (x^2/2!) + (x^3/3!) + (x^4/4!) ....so on
this what i have done
Code:
```#include<iostream.h>
int fact(int k)
{
int sum=1,i;
{
for (i=1;i<=k;i++)
sum *= i;
}
return sum;
}
int xval(int x)
{
int i,xval=1;
{
for(i=1;i<=x;i++)
xval=i*i;
}
return xval;
}

void main()
{
int i,n;
cout<<"Entr a number limiting value   : ";
cin>>n;

.
.
.
.
.
```
Help me complete it...

3. puja399New Member

Joined:
Jul 30, 2005
Messages:
324
0
Trophy Points:
0
Why don't u solve the equation first?
Anyways, here is what I managed to write (remember, there is no bounds checking, and using big numbers will give erroneous result, but u can correct it, right?)
Code:
```//Implemented in ANSI/ISO C++
#include <iostream>
#include <cmath>
using namespace std;
double factorial(double i)
{
return (i==1?1:(i*factorial(i-1)));
}
void main()
{

double num,result,sum=0;
int i=1;
cout<<"enter number:";
cin>>num;
do
{
result=pow(num,i)/factorial(i);
cout<<"Iteration "<<i<<" : "<<result<<endl;
i++;
sum+=result;
}
while (result>0);
cout<<"Final sum: "<<sum<<endl;
}```

4. robogeekNew Member

Joined:
Jul 1, 2005
Messages:
69
0
Trophy Points:
0
Itz very hard to imagine/expect. I checked the output. Ignore the warnings and just run the code. Truely amazing.

The C program that won first prize in International obfuscated C code

This C program won the first prize in International obfuscated C code contest (IOCCC) held recently.
can u guess the output of this prog?

#include <stdio.h>
main(int t,char _,char *a)
{
return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a )&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_, t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\ n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \ ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/") :t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK '(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

output:
On the first day of Christmas my true love gave to me
a partridge in a pear tree.

On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.

On the third day of Christmas my true love gave to me
three french hens, two turtle doves
and a partridge in a pear tree.

On the fourth day of Christmas my true love gave to me
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the fifth day of Christmas my true love gave to me
five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the sixth day of Christmas my true love gave to me
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the seventh day of Christmas my true love gave to me
seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eighth day of Christmas my true love gave to me
eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the ninth day of Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the eleventh day of Christmas my true love gave to me
eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.

Press any key to continue

It uses recursion with pointers. see if u cud trace the flow.

5. OP

Maverick340New Member

Joined:
Mar 19, 2004
Messages:
635
0
Trophy Points:
0
Second Querry:
Code:
```#include<iostream.h>
#include<string.h>
void  main()
{
char line[80];
do{
cin.getline(line,80);
if (*line)
cout<<"\t ["  <<  line   <<"] \n";
}while(*line);
}
```
Explain the functioning of the statements
if(*line)
while(*line)
I do know the basics of pointers...

6. puja399New Member

Joined:
Jul 30, 2005
Messages:
324
0
Trophy Points:
0

7. goldberg2k5New Member

Joined:
Nov 12, 2005
Messages:
3
0
Trophy Points:
0
Since you are using getline everytime, so (*line) is checking the first character of the line array if its null or not(ascii 0).

*line is eqv to line[0];
*(line+1) is eqv to line[1];
......
get the point!!

8. goldberg2k5New Member

Joined:
Nov 12, 2005
Messages:
3
0
Trophy Points:
0
Maverick ur first code is quite a bit long don't u think?

Well i thought u might want to calculate e^x-1 coz the series is its expansion. and can u tell me if i can open an bmp image in c?

Joined:
Nov 22, 2004
Messages:
149
0
Trophy Points:
0
I need a "easy to use " C to C++ convertor , can anyone suggest me one??

10. navjotjsinghActive Member

Joined:
Feb 12, 2005
Messages:
1,924
8
Trophy Points:
38
Location:
Delhi, India

Joined:
Nov 22, 2004
Messages:
149
0
Trophy Points:
0

I have downloaded it but i cant figure out how to use it , do u use it??

can u tell me how to use it?

12. navjotjsinghActive Member

Joined:
Feb 12, 2005
Messages:
1,924
8
Trophy Points:
38
Location:
Delhi, India
13. OP

Maverick340New Member

Joined:
Mar 19, 2004
Messages:
635
0
Trophy Points:
0
Well hovewer ill continue to use this thread to Post all the c++ quries here..less cluttered
Problem
I am using Tc 4.5.I tired to run a program that used BGI graphics file.
I also copied the BGI grphics file in the BIN folder..
HOwever an error came saying :
Code:
``` Compiling SNAKE.CPP:
Fatal ..\INCLUDE\GRAPHICS.H 19: Error directive: BGI graphics not supported under Windows```
What is the problem??

14. puja399New Member

Joined:
Jul 30, 2005
Messages:
324
0
Trophy Points:
0
This is exactly the problem!!!!! I'll reiterate it again...BGI graphics not supported under Windows!!!!!!

Grow up....use Dev-C++ for heaven's sake, and use Windows graphics, not the damn BGI!!!!!!! (Don't u read the other threads on C/C++ on this forum???)

15. jitunNew Member

Joined:
Nov 29, 2005
Messages:
24
0
Trophy Points:
0
Location:
Bhubaneswar
16. Gurpreet KaurNew Member

Joined:
Dec 9, 2005
Messages:
7
0
Trophy Points:
0
code for text color : -

#include <conio.h>

int main()
{
int color;

textattr(10);
return 0;
}

17. imported_dheeraj_kumarNew Member

Joined:
Mar 14, 2005
Messages:
198
0
Trophy Points:
0
Location:
Chennai, India
He Maverick,

how do you play audio(any audio lets say mp3 or even midi will do) files in dos mode?

mono or stereo anything is ok

18. OP

Maverick340New Member

Joined:
Mar 19, 2004
Messages:
635
0
Trophy Points:
0
Well this code is part of a project where we ARE NOT ALLOWED TO USE WINDOWS GRAPHICS
So quit whining about using Dev C++ and tell me how to rectify the problem..
MOstly there is no solution cuz the compiler uses windows graphics and not BGI..

19. puja399New Member

Joined:
Jul 30, 2005
Messages:
324
0
Trophy Points:
0
First of all, I am not whining on any problem, you are!!!!! (This is ur thread, remember?!!). So, camn down. And seems its hell of a project ur working on where modern technology is barred!!! Really, I feel so sorry about promising persons like you who are being seriously taught useless junk matters by a bunch of apathetic morons. The sad thing is u and I can do little about this, other than pointing to the right direction and prey. Not too long ago, someone on this board complained about the sorry state of affairs in his school where he and his unfortunate friends are compelled to learn junk technology. If u r interested 2 view other C/C++ threads on this forum, perhaps u know this already.

So, coming back to ur problem, you thought right, there is no solution to ur problem, but may be u can buy an old 486 or P-I, and install DOS and TC++, and happily live ever after!!!!!

20. OP

Joined:
Mar 19, 2004
Messages:
635