sqrt()function array

hello, i am trying to figure out how to use an if statement to out put N/A if the the answer you get from the sqrt () function is an error. in other words if the number being square rooted is negative.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void Base (int listB [], int numberOfRows, int startingPoint, int increment);
void PrintAll (int listB [], int listS[], int nRows);
void Square (int listB [], int listS[], int numberOfRows);
void SqRoot (int listB[], double ListSQR [], int numberOfRows);

int main() {
    int arraySizeB [25];
    int arraySizeS [25];
    double arraySizeSQR [25];
    int size;
    cout << "Enter the number of rows in your table between 1 and 25: ";
    cin >> size;
    cout << endl;
    while (size < 1 || size > 25)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number of rows in your table between 1 and 25: ";
        cin >> size;
    }

    int startPoint;
    cout << "Enter a number to start the table with between - 1000 and 1000: ";
    cin >> startPoint;
    cout << endl;
    while (startPoint < -1000 || startPoint > 1000)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number to start the table with between -1000 and 1000: ";
        cin >> startPoint;
    }

    int increment;
    cout << "Enter a number to increment table between 1 and 20: ";
    cin >> increment;
    cout << endl;
    while (increment < 1 || increment > 20)
    {
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number to increment table between 1 and 20: ";
        cin >> increment;
    }

    Base(arraySizeB, size, startPoint,increment);
    //Square(arraySizeB,arraySizeS,size);
    //PrintAll(arraySizeB, arraySizeS,size);

    for (int index = 0; index < size ; index++)
    {
        double newValue = sqrt(arraySizeB[index]);
        arraySizeSQR[index] = newValue;
        if (newValue != '0')
        {
            arraySizeSQR[index] = "N/A"
        }
    }





    return 0;
}


void Base (int list[], int numberOfRows, int startingPoint, int increment)
{
    list [0] = startingPoint;

    for (int index = 1; index < numberOfRows; index++)
    {
        int newValue = startingPoint += increment;
        list[index] = newValue;
    }
}

void Square (int listB [], int listS[], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        int newValue = listB[index] * listB[index];
        listS[index] = newValue;
    }
}

void SqRoot (int listB[], double listSQR [], int numberOfRows)
{
    for (int index = 0; index < numberOfRows ; index++)
    {
        double newValue = sqrt(listB[index]);
        listSQR[index] = newValue;
    }
}


void PrintAll (int listB [], int listS[], int nRows)
{
    for (int j = 0; j < nRows; j ++)
    {
        cout << listB [j] << " " << listS[j] << endl;
    }
}

i am lost in the for loop in the main. i can't figure out how to change the value in the arraySizeSQR to N/A. i guess i need to know how to change a value in the array from a double to string?
Last edited on
There might be a NaN value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

double foo() {
    if ( std::numeric_limits<double>::has_quiet_NaN )
        return std::numeric_limits<double>::quiet_NaN();
    else
        return -1;
}

int main () {
  std::cout << "How much: " << foo() << '\n';
  return 0;
}
alternately check first.
if (number < 0)
return NA_value
else
return(sqrt(number));
@jonnin:
The OP has two parts (of which we see only the first):
1. store root(s) in (array of) double
2. display roots

One has to store numbers in array of doubles. Strings are not an option.

The system may support double value NaN, which shows as "nan" in formatted output. The NaN is a number, special number.

When displaying the roots one can obviously test the values and print whatever:
1
2
3
4
for ( auto root : arraySizeSQR ) {
  if ( root < 42 ) std::cout << "small\n";
  else std::cout << root << '\n';
}


The point is that if NaN can be used and "nan" is ok output, then the display is simple:
1
2
3
for ( auto root : arraySizeSQR ) {
  std::cout << root << '\n';
}

Ah, I thought he specifically wanted to over-ride it to "N/A". I dislike storing NAN but I suppose that is fine.
Last edited on
Topic archived. No new replies allowed.