All C/C++ Qurries here

Status
Not open for further replies.

Maverick340

Ambassador of Buzz
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:
 
OP
Maverick340

Maverick340

Ambassador of Buzz
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...
 

puja399

In the zone
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;
}
 

robogeek

Broken In
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.
 
OP
Maverick340

Maverick340

Ambassador of Buzz
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...
 

puja399

In the zone
Maverick340 said:
Second Querry:
Code:
I do know the basics of pointers...[/quote]
Pardon me, but I think u forgot to type a 'not' between 'do' and 'know'. :)  :)  :)

BTW, why don't u trace the prog to c whats going on..? :wink:  :wink:
 

goldberg2k5

Right off the assembly line
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!!
 

goldberg2k5

Right off the assembly line
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?
 

navjotjsingh

Wise Old Owl
Sorry but the convertor is very hard to use and you asked for a easy to use convertor. This one requires a Python compiler - get it from www.python.org and requires well formatted c file with proper indentation. Get a software for doing that at *astyle.sourceforge.net/
Rest of the instructions are in zip file of convertor.

If you don't want to go through this long process search for more at *www.google.com/search?sourceid=nav...GGL,GGGL:2005-09,GGGL:en&q=C+to+C+++convertor
 
OP
Maverick340

Maverick340

Ambassador of Buzz
I see that this thread has not been made a sticky..
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??
 

puja399

In the zone
Maverick340 said:
Code:
... Error directive: BGI graphics not supported under Windows

What is the problem??

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???)
 

jitun

Broken In
Decoading "Twelve Days of Christmas"

Ok robogeek I have been seeing this program for a long time at a lot of spaces. So here is an explanation of what is happening inside the program. Here is the link. :lol:

*research.microsoft.com/~tball/papers/XmasGift/
 

Gurpreet Kaur

Right off the assembly line
code for text color : -


#include <conio.h>

int main()
{
int color;


textattr(10);
cprintf("This is blinking text\n");
return 0;
}
 
OP
Maverick340

Maverick340

Ambassador of Buzz
puja399 said:
Maverick340 said:
Code:
... Error directive: BGI graphics not supported under Windows

What is the problem??

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???)

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

puja399

In the zone
Maverick340 said:
...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..

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.

Maverick340 said:
...MOstly there is no solution cuz the compiler uses windows graphics and not BGI.

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!!!!!
 
Status
Not open for further replies.
Top Bottom