Help with writing function

I am having some more troubles with functions. This function will calculate and return the X-coordinate of the vertex of the parabola. It takes two double arguments: the a-coefficient for the parabola and the b-coefficient for the parabola. It returns a double: the X-coordinate of the vertex of the parabola.

The formula for calculating the X-coordinate of the vertex is:

X-Vertex = -b-coefficient / ( 2 * a-coefficient)


xVertex = calcXvertex( aCoeff, bCoeff );

Here is what I have done so far (not sure if I'm right)
1
2
3
double calcXvertex()
calcXvertex = -bCoeff / (2 *aCoeff)
xVertex = calcXvertex( aCoeff, bCoeff );
Thanks for the link (I read over it, and the C++ book), but that does not help me with the question above. I still have no clue how to tackle this problem.
that does not help me with the question above
It should. The problem is that you're trying to define a function in a way that makes no syntactical sense.
ok, so something like this:

1
2
3
4
5
6
double calcXvertex ( double, double );
	{
	xVertex = -bCoeff / ( 2 * aCoeff);
	
	return xVertex;	
	}


The parameter names are not optional.

EDIT: Also, you're not declaring xVertex.
Last edited on
I declared xVertex prior to this chunk of code (sorry for not informing you).

I remember reading in the article that the parameter names are the values that will be involved in the function.

so I am just going to hit you with my code. So you can see the whole code (in full).
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//*********************************************************************
//*
//*       Function Prototypes
//*
//*********************************************************************
double getA ();
double calcXvertex ( double, double );
double calcYvertex ( double, double, double );
void printConcavity ( double );
double calcDiscriminant ( double, double, double );
void printRoots ( double, double, double );



int main()
{
//*********************************************************************
//*
//* Variable Dictionary
//*
//* aCoeff, bCoeff, cCoeff  -  used to hold the coefficients
//*
//* xVertex  -  used to hold the X-coordinate of the vertex
//*
//* yVertex  -  used to hold the Y-coordinate of the vertex
//*
//*********************************************************************

double aCoeff, bCoeff, cCoeff;
double xVertex, yVertex;




//Setup the formatting

cout << fixed << setprecision(3);


//Get the A, B, and C coefficients from the user


double getA();
{
cout << "Enter the a coefficient (non -zero value:\n";
cin >> aCoeff;

while (aCoeff < 0)
	{
	cout << "Error: The a-value MUST be non-zero. Try Again:";
	cin >> aCoeff;
	} 
	
	return aCoeff;
	}
	

//Calculate the X and Y coordinates for the vertex of the parabola

double calcXvertex ( aCoeff, bCoeff );
	{
	xVertex = -bCoeff / ( 2 * aCoeff);
	
	return xVertex;	
	}

double calcYvertex ( double, double, double );
	{
	yVertex = ( aCoeff * (xVertex^2) ) + ( bCoeff * xVertex ) + cCoeff;
	
	return yVertex;	
	};


//Display the coefficients and vertices

cout << endl << "*******************************************"
     << endl << "        Quadratic Equation Analyzer"
     << endl << "*******************************************"
     << endl << "a Coefficient" << setw(30) << aCoeff
     << endl << "b Coefficient" << setw(30) << bCoeff
     << endl << "c Coefficient" << setw(30) << cCoeff
     << endl << "-------------------------------------------"
     << endl << "X Coordinate" << setw(31) << xVertex
     << endl << "Y Coordinate" << setw(31) << yVertex
     << endl << "-------------------------------------------";


//Display the concavity of the parabola

void printConcavity ( double );
	{
	if ( aCoeff > 0 )
		{
		cout << "Parabola opens UPWARD." << endl;	
		}	
	else 
		{
		cout << "Parabola opens DOWNWARD." << endl;	
		}	
		
	return 0;
	}


//Display the roots

cout << endl << "-------------------------------------------";

double calcDiscriminant ( double, double, double );
	{
	double discriminant;
	
	discriminant = (bCoeff^2 - 4) * (aCoeff * cCoeff);
	
	return discriminant;	
	}

void printRoots ( double, double, double );
	{
	if ( calcDiscriminant >	0 )
		{
		cout << "There are two real roots.";	
		}
	if else ( calcDiscriminant == 0 )
		{
		cout << "It has one root with a multiplicity of 2.";	
		}	
	else 
		{
		cout << "There are no real roots";	
		}	
	return 0;	
	}


cout << endl << endl;
system("pause");
return 0;
}


Do you think what I did in line 66 should be the status quo for every place on this script that has something like (double, double)? Also since I have given you the whole code, I might as well tell you that I have had some troubles compiling the thing, any clues why (except for the things you have already addressed)?

I really appreciated your help in dealing with me helios. I have spent hours writing this code, and I just want to go to bed feeling like I have accomplished something.
Last edited on
Okay, well, there's many things wrong, here.

1. You can't define functions inside of other functions. main() is a function.
2. Nearly every procedural programming language makes a distinction between formal parameters and actual parameters (look it up). I can very well do this:
1
2
3
4
5
6
7
int times2(int x){
    return 2*x;
}
//...
int foo=5;
int bar=times2(foo);
foo=times2(bar);
There doesn't need to be any relation between the expression you pass to a function and the name of the corresponding formal parameter. In fact, most of the time there won't be.
What this means is that the parameter name, as I said before, is not optional.
There is one exception, though: if you don't use the parameter in the function, you can leave it anonymous:
1
2
3
int foo(int){
    return 2;
}
The parameter type is always mandatory.
3. There are many semicolons where they don't belong. The semicolon terminates statements. If you misplace a semicolon, you can radically change the meaning of a piece of code:
1
2
3
4
5
6
7
8
9
10
int foo(int)
{
    return 2;
}
[code]Means something very different from
[code]
int foo(int);
{
    return 2;
}
Depending on the surrounding context, one or the other may compile and the other not. However, you'll practically never want the latter version.
4. In C/++, ^ is not exponentiation.
5. Logic error: The discriminant is b*b - 4*a*c, not (b*b-4)*a*c.
6. if else is not a valid sequence of tokens. The valid form is else if
7. If a function has return type void, the return statement should either not be there, or not be followed by an expression.
1
2
3
4
5
6
void foo(){
}

void bar(){
    return;
}


I'll clean the code up for you, so you can see how it should be done:
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// I'll omit the function declarations for stylistic reasons. Feel free to add
// them back if you want.

double getA(){
	double aCoeff;
	cout << "Enter the a coefficient (non -zero value:\n";
	cin >> aCoeff;

	while (aCoeff < 0){
		cout << "Error: The a-value MUST be non-zero. Try Again:";
		cin >> aCoeff;
	} 
	return aCoeff;
}

double calcXvertex(double aCoeff,double bCoeff){
	return -bCoeff / ( 2 * aCoeff);	
}

double calcYvertex(double aCoeff,double bCoeff,double cCoeff){
	double xVertex=calcXvertex(aCoeff,bCoeff);
	return aCoeff*xVertex*xVertex + bCoeff*xVertex + cCoeff;	
}

void printConcavity(double aCoeff){
	if (aCoeff>0)
		cout << "Parabola opens UPWARD." << endl;
	else
		cout << "Parabola opens DOWNWARD." << endl;
}

double calcDiscriminant(double a,double b,double c){
	return b*b - 4*a*c;	
}

void printRoots(double a,double b,double c){
	//The function has to actually be called to take effect:
	double discrimitant=calcDiscriminant(a,b,c);
	if (discrimitant>0)
		cout << "There are two real roots.";	
	else if (discrimitant==0)
		cout << "It has one root with a multiplicity of 2.";	
	else 
		cout << "There are no real roots";	
}

int main()
{
	double aCoeff, bCoeff, cCoeff;
	double xVertex, yVertex;

	cout << fixed << setprecision(3);

	//The function has to actually be called to take effect:
	aCoeff=getA();
	
	//TODO: get the other coefficients.
	
	xVertex=calcXvertex(aCoeff,bCoeff);
	yVertex=calcYvertex(aCoeff,bCoeff,cCoeff);

	//Display the coefficients and vertices
	cout << endl << "*******************************************"
		<< endl << "        Quadratic Equation Analyzer"
		<< endl << "*******************************************"
		<< endl << "a Coefficient" << setw(30) << aCoeff
		<< endl << "b Coefficient" << setw(30) << bCoeff
		<< endl << "c Coefficient" << setw(30) << cCoeff
		<< endl << "-------------------------------------------"
		<< endl << "X Coordinate" << setw(31) << xVertex
		<< endl << "Y Coordinate" << setw(31) << yVertex
		<< endl << "-------------------------------------------";


	//Once again, the function has to be called, or nothing happens.
	printConcavity(aCoeff);
	
	cout << endl << "-------------------------------------------";
	printRoots(aCoeff,bCoeff,cCoeff);
	
	cout << endl << endl;
	return 0;
}
Last edited on
Thank you helios, I am left speechless. I took 30min+ to read this code over and over again to insure that I did not waste your time in teaching me. I never knew that functions could even be placed before main() like that.

I only have one thing to say: YOU ARE THE BEST :).
Topic archived. No new replies allowed.