2

I want to display the range of x and f(x) and keep f(x) in array but i always get this error:

invalid type 'float*[float]' for array subscript

can someone help me? I'm still stuck.

Here's the code:

#include <iostream>
#include <cmath>
#include <math.h>
using std::cin;
using std::cout;

using namespace std;
void displayValue(float funx[], float j, float x);

int main()
{
    float num9[]={};
    float a, r;
    displayValue(num9, a, r);

    return 0;
}
void displayValue(float funx[], float j, float x)
{
    float i;
    cout << "Please enter range of x: " << endl;
    for (i=0; i<1; i++)
    {
        cin >> x >> j;
    }
    for (float i=1; i<=160.5; i++)
    {
        x+=0.5;
        funx[i]=1/sin(x)+1/tan(x);
         //1.2 Display f(x) and x within the range
    }cout << x << " = " << funx[i] << "\n";

}
9
  • 1
    Do you know what using std::cin and using std::cout does? Commented Jun 30, 2020 at 8:25
  • 6
    i is a float. You can't use floats as index. Is there any reason you use float here instead of int? Commented Jun 30, 2020 at 8:25
  • 2
    Use the title to describe your problem so people can determine at a glance if they can help. "Can someone help me" is not a problem. Commented Jun 30, 2020 at 8:26
  • 1
    Arrays need integer indexes, so you'll need to use an integer value to reference them. Commented Jun 30, 2020 at 8:27
  • 1
    Lastly: It's irrelavant what values you pass for j and x, they are overwritten anyway. So they should be local variables. Commented Jun 30, 2020 at 8:33

1 Answer 1

5

The problems you're trying to solve aren't actually problems you need to solve. There's a lot going wrong in this code that can simply be removed because you're using the wrong tools.

You don't need an array here. If you did you'd need to allocate one, not pass in something that's empty, or you'd be using it out of bounds. In C++ for arrays like this use std::vector.

That being said, here's a simplified version of the code:

#include <iostream>
#include <cmath>
#include <math.h>

// Don't add "using namespace std", that separation exists for a reason.

// Separate the math function to make it clear what's being done
float f(const float x) {
  return 1/sin(x)+1/tan(x);
}

// Define your functions before they're used to avoid having to declare
// then later define them.
void displayValue(const float min, const float max, const float step = 0.5)
{
    for (float x = min; x <= max; x += step)
    {
      // Note how the f(x) function here is a lot easier to follow
      std::cout << "f(" << x << ") = " << f(x) << std::endl;
    }
}

int main()
{
    std::cout << "Please enter range of x: " << std::endl;

    // Capture the range values once and once only
    float min, max;
    std::cin >> min >> max;
  
    // Display over the range of values
    displayValue(min, max);

    return 0;
}

There's some important C++ fundamentals here:

  • float num9[]={}; is not an empty array that you can later add to, it is a permanently zero-length array, or in other words, it's useless.
  • Pay close attention to the variables you've defined and avoid defining them twice in the same scope.
  • Turn on all your compiler warnings while you're learning to be alerted to potential problems. C++ is full of nuances and gotchas.
Sign up to request clarification or add additional context in comments.

3 Comments

i deleted my answer in favour of yours, but imho you should add a word about the float loop counter. In presence of rounding errors the loop might stop one iteration earlier/later than expected
@idclev463035818 It's good to note that, but floating point math is a considerable can of worms, so one thing at a time.
I would say that while not recommended, it's ok to use using namespace std. However, one should only use either using namespace libraryName and using libraryName::functionName, not both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.