program makes c++ slow

So I have the following program that makes c++ slower after I run it. I think it happened after I added the NewRapHorner function and the do while loop in the main. What can be the reason and how can it be fixed?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <iomanip>
#include <math.h>
#define MAXGRAD 15

using namespace std;

void IndtastGrad(int &n);
void IndtastKoefficienter(double a[MAXGRAD+1], int n);
void UdskrivKoefficienter(double a[MAXGRAD+1], int n);
void UdskrivPolynomium(double a[MAXGRAD+1], int n);
void RetPolynomium(double a[MAXGRAD+1], int n);
void Horner(int n, double a[MAXGRAD], double x, double &fx, double &dfx);
void IndtastNRData(double &x0, double &eps, int &N);
void NewRapHorner(double x0, double eps, int N, int n, double a[MAXGRAD+1], double &rod, int &success);
void HornerDivPol(int &n, double a[MAXGRAD+1],double rod);

int main() {
	int n,N,success;
	double a[MAXGRAD+1],fx,dfx,x,x0,eps,rod;
	char svar;

	IndtastGrad(n);
    IndtastKoefficienter(a,n);
    UdskrivKoefficienter(a,n);
    UdskrivPolynomium(a,n);
    cout<<"Vil du gerne ændre en eller flere af koefficienterne i polynomiet?"<<endl<< "Svar j for ja eller n for nej";
    cin>>svar;
    if(svar=='j')
    RetPolynomium(a,n);
    Horner(n,a,x,fx,dfx);
    IndtastNRData(x0,eps,N);
   do{
    NewRapHorner(x0,eps,N,n,a,rod,success);
    if(success==1)
    HornerDivPol(n,a,rod);
   }while(success==1);
		return 0;
}

void IndtastGrad(int &n){
	cout<<"Indtast graden af det polynomium du vil indtaste: ";
	cin>>n;
}

void IndtastKoefficienter(double a[MAXGRAD+1], int n){
	cout<<"Indtast koefficienterne i polynomiet med koefficienten tilhørende den højeste potens først: ";

	for(int i=0; i<=n;i++)
		cin>>a[i];
	cout<<endl;
}

void UdskrivKoefficienter(double a[MAXGRAD+1], int n){
	for(int i=0; i<=n;i++)
    cout<<a[i]<<" ";

	cout<<endl;
}

void UdskrivPolynomium(double a[MAXGRAD+1], int n){
	cout<<"f(x)=";
	for(int i=0;i<=n;i++){
		cout<<a[i];
		if(n-i!=0){
			cout<<"(x^"<<n-i<<")";
		}
		if(a[i+1]>=0){
			if(i==n){
				break;
			}
		cout<<"+";

		}
	}
	cout<<endl;
}

void RetPolynomium(double a[MAXGRAD+1], int n){
	int i;
	char svar;
	do{
	cout<<"Hvilken koefficient vil du ændre?";
	cin>>i;
	cout<<"Indtast det nye tal";
	cin>>a[i-1];
	cout<<"Vil du ændre en anden koefficient eller den samme? Tryk j for ja eller n for nej";
	cin>>svar;
	}while(svar=='j');

	cout<<"Polynomiet ser nu således ud"<<endl;

	cout<<"f(x)=";
	for(int i=0;i<=n;i++){
		cout<<a[i];
		if(n-i!=0){
			cout<<"(x^"<<n-i<<")";
		}
		if(a[i+1]>=0){
			if(i==n){
				break;
			}
		cout<<"+";

		}
	}

}

void Horner(int n, double a[MAXGRAD], double x, double &fx, double &dfx){

	double A,B;

	cout<<endl<<"Indtast punktet, hvor du ønsker at finde funktionsværdien og den afledte i punktet";
	cin>>x;

	fx=a[0];
    dfx=a[0];

for(int i=0;i<n;i++){

	fx=fx*x+a[i+1];

	if(i!=n-1)
	dfx=dfx*x+fx;
	}

	cout<<"fx = "<<fx<<endl;
    cout<<"dfx = "<<dfx<<endl;

    cout<<"Indtast et interval, hvor du vil se funktionsværdien for 20 forskellige punkter"<<endl;
    cin>>A;
    cin>>B;

    cout<< setfill(' ') << setw(1) << "|" << setw(10) << left << "  x  " << setw(1) << "|" << setw(15) << left << "    fx    " << endl;

    x=A;
    for(int i=1;i<=20;i++){
    	fx=a[0];

    	if(i!=1)
    	x+=(B-A)/19;

    	for(int i=0;i<n;i++){

    		fx=fx*x+a[i+1];

    		}
	    cout << "  " <<right<< x << '\t' << "" <<setw(20)<<setprecision(20)<<right<< fx << '\t' <<endl;

    }


}

void IndtastNRData(double &x0, double &eps, int &N){
	cout<<"Indtast parametre til Newton-Raphson.\nIndtast startpunkt x0: "<<endl;
	cin>>x0;
	cout<<"Indtast tolerance eps: "<<endl;
	cin>>eps;
	cout<<"Indtast maksimalt antal tilladte iterationer N: "<<endl;
	cin>>N;
}

void NewRapHorner(double x0, double eps, int N, int n, double a[MAXGRAD+1], double &rod, int &success){

	double fx,dfx;
	int i=0;

	rod=x0;

	do{
		fx=a[0];
		dfx=a[0];

		for(int k=0;k<n;k++){

			fx=fx*rod+a[k+1];

			if(k!=n-1)
			dfx=dfx*rod+fx;
			}


		i++;
		if(dfx==0){
		break;
		}
		rod=rod-(fx/dfx);


		} while (fabs(fx)>eps and i<N);

	if(dfx==0){
			cout<<" Afslutning da dfx=0";

				}
		else if(i>=N){
			cout<<" Afslutning grundet overskridelse af max antal iterationer";
		}
		else{
		cout<<"nulpunktet til x = "<<rod<<" hvor f(x) = "<<fx;
		success=1;
		}
}

void HornerDivPol(int &n, double a[MAXGRAD+1],double rod){
	double fx;

	fx=a[0];


for(int i=0;i<n;i++){

	fx=fx*rod+a[i+1];
    a[i+1]=fx;
	}
n+=-1;
}
Topic archived. No new replies allowed.