Inserting a string into a type double array

Hi

I wanted to find out how would insert a string into a array of type double?

1
2
3
  double myArray[20];

  myArray[5] = "Sentence"


How would you make a single element in the array display a string value if all the other values in the array are type double?
You cannot, as this violates the C++ type system. What are you trying to accomplish? There must be another way to approach your problem.
1
2
3
4
5
6
7
8
9
for (int i=0; i<19; i++)
    {
        tanArray[i]= tan(i*(5*pi/180));
    }

if (tanArray[18] = 90)
{
        tanArray18 = "No Solution"
}


There is no solution for tan90. So putting zero would be incorrect. Can you leave the array block open? Loop has to run from 0 to 18 though.
Last edited on
Say it's Not-a-Number (NaN)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <limits>

//...

for (int i=0; i<19; i++)
{
    int angleInDegrees = i * 5;
    if (angleInDegrees == 90)
    {
        tanArray[i] = std::numeric_limits<double>::quiet_NaN();
    }
    else
    {
        tanArray[i]= tan(angleInDegrees * pi / 180);
    }
} 


EDIT: From what I understand, this gets into implementation-specific nitty-gritty. I don't know how well this code would port across platforms or compilers. This code implies that your platform supports the IEEE 754 floating point standard (which is likely, but not 100% guaranteed).
Last edited on
Thanks! This will do quite fine.
Topic archived. No new replies allowed.