Hey, all,
Very new to C++ programming. I'm attempting to code a function that takes the inner product of two vectors and it's giving me the error message: subscript requires array or pointer type. Here is my code:
Code:
#include <iostream>#include <string>
using namespace std;
double innerProduct(double vectorA, double vectorB, int dim);
int main(){
int dim;
int i;
double vectorA[100];
double vectorB[100];
cout << "What is the dimension of the two vectors: (Enter an integer)\n";
cin >> dim;
while(dim <= 0 || dim > 100){
if(dim <= 0){
cout << "Enter an integer greater than 0.\n";
cin >> dim;
}
else{
cout << "Enter an integer less than or equal to 100.\n";
cin >> dim;
}
cout << "Enter " << dim << " numbers for vector A.\n";
for(i=0; i<dim; i=i+1)
cin >> vectorA[i];
cout << "Enter " << dim << " numbers for vector B.\n";
for(i=0; i<dim; i=i+1)
cin >> vectorB[i];
cout << "The inner product of Vectors A and B of dimension " << dim;
cout << " is " << innerProduct(vectorA, vectorB, dim) << "\n";
}
return 0;
}
double innerProduct(double vectorA, double vectorB, int dim){
double ip;
int i;
double cp[100];
ip=0;
for(i=0; i<dim; i=i+1)
cp[i]=vectorA[i]*vectorB[i];
for(i=0; i<dim; i=i+1)
ip=ip+cp[i];
return ip;
}
The error message in terminal appears twice and refers to line 53:
cp[i]=vectorA[i]*vectorB[i];
What's wrong? Thanks in advance!