# All C/C++ Qurries here



## Maverick340 (Nov 15, 2005)

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:


----------



## Maverick340 (Nov 15, 2005)

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

```
#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 (Nov 15, 2005)

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

```
//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 (Nov 15, 2005)

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.


----------



## Maverick340 (Nov 15, 2005)

Second Querry:

```
#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 (Nov 15, 2005)

Maverick340 said:
			
		

> Second Querry:
> 
> ```
> I do know the basics of pointers...[/quote]
> ...


----------



## goldberg2k5 (Nov 15, 2005)

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 (Nov 15, 2005)

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?


----------



## beaditya (Nov 16, 2005)

I need a "easy to use " C to C++ convertor , can anyone suggest me one??


----------



## navjotjsingh (Nov 16, 2005)

beaditya said:
			
		

> I need a "easy to use " C to C++ convertor , can anyone suggest me one??



Visit *www.scriptet.com/ctocpp.php


----------



## beaditya (Nov 17, 2005)

navjotjsingh said:
			
		

> beaditya said:
> 
> 
> 
> ...




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?


----------



## navjotjsingh (Nov 17, 2005)

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


----------



## Maverick340 (Dec 6, 2005)

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 :
	
	



```
Compiling SNAKE.CPP:
Fatal ..\INCLUDE\GRAPHICS.H 19: Error directive: BGI graphics not supported under Windows
```

What is the problem??


----------



## puja399 (Dec 6, 2005)

Maverick340 said:
			
		

> ```
> ... 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 (Dec 8, 2005)

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

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


----------



## Gurpreet Kaur (Dec 9, 2005)

code for text color : -


#include <conio.h>

int main()
{
int color;


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


----------



## imported_dheeraj_kumar (Dec 10, 2005)

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


----------



## Maverick340 (Dec 11, 2005)

puja399 said:
			
		

> 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..
MOstly there is no solution cuz the compiler uses windows graphics and not BGI..


----------



## puja399 (Dec 11, 2005)

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


----------



## Maverick340 (Dec 11, 2005)

YA  srry for flaring up..Its our stupid school project that we have to submit(Class 11th)..


----------



## imported_dheeraj_kumar (Dec 11, 2005)

maverick, i posted a topic in
*www.thinkdigit.com/forum/viewtopic.php?t=33900
care to reply?


----------



## Gurpreet Kaur (Dec 12, 2005)

*Hi*

Hi Do anyone know the code in C, C++
which ask for inputting any name say Raj
and after inputting name it gives the message
"Happy Birthday Raj" in a lot of windows??
if anyone know could you please tell me
actually i am a new user and learning C, c++
i am no expert so i can't make suh types of code.
please tell me if you know...thanks


----------



## Â«TechnoPhileÂ» (Dec 12, 2005)

*Dijkstra's Algorithm*

hey i need a well commented implementation dijkstra's algorithm in c and c++...


----------



## imported_dheeraj_kumar (Dec 12, 2005)

gurpreet put it in a loop, im sure you must have heard of loops

technophile go here

*en.wikisource.org/wiki/Dijkstra's_algorithm


----------



## puja399 (Dec 12, 2005)

*Re: Hi*



			
				Gurpreet Kaur said:
			
		

> ...it gives the message
> "Happy Birthday Raj" in a lot of windows??
> ...



Wht do u mean by that? and for what platform??


----------



## sumit_ind (Dec 13, 2005)

guys i want to know what are files in c and what is their use in c ?


----------



## aadipa (Dec 13, 2005)

sumit_ind said:
			
		

> guys i want to know what are files in c and what is their use in c ?



i didn't get you, please explain more...


----------



## praka123 (Dec 13, 2005)

Am trying to learn C in GNU/Linux(gcc/Anjuta) someone help me link to a good tutorial...Thanks./Consider me as a absolute beginner(diot)


----------



## Maverick340 (Dec 16, 2005)

*Re: Hi*



			
				Gurpreet Kaur said:
			
		

> ...it gives the message
> "Happy Birthday Raj" in a lot of windows??
> ...


If u r using C++ in the dos version ie Turbo C++ 3.0 or before then u can not do it as DOS dosent supoort windows ..HOwever if u want a repeated statement like output use a for loop

```
void main()
{
    char name[10];
    cout<<"Enter your name : ";
    getlinen(name);
      for( int i =0 ;i!=\0;i++)
      cout<<"HAppy Birthday<<name<<"\n";
}
```

int the for loop  (*for(int i=0;i<10;i++)*)
u can give i<//_any number u like..the stament will be printed that many times_//


----------



## puja399 (Dec 16, 2005)

*Re: Hi*



			
				Maverick340 said:
			
		

> ..HOwever if u want a repeated statement like output use a for loop
> 
> ```
> void main()
> ...



RU sure this will work??????
I can't get the damn thing to compile even....duhhh....!!!!!  
And how do I get my whole name within a 'char' ??? How did u do it, using 'cin' ????  :roll:  :roll:


----------



## QwertyManiac (Dec 16, 2005)

*Re: Hi*



			
				Maverick340 said:
			
		

> Gurpreet Kaur said:
> 
> 
> 
> ...



he asked WINDOWS and u didnt give any, where are the headers... 
and yeah, how the heck a whole name in a char ???


----------



## Maverick340 (Jan 15, 2006)

eddited

NExt problem:
Write a program to determine the max and min element in an unsorted array with altering the array.
func is :
void extremes(int& max, int& min, int a[ ], int n)


----------



## Maverick340 (Jan 15, 2006)

NExt problem:
find the second largest element in the above problem


----------



## vinaypatel (Jan 16, 2006)

... Error directive: BGI graphics not supported under Windows

try to initialize properly


----------



## sakumar79 (Jan 16, 2006)

The solution to both of Maverick340's problems is easy... Without altering the array, create a copy of it and sort the copy. Now, the smallest/largest, second smallest/largest, etc are just a call by index away...

If you dont want to use a second array, create variables larg,smal,seclarg etc, set them to the first element. Parse the list from second to last and at each stage update the larg,smal,seclarg variables to monitor the largest, smallest, second largest values of the numbers encountered upto that index... At the end, you should have the result.


Regarding Kaur's query for printing Happy Birthday <name> many times:
1. Change the char definition for name to char pointer or String.
2. If printing on screen instead of windows is okay, the cout statement should be 
 cout<<"Happy Birthday "<<name<<"\n";
A quotation marks was missing.
3. For creating windows, you will need to use graphics libraries which I am not proficient in... Many libraries I think are OS-dependant so you have to be careful and try to choose one that is available readily for all platforms (if you dont need to have cross-platform compatibility, this is not necessary).

Arun


----------



## Maverick340 (Jan 16, 2006)

Can sum1 give the code for finding second largest element.?


----------



## mako_123 (Jan 17, 2006)

Guys

Currently i use Turbo c++ ( dos one ) for programming in c++ . Now i want to shift to Dev C++ . Can anyone tell me whether there will be any diff in programming in dev c++ and what are its advantages over turbo c++ .


----------



## Maverick340 (Jan 17, 2006)

Why dont u use TC 4.5.
I prefer it over Dev C++.


----------



## puja399 (Jan 17, 2006)

mako_123 said:
			
		

> Guys
> 
> Currently i use Turbo c++ ( dos one ) for programming in c++ . Now i want to shift to Dev C++ . Can anyone tell me whether there will be any diff in programming in dev c++ and what are its advantages over turbo c++ .



There r a lot of differences between TC++ and Dev-c++.

First, Dev-c++ is the IDE that uses GNU C++ compiler (MingW version) which is (perhaps) the best C++ compiler available as on today. It supports all the standards of modern C++ (i.e., ISO/ANSI C++). Remember, even the compiler included in the Microsoft Visual C++ doesn't support all of the ISO/ANSI C++ standard. Moreover, its Dev-C++ is free unlike TC++ which is a commercial compiler (although its not supported anymore), obsolete and doesn't support modern C++ standard. What u can do with TC++ is an ancient (prehistoric !!) C++ which is actually worthless nowadays.

Second, Dev-c++ is written for modern 32 bit Windows, u can program for console, Windows GUI, OpenGL, and also easy textbased c++ programs. TC++ on the other hand has a lot of issues while running on WinNT/XP, simply bucuz, its not written for 32 bit windows. TC++ is only 16 bit, and when it was written, WinXP was unimaginable.

The only comparison with TC++ and Dev-C++ that comes to my mind is the one between a bullock curt and a ferrari running on an expressway. 

Some are still with TC++ due to graphics prgramming (the BGI thing), but just ask urself, did u notice one single application in the last 5 years that uses BGI?? NO, because its extinct. U can do more and far better graphics with Dev-C++ that with TC++. 

So, get rid of TC++, and use DevC++, becuz, otherwise, ur C++ skill (learned with TC++) will become useless in modern world.


----------



## hpotter606 (Jan 17, 2006)

1. what is a file descripter in C.
There is a fill in the blanks in todays o level 'C' paper.

2. *a is same as a[] in a parameter declaration (true or          false)

Can anybody please help me with these questions?


----------



## mako_123 (Jan 18, 2006)

Thanks Puja for such a wonderful reply . 

I want to make college projects in Dev c++ . I know a bit of graphics programming in turbo c++ . Can you provide me some resources or tutorials through which i can learn graphics in Dev c++ .


----------



## mehulved (Jan 18, 2006)

Mako you can browse through the documentation on dev c++ site. It will give you some idea on whatever features you want to use. Also, do query google for "dev-c++ codes + graphics" it will give you many results. 
Puja, why do you keep on complaining about the outdated technology taught by them? Is it going to change anything? You will have to accept it. I know how stupid it feels to use a monolithic technology in today's rapid paced computer's feild. I guess it won't change anytime this decade with the way everything operates. I also think it will be difficult to find teachers who are able and willing to teach newer technologies. Till that happens what we can do is just practise those technologies by oursleves with use of such forums and groups.


----------



## Maverick340 (Jan 18, 2006)

I would still suggest Tc ++ 3.0 (dos version ) for a beginer in C++.Ask any instructor that and hell tell u to do the same.
But ifu r a novice c++ guy, then i too suggest using dev c++.


----------



## puja399 (Jan 18, 2006)

tech_your_future said:
			
		

> Puja, why do you keep on complaining about the outdated technology taught by them? Is it going to change anything? You will have to accept it. I know how stupid it feels to use a monolithic technology in today's rapid paced computer's feild. I guess it won't change anytime this decade with the way everything operates. I also think it will be difficult to find teachers who are able and willing to teach newer technologies. Till that happens what we can do is just practise those technologies by oursleves with use of such forums and groups.



Perhaps ur right, but it feels so hopeless....   



			
				Maverick340 said:
			
		

> I would still suggest Tc ++ 3.0 (dos version ) for a beginer in C++.Ask any instructor that and hell tell u to do the same.
> But ifu r a novice c++ guy, then i too suggest using dev c++.



...And I thought 'beginner' and 'novice' have the same meaning!!!


----------



## mako_123 (Jan 18, 2006)

guys 

How is programming in Directx and openGL . Easy  or tough .


----------



## mako_123 (Jan 19, 2006)

I am interested in learning OpenGL programming . Please tell me the basics like what compiler , liblaries to use  etc .


----------



## mehulved (Jan 19, 2006)

puja399 said:
			
		

> Perhaps ur right, but it feels so hopeless....


Still its not as hopeless. We can recommend those people who're willing to use Dev-C++. So, are you a programmer, I have seen you give a lot of answers to questions on programming?


----------



## Maverick340 (Jan 19, 2006)

goldberg2k5 said:
			
		

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



Ya...its e^x+1 .Ive still not figured how do it!(sigh)


----------



## mako_123 (Feb 8, 2006)

Guys 
I need help in writing this program using files . Help me plz .


A data file contains records having the following fields:
Name: char(10)
Salary: char(8)
Number: integer
Records have the Number field as the primary key. 

Assume that I/O occurs in a block size of 512 bytes. Write a program to construct the data file and another file for the primary index. Store 30 records in the data file having values 1 to 30 in the Number field. Use your access method to retrieve record numbers 4 and 28 respectively. Output the numbers of the blocks of the index and data file to be brought into main memory to perform these accesses


----------

