[SOLVED] C++ error...floating point error abnormal program termination plz help

Status
Not open for further replies.

Qube

Member level 5
Joined
Nov 10, 2010
Messages
80
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Activity points
1,896
here's the program...

the idea

there are 2 groups named as hash and mad
hash has 85 people
mad has 15 people
hash group grows @ 10% per decade
mad group grows @ 30% per decade..

Now this program has to calculate after how many decade hash group n mad group will have same strength....

here's the code

#include<iostream.h>
#include<conio.h>
float growth_cal(float,int);
main()
{
clrscr();
cout<<" \t\t\t Growth by decade\n\n\n";
cout<<" Enter the values\n";
float hbase_pop=0.0,hpercent,hgrowth=0.0;
float mbase_pop=0.0,mpercent,mgrowth=0.0;
cout<<"Enter the Base value of M\n";
cout<<">>";
cin>>mbase_pop;
cout<<"Enter the growth percent of M\n";
cout<<">>";
cin>>mpercent;
cout<<"Enter the Base value of H\n";
cout<<">>";
cin>>hbase_pop;
cout<<"Enter the growth percent of H\n";
cout<<">>";
cin>>hpercent;
int targetdec=0;

loop:
mgrowth=growth_cal(mbase_pop,mpercent);
hgrowth=growth_cal(hbase_pop,hpercent);
mbase_pop=mbase_pop+mgrowth;
hbase_pop=hbase_pop+hgrowth;
targetdec++;

if(mbase_pop==hbase_pop)
{
cout<<"The number of decade when both are equal ="<<targetdec;
}
else
{
goto loop;
}
getch();
}

//Functions


float growth_cal(float b,int d)
{
float growth,growth1;
growth=b*d;
growth1=growth/100;
return growth1;
}


if i excecute ctrl F9..the output screen exits after i press alt+F5 ,


floating point errorverflow
abnormal program termination



plz debug this code...
 

^^ C++ Version 3.0 and Windows 7 operating system
 

The problem is that you are trying to get mbase_pop==hbase_pop

For example:


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
 
int main(int argc,char **argv)
{
    float hash_pop,hash_growth;
    float mad_pop,mad_growth;
    int decades = 0;
    cout<<"Initial state of hash group (Population growth): ";
    cin>>hash_pop>>hash_growth;
    cout<<"Initial state of mad group (Population growth): ";
    cin>>mad_pop>>mad_growth;
    hash_growth = hash_growth/100.0+1.0;
    mad_growth = mad_growth/100.0+1.0;
    while(hash_pop != mad_pop)
    {
        decades += 1;
        hash_pop += hash_pop*hash_growth;
        mad_pop += mad_pop*mad_growth;
        cout<<"decade "<<decades<<endl;
        cout<<"hash group: "<<hash_pop<<"\tmad group: "<<mad_pop<<endl;
    }
    cout<<"In "<<decades<<" decade hash and mad groups have the same strength\n";
 
    return 0;
}




Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Initial state of hash group (Population growth): 85 10
Initial state of mad group (Population growth): 15 30
...
 
 
hash group: 5.7904e+06  mad group: 3.99953e+06
decade 16
hash group: 1.21598e+07 mad group: 9.19891e+06
decade 17
hash group: 2.55357e+07 mad group: 2.11575e+07
decade 18
hash group: 5.36249e+07 mad group: 4.86622e+07
decade 19
hash group: 1.12612e+08 mad group: 1.11923e+08 <-- Decade 19
decade 20
hash group: 2.36486e+08 mad group: 2.57423e+08  <-- Decade 20
decade 21
hash group: 4.9662e+08  mad group: 5.92073e+08
decade 22
hash group: 1.0429e+09  mad group: 1.36177e+09
decade 23
...
 
hash group: 1.31222e+35 mad group: 2.7168e+38
decade 104
hash group: 2.75566e+35 mad group: inf  <-- Overflow
...
 
hash group: 2.18877e+38 mad group: inf
decade 114
hash group: inf mad group: inf



Replacing == (!=) by >


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
 
int main(int argc,char **argv)
{
    float hash_pop,hash_growth;
    float mad_pop,mad_growth;
    int decades = 0;
    cout<<"Initial state of hash group (Population growth): ";
    cin>>hash_pop>>hash_growth;
    cout<<"Initial state of mad group (Population growth): ";
    cin>>mad_pop>>mad_growth;
    hash_growth = hash_growth/100.0+1.0;
    mad_growth = mad_growth/100.0+1.0;
    while(hash_pop > mad_pop)
    {
        decades += 1;
        hash_pop += hash_pop*hash_growth;
        mad_pop += mad_pop*mad_growth;
        cout<<"decade "<<decades<<endl;
        cout<<"hash group: "<<hash_pop<<"\tmad group: "<<mad_pop<<endl;
    }
    cout<<"In "<<decades<<" decade hash and mad groups have the same strength\n";
 
    return 0;
}



Code C++ - [expand]
1
2
3
4
5
6
hash group: 5.36249e+07 mad group: 4.86622e+07
decade 19
hash group: 1.12612e+08 mad group: 1.11923e+08
decade 20
hash group: 2.36486e+08 mad group: 2.57423e+08
In 20 decades hash and mad groups have the same strength



The next step is to find the smaller value of abs(hash_pop-mad_pop) and select the decade in which hash group and mad group have the same strength


Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdlib.h>
using namespace std;
 
int main(int argc,char **argv)
{
    float hash_pop,hash_growth;
    float mad_pop,mad_growth;
    float diff, diff_temp;
    int decades = 0;
    cout<<"Initial state of hash group (Population growth): ";
    cin>>hash_pop>>hash_growth;
    cout<<"Initial state of mad group (Population growth): ";
    cin>>mad_pop>>mad_growth;
    hash_growth = hash_growth/100.0+1.0;
    mad_growth = mad_growth/100.0+1.0;
    diff = abs(hash_pop-mad_pop);
    while(true)
    {
        decades += 1;
        hash_pop += hash_pop*hash_growth;
        mad_pop += mad_pop*mad_growth;
        cout<<"decade "<<decades<<endl;
        cout<<"hash group: "<<hash_pop<<"\tmad group: "<<mad_pop<<endl;
        diff_temp = abs(hash_pop-mad_pop);
        if(diff_temp < diff)
            break;
        else
            diff = diff_temp;
    }
    cout<<"In "<<decades<<" decade hash and mad groups have the same strength\n";
 
    return 0;
}




Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Initial state of hash group (Population growth): 85 10
Initial state of mad group (Population growth): 15 30
decade 1
hash group: 178.5   mad group: 34.5
decade 2
hash group: 374.85  mad group: 79.35
decade 3
hash group: 787.185 mad group: 182.505
decade 4
hash group: 1653.09 mad group: 419.761
decade 5
hash group: 3471.49 mad group: 965.451
decade 6
hash group: 7290.12 mad group: 2220.54
decade 7
hash group: 15309.3 mad group: 5107.24
...
decade 15
hash group: 5.7904e+06  mad group: 3.99953e+06
decade 16
hash group: 1.21598e+07 mad group: 9.19891e+06
decade 17
hash group: 2.55357e+07 mad group: 2.11575e+07
decade 18
hash group: 5.36249e+07 mad group: 4.86622e+07
decade 19
hash group: 1.12612e+08 mad group: 1.11923e+08
In 19 decade hash and mad groups have the same strength



ups,there are some mistakes in code but thats is the general idea

---------- Post added at 19:41 ---------- Previous post was at 18:39 ----------

Corrected code

Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <stdlib.h>
using namespace std;
 
int main(int argc,char **argv)
{
    float hash_pop,hash_growth;
    float mad_pop,mad_growth;
    int decades = 0;
    cout<<"Initial state of hash group (Population growth): ";
    cin>>hash_pop>>hash_growth;
    cout<<"Initial state of mad group (Population growth): ";
    cin>>mad_pop>>mad_growth;
    hash_growth = hash_growth/100.0+1.0;
    mad_growth = mad_growth/100.0+1.0;
    while(hash_pop > mad_pop)
    {
        decades += 1;
        hash_pop = hash_pop*hash_growth;
        mad_pop = mad_pop*mad_growth;
        cout<<"decade "<<decades<<endl;
        cout<<"hash group: "<<hash_pop<<"\tmad group: "<<mad_pop<<endl;
    }
    cout<<"Between "<<decades-1<<" and "<<decades<<" decades hash and mad groups have the same strength\n";
 
    return 0;
}




Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Initial state of hash group (Population growth): 85 10
Initial state of mad group (Population growth): 15 30
decade 1
hash group: 93.5    mad group: 19.5
decade 2
hash group: 102.85  mad group: 25.35
decade 3
hash group: 113.135 mad group: 32.955
decade 4
hash group: 124.449 mad group: 42.8415
decade 5
hash group: 136.893 mad group: 55.6939
decade 6
hash group: 150.583 mad group: 72.4021
decade 7
hash group: 165.641 mad group: 94.1228
decade 8
hash group: 182.205 mad group: 122.36
decade 9
hash group: 200.426 mad group: 159.067
decade 10
hash group: 220.468 mad group: 206.788
decade 11
hash group: 242.515 mad group: 268.824
Between 10 and 11 decades hash and mad groups have the same strength

 
Last edited:
Reactions: Qube

    Qube

    Points: 2
    Helpful Answer Positive Rating
Thank u aarmalo...the error was if(mbase_pop==hbase_pop)....
The program goes looping till the both mbase_pop n hbase_pop are really equal in decimal places too..so it creates too much floating points....
by changing that == to >= ,it will stop the loop n prints the decade value when the mbase_pop goes more than hbase_pop

Corrected code
Code:
 #include<iostream.h>
#include<conio.h>
float growth_cal(float,int);
main()
{
clrscr();
cout<<" \t\t\t Growth by decade\n\n\n";
cout<<" Enter the values\n";
float hbase_pop=0.0,hpercent,hgrowth=0.0;
float mbase_pop=0.0,mpercent,mgrowth=0.0;
cout<<"Enter the Base value of M\n";
cout<<">>";
cin>>mbase_pop;
cout<<"Enter the growth percent of M\n";
cout<<">>";
cin>>mpercent;
cout<<"Enter the Base value of H\n";
cout<<">>";
cin>>hbase_pop;
cout<<"Enter the growth percent of H\n";
cout<<">>";
cin>>hpercent;
int targetdec=0;

loop:
mgrowth=growth_cal(mbase_pop,mpercent);
hgrowth=growth_cal(hbase_pop,hpercent);
mbase_pop=mbase_pop+mgrowth;
hbase_pop=hbase_pop+hgrowth;
targetdec++;

[B]if(mbase_pop[U]>=[/U]hbase_pop)[/B]
{
cout<<"The number of decade when both are equal ="<<targetdec;
}
else
{
goto loop;
}
getch();
}

//Functions


float growth_cal(float b,int d)
{
float growth,growth1;
growth=b*d;
growth1=growth/100;
return growth1;
}



Thank u for ur help...:smile:
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…