Hello, I have been trying to figure out how to make an array work with a calculator that uses up to 100 different inputs to calculate sum, difference, standard deviation min max etc. When I try to compile the code I get (error: no match for 'operator<<' on line 64 being max=arrayVal[i]'. I've only done very basic concepts with arrays till now where I would ask for individual input for a 4 or 5 element array. So i do not know if I am using the interger and for statements in this properly. Thank you all for the help in advance =D.

#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main ()
{
float sum = 0.0;
float sub = 0.0;
float variance = 0.0;
float mult = 1.0;
float mean = 0.0;
  int i;
float arrayVal[100];
int n=0;



 cout<<"input your values"<<endl;

  do
  {
    for (i=0;i<100;i++)
      cin>>arrayVal[i];

      sum += arrayVal[i];

      if (n == 0)
      {
        sub = arrayVal[i];
      }

      else
      {
        sub-=arrayVal[i];
      }

    mult *= arrayVal[i];

    float max = arrayVal[0];
    if (max>= arrayVal[i])
    {
      max=arrayVal[i];
    }

    float min = arrayVal[0];
      if (min <= arrayVal[i])
      {
        min=arrayVal[i];    
      }
    n++; // n represents the number of values input by the user 
  }
    while (i<100 && arrayVal[i]!=0);

    mean = sum/n;
      for (i=0;i<100;i++)
      {
        variance += pow((arrayVal[i]-mean),2);
      }
 float standDev = sqrt(variance);

    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<max<<endl;
    cout<<"Min = "<<min<<endl;
    cout<<"Mean = "<<mean<<endl;
    cout<<"Variance = "<<variance<<endl;
    cout<<"Standard Deviation = "<<standDev<<endl;

    return 0;
}

Recommended Answers

All 7 Replies

max and min are keywords, also when you declare a variable inside a loop, it's local to that loop.

try something like this:

#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main ()
{
    float sum = 0.0;
    float sub = 0.0;
    float variance = 0.0;
    float mult = 1.0;
    float mean = 0.0;
    float Max = 0;
    float Min = 0;
    float mMx = 0;
    int i;
    float arrayVal[100];
    int n=0;
    cout<<"input your values"<<endl;
    do
    {
        for (i=0; i<100; i++)
            cin>>arrayVal[i];
        sum += arrayVal[i];
        if (n == 0)
        {
            sub = arrayVal[i];
        }
        else
        {
            sub-=arrayVal[i];
        }
        mult *= arrayVal[i];
        mMx = arrayVal[0];
        if (Max>= arrayVal[i])
        {
            Max=arrayVal[i];
        }
        Min = arrayVal[0];
        if (Min <= arrayVal[i])
        {
            Min=arrayVal[i];
        }
        n++; // n represents the number of values input by the user
    }
    while (i<100 && arrayVal[i]!=0);
    mean = sum/n;
    for (i=0; i<100; i++)
    {
        variance += pow((arrayVal[i]-mean),2);
    }
    float standDev = sqrt(variance);
    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<Max<<endl;
    cout<<"Min = "<<Min<<endl;
    cout<<"Mean = "<<mean<<endl;
    cout<<"Variance = "<<variance<<endl;
    cout<<"Standard Deviation = "<<standDev<<endl;
    return 0;
}

Your syntactical error is that max goes out of scope, but you have many more errors that are logical. For instance that for loop will run through and gather all the data into the array. Afterwards i will be 100... so you will only be using the very last thing entered. I am not sure how you would want to fix this. I would put the input into the do-while loop and forget about the for loop completely. Here is your code with the errors highlighted by comment:

    #include <iostream>
    #include <math.h>
    #include <cmath>
    #include <climits>//for INT_MIN, INT_MAX
    using namespace std;
    int main ()
    {
    float sum = 0.0;
    float sub = 0.0;
    float variance = 0.0;
    float mult = 1.0;
    float mean = 0.0;
    float min=INT_MAX,max=INT_MIN;//create min and max here
    int i;//remove this and change all the i's to n's
    float arrayVal[100];
    int n=0;
    cout<<"input your values"<<endl;
    do
    {//note the removed for loop.
    cin>>arrayVal[i];
    sum += arrayVal[i];
    if (n == 0)
    {
    sub = arrayVal[i];
    }
    else
    {
    sub-=arrayVal[i];
    }
    mult *= arrayVal[i];
    //do not recreate max
    if (max>= arrayVal[i])
    {
    max=arrayVal[i];
    }
    //do not recreate min
    if (min <= arrayVal[i])
    {
    min=arrayVal[i];
    }
    n++; // n represents the number of values input by the user
    }
    while (i<100 && arrayVal[i]!=0);
    mean = sum/n;
    for (i=0;i<100;i++)
    {
    variance += pow((arrayVal[i]-mean),2);//using pow for squaring is sorta lame... (arrayVal[i]-mean)*(arrayVal[i]-mean) is arguably better
    }
    float standDev = sqrt(variance);
    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<max<<endl;
    cout<<"Min = "<<min<<endl;
    cout<<"Mean = "<<mean<<endl;
    cout<<"Variance = "<<variance<<endl;
    cout<<"Standard Deviation = "<<standDev<<endl;
    return 0;
    }

I've never used a power in any of my calculators so just wanted to give it a shot with the pow(). But my program won't output anything after I type in my inputs. When I tried it on my Ipad it would only input the sum properly. My values were 7,8,9,10,12,14,0 my answers were using symbol to cut down on typing +60
-0 x0 max -2.14e9 min 2.14e9 mean inf variance-nan stand dev -nan.

#include <iostream>
#include <math.h>
#include <cmath>
#include <climits>
using namespace std;
int main ()
{
float sum = 0.0;
float sub = 0.0;
float variance = 0.0;
float mult = 1.0;
float mean = 0.0;
float min =INT_MAX,max = INT_MIN;
float arrayVal[100];
int n=0;



 cout<<"input your values"<<endl;

  do
  {

      cin>>arrayVal[n];

      sum += arrayVal[n];

      if (n == 0)
      {
        sub = arrayVal[n];
      }

      else
      {
        sub-=arrayVal[n];
      }

    mult *= arrayVal[n];


    if (max>= arrayVal[n])
    {
      max=arrayVal[n];
    }


      if (min <= arrayVal[n])
      {
        min=arrayVal[n];    
      }
    n++;
  }
    while (n<100 && arrayVal[n]!=0);

    mean = sum/n;
      for (n=0;n<100;n++)
      {
        variance += pow((arrayVal[n]-mean),2);
      }
 float standDev = sqrt(variance);

    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<max<<endl;
    cout<<"Min = "<<min<<endl;
    cout<<"Mean = "<<mean<<endl;
    cout<<"Variance = "<<variance<<endl;
    cout<<"Standard Deviation = "<<standDev<<endl;
    return 0;
}

woops i forgot to put n++ on my ipad. the outputs were sum = 7 sub = 7 mult = 7 max -2.14 e9 min 2.14e9
mean 7 varian -nan standard -nan

You were including the 0 value, meant to signal the end of the input, in your calculations. The loop for the variance went to the end of the array instead of the input, and the variance calculation didn't divide by the number of elements - 1.

See if this helps:

#include <iostream>
#include <math.h>
#include <cmath>
#include <climits>
#include <Cfloat>
using namespace std;
int main ()
{
    float sum = 0.0;
    float sub = 0.0;
    float variance = 0.0;
    float mult = 1.0;
    float mean = 0.0;
    float Min =FLT_MAX,Max = FLT_MIN;
    float arrayVal[100];
    int n=0;
    cout<<"input your values"<<endl;
    for(n=0; n<100; n++)
    {
        cin>>arrayVal[n];
        if (arrayVal[n] == 0)
        {
            break;
        }
        sum += arrayVal[n];
        sub-=arrayVal[n];
        mult *= arrayVal[n];
        if (arrayVal[n] > Max)
        {
            Max=arrayVal[n];
        }
        if (arrayVal[n] < Min)
        {
            Min=arrayVal[n];
        }
    }
    mean = sum/n;
    for (int i=0; i<n; i++)
    {
        variance += (arrayVal[i] - mean) * (arrayVal[i] - mean);
    }
    variance /= n-1;
    float standDev = sqrt(variance);
    cout<<"Sum = "<<sum<<endl;
    cout<<"Sub = "<<sub<<endl;
    cout<<"Mult = "<<mult<<endl;
    cout<<"Max = "<<Max<<endl;
    cout<<"Min = "<<Min<<endl;
    cout<<"Mean = "<<mean<<endl;
    cout<<"Variance = "<<variance<<endl;
    cout<<"Standard Deviation = "<<standDev<<endl;
    return 0;
}

Ah Ty that clears up about 99% of the issues the only thing left is a logical error with sub it is solving it as (saying I use the inputs 1 2 3 4) 0-1-2-3-4 instead of 1-2-3-4. When I made my last calculator I used an if statement of if n=0 { sub = first input) } else {sub-=input} and with that break statement I can't use it in the if statement you provided.

Nm I was misreading the code XD I could nest another if statement for sub. Thanks a lot for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.