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;
}
Related
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 am trying to subtract the initial value that I gave my code from the sum and then have that sum be subtracted by the initial value - 1. For example, my number is 4 so 0+1+2+3+4= 10 and then have 4 subtract from 10 to get 6, then 6 subtracted by 3 to get 3, and so on. I'm extremely new to c++ and I just need a nudge in the right direction.
#include <iostream>
using namespace std;
int a;
int b = 0;
int c = 0;
int sum = 0;
void main() {
cout << "Please give me a number" << endl;
cin >> a;
do {
if (a <= 0) {
cout << "Incorrect number. The input must be positive" << endl;
cin >> a;
}
} while (a <= b);
for (int i = 0; i <= a; i++) {
cout << i;
cout << "+";
sum += i;
}
cout << endl;
cout << "All numbers from 0 to " << a << " is " << sum << endl;
cout << "Starting with the sum of " << sum << endl;
while (a > 0) {
c = sum - a;
cout << "After subtracting " << a << ", I got the number " << c << endl;
a--;
}
#include <iostream>
using namespace std;
int a;
int b = 0;
int c = 0;
int sum = 0;
int main() {
cout << "Please give me a number" << endl;
cin >> a;
do {
if (a <= 0) {
cout << "Incorrect number. The input must be positive" << endl;
cin >> a;
}
} while (a <= b);
for (int i = 0; i <= a; i++) {
cout << i;
cout << "+";
sum += i;
}
cout << endl;
cout << "All numbers from 0 to " << a << " is " << sum << endl;
cout << "Starting with the sum of " << sum << endl;
c = sum; /*Initialize c */
while (a > 0) {
c = c - a;
cout << "After subtracting " << a << ", I got the number " << c << endl;
a--;
}
return 0;
}
#include <iostream>
using namespace std;
int a;
int sum = 0;
int main() {
cin>>a;
while(a<=0){
cin>>a;
}
for(int i = 0; i <= a; i++){ cout<<i; cout<<"+"; sum+=i;}
while(a > 0){
sum-=(a--);
}
}
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]<<" ";
}
...
Hey so the code I made should be working to calculate the passer rating for quarterbacks in the NFL. The program, however, returns a value of 0 for almost anything, unless I put ridiculously large numbers, in which case it gives 100. What's wrong with it?
#include <iostream>
using namespace std;
int main()
{
int PassCompletions;
cout << "Enter pass completions" << endl;
cin >> PassCompletions;
int PassAttempts;
cout << "Enter pass attempts" << endl;
cin >> PassAttempts;
int TotalPassY;
cout << "Enter total yards" << endl;
cin >> TotalPassY;
int Touch;
cout << "Enter touchdowns" << endl;
cin >> Touch;
int Int;
cout << "Enter interceptions" << endl;
cin >> Int;
int C = (PassCompletions/PassAttempts-0.30)*5;
int Y = (TotalPassY/PassAttempts-3)*0.25;
int T = (Touch/PassAttempts)*20;
int I = 2.375 - (Int/PassAttempts*25);
if (C<0){
C=0;
}
if (Y<0){
Y=0;
}
if (T<0){
T=0;
}
if (I<0){
I=0;
}
if (C>2.375){
C=2.375;
}
if (Y>2.375){
Y=2.375;
}
if (T>2.375){
T=2.375;
}
if (I>2.375){
I=2.375;
}
int PasserRating = (C+Y+T+I)/6*100;
if (PasserRating <= 85){
cout << "Rating " << PasserRating << ", this is poor" << endl;
}
if (PasserRating > 85 && PasserRating < 90){
cout << "Rating " << PasserRating << ", this is mediocre" << endl;
}
if (PasserRating > 90 && PasserRating < 95){
cout << "Rating " << PasserRating << ", this is good" << endl;
}
if (PasserRating > 95){
cout << "Rating " << PasserRating << ", this is great" << endl;
}
You need use data type which is suitable to store fractional value. For this purpose use float instead of int for these statements:
float C = (PassCompletions/PassAttempts-0.30)*5;
float Y = (TotalPassY/PassAttempts-3)*0.25;
float T = (Touch/PassAttempts)*20;
float I = 2.375 - (Int/PassAttempts*25);
The variable type int is only used to store whole numbers, eg 1,2,3...
Any expression with a decimal will be truncated and rounded down. Since you are doing a lot of calculations with floating point numbers, eg. 2.375, I would suggest you changing your int's to float's
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.