Post your C/C++ Programs Here

Status
Not open for further replies.

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

Sykora said:
^^^ Are you asking how to calculate up to the 40th, because you're unable to reach past 20? If that's the case, declare your variables as unsigned long, and then try it.

I don't think that the 40th number might be above integer range.
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

A C Program to convert decimal number to its Binary Number Equivalent

Code:
#include <stdio.h>
#include<conio.h>
void main()
{
	auto	int	dec, bin;
	int	dec_to_bin (int);
	printf("Enter a decimal number \n");
	scanf("%d", &dec);
	bin = dec_to_bin (dec);
	printf("The decimal number is = %d \n", dec);
	printf("The binary number is = %d \n", bin);
}
/*	Funtion to find the binary equivalent	*/
int	dec_to_bin (int d)
{
	auto	int	b,r,k;
		b=0;
		k=0;
		while(d>0)
		{
			r = d % 2;
			d = d / 2;
			b = b + r * k;
			k = k * 10;
		}
		return (b);
getch();
}
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

You're better off writing the output binary number to a string rather than an int with digits 0 and 1. That way you'll be able to handle numbers above 31. Right now, the maximum binary number you can store is 11111, which is 31. Above that, and you'll get a wrap around.
 

The_Devil_Himself

die blizzard die! D3?
Re: Post ur C/C++ Programs Here

^thats true but what if we are required to first covert 2 decimal no. into binary and then do some arithmatic calculation and then convert the result back to decimal?
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

If you're that into Binary operations, you ought to be writing a class for binary numbers and overloading their operators. If you go that way, there's all sorts of cool things you can do. But that's in C++/Java/Python/Some other OO language.

If you want to stick with C, it is possible to write arithmetic functions over strings, but its slightly cumbersome.
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

aditya.shevade said:
I don't think that the 40th number might be above integer range.
Yes its odd, an int actually displays numbers even upto the 47th Fibonacci value :?

A sample output from the program below:
Code:
38: 24157817
39: 39088169
[B]40: 63245986[/B]
41: 102334155
42: 165580141
43: 267914296
44: 433494437
45: 701408733
46: 1134903170
47: 1836311903
48: -1323752223 (Goes wrong here onwards)

All this through an Integer variable whose max size is supposed to be 32768?! Or is it some kind of a clever compiler behavior, etc?

Here's the code which did the above:
Code:
#include<iostream>

using namespace std;

int main()
{
    int prev=0, fib=0, final=1;
    cout<<"1: "<<prev<<endl<<"2: "<<final<<endl;
    for(int i=0;i<=97;i++)
    {
        fib=final+prev;
        prev = final;
        final = fib;
        cout<<i+3<<": "<<fib<<endl;
    }
    cout<<endl<<"Final: "<<fib<<endl;
    return 0;
}

Doing the same with float yeilds right values upto 100, which would be the obvious way to do so correctly:
Code:
#include<iostream>

using namespace std;

int main()
{
    float prev=0, fib=0, final=1;
    cout.precision(30);
    cout<<"1: "<<prev<<endl<<"2: "<<final<<endl;
    for(int i=0;i<=97;i++)
    {
        fib=final+prev;
        prev = final;
        final = fib;
        cout<<i+3<<": "<<fib<<endl;
    }
    cout<<endl<<"Final: "<<fib<<endl;
    return 0;
}

Oddly, using long int results in the same execution as int itself, with numbers crapping out at 48th value and above.
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

Qwerty, compile and run this code, and tell me what you get.

Code:
#include <iostream>
#include <limits>

using namespace std;

int main () {
	cout << "Data Type\t\tSize\t\tMinimum\t\t\tMaximum\n";
	cout << "char\t\t\t" << sizeof(char) << "\t\t" << CHAR_MIN << "\t\t\t" << CHAR_MAX << endl;
	cout << "short int\t\t" << sizeof(short int) << "\t\t" << SHRT_MIN << "\t\t\t" <<  SHRT_MAX << endl;
	cout << "int\t\t\t" << sizeof(int) << "\t\t" << INT_MIN << "\t\t" << INT_MAX << endl;
	cout << "long int\t\t" << sizeof(long int) << "\t\t" << LONG_MIN << "\t\t" << LONG_MAX << endl;
	cout << "unsigned char\t\t" << sizeof(unsigned char) << "\t\t" << '0' << "\t\t\t" << UCHAR_MAX << endl; 
	cout << "unsigned short\t\t" << sizeof(unsigned short) << "\t\t" << '0' << "\t\t\t" << USHRT_MAX << endl;
	cout << "unsigned int\t\t" << sizeof(unsigned int) << "\t\t" << '0' << "\t\t\t" << UINT_MAX << endl; 
	cout << "unsigned long\t\t" << sizeof(unsigned long) << "\t\t" << '0' << "\t\t\t" << ULONG_MAX << endl;
	return 0;
}

You're right, I'm getting the same values for INT_MAX and LONG_MAX. 32767 is for SHRT_MAX.
 

The_Devil_Himself

die blizzard die! D3?
Re: Post ur C/C++ Programs Here

got this:
Code:
Data Type               Size            Minimum                 Maximum
char                    1               -128                    127
short int               2               -32768                  32767
int                     4               -2147483648             2147483647
long int                4               -2147483648             2147483647
unsigned char           1               0                       255
unsigned short          2               0                       65535
unsigned int            4               0                       4294967295
unsigned long           4               0                       4294967295
 
Last edited:

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Am getting the same:
Code:
Data Type               Size            Minimum                 Maximum
char                    1               -128                    127
short int               2               -32768                  32767
int                     4               -2147483648             2147483647
long int                4               -2147483648             2147483647
unsigned char           1               0                       255
unsigned short          2               0                       65535
unsigned int            4               0                       4294967295
unsigned long           4               0                       4294967295

So my educational system is at fault? :p
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

So it would seem. We'll only be able to tell if you run the same thing on TC++.
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

I don't have that but here's anomit's screen-shot of it running in TC++:

*img216.imageshack.us/img216/7272/opuk5.jpg

So TC++'s int is short int, guess that solves the issue.
 

Zeeshan Quireshi

C# Be Sharp !
Re: Post ur C/C++ Programs Here

Sykora said:
So it would seem. We'll only be able to tell if you run the same thing on TC++.
Well TC++ Generates 16 Bit Executables , so it's Int n Floats would be Half the size of a Present Day 32 Bit Compiler , so in short this code should not work in TC++ :)

QwertyManiac said:
So TC++'s int is short int, guess that solves the issue.
Yups , Int on a 16 Bit System is Equal to Short on a 32 Bit System .
 

Desi-Tek.com

In the zone
Re: Post ur C/C++ Programs Here

The_Devil_Himself said:
yea man dev c++ looks more geeky(hence cool).Lols.dev c++ is light on resources and thats very important for a IDE.
Zeeshan Quireshi said:
dud then u seriously need to use Visual C++ Express or Eclipse . You'll forget DevC++ after using them .

Download Visual C++ 2005 Express Here [FREE]:
*msdn2.microsoft.com/hi-in/express/aa700735.aspx

or you can down Eclipse C++ Developer Pack Here [FREE}:
*www.eclipse.org/downloads/

The only problem with Eclipse is , t does not Bundle a Compiler with it . so you need to Install GCC(MinGW or Cygwin or DJGPP on Windows) before installing it and then configure it to use ur Installation of GCC , whereas Visual C++ 2005 Express bundles everything into a neat package .
or try netbeans
*www.netbeans.org/images/v5/product-pages/nb-cc-s.png
*www.netbeans.org/products/cplusplus/
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

Anyone here running a 64bit OS who can try that out and tell us what size their int is?
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

how to use different colors for different letters, while the program is executed.
for example, if i input GIGACORE, it should show all those each every letters in the word GIGACORE with different colors
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

In linux you can use command line ANSI escape sequences, I don't know about windows though.
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

and which is the best C compiler for linux platform. i've heard about GC++ or something that sounds like that. is it good?
 

timemachine

Boom Bhadam Dhishkyao
Re: Post ur C/C++ Programs Here

its gcc buddy
gcc is a c compiler and g++ is a c++ comiler
trust me, its something awesome. try it and feel its real strength.

in TurboC++ compiler, color scheme is decided by textcolor in text mode.......if in graphics mode, settextstyle is used.

set the logic according to the output and use the above functions. i think it will do the job;)
 
Last edited:
Status
Not open for further replies.
Top Bottom