octal - c++ - why the result is minus? -


i have problem simple program if input more 295600127, result minus (-).

here :

#‎include‬ <iostream> #include <windows.h> using namespace std; int konarray(int b); void konbil(int a[], int &n); int kali(int x); main(){    int b;    char *awal,akhir,pil;    awal:    system("color 9f");    cout<<"enter decimal\t= ";cin>>b;    //calling function of conversion array of integers    konarray(b);    akhir:    cout<<"\n\ndo want reply ? (y/n) : ";    cin >> pil;    cout<<endl;    switch(pil){       case 'y' :       case 'y' :          system ("cls");          goto awal;           break;       case'n':       case 'n' :           break;       default:       system("color c0");       system ("cls");       cout << "please, make sure entering choise!!\n\n\n\n";       goto akhir;    } } //convertion numer array int konarray(int b){    int a[30];    int i,n,h,s;    i=0;    do{       h=b/8;       s=b%8;       b=h;       a[i]=s;       i++;    }    while(h!=0);    n=i;    for(i=0;i<n;i++)       cout<<a[i]<<" ";    konbil(a,n); } //array octal void konbil(int a[],int &n){    int c,i;    c=a[0];    for(i=1;i<n;i++)       c=c+a[i]*kali(i);    system("color f0");    cout<<endl<<"the results of conversion are\t= ";    cout<<c<<endl; } int kali(int x){    if (x==1)       return (10);    else       return(10*kali(x-1)); } 

i have tried change of int long, same.
i want know reason why?
and how fix it?

have tried

long long int 

this program

int main(int argc, char** argv) {     cout << sizeof(int) << endl;     cout << sizeof(long int) << endl;     cout << sizeof(long long int) << endl;       return 0; } 

gives

4 4 8 

showing need long long int 64 bits

change this:

void konbil(int a[],int &n){    unsigned long long c,i;     // unsigned long long     c=a[0];    for(i=1;i<n;i++)       c=c+a[i]*kali(i);    system("color f0");    cout<<endl<<"the results of conversion are\t= ";    cout<<c<<endl; } 

the largest positive number can store int int 2147483647 (2^31 - 1). adding 1 number result in value -2147483648 (- 2^31).

so answer have overflow while using int. therefore need long long int or better unsigned long long.

unsigned long long can't negative , allows maximum value (2^64 - 1).

edit: question added in comment - therefore edit.

int

on systems int 32 bits.

it can take values -2^31 2^31-1, i.e. -2147483648 2147483647

unsigned int

an unsigned int 32 bits. unsigned int can not negative.

instead has range 0 2^32-1, i.e. 0 4294967295

long long int

when use long long int have 64 bits.

so valid range -2^63 2^63-1, i.e. -9223372036854775808 9223372036854775807

unsigned long long

a unsigned long long 64 bits can not negative.

the range 0 2^64-1, i.e. 0 18446744073709551615

try code:

int main() {     cout << endl << "testing int:" << endl;      int x = 2147483647;   // max positive value     cout << x << endl;      x = x + 1;            // overflow     cout << x << endl;      cout << endl << "testing unsigned int:" << endl;      unsigned int y = 4294967295;   // max positive value     cout << y << endl;      y = y + 1;            // overflow     cout << y << endl;      cout << endl << "testing long long int:" << endl;      long long int xl = 9223372036854775807ll;   // max positive value     cout << xl << endl;      xl = xl + 1;            // overflow     cout << xl << endl;      cout << endl << "testing unsigned long long:" << endl;      unsigned long long yl = 18446744073709551615ull;   // max positive value     cout << yl << endl;      yl = yl + 1;            // overflow     cout << yl << endl;      return 0; } 

it give you

testing int: 2147483647 -2147483648  testing unsigned int: 4294967295 0  testing long long int: 9223372036854775807 -9223372036854775808  testing unsigned long long: 18446744073709551615 0 

showing how overflow max positive value adding 1.

also see link http://www.cplusplus.com/reference/climits/


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -