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]<<" ";
}
...
Related
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;
}
#include <iostream>
using namespace std;
int main()
{
int numint, sum, a, b, ctr;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr = 0;
while (ctr != numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << "]";
return 0;
}
I need to add the upper bounds of the interval (a) and lower bounds of the interval (b). This depends on the amt of intervals there are.
here:
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << ]";
you are not adding the values but instead just printing them
instead use some accumulator variable and do the math before printing it, like:
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
result = a + b ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}
As for what I understood from the question, you first need a int resultA=0 and another for b, and keep adding in the loop:
#include <iostream>
using namespace std;
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
int resultA=0;
int resultB=0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
resultA+=a;
resultB+=b;
ctr++;
}
result = resultA + resultB ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}
I think this is what you are looking for:
#include <iostream>
using namespace std;
int main() {
int numberOfIntervals;
int lowerBound;
int upperBound;
int counter;
int lowerBoundSum = 0;
int upperBoundSum = 0;
cout << "Enter the number of intervals: ";
cin >> numberOfIntervals;
counter = 0;
while (counter != numberOfIntervals) {
cout << "Enter the lower and upper bounds of interval "
<< counter << ": ";
cin >> lowerBound;
cin >> upperBound;
lowerBoundSum += lowerBound;
upperBoundSum += upperBound;
counter++;
}
cout << "The sum of the lower bounds is " << lowerBoundSum
<< endl;
cout << "The sum of the upper bounds is " << upperBoundSum
<< endl;
return 0;
}
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.
I am fairly new to programming and I wanted to see if this was an efficient way to write a program to solve for lowest number out of 3 using a function. I could not think of more efficient way with what I have learned thus far.
#include <iostream>
using namespace std;
float SmallestNum( float a, float b, float c)
{
if ((a <= c) && (a <= b))
{
float min;
min = a;
return min;
}
else if ((b <= c) && (b <= a))
{
float min;
min = b;
return min;
}
else if ((c <= b) && (c <= a))
{
float min;
min = c;
return min;
}
}
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = SmallestNum(a,b,c);
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
In my opinion the most readable way is to use standard algorithms. For example
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = std::min( { a, b, c } );
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
If you want to write the function yourself then the code can look like
#include <iostream>
#include <cstdlib>
using namespace std;
float SmallestNum( float a, float b, float c)
{
float min = a;
if ( b < min ) min = b;
if ( c < min ) min = c;
return min;
}
int main()
{
float a, b, c, z;
cout << "Please enter 3 seperate numbers : " << endl << endl;
cout << "1st: ";
cin >> a;
cout << "2nd: ";
cin >> b;
cout << "3rd: ";
cin >> c;
cout << endl << endl;
z = SmallestNum( a, b, c );
cout << z << " is your lowest number" << endl;
cout << endl;
system("pause");
return 0;
}
I'm new to programming (in general) and C++ (in particular) and currently learning classes and objects.
I've defined the following as an exercise:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNum_1(int);
void setNum_2(int);
void setNum_3(int);
void setNum_4(int);
double getNum_1();
double getNum_2(int num_1);
double getNum_3(int num_1, int num_2);
double getNum_4(int num_1, int num_2, int num_3);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNum_1(lNum_1);
testObject.setNum_2(lNum_2);
testObject.setNum_3(lNum_3);
testObject.setNum_4(lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
cout << endl;
return 0;
}
void X::setNum_1(int n_1)
{
num_1 = n_1;
}
void X::setNum_2(int n_2)
{
num_2 = n_2;
}
void X::setNum_3(int n_3)
{
num_3 = n_3;
}
void X::setNum_4(int n_4)
{
num_4 = n_4;
}
double X::getNum_1()
{
return sqrt(num_1);
}
double X::getNum_2(int num_1)
{
return pow(num_2,3);
}
double X::getNum_3(int num_1, int num_2)
{
return num_1 * num_2;
}
double X::getNum_4(int num_1, int num_2, int num_3)
{
return (num_1 + num_2) / num_3;
}
Can anyone offer some guidance on how to modify this class so that it has only one member function with all of the arguments defaulted?
Thanks in advance,
Ryan
Ofcourse there are MANY ways to do this and most of them will be more elegant than what I have done here. But this should give you some ideas.
a. You should use the variables defined in the class
b. If you need to perform certain conditional operations in a function, use a switch case or an if statement. And decide the operation based on a parameter passed.
Again, many ways to make this more elegant, but this should get you started and thinking.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNums(int, int, int, int);
double performOp(int);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNums(lNum_1,lNum_2,lNum_3,lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.performOp(1) << endl;
cout << "The 2nd number returned is: " << testObject.performOp(2) << endl;
cout << "The 3rd number returned is: " << testObject.performOp(3) << endl;
cout << "The 4th number returned is: " << testObject.performOp(4) << endl;
cout << endl;
return 0;
}
void X::setNums(int n_1, int n_2, int n_3, int n_4)
{
num_1 = n_1;
num_2 = n_2;
num_3 = n_3;
num_4 = n_4;
}
double X::performOp(int n)
{
if(n == 1) return sqrt(num_1);
if(n == 2) return pow(num_2,3);
if(n == 3) return num_1 * num_2;
if(n == 4) return (num_1 + num_2) / num_3;
}