By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

Multiplying an array using pointers

P: 4
Hey guys, I have put together a class called Mu
Expand|Select|Wrap|Line Numbers
  1. class Mu
  2. {
  3.       private:
  4.               double **s;
  5.               int velocity,numbers;
  6.               double sPeak,muPeak,mu1;
  7.               double a1,b1,c1,u_s[100];
  8.  
  9.  
  10.       public:
  11.              Mu(int);
  12.              ~Mu();
  13.              void muCurve();
  14.              double get_data(int);             
  15.              int randomVelocity(int);
  16.              //double sValues();
  17. };
My constructor looks like this:
Expand|Select|Wrap|Line Numbers
  1. Mu::Mu(int n)
  2. {
  3.     //allocate 100 doubles and assign the pointer of the first element to s. 
  4.     *s = new double [100];
  5. }
This class takes an array s with 100 elements in it. It also pulls in data from a file. In my member function muCurve, I want to multiply my data from a file with each element of my s array. Unfortunately I keep getting the error "invalid operands of type 'double' and 'double*' to binary 'operator*'"
Can someone tell me how I can fix this?Any tips to point me in the right direction would be greatly appreciated, thanks.

Here is my muCurve function:
Expand|Select|Wrap|Line Numbers
  1. void Mu::muCurve()
  2. {
  3.        double v;
  4.        int n=100;
  5.        cout<<sPeak<<" "<<muPeak<<" "<<mu1<<endl;
  6.        a1 = ((muPeak*mu1)/(muPeak-mu1))*pow((1-sPeak),2);
  7.        b1 = pow(sPeak,2);
  8.        c1 = ((mu1*(1+b1))-(2*muPeak*sPeak))/(muPeak-mu1);
  9.        for(int i=0;i<n;i++){
  10.                //v=s[i];
  11.                u_s = (sPeak*s[i])/(muPeak+mu1*s[i]+pow(s[i],2));
  12.                //cout << u_s<<"s values"<<endl;
  13.                cout<< u_s<<" "<<endl;
  14.                //n++;
  15.        }   
  16. }
Aug 11 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
[quote=EJSpin]Hey guys, I have put together a class called Mu
Expand|Select|Wrap|Line Numbers
  1. class Mu
  2. {
  3.       private:
  4.               double **s;
  5. ...
  6. Mu::Mu(int n)
  7. {
  8.     //allocate 100 doubles and assign the pointer of the first element to s. 
  9.     *s = new double [100];
  10. }
I didn't read real closely (I didn't have my espresso yet ;-) but that code fragment
stinks. Variable s is suposed to point to a double pointer (i.e. a thing that points
to a double). s doesn't point to a 'thing' yet but you want that 'thing' to point to
your new double array.

kind regards,

Jos
Aug 11 '07 #2

P: 4
I don't understand, you're saying I should create a pointer when I allocate memory?
Aug 11 '07 #3

Expert 10K+
P: 11,448
I don't understand, you're saying I should create a pointer when I allocate memory?
After reading the rest of your code you most like want 's' to be a double*, not a
double**. Try that first. You want 's' to point to the first double of the 100 doubles
you just created using the 'new' operator.

kind regards,

Jos
Aug 11 '07 #4

P: 4
After reading the rest of your code you most like want 's' to be a double*, not a
double**. Try that first. You want 's' to point to the first double of the 100 doubles
you just created using the 'new' operator.

kind regards,

Jos

Thanks that fixed of my problem. Unfortunately I don't seem to be reading my private data correctly in my muCurve member function. I'm reading in a text file from my get_data member function and setting those values in my class. But I can't seem to access them in muCurve.

Expand|Select|Wrap|Line Numbers
  1. //Snippet of get_data
  2. double Mu::get_data(int range)
  3. {
  4.     int x;
  5.     double sPeak,muPeak,mu1;
  6.     srand((unsigned)time(NULL));
  7.  
  8.     x = 1;//(rand() % range)+1;
  9.  
  10.      if(x==1) {cout << "It's sunny and dry" << endl<<endl;     
  11.      ifstream input1("Sunny.txt",ios::in);
  12.      if (input1.fail()) {
  13.         cout << "error: could not find file" << endl;
  14.         system("PAUSE");
  15.         exit(1); //abort program
  16.      }
  17.      input1 >>sPeak >> muPeak >> mu1;
  18.      cout <<sPeak<<" "<<muPeak<<" "<<mu1<<endl;
  19.      cout << endl;
  20.  
  21.      }

Expand|Select|Wrap|Line Numbers
  1. void Mu::muCurve()
  2. {
  3.        double v;
  4.        int n=100;
  5.        cout<<sPeak<<" "<<muPeak<<" "<<mu1<<endl;  //Not getting the right values
  6.        a1 = ((muPeak*mu1)/(muPeak-mu1))*pow((1-sPeak),2);
  7.        b1 = pow(sPeak,2);
  8.        c1 = ((mu1*(1+b1))-(2*muPeak*sPeak))/(muPeak-mu1);
  9.        for(int i=0;i<n;i++){
  10.                //v=s[i];
  11.                u_s[i] = (sPeak*s[i])/(muPeak+mu1*s[i]+pow(s[i],2));
  12.                //cout << u_s<<"s values"<<endl;
  13.                cout<< u_s[i]<<" "<<endl;
  14.                //n++;
  15.        }   
  16. }
  17.  
Aug 11 '07 #5

P: 4
Nevermind I can see from the snippet I posted what I did wrong. I accidentally defined a seperate case of my data "sPeak, muPeak, and mu1" in my get_data function, and the data I was trying to call in muCurve was never set in my class. Anyways, I thank you for your previous tips.
Aug 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.