First project

Hi all,

I am new to programming and C++. I need to make an assignment for school about
numerical ways to calculate pH values of acid-base titrations.

i got this so far:

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
#include <iostream>
#include <math.h>

using namespace std;

double Kw = 1e-14;
int Newton_Length;
double Newton_Results [];

double pH_function (double H, double Va, double Ca, double Ka, double Vb, double Cb, double Kb)
{
	double fH;
	fH = Va*((Kw/H)-H+Ca*(Ka/(Ka+H)))-Vb*(H-(Kw/H)+Cb*(H/(Kb+H)));
	return fH;
}

double pH_function_1d (double H, double Va, double Ca, double Ka, double Vb, double Cb, double Kb)
{
	double fH;
	fH = Vb*(-(Kw/pow(H,2))-1-Cb*(Kb/(pow(Kb+H,2))))-Va*((Kw/pow(H,2))+1+Ca*(Ka/(pow(Ka+H,2))));
	return fH;
}

void Newton (double Newton_Va, double Newton_Ca, double Newton_Ka, double Newton_Vb, double Newton_Cb, double Newton_Kb)
{
	double fx, fx_1d;
	double diff = 1;
	Newton_Length = 0;
	Newton_Results [0] = 1e-14;

	while (diff > 0.001)
	{
		fx = pH_function (Newton_Results [Newton_Length], Newton_Va, Newton_Ca, Newton_Ka, Newton_Vb, Newton_Cb, Newton_Kb);
		fx_1d = pH_function_1d (Newton_Results [Newton_Length], Newton_Va, Newton_Ca, Newton_Ka, Newton_Vb, Newton_Cb, Newton_Kb);
		Newton_Results [Newton_Length + 1] = Newton_Results [Newton_Length] - (fx/fx_1d);
		Newton_Length ++;
		diff = fabs((-log10(Newton_Results [Newton_Length])) - (-log10(Newton_Results [Newton_Length + 1])));
	}
}

int main()
{
	//Basis input
	double Main_Va, Main_Ca, Main_pKa, Main_Vb, Main_Cb, Main_pKb;
	cout << "Volume van het zuur? ";
	cin >> Main_Va;
	cout << "Concentratie van het zuur? ";
	cin >> Main_Ca;
	cout << "Zuurconstante (pKah) van het zuur? ";
	cin >> Main_pKa;
	cout << "Volume van de base? ";
	cin >> Main_Vb;
	cout << "Concentratie van de base? ";
	cin >> Main_Cb;
	cout << "Zuurconstante (pKbh+) van de base? ";
	cin >> Main_pKb;

	//Omvorming pKa en pKb naar Ka en Kb
	double Main_Ka, Main_Kb;
	Main_Ka = pow(10, -Main_pKa);
	Main_Kb = pow(10, -Main_pKb);

	Newton (Main_Va, Main_Ca, Main_Ka, Main_Vb, Main_Cb, Main_Kb);

	int Max_Length = 0;
	int cycle = 0;
	if (Max_Length < Newton_Length)
	{
		Max_Length = Newton_Length;
	}

	while (cycle<Max_Length)
	{
		cout << "Cyclus		Newton (pH) \n";
		cout << cycle << "    		" << -log10(Newton_Results [cycle]);
	}

	system("PAUSE");
	return 0;
}


but i get the error:
1>------ Build started: Project: Test comp, Configuration: Debug Win32 ------
1> Main.cpp
1>Main.obj : error LNK2001: unresolved external symbol "double * Newton_Results" (?Newton_Results@@3PANA)
1>C:\Users\Stefaan\Documents\Visual Studio 2010\Projects\Test comp\Debug\Test comp.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I dont know what i did wrong and i dont get the error.
Some help would be realy appreciated
The way to include the math functions is using <cmath> rather than the old, deprecated <math.h>.

Also your array Newton_Results needs to be given a size in the brackets

1
2
3
4
5
6
7
8
9
10

#include <iostream>
//#include <math.h> // please don't use this
#include <cmath> // Please us this instead

using namespace std;

double Kw = 1e-14;
int Newton_Length;
double Newton_Results []; // needs size 
Last edited on
Also it is good to avoid system() calls:

1
2
3
4
5
6
7
8


	// system("PAUSE"); // Please avoid using system()
	std::cin.ignore(); // You can use these statemnts instead
	std::cin.get();

	return 0;
}
The problem is i don't know what size the Newton_Results array is gonna be, because the amount
of iterations used by newton-raphson to converge on the solution of the equation differs with
given variables. So is there a way to declare a sizeless array or should i just make it big engough?
If you don't know the size of your array, it would be better to use a vector. For this you need to #include <vector>. A vector is declared like this: vector <double> Newton_Results;
kaotic wrote:
So is there a way to declare a sizeless array or should i just make it big engough?


As fafner says the best way is a std::vector<>. Its basically a managed array from the standard library.

In C++ arrays can't automatically grow in size. You need to know how big they are beforehand or else you need to be prepared to make them bigger as needed yourself.

This is exactly what vectors are for.

1
2
3
4
5
6
7
8
9
#include <vector>
#include <iostream>
#include <math.h>

using namespace std;

double Kw = 1e-14;
//int Newton_Length; // not needed, vector will remember its length
std::vector<double> Newton_Results;


Then you can use it like this:

1
2
3
4
5
6

Newton_Results.clear(); // delete all elements
Newton_Results.push_back(1e-14); // add one element to end of the array
Newton_Results.back(); // access the last element in the array
Newton_Results.size(); // number of elements in the array
Newton_Results.at(3); // 4th element of the array 0,1,2,3  
Last edited on
Allright guys it seems that using the vector instead of the array solved my error, thx for your help
Topic archived. No new replies allowed.