So, I have an application that uses a bunch of math formulas in a DOS choose a number style. So I made a static library and I currently have them linked but when I try using a function it will say no members detected. Here's my code (the important parts):
// The library header file:
namespace Library_ {
static float min, max, n, rem, i, r, l, w, m, v, k, r1, r2, r3, r4, h, b,
b1, b2, s, value, count = 0; static float f = 1;
static class Main
{
public:
class Geometry_2Dimensional
{
static void rectangle();
static void circle();
static void ETGRadius();
static void ETGSide();
};
class Geometry_3Dimensional
{
static void prismRectangle();
static void prismTriangle();
static void cylinder();
static void sphere();
};
class Computing
{
static void rng();
static void binary(int num);
};
class Number_Theory
{
static void average();
static void primeCheck();
static void primeFactors();
static void factorial();
};
static void Error();
};
}
// The Library source file:
include "CalculatorLib.h"
include <stdexcept>
include <iostream>
include <time.h>
include <math.h>
include <cstdlib>
include <conio.h>
using namespace std;
namespace Library_
{
void Main::Error()
{
cout << "Not a valid option." << endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::rectangle()
{
cout << "Length: "; cin >> l;
cout << "Width: "; cin >> w;
cout << "Area: " << l * w << "\nPerimeter: " << (2 * l) + (2 * w) <<
endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::circle()
{
cout << "Radius: "; cin >> r;
cout << "Area: " << r * 3.14 * r << "\nCircumference: " << r * 6.28 <<
endl;
system("PAUSE");
}
void Main::Geometry_2Dimensional::ETGRadius()
{
cout << "Integer part: "; cin >> r1;
cout << "Radicant: "; cin >> r2;
r3 = sqrt(r2);
m = (sqrt(3) / 3);
if (r2 == 3)
{
n = r1 * 3;
cout << "The side length of the triangle is: " << n << endl;
v = n / 2;
k = sqrt((n * n) - (v * v));
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
else
{
r4 = r3 * r1;
n = r4 / m;
cout << "The side length of the triangle is: " << n << endl;
v = n / 2;
k = sqrt((n*n) - (v*v));
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
}
void Main::Geometry_2Dimensional::ETGSide()
{
cout << "Enter the side length: "; cin >> s;
r = (s / 3) * sqrt(3);
cout << "The radius of the inner circle is: " << r << endl;
v = s / 2;
k = sqrt((s*s) - (v*v));
n = k * s;
cout << "The area of the triangle is: " << n / 2 << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::prismRectangle()
{
cout << "Length: "; cin >> l;
cout << "Width: "; cin >> w;
cout << "Height: "; cin >> h;
cout << "Area: " << l*w*h << "\nSurface Area: " << (2 * (l*w)) + (2 *
(l*h)) + (2 * (h*w)) << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::sphere()
{
cout << "Radius: "; cin >> r;
cout << "Volume: " << 3.14 * (4 / 3) * r * r * r << "\nSurface Area: "
<< 12.56 * r * r << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::cylinder()
{
cout << "Radius: "; cin >> r;
cout << "Height: "; cin >> h;
cout << "Volume: " << 3.14 * r * r * h << "\nSurface Area: " << 6.28 * r
* h * (r + h) << endl;
system("PAUSE");
}
void Main::Geometry_3Dimensional::prismTriangle()
{
cout << "Base of the base: "; cin >> b;
cout << "Height: "; cin >> h;
cout << "Length: "; cin >> l;
cout << "Other sides of base: " << "\nFirst other side: "; cin >> b1;
cout << "Second other side: "; cin >> b2;
cout << "Volume: " << h * b * l << "\nSurface Area: " << (2 * b * h) +
((b + b1 + b2) * l) << endl;
system("PAUSE");
}
void Main::Computing::rng()
{
cout << "Enter the minimum: "; cin >> min;
cout << "Enter the maximumL "; cin >> max;
cout << "Enter the number of generations: "; cin >> n;
int range = max - min + 1;
unsigned first = time(NULL);
srand(first);
for (i = 0; i < n; i++)
{
r = rand() / 100 % range + min;
cout << r << " ";
}
cout << endl;
system("PAUSE");
}
void Main::Computing::binary(int num)
{
if (num <= 1)
{
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
void Main::Number_Theory::average()
{
cout << "Enter the number of values: "; cin >> n;
float average(0);
for (int i = 0; i < n; ++i)
{
cin >> value;
average += value;
}
average /= n;
cout << "Average is " << average << endl;
system("PAUSE");
}
void Main::Number_Theory::factorial()
{
cout << "Enter the number: "; cin >> n;
for (int a = 1; a <= n; a++)
{
f *= a;
}
cout << "The factorial is: " << f << endl;
system("PAUSE");
}
void Main::Number_Theory::primeCheck()
{
cout << "Enter the number: "; cin >> n;
int count = 0;
for (int a = 1; a <= n; a++)
{
if (fmod(n, a) == 0)
{
count++;
}
}
if (count == 2)
{
cout << "The number " << n << " is prime!" << endl;
system("PAUSE");
}
else
{
cout << "The number " << n << " is not prime." << endl;
system("PAUSE");
}
}
void Main::Number_Theory::primeFactors()
{
cout << "Enter the number: "; cin >> n;
while (fmod(n, 2) == 0)
{
cout << "2, ";
n /= 2;
}
for (int i = 3; i <= sqrt(n); i += 2)
{
while (fmod(n, 1) == 0)
{
cout << i << ", ";
n /= i;
}
}
if (n > 2)
{
cout << n << ", ";
}
}
}
And then the main source file, that can't use any of the functions because intellisense says no members included.
Side note: I am including both the header file and using the namespace Library_.
If this seems rushed, it is because I have to get off. And also, in addition to solving this problem, can someone tell me when and when not to use static?
Thanks!
You are making so many mistakes here it is hard to know where to begin, using classes as if they were namespaces, using global variables, doing input and output in library code, not using function parameters and function returns, using static inappropriately, etc. etc. Also it is impossible to answer your question because you don't show how you are trying to call your functions.
I can answer one question which is 'when to use static'. At your level of experience the answer is never use static. You've got to learn the basics first.
Forget classes. forget namespaces, forget global variables, first learn how to how to write functions, how to use local variables, how to pass values to and return values from functions.
For example the following is perfectly acceptable beginner code
void Geometry_2Dimensional_ETGRadius(float r1, float r2, float& n, float& k)
{
float r3 = sqrt(r2);
float m = (sqrt(3) / 3);
if (r2 == 3)
{
n = r1 * 3;
float v = n / 2;
k = sqrt((n * n) - (v * v));
}
else
{
float r4 = r3 * r1;
n = r4 / m;
float v = n / 2;
k = sqrt((n*n) - (v*v));
}
}
int main()
{
float r1, r2, n, k;
cout << "Integer part: "; cin >> r1;
cout << "Radicant: "; cin >> r2;
Geometry_2Dimensional_ETGRadius(r1, r2, n, k);
cout << "The side length of the triangle is: " << n << endl;
cout << "The area of the triangle is: " << (k * n) / 2 << endl;
system("PAUSE");
}
See how the variables only used inside the function (r3, m etc) are declared inside the function. The values needed by the function are declared as parameters of the function (r1, r2) and the values returned by the function are declared as reference parameters (n and k). This is basic stuff and it is what you should get familiar with before you try anything more advanced.
Related
Given: two real numbers a, b (a < b) and a positive integer
n. Calculate the definite integral using the Newton-Leibniz formula:
a(top) ∫ b(bottom) (f(x,n)dx = F(b,n)-F(a,n)
where F(x,n) is the first-order (antiderivative, inverse derivative, primitive function, primitive integral) function f(x,n):
F(x, n) = integral(f(x,n)dx)
Implement the calculation of F(x,n) in two ways:
as a recursive.
as a nonrecursive function.
That's what needs to solve. Click on the link to open the photo.
F(x,n) = ∫(ctg^n*xdx)
I solved this problem using the pow function. My teacher asks me to solve this problem without using the pow function. Help me with the solution without using the pow function.
Recursion solution
// Recursion
double F(double x, int n)
{
if (n == 0)
return x;
else if (n == 1)
return log(fabs(sin(x)));
else
return -pow(cos(x) / sin(x), n - 1) / (n - 1) - F(x, n - 2);
}
Solving with a loop
// For loop
double F_for(double x, int n)
{
if (n == 0)
return x;
else if (n == 1)
return log(fabs(sin(x)));
else
{
double F1 = x;
double F2 = log(fabs(sin(x)));
double res = 0.0;
for (int i = 2; i <= n; i++)
{
res = -pow(cos(x) / sin(x), i - 1) / (i - 1) - F1;
F1 = F2;
F2 = res;
}
return res;
}
}
main function
int main()
{
double a, b;
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
int n;
cout << "Enter n: ";
cin >> n;
cout << "Recursion: " << F(b, n) - F(a, n) << std::endl;
cout << "A loop: " << F_for(b, n) - F_for(a, n);
return 0;
}
P.S.
Help me solve it without using the pow function, please.
I already wrote my function "my_power".
My instructor said: "You can't use a power function, neither the standard one (pow) nor your own (power). The reason is that any use of such functions leads to a loop, and your loop, or the one in the standard function, are details. A recursive function is itself a loop; using a variable-increment results in a loop - that's not efficient."
double power(double x, int y)
{
double temp;
if (y == 0)
return 1;
temp = power(x, y / 2);
if ((y % 2) == 0) {
return temp * temp;
}
else {
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
I also tested this function
void test_my_power()
{
cout << "pow" << pow(2, -3) << "0" << endl;
cout << "power" << power(2, -3) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, -3) << "0" << endl;
cout << "power" << power(0, -3) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, 1) << "0" << endl;
cout << "power" << power(0, 1) << "0" << endl;
cout << endl;
cout << "pow" << pow(0, -1) << "0" << endl;
cout << "power" << power(0, -1) << "0" << endl;
cout << endl;
cout << "pow" << pow(8, 4) << "0" << endl;
cout << "power" << power(8, 4) << "0" << endl;
cout << endl;
cout << "pow" << pow(11, 3) << "0" << endl;
cout << "power" << power(11, 3) << "0" << endl;
cout << endl;
cout << "pow" << pow(5, 6) << "0" << endl;
cout << "power" << power(5, 6) << "0" << endl;
cout << endl;
}
Your teacher probably wants you to compute pow on the fly as you iterate the loop.
This code:
const auto t = cos(x) / sin(x);
for(int i = 2; i <= n; ++i) {
const auto val = pow(t, i - 1);
...
}
is equivalent to:
auto power = t;
for(int i = 2; i <= n; ++i) {
const auto val = power;
...
power *= t;
}
and doesn't use pow.
I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}
You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");
This is how the code currently looks like.
#include <iostream>
#include <iomanip>
using namespace std;
int cos(angle)
{
float rad = angle;
float radPh = 1;
float facto = 1;
float tOld = 1;
float tNew = 0;
float tPh = 1;
float tDiff = 1;
float cosAns;
precision = precision + 1;
float x = 10;
float finalPr = 1;
for (int counter = 0; counter < precision; ++counter)
{
finalPr /= x;
}
finalPr = 0.5 * finalPr;
while (tDiff > finalPr)
{
tOld = tPh;
tNew = ((-1)*tOld*rad*rad)/((facto+1)*(facto+2));
tPh = tNew;
radPh = tNew;
facto = facto + 2;
tDiff = tOld - tNew;
cosAns = radPh;
}
return cosAns;
}
int main()
{
int precision = 10;
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << endl << "Current Precision: " << precision;
cout << endl << "Select:";
cout << endl << "1. Change Decimal Precision";
cout << endl << "2. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
cout << endl << "Please enter a value between 2 and 10. => ";
cin >> precision;
cout << "Current precision is " << precision;
}
if (choice == 2)
{
float angle, anglePh;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
cout << endl << "Please enter an angle. => ";
cin >> angle;
cout << endl << "Is the angle in degrees or in radian?";
cout << endl << "Input D if it's in degrees,";
cout << endl << "Input R if it's in radian. => ";
cin >> angleType;
if (angleType == 'D')
{
anglePh = angle;
angle = angle*pi/180;
cout << anglePh << " degrees = " << angle << " radian";
}
cout << endl << "Calculating Cos...";
cos(angle);
cout << endl << "Calculating Sin...";
}
if (choice == 9)
{
break;
}
}
}
I am building a program that gets the angle from the user and then convert it into radian before calculating its Sine and Cosine values. When I run this code it tells me that the angle from "int cos(angle)" was not defined, so I tried moving it into the if loop for choice 2, but it didn't solve the issue. It also says that cos(angle) cannot be used as a function when I tried to trigger it from the if loop for choice 2. Any ideas on this?
I actually made a similar program using Python back then, and this method that I used to write the program worked. I know there are some differences between Python and C++, but I thought the general idea would still apply.
Thanks in advance.
Cheers.
In the function definition/declaration you must mention the argument data type.
int cos(float angle, int precision)
#include <iostream>
#include <iomanip>
using namespace std;
int cos(float angle, int precision)
{
float rad = angle;
float radPh = 1;
float facto = 1;
float tOld = 1;
float tNew = 0;
float tPh = 1;
float tDiff = 1;
float cosAns;
precision = precision + 1;
float x = 10;
float finalPr = 1;
for (int counter = 0; counter < precision; ++counter)
{
finalPr /= x;
}
finalPr = 0.5 * finalPr;
while (tDiff > finalPr)
{
tOld = tPh;
tNew = ((-1)*tOld*rad*rad)/((facto+1)*(facto+2));
tPh = tNew;
radPh = tNew;
facto = facto + 2;
tDiff = tOld - tNew;
cosAns = radPh;
}
return cosAns;
}
int main()
{
int precision = 10;
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << endl << "Current Precision: " << precision;
cout << endl << "Select:";
cout << endl << "1. Change Decimal Precision";
cout << endl << "2. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
cout << endl << "Please enter a value between 2 and 10. => ";
cin >> precision;
cout << "Current precision is " << precision;
}
if (choice == 2)
{
float angle, anglePh;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
cout << endl << "Please enter an angle. => ";
cin >> angle;
cout << endl << "Is the angle in degrees or in radian?";
cout << endl << "Input D if it's in degrees,";
cout << endl << "Input R if it's in radian. => ";
cin >> angleType;
if (angleType == 'D')
{
anglePh = angle;
angle = angle*pi/180;
cout << anglePh << " degrees = " << angle << " radian";
}
cout << endl << "Calculating Cos...";
cout <<cos(angle, precision);
cout << endl << "Calculating Sin...";
}
if (choice == 9)
{
break;
}
}
}
I'm currently writing a code that sums together the coefficient of two polynomials.
This all works fine as long as the grade of the first polynomial is smaller or equal to the second. "if (d1 <= d2)"
However, when "d1 > d2", the code seems to have some kind of issue I don't understand.
I'm sitting on this for a while now but don't seem to find the problem. Does anybody have any hints?
Thanks in advance.
#include <iostream>
#include <math.h>
using namespace std;
double * poli_sum (double * p1, int d1, double * p2, int d2, int & dr);
double * read_poly(int &n){
cout << "Grade of function: ";
cin >> n;
double * c=new double[n+1];
for(int i=0;i<=n;i++){
cout << "Coefficient of degree " << i << ": ";
cin >> c[i];
}
return c;
}
double * read_second_poly(int &m){
cout << "Grade of second function: ";
cin >> m;
double * g=new double[m+1];
for(int i=0;i<=m;i++){
cout << "Coefficient of degree " << i << ": ";
cin >> g[i];
}
return g;
}
double * poli_sum (double * p1, int d1, double * p2, int d2, int & dr){
dr = 0;
if (d1 <= d2){
cout << "You're in first, d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl <<endl;
for (int i = 0; i <= d2; i++){
if (i >d1) p1[i] = 0; // setting array[i] to zero when it gets outside the defined grade
cout << (p1[i]+p2[i])<< endl;
cout << "pi1( "<< i << "): "<< p1[i] << endl;
cout << "pi2( "<< i << "): "<< p2[i] << endl << endl;
dr++;
}
}
else if (d1 > d2){
cout << "You're in second, d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl <<endl;
for (int i = 0; i <= d1; i++){
if (i > d2) p2[i] = 0; // setting array[i] to zero when it gets outside the defined grade
cout << (p1[i]+p2[i])<< endl;
cout << "pi1( "<< i << "): "<< p1[i] << endl;
cout << "pi2( "<< i << "): "<< p2[i] << endl << endl;
dr++;
}
}
cout << "dr: "<< dr << endl;
return 0;
}
int main()
{
double *p1;
double *p2;
int g1, g2, g3;
p1=read_poly(g1);
p2 = read_second_poly(g2);
poli_sum(p1,g1, p2, g2, g3);
delete[] p1;
delete[] p2;
return 0;
}
Below is some code that I am trying to write for my class. I have the dot product worked out. Cross product is giving me some trouble. Having trouble understanding the error I get:
invalid conversion from 'int*' to 'int'
#include <iostream>
#include <string>
using namespace std;
int A[3];
int B[3];
int length = 3;
int cross[3];
int dotProduct (int A[], int B[]){
int product = 0;
for (int i = 0; i < length; i++)
{
product = product + A[i] * B[i];
}
return product;
}
int crossProduct(int A[], int B[], int cross[]){
cross[0]=A[1] * B[2]-A[2] * B[1];
cross[1]=A[2] * B[0]-A[0] * B[2];
cross[2]=A[0] * B[1]-A[1] * B[0];
return cross;
}
int main (){
cout << "Please enter the coordinates for vector A: " << endl;
cout << "X: ";
cin >> A[0];
cout << endl << "Y: ";
cin >> A[1];
cout << endl << "Z: ";
cin >> A[2];
cout << endl;
cout << "Please enter the coordinates for vector B: " << endl;
cout << "X: ";
cin >> B[0];
cout << endl << "Y: ";
cin >> B[1];
cout << endl << "Z: ";
cin >> B[2];
cout << endl;
cout << "Dot Product is: " << dotProduct (A, B) << endl;
crossProduct (A, B, cross);
cout << "Cross Product is: ";
for (int i=0;i<length;i++){
cout << crossProduct[i]<<" ";
}
return 0;
}
For the dot product, I would write:
int
dotProduct(const int A[], const int B[])
{
return std::inner_product(A, A + length, B, 0);
}
For the cross product, you should rather write:
...
crossProduct (A, B, cross);
cout << "Cross Product is: ";
for (int i=0;i<length;i++){
cout << cross[i]<<" ";
}
...